@@ -2,24 +2,19 @@ import React, { useState, useEffect } from 'react'
2
2
import ApiKeyRedirect from './ApiKeyRedirect'
3
3
import { useLocation } from 'react-router'
4
4
import {
5
- saveState ,
6
5
NoHashQueryStringUtils ,
7
- saveConfigRequest ,
8
- getConfigRequest ,
9
6
saveIssuer ,
10
7
getIssuer
11
8
} from './TokenController'
12
9
import queryString from 'query-string'
13
10
import { uuidv4 } from './Util'
14
11
import { useSelector , useDispatch } from 'react-redux'
15
12
import {
16
- getUserInfo ,
17
13
getAPIAccessToken ,
18
14
checkLicensePresent ,
19
- getRandomChallengePair ,
20
15
} from 'Redux/actions'
21
16
import SessionTimeout from 'Routes/Apps/Gluu/GluuSessionTimeout'
22
- import { checkLicenseConfigValid } from '../redux/actions'
17
+ import { checkLicenseConfigValid , getOAuth2Config , getUserInfoResponse } from '../redux/actions'
23
18
import GluuTimeoutModal from 'Routes/Apps/Gluu/GluuTimeoutModal'
24
19
import GluuErrorModal from 'Routes/Apps/Gluu/GluuErrorModal'
25
20
import {
@@ -34,21 +29,20 @@ import {
34
29
AuthorizationNotifier ,
35
30
GRANT_TYPE_AUTHORIZATION_CODE ,
36
31
} from '@openid/appauth'
32
+ import { fetchUserInformation } from 'Redux/api/backend-api'
33
+ import jwt_decode from "jwt-decode" ;
37
34
38
35
export default function AppAuthProvider ( props ) {
39
36
const dispatch = useDispatch ( )
40
37
const location = useLocation ( )
41
- const [ showContent , setShowContent ] = useState ( false )
42
38
const [ roleNotFound , setRoleNotFound ] = useState ( false )
39
+ const [ showAdminUI , setShowAdminUI ] = useState ( false )
43
40
const {
44
41
config,
45
42
userinfo,
46
43
userinfo_jwt,
47
44
token,
48
45
backendIsUp,
49
- codeChallenge,
50
- codeVerifier,
51
- codeChallengeMethod,
52
46
issuer,
53
47
} = useSelector ( ( state ) => state . authReducer )
54
48
const {
@@ -63,7 +57,6 @@ export default function AppAuthProvider(props) {
63
57
const params = queryString . parse ( location . search )
64
58
if ( ! ( params . code && params . scope && params . state ) ) {
65
59
dispatch ( checkLicenseConfigValid ( ) )
66
- // dispatch(getRandomChallengePair())
67
60
}
68
61
} , [ ] )
69
62
@@ -102,7 +95,6 @@ export default function AppAuthProvider(props) {
102
95
extras,
103
96
} )
104
97
saveIssuer ( issuer )
105
- saveConfigRequest ( authRequest )
106
98
authorizationHandler . performAuthorizationRequest (
107
99
response ,
108
100
authRequest
@@ -123,13 +115,10 @@ export default function AppAuthProvider(props) {
123
115
new DefaultCrypto ( )
124
116
)
125
117
const notifier = new AuthorizationNotifier ( )
126
- const config = getConfigRequest ( )
127
118
const issuer = getIssuer ( )
128
119
129
120
notifier . setAuthorizationListener ( ( request , response , error ) => {
130
- console . log ( 'the request' , request )
131
121
if ( response ) {
132
- console . log ( `Authorization Code ${ response . code } ` )
133
122
134
123
let extras = null
135
124
if ( request . internal ) {
@@ -144,17 +133,41 @@ export default function AppAuthProvider(props) {
144
133
code : response . code ,
145
134
extras : { code_verifier : request . internal . code_verifier , scope : request . scope } ,
146
135
} )
147
- console . log ( `tokenRequest` , tokenRequest )
136
+ let authConfigs
137
+ dispatch ( getOAuth2Config ( ) )
148
138
149
139
AuthorizationServiceConfiguration . fetchFromIssuer (
150
140
issuer ,
151
141
new FetchRequestor ( )
152
142
)
153
143
. then ( ( configuration ) => {
144
+ authConfigs = configuration
154
145
return tokenHandler . performTokenRequest ( configuration , tokenRequest )
155
146
} )
156
147
. then ( ( token ) => {
157
- localStorage . setItem ( 'access_token' , token . accessToken )
148
+ return fetchUserInformation ( { userInfoEndpoint : authConfigs . userInfoEndpoint , access_token : token . accessToken , token_type : token . tokenType } )
149
+ } )
150
+ . then ( ( ujwt ) => {
151
+ if ( ! userinfo ) {
152
+ dispatch ( getUserInfoResponse ( { userinfo : jwt_decode ( ujwt ) , ujwt : ujwt } ) )
153
+ dispatch ( getAPIAccessToken ( ujwt ) )
154
+ setShowAdminUI ( true )
155
+ } else {
156
+ if ( ! userinfo . jansAdminUIRole || userinfo . jansAdminUIRole . length == 0 ) {
157
+ setShowAdminUI ( false )
158
+ alert (
159
+ 'The logged-in user do not have valid role. Logging out of Admin UI'
160
+ )
161
+ setRoleNotFound ( true )
162
+ const state = uuidv4 ( )
163
+ const sessionEndpoint = `${ authConfigs . endSessionEndpoint } ?state=${ state } &post_logout_redirect_uri=${ localStorage . getItem ( 'postLogoutRedirectUri' ) } `
164
+ window . location . href = sessionEndpoint
165
+ return null
166
+ }
167
+ if ( ! token ) {
168
+ dispatch ( getAPIAccessToken ( userinfo_jwt ) )
169
+ }
170
+ }
158
171
} )
159
172
. catch ( ( oError ) => {
160
173
setError ( oError )
@@ -177,7 +190,7 @@ export default function AppAuthProvider(props) {
177
190
178
191
return (
179
192
< React . Fragment >
180
- < SessionTimeout isAuthenticated = { showContent } />
193
+ < SessionTimeout isAuthenticated = { showAdminUI } />
181
194
< GluuTimeoutModal
182
195
description = {
183
196
'The request has been terminated as there is no response from the server for more than 60 seconds.'
@@ -191,18 +204,17 @@ export default function AppAuthProvider(props) {
191
204
}
192
205
/>
193
206
) }
194
- { showContent && props . children }
195
- { ! showContent && (
207
+ { showAdminUI && props . children }
208
+ { ! showAdminUI && (
196
209
< ApiKeyRedirect
197
210
backendIsUp = { backendIsUp }
198
211
isLicenseValid = { isLicenseValid }
199
- redirectUrl = { config . redirectUrl }
200
212
isConfigValid = { isConfigValid }
201
213
islicenseCheckResultLoaded = { islicenseCheckResultLoaded }
202
214
isLicenseActivationResultLoaded = { isLicenseActivationResultLoaded }
215
+ roleNotFound = { roleNotFound }
203
216
/>
204
217
) }
205
218
</ React . Fragment >
206
219
)
207
- }
208
-
220
+ }
0 commit comments