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' ;
53import { HttpHandler , HttpHandlerContext , HttpHandlerResponse } from '../util/http/models/HttpHandler' ;
64import { verifyRequest } from '../util/HttpMessageSignatures' ;
7- import { jwtDecrypt } from 'jose' ;
85
96
107type IntrospectionResponse = {
@@ -28,71 +25,32 @@ export class IntrospectionHandler extends HttpHandler {
2825 /**
2926 * Creates an introspection handler for tokens in the given token store.
3027 *
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.
3329 */
3430 constructor (
35- private readonly tokenStore : KeyValueStorage < string , AccessToken > ,
36- private readonly jwtTokenFactory : JwtTokenFactory ,
31+ private readonly tokenFactory : TokenFactory ,
3732 ) {
3833 super ( ) ;
3934 }
4035
41- async handle ( { request} : HttpHandlerContext ) : Promise < HttpHandlerResponse < any > > {
36+ async handle ( { request} : HttpHandlerContext ) : Promise < HttpHandlerResponse < IntrospectionResponse > > {
4237 if ( ! await verifyRequest ( request ) ) throw new UnauthorizedHttpError ( ) ;
4338
44- if ( ! request . body /*|| !(request.body instanceof Object) */ ) { // todo: why was the object check here??
39+ if ( ! request . body ) {
4540 throw new BadRequestHttpError ( 'Missing request body.' ) ;
4641 }
4742
4843 const token = new URLSearchParams ( request . body as Record < string , string > ) . get ( 'token' ) ;
4944 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 ) ;
5247 return {
5348 status : 200 ,
54- body : unsignedToken ,
49+ body : { ... unsignedToken , active : true } ,
5550 } ;
5651 } catch ( e ) {
57- // Todo: The JwtTokenFactory DOES NOT STORE THE TOKEN IN THE TOKENSTORE IN A WAY WE CAN RETRIEVE HERE! How to fix?
5852 this . logger . warn ( `Token introspection failed: ${ e } ` )
5953 throw new BadRequestHttpError ( 'Invalid request body.' ) ;
6054 }
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 ) ;
9655 }
97-
9856}
0 commit comments