1
1
import React , { useState , useEffect } from 'react'
2
2
import ApiKeyRedirect from './ApiKeyRedirect'
3
3
import { useLocation } from 'react-router'
4
- import { saveState } from './TokenController'
4
+ import {
5
+ saveState ,
6
+ NoHashQueryStringUtils ,
7
+ saveConfigRequest ,
8
+ getConfigRequest ,
9
+ saveIssuer ,
10
+ getIssuer
11
+ } from './TokenController'
5
12
import queryString from 'query-string'
6
13
import { uuidv4 } from './Util'
7
14
import { useSelector , useDispatch } from 'react-redux'
@@ -19,13 +26,16 @@ import {
19
26
FetchRequestor ,
20
27
AuthorizationServiceConfiguration ,
21
28
AuthorizationRequest ,
29
+ TokenRequest ,
22
30
RedirectRequestHandler ,
31
+ LocalStorageBackend ,
32
+ DefaultCrypto ,
33
+ BaseTokenRequestHandler ,
34
+ AuthorizationNotifier ,
35
+ GRANT_TYPE_AUTHORIZATION_CODE ,
23
36
} from '@openid/appauth'
24
37
25
- let configuration
26
-
27
38
export default function AppAuthProvider ( props ) {
28
- const authorizationHandler = new RedirectRequestHandler ( )
29
39
const dispatch = useDispatch ( )
30
40
const location = useLocation ( )
31
41
const [ showContent , setShowContent ] = useState ( false )
@@ -53,8 +63,8 @@ export default function AppAuthProvider(props) {
53
63
const params = queryString . parse ( location . search )
54
64
if ( ! ( params . code && params . scope && params . state ) ) {
55
65
dispatch ( checkLicenseConfigValid ( ) )
66
+ // dispatch(getRandomChallengePair())
56
67
}
57
- dispatch ( getRandomChallengePair ( ) )
58
68
} , [ ] )
59
69
60
70
useEffect ( ( ) => {
@@ -63,120 +73,107 @@ export default function AppAuthProvider(props) {
63
73
dispatch ( checkLicensePresent ( ) )
64
74
}
65
75
} , [ isConfigValid ] )
76
+ const [ error , setError ] = useState ( null )
77
+ const [ code , setCode ] = useState ( null )
66
78
67
79
useEffect ( ( ) => {
68
- getDerivedStateFromProps ( )
69
- } , [ isLicenseValid ] )
80
+ const authorizationHandler = new RedirectRequestHandler (
81
+ new LocalStorageBackend ( ) ,
82
+ new NoHashQueryStringUtils ( ) ,
83
+ window . location ,
84
+ new DefaultCrypto ( )
85
+ )
70
86
71
- const buildAuthzUrl = ( state , nonce ) => {
72
- console . log ( 'Config' , config )
73
- const {
74
- authzBaseUrl,
75
- clientId,
76
- scope,
77
- redirectUrl,
78
- responseType,
79
- acrValues,
80
- } = config
81
- if (
82
- ! authzBaseUrl ||
83
- ! clientId ||
84
- ! scope ||
85
- ! redirectUrl ||
86
- ! responseType ||
87
- ! acrValues ||
88
- ! state ||
89
- ! nonce ||
90
- ! codeChallenge ||
91
- ! codeVerifier ||
92
- ! codeChallengeMethod
93
- ) {
94
- console . warn ( 'Parameters to process authz code flow are missing.' )
95
- return
96
- }
97
- return `${ authzBaseUrl } ?acr_values=${ acrValues } &response_type=${ responseType } &redirect_uri=${ redirectUrl } &client_id=${ clientId } &scope=${ scope } &state=${ state } &nonce=${ nonce } &code_challenge_method=${ codeChallengeMethod } &code_challenge=${ codeChallenge } `
98
- }
99
-
100
- const getDerivedStateFromProps = async ( ) => {
101
- if ( window . location . href . indexOf ( 'logout' ) > - 1 ) {
102
- setShowContent ( true )
103
- return null
104
- }
105
- if ( ! isLicenseValid ) {
106
- setShowContent ( false )
107
- }
108
- if ( ! isConfigValid ) {
109
- setShowContent ( false )
110
- }
111
- if ( ! showContent ) {
112
- if ( ! userinfo ) {
113
- const params = queryString . parse ( location . search )
114
- if ( params . code && params . scope && params . state && ! isLicenseValid ) {
115
- dispatch ( getUserInfo ( params . code , codeVerifier ) )
116
- } else {
117
- if ( ! showContent && Object . keys ( config ) . length ) {
118
- // const state = uuidv4()
119
- // saveState(state)
120
- // const authzUrl = buildAuthzUrl(state, uuidv4())
121
- // if (authzUrl) {
122
- // window.location.href = authzUrl
123
- // }
124
- // new
125
- const state = uuidv4 ( )
126
- saveState ( state )
127
- configuration =
128
- await AuthorizationServiceConfiguration . fetchFromIssuer (
129
- issuer ,
130
- new FetchRequestor ( )
131
- )
132
- console . log ( `COMPONENT configuration` , configuration )
133
- let extras = {
134
- acr_values : config . acrValues ,
135
- nonce : uuidv4 ( ) ,
136
- code_challenge_method : codeChallengeMethod ,
137
- code_challenge : codeChallenge ,
138
- }
139
- let request = new AuthorizationRequest ( {
140
- client_id : config . clientId ,
141
- redirect_uri : config . redirectUrl ,
142
- scope : config . scope ,
143
- response_type : config . responseType ,
144
- state : state ,
145
- extras,
146
- } )
147
- console . log ( `request` , request )
148
- authorizationHandler . performAuthorizationRequest (
149
- configuration ,
150
- request
151
- )
152
- return
87
+ if ( isLicenseValid ) {
88
+ AuthorizationServiceConfiguration . fetchFromIssuer (
89
+ issuer ,
90
+ new FetchRequestor ( )
91
+ )
92
+ . then ( ( response ) => {
93
+ let extras = {
94
+ acr_values : config . acrValues ,
153
95
}
154
- }
155
- setShowContent ( false )
156
- return null
157
- } else {
158
- if ( ! userinfo . jansAdminUIRole || userinfo . jansAdminUIRole . length == 0 ) {
159
- setShowContent ( false )
160
- setRoleNotFound ( true )
161
- alert (
162
- 'The logged-in user do not have valid role. Logging out of Admin UI'
96
+ const authRequest = new AuthorizationRequest ( {
97
+ client_id : config . clientId ,
98
+ redirect_uri : config . redirectUrl ,
99
+ scope : config . scope ,
100
+ response_type : AuthorizationRequest . RESPONSE_TYPE_CODE ,
101
+ state : undefined ,
102
+ extras,
103
+ } )
104
+ saveIssuer ( issuer )
105
+ saveConfigRequest ( authRequest )
106
+ authorizationHandler . performAuthorizationRequest (
107
+ response ,
108
+ authRequest
163
109
)
164
- const state = uuidv4 ( )
165
- const sessionEndpoint = `${ config . endSessionEndpoint } ?state=${ state } &post_logout_redirect_uri=${ config . postLogoutRedirectUri } `
166
- window . location . href = sessionEndpoint
167
- return null
168
- }
169
- if ( ! token ) {
170
- dispatch ( getAPIAccessToken ( userinfo_jwt ) )
110
+ } )
111
+ . catch ( ( error ) => {
112
+ setError ( error )
113
+ } )
114
+ }
115
+ } , [ isLicenseValid ] )
116
+
117
+ useEffect ( ( ) => {
118
+ const tokenHandler = new BaseTokenRequestHandler ( new FetchRequestor ( ) )
119
+ const authorizationHandler = new RedirectRequestHandler (
120
+ new LocalStorageBackend ( ) ,
121
+ new NoHashQueryStringUtils ( ) ,
122
+ window . location ,
123
+ new DefaultCrypto ( )
124
+ )
125
+ const notifier = new AuthorizationNotifier ( )
126
+ const config = getConfigRequest ( )
127
+ const issuer = getIssuer ( )
128
+
129
+ notifier . setAuthorizationListener ( ( request , response , error ) => {
130
+ console . log ( 'the request' , request )
131
+ if ( response ) {
132
+ console . log ( `Authorization Code ${ response . code } ` )
133
+
134
+ let extras = null
135
+ if ( request . internal ) {
136
+ extras = { }
137
+ extras . code_verifier = request . internal . code_verifier
171
138
}
172
- setShowContent ( true )
173
- return null
139
+
140
+ const tokenRequest = new TokenRequest ( {
141
+ client_id : request . clientId ,
142
+ redirect_uri : request . redirectUri ,
143
+ grant_type : GRANT_TYPE_AUTHORIZATION_CODE ,
144
+ code : response . code ,
145
+ extras : { code_verifier : request . internal . code_verifier , scope : request . scope } ,
146
+ } )
147
+ console . log ( `tokenRequest` , tokenRequest )
148
+
149
+ AuthorizationServiceConfiguration . fetchFromIssuer (
150
+ issuer ,
151
+ new FetchRequestor ( )
152
+ )
153
+ . then ( ( configuration ) => {
154
+ return tokenHandler . performTokenRequest ( configuration , tokenRequest )
155
+ } )
156
+ . then ( ( token ) => {
157
+ localStorage . setItem ( 'access_token' , token . accessToken )
158
+ } )
159
+ . catch ( ( oError ) => {
160
+ setError ( oError )
161
+ } )
174
162
}
175
- } else {
176
- setShowContent ( true )
177
- return true
163
+ } )
164
+
165
+ const params = new URLSearchParams ( location . search )
166
+ setCode ( params . get ( 'code' ) )
167
+
168
+ if ( ! code ) {
169
+ setError ( 'Unable to get authorization code' )
170
+ return
178
171
}
179
- }
172
+
173
+ authorizationHandler . setAuthorizationNotifier ( notifier )
174
+ authorizationHandler . completeAuthorizationRequestIfPossible ( )
175
+ } , [ code ] )
176
+
180
177
181
178
return (
182
179
< React . Fragment >
@@ -208,3 +205,4 @@ export default function AppAuthProvider(props) {
208
205
</ React . Fragment >
209
206
)
210
207
}
208
+
0 commit comments