drewtoto dt-ati nsarrazin HF Staff commited on
Commit
b611f21
·
unverified ·
1 Parent(s): 3360a5d

feat: explicit config of id token alg (#1567) (#1568)

Browse files

* 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 <[email protected]>
Co-authored-by: Nathan Sarrazin <[email protected]>

Files changed (1) hide show
  1. src/lib/server/auth.ts +11 -2
src/lib/server/auth.ts CHANGED
@@ -42,6 +42,7 @@ export const OIDConfig = z
42
  ),
43
  TOLERANCE: stringWithDefault(env.OPENID_TOLERANCE),
44
  RESOURCE: stringWithDefault(env.OPENID_RESOURCE),
 
45
  })
46
  .parse(JSON5.parse(env.OPENID_CONFIG || "{}"));
47
 
@@ -103,13 +104,21 @@ export async function generateCsrfToken(sessionId: string, redirectUrl: string):
103
  async function getOIDCClient(settings: OIDCSettings): Promise<BaseClient> {
104
  const issuer = await Issuer.discover(OIDConfig.PROVIDER_URL);
105
 
106
- return new issuer.Client({
107
  client_id: OIDConfig.CLIENT_ID,
108
  client_secret: OIDConfig.CLIENT_SECRET,
109
  redirect_uris: [settings.redirectURI],
110
  response_types: ["code"],
111
  [custom.clock_tolerance]: OIDConfig.TOLERANCE || undefined,
112
- });
 
 
 
 
 
 
 
 
113
  }
114
 
115
  export async function getOIDCAuthorizationUrl(
 
42
  ),
43
  TOLERANCE: stringWithDefault(env.OPENID_TOLERANCE),
44
  RESOURCE: stringWithDefault(env.OPENID_RESOURCE),
45
+ ID_TOKEN_SIGNED_RESPONSE_ALG: z.string().optional(),
46
  })
47
  .parse(JSON5.parse(env.OPENID_CONFIG || "{}"));
48
 
 
104
  async function getOIDCClient(settings: OIDCSettings): Promise<BaseClient> {
105
  const issuer = await Issuer.discover(OIDConfig.PROVIDER_URL);
106
 
107
+ const client_config: ConstructorParameters<typeof issuer.Client>[0] = {
108
  client_id: OIDConfig.CLIENT_ID,
109
  client_secret: OIDConfig.CLIENT_SECRET,
110
  redirect_uris: [settings.redirectURI],
111
  response_types: ["code"],
112
  [custom.clock_tolerance]: OIDConfig.TOLERANCE || undefined,
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);
122
  }
123
 
124
  export async function getOIDCAuthorizationUrl(