Skip to content

Commit 58d281f

Browse files
committed
add more details to the #findById() result #claim() call to allow for fine grain tuning of returned claims
1 parent a792262 commit 58d281f

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

docs/configuration.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,11 +870,18 @@ affects: authorization, authorization_code and refresh_token grants, id token cl
870870
default value:
871871
```js
872872
async findById(ctx, id, token) {
873-
// token is a reference to the token used for which a given account is being loaded,
874-
// is undefined in scenarios where claims are returned from authorization endpoint
873+
// "token" is a reference to the token used for which a given account is being loaded,
874+
// is undefined in scenarios where claims are returned from authorization endpoint
875875
return {
876876
accountId: id,
877-
async claims() { return { sub: id }; },
877+
async claims(use, scope) {
878+
// "use" can either be "id_token" or "userinfo", depending on where the specific claims are
879+
// intended to be put in
880+
// "scope" is the intended scope, while oidc-provider will mask claims depending on the
881+
// scope automatically you might want to skip loading some claims from external resources
882+
// etc. based on this detail or not return them in id tokens but only userinfo and so on.
883+
return { sub: id };
884+
},
878885
};
879886
}
880887
```
@@ -1112,7 +1119,7 @@ default value:
11121119

11131120
### routes
11141121

1115-
Routing values used by the OP
1122+
Routing values used by the OP. Only provide routes starting with "/"
11161123
affects: routing
11171124

11181125
default value:

lib/actions/authorization/process_response_types.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ module.exports = (provider) => {
5353
}
5454

5555
async function idTokenHandler(ctx) {
56-
const token = new IdToken(Object.assign({}, await ctx.oidc.account.claims(), {
57-
acr: ctx.oidc.acr,
58-
amr: ctx.oidc.amr,
59-
auth_time: ctx.oidc.session.authTime(),
60-
}), ctx.oidc.client.sectorIdentifier);
56+
const token = new IdToken(Object.assign(
57+
{},
58+
await ctx.oidc.account.claims('id_token', ctx.oidc.params.scope),
59+
{
60+
acr: ctx.oidc.acr,
61+
amr: ctx.oidc.amr,
62+
auth_time: ctx.oidc.session.authTime(),
63+
},
64+
), ctx.oidc.client.sectorIdentifier);
6165

6266
token.scope = ctx.oidc.params.scope;
6367
token.mask = get(ctx.oidc.claims, 'id_token', {});

lib/actions/grants/authorization_code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module.exports.handler = function getAuthorizationCodeHandler(provider) {
9797
refreshToken = await rt.save();
9898
}
9999

100-
const token = new IdToken(Object.assign({}, await account.claims(), {
100+
const token = new IdToken(Object.assign({}, await account.claims('id_token', code.scope), {
101101
acr: code.acr,
102102
amr: code.amr,
103103
auth_time: code.authTime,

lib/actions/grants/refresh_token.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ module.exports.handler = function getRefreshTokenHandler(provider) {
8787
const accessToken = await at.save();
8888
const { expiresIn } = AccessToken;
8989

90-
const token = new IdToken(Object.assign({}, await account.claims(), {
90+
const token = new IdToken(Object.assign({}, await account.claims('id_token', at.scope), {
9191
acr: refreshToken.acr,
9292
amr: refreshToken.amr,
9393
auth_time: refreshToken.authTime,

lib/actions/userinfo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ module.exports = function userinfoAction(provider) {
118118
if (client.userinfoSignedResponseAlg || client.userinfoEncryptedResponseAlg) {
119119
const { IdToken } = provider;
120120
const token = new IdToken(
121-
await ctx.oidc.account.claims(),
121+
await ctx.oidc.account.claims('userinfo', scope),
122122
client.sectorIdentifier,
123123
);
124124

@@ -137,7 +137,7 @@ module.exports = function userinfoAction(provider) {
137137
ctx.type = 'application/jwt; charset=utf-8';
138138
} else {
139139
const mask = new Claims(
140-
await ctx.oidc.account.claims(),
140+
await ctx.oidc.account.claims('userinfo', scope),
141141
client.sectorIdentifier,
142142
);
143143

lib/helpers/defaults.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const DEFAULTS = {
178178
/*
179179
* routes
180180
*
181-
* description: Routing values used by the OP
181+
* description: Routing values used by the OP. Only provide routes starting with "/"
182182
* affects: routing
183183
*/
184184
routes: {
@@ -450,12 +450,19 @@ const DEFAULTS = {
450450
* affects: authorization, authorization_code and refresh_token grants, id token claims
451451
*/
452452
async findById(ctx, id, token) { // eslint-disable-line no-unused-vars
453-
// token is a reference to the token used for which a given account is being loaded,
454-
// is undefined in scenarios where claims are returned from authorization endpoint
453+
// "token" is a reference to the token used for which a given account is being loaded,
454+
// is undefined in scenarios where claims are returned from authorization endpoint
455455
changeme('findById', 'to use your own account model');
456456
return {
457457
accountId: id,
458-
async claims() { return { sub: id }; },
458+
async claims(use, scope) { // eslint-disable-line no-unused-vars
459+
// "use" can either be "id_token" or "userinfo", depending on where the specific claims are
460+
// intended to be put in
461+
// "scope" is the intended scope, while oidc-provider will mask claims depending on the
462+
// scope automatically you might want to skip loading some claims from external resources
463+
// etc. based on this detail or not return them in id tokens but only userinfo and so on.
464+
return { sub: id };
465+
},
459466
};
460467
},
461468

0 commit comments

Comments
 (0)