Skip to content

Commit e1d57b5

Browse files
drewtotoDrew Totonsarrazin
authored
feat: explicit config of id token alg (#1567) (#1568)
* feat: explicit config of id token alg (#1567) - support to configure `id_token_signed_response_alg` field in the configuration object passed to `issuer.Client` in `src/lib/server/auth.ts`. - allow `id_token_signed_response_alg` to be set from environment variable `OIDConfig.ID_TOKEN_SIGNED_RESPONSE_ALG` or obtained via OP metadata during issuer discovery when `RS256` is not included. * fix: refacto a bit, narrow types and add zod validation --------- Co-authored-by: Drew Toto <drew_toto@alliedtelesis.com> Co-authored-by: Nathan Sarrazin <sarrazin.nathan@gmail.com>
1 parent 9542b2c commit e1d57b5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/lib/server/auth.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const OIDConfig = z
4242
),
4343
TOLERANCE: stringWithDefault(env.OPENID_TOLERANCE),
4444
RESOURCE: stringWithDefault(env.OPENID_RESOURCE),
45+
ID_TOKEN_SIGNED_RESPONSE_ALG: z.string().optional(),
4546
})
4647
.parse(JSON5.parse(env.OPENID_CONFIG || "{}"));
4748

@@ -103,13 +104,21 @@ export async function generateCsrfToken(sessionId: string, redirectUrl: string):
103104
async function getOIDCClient(settings: OIDCSettings): Promise<BaseClient> {
104105
const issuer = await Issuer.discover(OIDConfig.PROVIDER_URL);
105106

106-
return new issuer.Client({
107+
const client_config: ConstructorParameters<typeof issuer.Client>[0] = {
107108
client_id: OIDConfig.CLIENT_ID,
108109
client_secret: OIDConfig.CLIENT_SECRET,
109110
redirect_uris: [settings.redirectURI],
110111
response_types: ["code"],
111112
[custom.clock_tolerance]: OIDConfig.TOLERANCE || undefined,
112-
});
113+
id_token_signed_response_alg: OIDConfig.ID_TOKEN_SIGNED_RESPONSE_ALG || undefined,
114+
};
115+
116+
const alg_supported = issuer.metadata["id_token_signing_alg_values_supported"];
117+
118+
if (Array.isArray(alg_supported) && !alg_supported.includes("RS256")) {
119+
client_config.id_token_signed_response_alg ??= alg_supported[0];
120+
}
121+
return new issuer.Client(client_config);
113122
}
114123

115124
export async function getOIDCAuthorizationUrl(

0 commit comments

Comments
 (0)