2022-10-21 13:14:17 +08:00
|
|
|
import type { CustomClientMetadata, OidcClientMetadata } from '@logto/schemas';
|
|
|
|
import { ApplicationType, customClientMetadataGuard, GrantType } from '@logto/schemas';
|
2022-09-21 13:06:56 +08:00
|
|
|
import { conditional } from '@silverhand/essentials';
|
2022-10-21 13:14:17 +08:00
|
|
|
import type { AllClientMetadata, ClientAuthMethod } from 'oidc-provider';
|
|
|
|
import { errors } from 'oidc-provider';
|
2021-08-18 00:24:00 +08:00
|
|
|
|
2023-01-11 16:41:53 +08:00
|
|
|
import type { EnvSet } from '#src/env-set/index.js';
|
2023-01-09 17:34:13 +08:00
|
|
|
|
2023-01-11 16:41:53 +08:00
|
|
|
export const getConstantClientMetadata = (
|
|
|
|
envSet: EnvSet,
|
|
|
|
type: ApplicationType
|
|
|
|
): AllClientMetadata => {
|
2023-01-09 17:34:13 +08:00
|
|
|
const { jwkSigningAlg } = envSet.oidc;
|
|
|
|
|
2022-09-21 13:06:56 +08:00
|
|
|
const getTokenEndpointAuthMethod = (): ClientAuthMethod => {
|
|
|
|
switch (type) {
|
|
|
|
case ApplicationType.Native:
|
2023-02-16 23:49:03 +08:00
|
|
|
case ApplicationType.SPA: {
|
2022-09-21 13:06:56 +08:00
|
|
|
return 'none';
|
2023-02-16 23:49:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
2022-09-21 13:06:56 +08:00
|
|
|
return 'client_secret_basic';
|
2023-02-16 23:49:03 +08:00
|
|
|
}
|
2022-09-21 13:06:56 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
application_type: type === ApplicationType.Native ? 'native' : 'web',
|
|
|
|
grant_types:
|
|
|
|
type === ApplicationType.MachineToMachine
|
|
|
|
? [GrantType.ClientCredentials]
|
|
|
|
: [GrantType.AuthorizationCode, GrantType.RefreshToken],
|
|
|
|
token_endpoint_auth_method: getTokenEndpointAuthMethod(),
|
|
|
|
response_types: conditional(type === ApplicationType.MachineToMachine && []),
|
2023-01-09 17:34:13 +08:00
|
|
|
// https://www.scottbrady91.com/jose/jwts-which-signing-algorithm-should-i-use
|
|
|
|
authorization_signed_response_alg: jwkSigningAlg,
|
|
|
|
userinfo_signed_response_alg: jwkSigningAlg,
|
|
|
|
id_token_signed_response_alg: jwkSigningAlg,
|
|
|
|
introspection_signed_response_alg: jwkSigningAlg,
|
2022-09-21 13:06:56 +08:00
|
|
|
};
|
|
|
|
};
|
2021-10-11 17:55:17 +08:00
|
|
|
|
2022-01-11 11:58:58 +08:00
|
|
|
export const buildOidcClientMetadata = (metadata?: OidcClientMetadata): OidcClientMetadata => ({
|
2021-08-26 13:05:23 +08:00
|
|
|
redirectUris: [],
|
|
|
|
postLogoutRedirectUris: [],
|
2021-10-11 17:55:17 +08:00
|
|
|
...metadata,
|
2021-08-18 00:24:00 +08:00
|
|
|
});
|
2022-04-08 18:16:20 +08:00
|
|
|
|
|
|
|
export const validateCustomClientMetadata = (key: string, value: unknown) => {
|
|
|
|
const result = customClientMetadataGuard.pick({ [key]: true }).safeParse({ [key]: value });
|
|
|
|
|
|
|
|
if (!result.success) {
|
|
|
|
throw new errors.InvalidClientMetadata(key);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-05 17:36:43 +08:00
|
|
|
export const isOriginAllowed = (
|
|
|
|
origin: string,
|
2022-07-05 21:09:57 +08:00
|
|
|
{ corsAllowedOrigins = [] }: CustomClientMetadata,
|
2022-07-05 17:36:43 +08:00
|
|
|
redirectUris: string[] = []
|
|
|
|
) => {
|
|
|
|
const redirectUriOrigins = redirectUris.map((uri) => new URL(uri).origin);
|
|
|
|
|
|
|
|
return [...corsAllowedOrigins, ...redirectUriOrigins].includes(origin);
|
|
|
|
};
|
2023-07-07 15:14:29 +08:00
|
|
|
|
|
|
|
export const getUtcStartOfToday = () => {
|
|
|
|
const now = new Date();
|
|
|
|
|
|
|
|
return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 0, 0, 0, 0));
|
|
|
|
};
|