1
- import { AuthorizationCode } from 'simple-oauth2'
2
- import { ModuleOptions } from 'simple-oauth2'
3
- import { randomBytes } from 'crypto'
4
- import cookie from 'cookie'
5
-
6
- import { HydraAuthorizationCodeClientOptions } from './hydra-authorization-code.interfaces'
7
- import { HydraAuthorizationCodeResult } from './hydra-authorization-code.interfaces'
8
- import { AuthenticationStateOptions } from './hydra-authorization-code.interfaces'
9
- import { State } from './hydra-authorization-code.interfaces'
10
- import { serializeState } from './state.utils'
11
- import { parseState } from './state.utils'
1
+ import type { Request } from 'express'
2
+ import type { Response } from 'express'
3
+ import type { ModuleOptions } from 'simple-oauth2'
4
+
5
+ import type { HydraAuthorizationCodeClientOptions } from './hydra-authorization-code.interfaces.js'
6
+ import type { HydraAuthorizationCodeResult } from './hydra-authorization-code.interfaces.js'
7
+ import type { AuthenticationStateOptions } from './hydra-authorization-code.interfaces.js'
8
+ import type { State } from './hydra-authorization-code.interfaces.js'
9
+
10
+ import { AuthorizationCode } from 'simple-oauth2'
11
+ import { randomBytes } from 'crypto'
12
+ import cookie from 'cookie'
13
+
14
+ import { serializeState } from './state.utils.js'
15
+ import { parseState } from './state.utils.js'
12
16
13
17
export class HydraAuthorizationCodeClient {
14
18
static NONCE_TOKEN = 'anonce'
15
19
20
+ logoutUrl : string
21
+
16
22
private client : AuthorizationCode
17
23
18
24
private redirectUri : string
19
25
20
- private scope : string [ ]
21
-
22
- logoutUrl : string
26
+ private scope : Array < string >
23
27
24
28
constructor ( options : HydraAuthorizationCodeClientOptions ) {
25
29
const credentials : ModuleOptions = {
@@ -44,11 +48,11 @@ export class HydraAuthorizationCodeClient {
44
48
this . logoutUrl = new URL ( '/oauth2/sessions/logout' , options . tokenHost ) . toString ( )
45
49
}
46
50
47
- getReturnToUrl ( req ) : string | undefined {
51
+ getReturnToUrl ( req : Request ) : string | undefined {
48
52
const query = req . query || req . params
49
53
50
54
if ( query . return_to ) {
51
- return query . return_to
55
+ return query . return_to as string
52
56
}
53
57
54
58
const referrer = req . get ( 'referrer' )
@@ -63,8 +67,8 @@ export class HydraAuthorizationCodeClient {
63
67
return undefined
64
68
}
65
69
66
- setNonce ( req , res , nonce : string ) {
67
- let setCookieHeader = req . get ( 'Set-Cookie' ) || [ ]
70
+ setNonce ( req : Request , res : Response , nonce : string ) : void {
71
+ let setCookieHeader = req . get ( 'Set-Cookie' ) || ( [ ] as Array < string > )
68
72
69
73
if ( ! Array . isArray ( setCookieHeader ) ) {
70
74
setCookieHeader = [ setCookieHeader ]
@@ -81,7 +85,7 @@ export class HydraAuthorizationCodeClient {
81
85
res . set ( 'Set-Cookie' , setCookieHeader )
82
86
}
83
87
84
- getAuthorizationUrl ( params = { } ) {
88
+ getAuthorizationUrl ( params = { } ) : string {
85
89
const state = serializeState ( params )
86
90
87
91
return this . client . authorizeURL ( {
@@ -91,29 +95,34 @@ export class HydraAuthorizationCodeClient {
91
95
} )
92
96
}
93
97
94
- authenticate ( req , res , options : AuthenticationStateOptions = { } ) {
98
+ authenticate ( req : Request , res : Response , options : AuthenticationStateOptions = { } ) : void {
95
99
const params = {
96
100
...options ,
101
+ // eslint-disable-next-line react/no-is-mounted
97
102
returnTo : this . getReturnToUrl ( req ) ,
98
103
nonce : randomBytes ( 20 ) . toString ( 'hex' ) ,
99
104
}
100
105
106
+ // eslint-disable-next-line react/no-is-mounted
101
107
this . setNonce ( req , res , params . nonce )
102
108
103
- return res . redirect ( this . getAuthorizationUrl ( params ) )
109
+ // eslint-disable-next-line react/no-is-mounted
110
+ res . redirect ( this . getAuthorizationUrl ( params ) )
104
111
}
105
112
106
- async verify ( req , res ) : Promise < HydraAuthorizationCodeResult > {
113
+ async verify ( req : Request , res : Response ) : Promise < HydraAuthorizationCodeResult > {
107
114
const query = req . query || req . params
108
115
109
116
const tokenConfig = {
110
117
redirect_uri : this . redirectUri ,
111
- code : query . code ,
112
- scope : query . scope ,
118
+ code : query . code as string ,
119
+ scope : query . scope as string ,
113
120
}
114
121
122
+ // @ts -expect-error
115
123
const state : State = parseState ( query . state ) || { }
116
124
125
+ // @ts -expect-error
117
126
const cookies = cookie . parse ( req . get ( 'cookie' ) )
118
127
119
128
if ( state . nonce !== cookies [ HydraAuthorizationCodeClient . NONCE_TOKEN ] ) {
0 commit comments