Skip to content

Commit 4c7a3a8

Browse files
committed
feat: allow audiences helper to return a single string audience
1 parent f104796 commit 4c7a3a8

File tree

4 files changed

+30
-35
lines changed

4 files changed

+30
-35
lines changed

docs/README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ true
14711471

14721472
[draft-ietf-oauth-resource-indicators-05](https://tools.ietf.org/html/draft-ietf-oauth-resource-indicators-05) - Resource Indicators for OAuth 2.0
14731473

1474-
Enables the use of `resource` parameter for the authorization and token endpoints. In order for the feature to be any useful you must also use the `audiences` helper function to validate the resource(s) and transform it to the token audience.
1474+
Enables the use of `resource` parameter for the authorization and token endpoints. In order for the feature to be any useful you must also use the `audiences` helper function to validate the resource(s) and transform it to the Access Token audience.
14751475

14761476

14771477

@@ -1528,10 +1528,7 @@ This example will
15281528
},
15291529
formats: {
15301530
AccessToken(ctx, token) {
1531-
if (Array.isArray(token.aud)) {
1532-
return 'jwt';
1533-
}
1534-
return 'opaque';
1531+
return token.aud ? 'jwt' : 'opaque';
15351532
}
15361533
},
15371534
// ...
@@ -1628,7 +1625,7 @@ _**default value**_:
16281625

16291626
### audiences
16301627

1631-
Helper used by the OP to push additional audiences to issued Access and ClientCredentials Tokens. The return value should either be falsy to omit adding additional audiences or an array of strings to push.
1628+
Helper used by the OP to set an audience to issued Access Tokens. The return value should either be falsy use the default audience (client) or an array of string aud values, or a single string aud value.
16321629

16331630

16341631
_**default value**_:
@@ -2026,21 +2023,18 @@ Configure `formats`:
20262023
{ AccessToken: 'paseto' }
20272024
```
20282025
</details>
2029-
<a name="formats-to-dynamically-decide-on-the-format-used-e-g-only-if-it-is-intended-for-more-audiences"></a><details>
2030-
<summary>(Click to expand) To dynamically decide on the format used, e.g. only if it is intended for more audiences</summary>
2026+
<a name="formats-to-dynamically-decide-on-the-format-used-e-g-only-if-it-is-intended-for-a-resource"></a><details>
2027+
<summary>(Click to expand) To dynamically decide on the format used, e.g. only if it is intended for a resource</summary>
20312028
<br>
20322029

20332030

2034-
Configure `formats`:
2031+
server Configure `formats`:
20352032

20362033

20372034
```js
20382035
{
20392036
AccessToken(ctx, token) {
2040-
if (Array.isArray(token.aud)) {
2041-
return 'jwt';
2042-
}
2043-
return 'opaque';
2037+
return token.aud ? 'jwt' : 'opaque';
20442038
}
20452039
}
20462040
```

lib/helpers/defaults.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ const DEFAULTS = {
995995
*
996996
* description: Enables the use of `resource` parameter for the authorization and token
997997
* endpoints. In order for the feature to be any useful you must also use the `audiences`
998-
* helper function to validate the resource(s) and transform it to the token audience.
998+
* helper function to validate the resource(s) and transform it to the Access Token audience.
999999
*
10001000
* example: Example use
10011001
* This example will
@@ -1042,11 +1042,7 @@ const DEFAULTS = {
10421042
* },
10431043
* formats: {
10441044
* AccessToken(ctx, token) {
1045-
* if (Array.isArray(token.aud)) {
1046-
* return 'jwt';
1047-
* }
1048-
*
1049-
* return 'opaque';
1045+
* return token.aud ? 'jwt' : 'opaque';
10501046
* }
10511047
* },
10521048
* // ...
@@ -1211,17 +1207,14 @@ const DEFAULTS = {
12111207
* { AccessToken: 'paseto' }
12121208
* ```
12131209
*
1214-
* example: To dynamically decide on the format used, e.g. only if it is intended for more audiences
1210+
* example: To dynamically decide on the format used, e.g. only if it is intended for a resource
1211+
* server
12151212
*
12161213
* Configure `formats`:
12171214
* ```js
12181215
* {
12191216
* AccessToken(ctx, token) {
1220-
* if (Array.isArray(token.aud)) {
1221-
* return 'jwt';
1222-
* }
1223-
*
1224-
* return 'opaque';
1217+
* return token.aud ? 'jwt' : 'opaque';
12251218
* }
12261219
* }
12271220
* ```
@@ -1834,9 +1827,9 @@ const DEFAULTS = {
18341827
/*
18351828
* audiences
18361829
*
1837-
* description: Helper used by the OP to push additional audiences to issued Access and
1838-
* ClientCredentials Tokens. The return value should either be falsy to omit adding additional
1839-
* audiences or an array of strings to push.
1830+
* description: Helper used by the OP to set an audience to issued Access Tokens. The return value
1831+
* should either be falsy use the default audience (client) or an array of string aud values,
1832+
* or a single string aud value.
18401833
*/
18411834
async audiences(ctx, sub, token, use) { // eslint-disable-line no-unused-vars
18421835
// @param ctx - koa request context

lib/helpers/ensure_conform.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
const assert = require('assert');
22

3-
module.exports = function ensureConform(audiences) {
4-
assert(Array.isArray(audiences) && audiences.length, 'audiences must be an array with members');
3+
module.exports = function ensureConform(audience) {
4+
assert(
5+
(Array.isArray(audience) || typeof audience === 'string') && audience.length,
6+
'audiences must be an array with members or a single string value',
7+
);
58

6-
const value = audiences.slice();
7-
value.forEach((audience) => {
8-
assert(audience && typeof audience === 'string', 'audiences must be non-empty string values');
9-
});
9+
let value;
10+
if (typeof audience === 'string') {
11+
value = audience;
12+
} else {
13+
value = [...audience];
14+
value.forEach((aud) => {
15+
assert(typeof aud === 'string' && aud.length, 'audiences must be non-empty string values');
16+
});
17+
}
1018

1119
return value;
1220
};

test/resource_indicators/resource_indicators.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ config.audiences = ({ oidc: { params, route, entities } }, sub, token, use) => {
5252
}
5353
});
5454

55-
return resources;
55+
return resources.length === 1 ? resources[0] : resources;
5656
}
5757

5858
return undefined;

0 commit comments

Comments
 (0)