Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added OPENID_SUBJECT_PREFIX config to prefix subject #3636

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,7 @@ ENTERPRISE_ORY_PROJECT_ID=
# Log file to write to
LOG_FILE=
# Log levels - "fatal" | "error" | "warn" | "info" (default) | "debug" | "trace"
LOG_LEVEL=
LOG_LEVEL=

# Set this config to add prefix OIDC subject wiht tenant and product to avoid any potential collissions with SAML IdP profile IDs
# OPENID_SUBJECT_PREFIX=true
1 change: 1 addition & 0 deletions lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const jacksonOptions: JacksonOption = {
},
requestProfileScope: process.env.OPENID_REQUEST_PROFILE_SCOPE === 'false' ? false : true,
forwardOIDCParams: process.env.OPENID_REQUEST_FORWARD_PARAMS === 'true' ? true : false,
subjectPrefix: process.env.OPENID_SUBJECT_PREFIX === 'true' ? true : false,
},
certs: {
publicKey: process.env.PUBLIC_KEY || '',
Expand Down
15 changes: 12 additions & 3 deletions npm/src/controller/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,15 @@ export class OAuthController implements IOAuthController {
protocol,
};

let subject = codeVal.profile.claims.id;
if (this.opts.openid?.subjectPrefix) {
subject =
codeVal.requested?.tenant + ':' + codeVal.requested?.product + ':' + codeVal.profile.claims.id;
if (subject.length > 255) {
subject = crypto.createHash('sha512').update(subject).digest('hex');
}
}

const requestHasNonce = !!codeVal.requested?.nonce;
if (requestedOIDCFlow) {
const { jwtSigningKeys, jwsAlg } = this.opts.openid ?? {};
Expand All @@ -1347,7 +1356,7 @@ export class OAuthController implements IOAuthController {
let claims: Record<string, string> = requestHasNonce ? { nonce: codeVal.requested.nonce } : {};
claims = {
...claims,
id: codeVal.profile.claims.id,
id: subject,
email: codeVal.profile.claims.email,
firstName: codeVal.profile.claims.firstName,
lastName: codeVal.profile.claims.lastName,
Expand All @@ -1360,12 +1369,12 @@ export class OAuthController implements IOAuthController {
.setProtectedHeader({ alg: jwsAlg!, kid })
.setIssuedAt()
.setIssuer(this.opts.externalUrl)
.setSubject(codeVal.profile.claims.id)
.setSubject(subject)
.setAudience(tokenVal.requested.client_id)
.setExpirationTime(`${this.opts.db.ttl}s`) // identity token only really needs to be valid long enough for it to be verified by the client application.
.sign(signingKey);
tokenVal.id_token = id_token;
tokenVal.claims.sub = codeVal.profile.claims.id;
tokenVal.claims.sub = subject;
}

const { hexKey, encVal } = encrypt(tokenVal);
Expand Down
1 change: 1 addition & 0 deletions npm/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ export interface JacksonOption {
};
requestProfileScope?: boolean; // defaults to true
forwardOIDCParams?: boolean; // defaults to false
subjectPrefix?: boolean; // defaults to false
};
certs?: {
publicKey: string;
Expand Down
Loading