Skip to content

Commit 30367af

Browse files
committed
refactor: pull structured token rejection to a shared middleware
1 parent a7e47e4 commit 30367af

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

lib/actions/introspection.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { decodeProtectedHeader } from 'jose';
2-
31
import presence from '../helpers/validate_presence.js';
42
import getClientAuth from '../shared/client_auth.js';
53
import noCache from '../shared/no_cache.js';
64
import instance from '../helpers/weak_cache.js';
75
import { urlencoded as parseBody } from '../shared/selective_body.js';
86
import rejectDupes from '../shared/reject_dupes.js';
97
import paramsMiddleware from '../shared/assemble_params.js';
10-
import { InvalidRequest, UnsupportedTokenType } from '../helpers/errors.js';
8+
import { InvalidRequest } from '../helpers/errors.js';
9+
import rejectStructuredTokens from '../shared/reject_structured_tokens.js';
1110

1211
const introspectable = new Set(['AccessToken', 'ClientCredentials', 'RefreshToken']);
1312
const JWT = 'application/token-introspection+jwt';
@@ -63,6 +62,8 @@ export default function introspectionAction(provider) {
6362
await next();
6463
},
6564

65+
rejectStructuredTokens,
66+
6667
async function jwtIntrospectionResponse(ctx, next) {
6768
if (jwtIntrospection.enabled) {
6869
const { client } = ctx.oidc;
@@ -97,15 +98,6 @@ export default function introspectionAction(provider) {
9798
async function renderTokenResponse(ctx) {
9899
const { params } = ctx.oidc;
99100

100-
let tokenIsJWT;
101-
try {
102-
tokenIsJWT = !!decodeProtectedHeader(params.token);
103-
} catch {}
104-
105-
if (tokenIsJWT) {
106-
throw new UnsupportedTokenType('Structured JWT Tokens cannot be introspected via the introspection_endpoint');
107-
}
108-
109101
ctx.body = { active: false };
110102

111103
let token;

lib/actions/revocation.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { decodeProtectedHeader } from 'jose';
2-
3-
import { UnsupportedTokenType } from '../helpers/errors.js';
41
import presence from '../helpers/validate_presence.js';
52
import instance from '../helpers/weak_cache.js';
63
import getClientAuth from '../shared/client_auth.js';
74
import { urlencoded as parseBody } from '../shared/selective_body.js';
85
import rejectDupes from '../shared/reject_dupes.js';
96
import paramsMiddleware from '../shared/assemble_params.js';
7+
import rejectStructuredTokens from '../shared/reject_structured_tokens.js';
108
import revoke from '../helpers/revoke.js';
119

1210
const revokeable = new Set(['AccessToken', 'ClientCredentials', 'RefreshToken']);
@@ -54,6 +52,8 @@ export default function revocationAction(provider) {
5452
await next();
5553
},
5654

55+
rejectStructuredTokens,
56+
5757
async function renderTokenResponse(ctx, next) {
5858
ctx.status = 200;
5959
ctx.body = '';
@@ -63,15 +63,6 @@ export default function revocationAction(provider) {
6363
async function revokeToken(ctx) {
6464
const { params } = ctx.oidc;
6565

66-
let tokenIsJWT;
67-
try {
68-
tokenIsJWT = !!decodeProtectedHeader(params.token);
69-
} catch {}
70-
71-
if (tokenIsJWT) {
72-
throw new UnsupportedTokenType('Structured JWT Tokens cannot be revoked via the revocation_endpoint');
73-
}
74-
7566
let token;
7667
switch (params.token_type_hint) {
7768
case 'access_token':
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { decodeProtectedHeader } from 'jose';
2+
3+
import { UnsupportedTokenType } from '../helpers/errors.js';
4+
5+
export default async function rejectStructuredTokens(ctx, next) {
6+
const { params } = ctx.oidc;
7+
8+
let tokenIsJWT;
9+
try {
10+
tokenIsJWT = !!decodeProtectedHeader(params.token);
11+
} catch {}
12+
13+
if (tokenIsJWT) {
14+
throw new UnsupportedTokenType(`Structured JWT Tokens cannot be ${ctx.oidc.route === 'revocation' ? 'revoked' : 'introspected'} via the ${ctx.oidc.route}_endpoint`);
15+
}
16+
17+
return next();
18+
}

0 commit comments

Comments
 (0)