1
- import { BadRequestHttpError , getLoggerFor , KeyValueStorage , UnauthorizedHttpError } from '@solid/community-server' ;
2
- import { AccessToken } from '../tokens/AccessToken' ;
3
- import { JwtTokenFactory } from '../tokens/JwtTokenFactory' ;
4
- import { SerializedToken } from '../tokens/TokenFactory' ;
1
+ import { BadRequestHttpError , getLoggerFor , UnauthorizedHttpError } from '@solid/community-server' ;
2
+ import { TokenFactory } from '../tokens/TokenFactory' ;
5
3
import { HttpHandler , HttpHandlerContext , HttpHandlerResponse } from '../util/http/models/HttpHandler' ;
6
4
import { verifyRequest } from '../util/HttpMessageSignatures' ;
7
- import { jwtDecrypt } from 'jose' ;
8
5
9
6
10
7
type IntrospectionResponse = {
@@ -28,71 +25,32 @@ export class IntrospectionHandler extends HttpHandler {
28
25
/**
29
26
* Creates an introspection handler for tokens in the given token store.
30
27
*
31
- * @param tokenStore - The store containing the tokens.
32
- * @param jwtTokenFactory - The factory with which to produce JWT representations of the tokens.
28
+ * @param tokenFactory - The factory with which tokens were produced.
33
29
*/
34
30
constructor (
35
- private readonly tokenStore : KeyValueStorage < string , AccessToken > ,
36
- private readonly jwtTokenFactory : JwtTokenFactory ,
31
+ private readonly tokenFactory : TokenFactory ,
37
32
) {
38
33
super ( ) ;
39
34
}
40
35
41
- async handle ( { request} : HttpHandlerContext ) : Promise < HttpHandlerResponse < any > > {
36
+ async handle ( { request} : HttpHandlerContext ) : Promise < HttpHandlerResponse < IntrospectionResponse > > {
42
37
if ( ! await verifyRequest ( request ) ) throw new UnauthorizedHttpError ( ) ;
43
38
44
- if ( ! request . body /*|| !(request.body instanceof Object) */ ) { // todo: why was the object check here??
39
+ if ( ! request . body ) {
45
40
throw new BadRequestHttpError ( 'Missing request body.' ) ;
46
41
}
47
42
48
43
const token = new URLSearchParams ( request . body as Record < string , string > ) . get ( 'token' ) ;
49
44
try {
50
- if ( ! token ) throw new Error ( 'could not extract token from request body' )
51
- const unsignedToken = await this . processJWTToken ( token )
45
+ if ( ! token ) throw new Error ( 'could not extract token from request body' )
46
+ const unsignedToken = await this . tokenFactory . deserialize ( token ) ;
52
47
return {
53
48
status : 200 ,
54
- body : unsignedToken ,
49
+ body : { ... unsignedToken , active : true } ,
55
50
} ;
56
51
} catch ( e ) {
57
- // Todo: The JwtTokenFactory DOES NOT STORE THE TOKEN IN THE TOKENSTORE IN A WAY WE CAN RETRIEVE HERE! How to fix?
58
52
this . logger . warn ( `Token introspection failed: ${ e } ` )
59
53
throw new BadRequestHttpError ( 'Invalid request body.' ) ;
60
54
}
61
-
62
-
63
- // Opaque token left-overs - ask Wouter?
64
-
65
- // try {
66
- // const opaqueToken = new URLSearchParams(request.body).get('token');
67
- // if (!opaqueToken) throw new Error ();
68
-
69
- // const jwt = this.opaqueToJwt(opaqueToken);
70
- // return {
71
- // headers: {'content-type': 'application/json'},
72
- // status: 200,
73
- // body: jwt,
74
- // };
75
- // } catch (e) {
76
- // throw new BadRequestHttpError('Invalid request body.');
77
- // }
78
-
79
- }
80
-
81
-
82
- private async processJWTToken ( signedJWT : string ) : Promise < IntrospectionResponse > {
83
- this . logger . info ( JSON . stringify ( this . tokenStore . entries ( ) . next ( ) , null , 2 ) )
84
- const token = ( await this . tokenStore . get ( signedJWT ) ) as IntrospectionResponse ;
85
- if ( ! token ) throw new Error ( 'Token not found.' ) ;
86
- token . active = true
87
- return token
88
- }
89
-
90
- // todo: check with Wouter what the goal here is? Since the Opaque Token Factory is not used atm?
91
- private async opaqueToJwt ( opaque : string ) : Promise < SerializedToken > {
92
- const token = await this . tokenStore . get ( opaque ) ;
93
- if ( ! token ) throw new Error ( 'Token not found.' ) ;
94
-
95
- return this . jwtTokenFactory . serialize ( { ...token , active : true } as AccessToken ) ;
96
55
}
97
-
98
56
}
0 commit comments