Skip to content

Commit 37eb89b

Browse files
committed
Add predicates for Auth types
1 parent 9e26f9e commit 37eb89b

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/start-proxy.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,12 @@ test.serial(
350350
t.is(results.length, 1);
351351
t.is(results[0].type, "git_server");
352352
t.is(results[0].host, "https://github.com/");
353-
t.assert(results[0].password?.startsWith("ghp_"));
353+
354+
if (startProxyExports.isUsernamePassword(results[0])) {
355+
t.assert(results[0].password?.startsWith("ghp_"));
356+
} else {
357+
t.fail("Expected a `UsernamePassword`-based credential.");
358+
}
354359

355360
// A warning should have been logged.
356361
checkExpectedLogMessages(t, loggedMessages, [

src/start-proxy/types.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDefined } from "../util";
2+
13
/**
24
* After parsing configurations from JSON, we don't know whether all the keys we expect are
35
* present or not. This type is used to represent such values, which we expect to be
@@ -11,6 +13,11 @@ export type Username = {
1113
username?: string;
1214
};
1315

16+
/** Decides whether `config` has a username. */
17+
export function hasUsername(config: Partial<AuthConfig>): config is Username {
18+
return "username" in config;
19+
}
20+
1421
/**
1522
* Fields expected for authentication based on a username and password.
1623
* Both username and password are optional.
@@ -20,6 +27,13 @@ export type UsernamePassword = {
2027
password?: string;
2128
} & Username;
2229

30+
/** Decides whether `config` is based on a username and password. */
31+
export function isUsernamePassword(
32+
config: AuthConfig,
33+
): config is UsernamePassword {
34+
return hasUsername(config) && "password" in config;
35+
}
36+
2337
/**
2438
* Fields expected for token-based authentication.
2539
* Both username and token are optional.
@@ -29,9 +43,26 @@ export type Token = {
2943
token?: string;
3044
} & Username;
3145

46+
/** Decides whether `config` is token-based. */
47+
export function isToken(config: Partial<AuthConfig>): config is Token {
48+
return "token" in config;
49+
}
50+
3251
/** Configuration for Azure OIDC. */
3352
export type AzureConfig = { tenant_id: string; client_id: string };
3453

54+
/** Decides whether `config` is an Azure OIDC configuration. */
55+
export function isAzureConfig(
56+
config: Partial<AuthConfig>,
57+
): config is AzureConfig {
58+
return (
59+
"tenant_id" in config &&
60+
"client_id" in config &&
61+
isDefined(config.tenant_id) &&
62+
isDefined(config.client_id)
63+
);
64+
}
65+
3566
/** Configuration for AWS OIDC. */
3667
export type AWSConfig = {
3768
aws_region: string;
@@ -42,18 +73,47 @@ export type AWSConfig = {
4273
audience?: string;
4374
};
4475

76+
/** Decides whether `config` is an AWS OIDC configuration. */
77+
export function isAWSConfig(config: Partial<AuthConfig>): config is AWSConfig {
78+
// All of these properties are required.
79+
const requiredProperties = [
80+
"aws_region",
81+
"account_id",
82+
"role_name",
83+
"domain",
84+
"domain_owner",
85+
];
86+
87+
for (const property of requiredProperties) {
88+
if (!(property in config) || !isDefined(config[property])) {
89+
return false;
90+
}
91+
}
92+
return true;
93+
}
94+
4595
/** Configuration for JFrog OIDC. */
4696
export type JFrogConfig = {
4797
jfrog_oidc_provider_name: string;
4898
audience?: string;
4999
identity_mapping_name?: string;
50100
};
51101

102+
/** Decides whether `config` is a JFrog OIDC configuration. */
103+
export function isJFrogConfig(
104+
config: Partial<AuthConfig>,
105+
): config is JFrogConfig {
106+
return (
107+
"jfrog_oidc_provider_name" in config &&
108+
isDefined(config.jfrog_oidc_provider_name)
109+
);
110+
}
111+
52112
/** Represents all supported OIDC configurations. */
53113
export type OIDC = AzureConfig | AWSConfig | JFrogConfig;
54114

55115
/** All authentication-related fields. */
56-
export type AuthConfig = UsernamePassword & Token & OIDC;
116+
export type AuthConfig = UsernamePassword | Token | OIDC;
57117

58118
/**
59119
* A package registry configuration includes identifying information as well as

0 commit comments

Comments
 (0)