Skip to content

Commit 3e8824a

Browse files
committed
Add schemas for basic credential types
1 parent 4ee4567 commit 3e8824a

4 files changed

Lines changed: 53 additions & 33 deletions

File tree

lib/start-proxy-action.js

Lines changed: 18 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/start-proxy/types.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ test("credentialToStr - hides passwords", (t) => {
111111
const secret = "password123";
112112
const credential = {
113113
type: "maven_credential",
114+
username: null,
114115
password: secret,
115116
url: "https://localhost",
116-
};
117+
} satisfies types.Credential;
117118

118119
const str = types.credentialToStr(credential);
119120

@@ -125,9 +126,10 @@ test("credentialToStr - hides tokens", (t) => {
125126
const secret = "password123";
126127
const credential = {
127128
type: "maven_credential",
129+
username: null,
128130
token: secret,
129131
url: "https://localhost",
130-
};
132+
} satisfies types.Credential;
131133

132134
const str = types.credentialToStr(credential);
133135

src/start-proxy/types.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,60 @@ import { isDefined } from "../util";
99
*/
1010
export 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. */
3342
export 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. */
4962
export 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. */

src/start-proxy/validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as core from "@actions/core";
33
import * as json from "../json";
44
import { isDefined } from "../util";
55

6-
import type { AuthConfig, Token, UsernamePassword } from "./types";
6+
import type { AuthConfig, UsernamePassword } from "./types";
77
import * as types from "./types";
88

99
/** Constructs a new object from `obj` with only keys that exist in `schema`. */
@@ -55,7 +55,7 @@ export function getAuthConfig(
5555
core.setSecret(config.token);
5656
}
5757

58-
return { username: config.username, token: config.token } satisfies Token;
58+
return cloneCredential(types.tokenSchema, config);
5959
} else {
6060
let username: string | undefined = undefined;
6161
let password: string | undefined = undefined;

0 commit comments

Comments
 (0)