@@ -10,10 +10,15 @@ import { getJwkKeys, getProviderEndpoints } from "./preAuth";
10
10
import { AUTH_ENDPOINT , getOidcClient } from "../states/clients" ;
11
11
import { getLoginCache } from "../states/cache" ;
12
12
import { logger } from "./logger" ;
13
- import { getRandomString } from "./helpers" ;
13
+ import { decodeB64 , getRandomString } from "./helpers" ;
14
14
import type { ActiveOidcToken , InactiveOidcToken } from "../models/authModel" ;
15
15
import type { LoginSession , LoginCache } from "../models/loginModel" ;
16
- import { LOGIN_AUTH_FLOW , LOGIN_SCOPE } from "../models/dotenvModel" ;
16
+ import {
17
+ LOGIN_AUTH_FLOW ,
18
+ LOGIN_SCOPE ,
19
+ JWT_TOKEN_TYPE ,
20
+ } from "../models/dotenvModel" ;
21
+ import { validateTokenRoles } from "./postAuth" ;
17
22
18
23
/* eslint-disable @typescript-eslint/no-non-null-assertion */
19
24
const JWT_STRICT_AUDIENCE = [ "true" , "True" , "1" ] . includes (
@@ -114,35 +119,45 @@ export const handleCallback = async (
114
119
{ code_verifier : cache . code_verifier , state : params . state }
115
120
) ;
116
121
}
122
+ // for some reason we have make a copy
123
+ const token = JSON . parse ( JSON . stringify ( tokenSet [ JWT_TOKEN_TYPE ] as string ) ) ;
117
124
118
125
// create login session
119
126
req . session . regenerate ( ( err ) => {
120
127
if ( err ) {
121
128
next ( err ) ;
122
129
}
123
- ( req . session as LoginSession ) . access_token = tokenSet . access_token ;
124
- req . session . save ( ( err ) => {
125
- if ( err ) {
126
- return next ( err ) ;
127
- }
128
- if ( req . headers [ "x-forwarded-uri" ] ) {
129
- const originSchema = req . headers [ "x-forwarded-proto" ] ;
130
- const originHost = req . headers [ "x-forwarded-host" ] ;
131
- const originUri = req . headers [ "x-forwarded-uri" ] ;
132
- const url = `${ originSchema } ://${ originHost } ${ originUri } ` ;
133
- res . redirect ( url ) ;
134
- } else {
135
- return next ( new Error ( "Missing `X-Forwarded-Uri` Header" ) ) ;
136
- }
137
- } ) ;
130
+ try {
131
+ const payload = JSON . parse ( decodeB64 ( token . split ( "." ) [ 1 ] ) ) ;
132
+ validateTokenRoles ( payload ) ;
133
+ ( req . session as LoginSession ) . token = tokenSet [ JWT_TOKEN_TYPE ] as string ;
134
+
135
+ req . session . save ( ( err ) => {
136
+ if ( err ) {
137
+ return next ( err ) ;
138
+ }
139
+ if ( req . headers [ "x-forwarded-uri" ] ) {
140
+ const originSchema = req . headers [ "x-forwarded-proto" ] ;
141
+ const originHost = req . headers [ "x-forwarded-host" ] ;
142
+ const originUri = req . headers [ "x-forwarded-uri" ] ;
143
+ const url = `${ originSchema } ://${ originHost } ${ originUri } ` ;
144
+ res . redirect ( url ) ;
145
+ } else {
146
+ return next ( new Error ( "Missing `X-Forwarded-Uri` Header" ) ) ;
147
+ }
148
+ } ) ;
149
+ } catch ( err ) {
150
+ // return Promise.reject(`${err}`);
151
+ res . status ( 403 ) . send ( `${ err } ` ) ;
152
+ }
138
153
} ) ;
139
154
} ;
140
155
141
156
/**
142
157
* Verify token via JWT - decode it using providers JWK Keys.
143
158
*
144
- * @param token JWT access_token
145
- * @returns decoded access_token payload
159
+ * @param token JWT token ( access_token or id_token)
160
+ * @returns decoded token payload
146
161
*/
147
162
export const verifyTokenViaJwt = async ( token : string ) : Promise < JWTPayload > => {
148
163
if ( ! token . includes ( "." ) ) {
@@ -165,11 +180,11 @@ export const verifyTokenViaJwt = async (token: string): Promise<JWTPayload> => {
165
180
} ;
166
181
167
182
/**
168
- * Verify token via Token Introspection - validate access_token on the
183
+ * Verify token via Token Introspection - validate token on the
169
184
* provider authorization server.
170
185
*
171
- * @param token JWT access_token
172
- * @returns decoded access_token payload
186
+ * @param token JWT token ( access_token or id_token)
187
+ * @returns decoded token payload
173
188
*/
174
189
export const verifyTokenViaIntrospection = async ( token : string ) => {
175
190
if ( ! token . includes ( "." ) ) {
0 commit comments