Soft required field

This commit is contained in:
Gregory Schier
2025-02-21 13:43:19 -08:00
parent 597b5bb783
commit 52937c3097

View File

@@ -239,56 +239,56 @@ export const plugin: PluginDefinition = {
}, },
], ],
async onApply(ctx, { values, contextId }) { async onApply(ctx, { values, contextId }) {
const headerPrefix = optionalString(values, 'headerPrefix') ?? ''; const headerPrefix = stringArg(values, 'headerPrefix');
const grantType = requiredString(values, 'grantType') as GrantType; const grantType = stringArg(values, 'grantType') as GrantType;
const credentialsInBody = values.credentials === 'body'; const credentialsInBody = values.credentials === 'body';
let token: AccessToken; let token: AccessToken;
if (grantType === 'authorization_code') { if (grantType === 'authorization_code') {
const authorizationUrl = requiredString(values, 'authorizationUrl'); const authorizationUrl = stringArg(values, 'authorizationUrl');
const accessTokenUrl = requiredString(values, 'accessTokenUrl'); const accessTokenUrl = stringArg(values, 'accessTokenUrl');
token = await getAuthorizationCode(ctx, contextId, { token = await getAuthorizationCode(ctx, contextId, {
accessTokenUrl: accessTokenUrl.match(/^https?:\/\//) ? accessTokenUrl : `https://${accessTokenUrl}`, accessTokenUrl: accessTokenUrl.match(/^https?:\/\//) ? accessTokenUrl : `https://${accessTokenUrl}`,
authorizationUrl: authorizationUrl.match(/^https?:\/\//) ? authorizationUrl : `https://${authorizationUrl}`, authorizationUrl: authorizationUrl.match(/^https?:\/\//) ? authorizationUrl : `https://${authorizationUrl}`,
clientId: requiredString(values, 'clientId'), clientId: stringArg(values, 'clientId'),
clientSecret: requiredString(values, 'clientSecret'), clientSecret: stringArg(values, 'clientSecret'),
redirectUri: optionalString(values, 'redirectUri'), redirectUri: stringArgOrNull(values, 'redirectUri'),
scope: optionalString(values, 'scope'), scope: stringArgOrNull(values, 'scope'),
state: optionalString(values, 'state'), state: stringArgOrNull(values, 'state'),
credentialsInBody, credentialsInBody,
pkce: values.usePkce ? { pkce: values.usePkce ? {
challengeMethod: requiredString(values, 'pkceChallengeMethod'), challengeMethod: stringArg(values, 'pkceChallengeMethod'),
codeVerifier: optionalString(values, 'pkceCodeVerifier'), codeVerifier: stringArgOrNull(values, 'pkceCodeVerifier'),
} : null, } : null,
}); });
} else if (grantType === 'implicit') { } else if (grantType === 'implicit') {
const authorizationUrl = requiredString(values, 'authorizationUrl'); const authorizationUrl = stringArg(values, 'authorizationUrl');
token = await getImplicit(ctx, contextId, { token = await getImplicit(ctx, contextId, {
authorizationUrl: authorizationUrl.match(/^https?:\/\//) ? authorizationUrl : `https://${authorizationUrl}`, authorizationUrl: authorizationUrl.match(/^https?:\/\//) ? authorizationUrl : `https://${authorizationUrl}`,
clientId: requiredString(values, 'clientId'), clientId: stringArg(values, 'clientId'),
redirectUri: optionalString(values, 'redirectUri'), redirectUri: stringArgOrNull(values, 'redirectUri'),
responseType: requiredString(values, 'responseType'), responseType: stringArg(values, 'responseType'),
scope: optionalString(values, 'scope'), scope: stringArgOrNull(values, 'scope'),
state: optionalString(values, 'state'), state: stringArgOrNull(values, 'state'),
}); });
} else if (grantType === 'client_credentials') { } else if (grantType === 'client_credentials') {
const accessTokenUrl = requiredString(values, 'accessTokenUrl'); const accessTokenUrl = stringArg(values, 'accessTokenUrl');
token = await getClientCredentials(ctx, contextId, { token = await getClientCredentials(ctx, contextId, {
accessTokenUrl: accessTokenUrl.match(/^https?:\/\//) ? accessTokenUrl : `https://${accessTokenUrl}`, accessTokenUrl: accessTokenUrl.match(/^https?:\/\//) ? accessTokenUrl : `https://${accessTokenUrl}`,
clientId: requiredString(values, 'clientId'), clientId: stringArg(values, 'clientId'),
clientSecret: requiredString(values, 'clientSecret'), clientSecret: stringArg(values, 'clientSecret'),
scope: optionalString(values, 'scope'), scope: stringArgOrNull(values, 'scope'),
credentialsInBody, credentialsInBody,
}); });
} else if (grantType === 'password') { } else if (grantType === 'password') {
const accessTokenUrl = requiredString(values, 'accessTokenUrl'); const accessTokenUrl = stringArg(values, 'accessTokenUrl');
token = await getPassword(ctx, contextId, { token = await getPassword(ctx, contextId, {
accessTokenUrl: accessTokenUrl.match(/^https?:\/\//) ? accessTokenUrl : `https://${accessTokenUrl}`, accessTokenUrl: accessTokenUrl.match(/^https?:\/\//) ? accessTokenUrl : `https://${accessTokenUrl}`,
clientId: requiredString(values, 'clientId'), clientId: stringArg(values, 'clientId'),
clientSecret: requiredString(values, 'clientSecret'), clientSecret: stringArg(values, 'clientSecret'),
username: requiredString(values, 'username'), username: stringArg(values, 'username'),
password: requiredString(values, 'password'), password: stringArg(values, 'password'),
scope: optionalString(values, 'scope'), scope: stringArgOrNull(values, 'scope'),
credentialsInBody, credentialsInBody,
}); });
} else { } else {
@@ -306,14 +306,14 @@ export const plugin: PluginDefinition = {
}, },
}; };
function optionalString(values: Record<string, JsonPrimitive | undefined>, name: string): string | null { function stringArgOrNull(values: Record<string, JsonPrimitive | undefined>, name: string): string | null {
const arg = values[name]; const arg = values[name];
if (arg == null || arg == '') return null; if (arg == null || arg == '') return null;
return `${arg}`; return `${arg}`;
} }
function requiredString(values: Record<string, JsonPrimitive | undefined>, name: string): string { function stringArg(values: Record<string, JsonPrimitive | undefined>, name: string): string {
const arg = optionalString(values, name); const arg = stringArgOrNull(values, name);
if (!arg) throw new Error(`Missing required argument ${name}`); if (!arg) return '';
return arg; return arg;
} }