@@ -9,53 +9,60 @@ import { isDefined } from "../util";
99 */
1010export type RawCredential = UnvalidatedObject < Credential > ;
1111
12- /** Usernames may be present for both authentication with tokens or passwords . */
13- export type Username = {
12+ /** A schema for credential objects with a username . */
13+ export const usernameSchema = {
1414 /** The username needed to authenticate to the package registry, if any. */
15- username ?: string ;
16- } ;
15+ username : json . optional ( json . string ) ,
16+ } as const satisfies json . Schema ;
17+
18+ /** Usernames may be present for both authentication with tokens or passwords. */
19+ export type Username = json . FromSchema < typeof usernameSchema > ;
1720
1821/** Decides whether `config` has a username. */
19- export function hasUsername ( config : AuthConfig ) : config is Username {
20- return "username" in config ;
22+ export function hasUsername (
23+ config : UnvalidatedObject < unknown > ,
24+ ) : config is Username {
25+ return json . validateSchema ( usernameSchema , config ) ;
2126}
2227
28+ /** A schema for credential objects with a username and password. */
29+ export const usernamePasswordSchema = {
30+ /** The password needed to authenticate to the package registry, if any. */
31+ password : json . optional ( json . string ) ,
32+ ...usernameSchema ,
33+ } as const satisfies json . Schema ;
34+
2335/**
2436 * Fields expected for authentication based on a username and password.
2537 * Both username and password are optional.
2638 */
27- export type UsernamePassword = {
28- /** The password needed to authenticate to the package registry, if any. */
29- password ?: string ;
30- } & Username ;
39+ export type UsernamePassword = json . FromSchema < typeof usernamePasswordSchema > ;
3140
3241/** Decides whether `config` is based on a username and password. */
3342export function isUsernamePassword (
3443 config : AuthConfig ,
3544) : config is UsernamePassword {
36- return hasUsername ( config ) && "password" in config ;
45+ return json . validateSchema ( usernamePasswordSchema , config ) ;
3746}
3847
48+ /** A schema for credential objects for token-based authentication. */
49+ export const tokenSchema = {
50+ /** The token needed to authenticate to the package registry, if any. */
51+ token : json . string ,
52+ ...usernameSchema ,
53+ } as const satisfies json . Schema ;
54+
3955/**
4056 * Fields expected for token-based authentication.
4157 * Both username and token are optional.
4258 */
43- export type Token = {
44- /** The token needed to authenticate to the package registry, if any. */
45- token ?: string ;
46- } & Username ;
59+ export type Token = json . FromSchema < typeof tokenSchema > ;
4760
4861/** Decides whether `config` is token-based. */
4962export function isToken (
5063 config : UnvalidatedObject < AuthConfig > ,
5164) : config is Token {
52- // The "username" field is optional, but should be a string if present.
53- if ( "username" in config && ! json . isStringOrUndefined ( config . username ) ) {
54- return false ;
55- }
56-
57- // The "token" field is required, and must be a string or undefined.
58- return "token" in config && json . isStringOrUndefined ( config . token ) ;
65+ return json . validateSchema ( tokenSchema , config ) ;
5966}
6067
6168/** A schema for Azure OIDC configurations. */
0 commit comments