diff --git a/src/token/token.service.ts b/src/token/token.service.ts index cfc0c98c..d6675974 100644 --- a/src/token/token.service.ts +++ b/src/token/token.service.ts @@ -1,6 +1,7 @@ //SDPX-License-Identifier: MIT //SDPX-FileCopyrightText: 2022 Philip Rebbe //SDPX-FileCopyrightText: 2022 Raghunandan Arava +//SDPX-FileCopyrightText: 2022 Sarah Julia Kriesch import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; import * as jose from 'jose'; @@ -94,13 +95,14 @@ export class TokenService { return await this.getToken(String(issuer.token_endpoint), grantBody); } - async decodeToken(tokenString: string): Promise<[string, string]> { - const [header, payload] = this.decodeTokenString(tokenString); + async decodeToken(tokenString: string): Promise<[string, string, string]> { + const [header, payload, signature] = this.decodeTokenString(tokenString); const formattedHeader = JSON.stringify(header, undefined, 2); const formattedPayload = JSON.stringify(payload, undefined, 2); + const formattedSignature = JSON.stringify(signature, undefined, 2); - return [formattedHeader, formattedPayload]; + return [formattedHeader, formattedPayload, formattedSignature]; } async validateTokenSignature( @@ -137,7 +139,7 @@ export class TokenService { ); } - private decodeTokenString(tokenString: string): [string, string] { + private decodeTokenString(tokenString: string): [string, string, string] { if (tokenString === undefined || tokenString === '') { throw new HttpException('There was no token to decode!', 400); } @@ -150,14 +152,20 @@ export class TokenService { const header = this.decodeBase64EncodedString(tokenParts[0]); const body = this.decodeBase64EncodedString(tokenParts[1]); + const signatures = this.decodeBase64EncodedStringKey(tokenParts[2]); - return [header, body]; + + return [header, body, signatures]; } private decodeBase64EncodedString(input: string): string { return JSON.parse(new TextDecoder().decode(jose.base64url.decode(input))); } + private async decodeBase64EncodedStringKey(input: string): Promise { + return Promise.string(await jose.JWK.asKey(new TextDecoder().decode(jose.base64url.decode(input)))); + } + private async validateTokenStringWithExternalKeys( tokenString: string, issuer: string,