11import { ArcGISIdentityManager } from "@esri/arcgis-rest-request"
22import { ArcGISAuthConfig , AuthType , FeatureServiceConfig , OAuthAuthConfig , TokenAuthConfig , UsernamePasswordAuthConfig } from './ArcGISConfig'
33import { HttpClient } from "./HttpClient" ;
4+ import { ObservationProcessor } from "./ObservationProcessor" ;
45
56interface ArcGISIdentityManagerFactory {
6- create ( portal : string , server : string , config : ArcGISAuthConfig , httpClient ?: HttpClient ) : Promise < ArcGISIdentityManager >
7+ create ( portal : string , server : string , config : ArcGISAuthConfig , httpClient ?: HttpClient , processor ?: ObservationProcessor ) : Promise < ArcGISIdentityManager >
78}
89
910const OAuthIdentityManagerFactory : ArcGISIdentityManagerFactory = {
10- async create ( portal : string , server : string , auth : OAuthAuthConfig , httpClient : HttpClient ) : Promise < ArcGISIdentityManager > {
11+ async create ( portal : string , server : string , auth : OAuthAuthConfig , httpClient : HttpClient , processor : ObservationProcessor ) : Promise < ArcGISIdentityManager > {
1112 console . debug ( 'Client ID provided for authentication' )
1213 const { clientId, authToken, authTokenExpires, refreshToken, refreshTokenExpires } = auth
1314
@@ -22,14 +23,30 @@ const OAuthIdentityManagerFactory: ArcGISIdentityManagerFactory = {
2223 } else if ( refreshToken && new Date ( refreshTokenExpires || 0 ) > new Date ( ) ) {
2324 // TODO: find a way without using constructor nor httpClient
2425 const url = `${ portal } /oauth2/token?client_id=${ clientId } &refresh_token=${ refreshToken } &grant_type=refresh_token`
25- const response = await httpClient . sendGet ( url )
26- // TODO: error handling
27- return ArcGISIdentityManager . fromToken ( {
28- clientId : clientId ,
29- token : response . access_token ,
30- portal : portal
31- } ) ;
32- // TODO: update authToken to new token
26+ try {
27+ const response = await httpClient . sendGet ( url )
28+ // Update authToken to new token
29+ const config = await processor . safeGetConfig ( ) ;
30+ let service = config . featureServices . find ( service => service . url === portal ) ?. auth as OAuthAuthConfig ;
31+ const date = new Date ( ) ;
32+ date . setSeconds ( date . getSeconds ( ) + response . expires_in || 0 ) ;
33+ service = {
34+ ...service ,
35+ authToken : response . access_token ,
36+ authTokenExpires : date . getTime ( )
37+ }
38+
39+ await processor . putConfig ( config )
40+ return ArcGISIdentityManager . fromToken ( {
41+ clientId : clientId ,
42+ token : response . access_token ,
43+ tokenExpires : date ,
44+ portal : portal
45+ } ) ;
46+ } catch ( error ) {
47+ throw new Error ( 'Error occurred when using refresh token' )
48+ }
49+
3350 } else {
3451 // TODO the config, we need to let the user know UI side they need to authenticate again
3552 throw new Error ( 'Refresh token missing or expired' )
@@ -46,7 +63,7 @@ const TokenIdentityManagerFactory: ArcGISIdentityManagerFactory = {
4663 server : server ,
4764 // TODO: what do we really want to do here? esri package seems to need this optional parameter.
4865 // Use authTokenExpires if defined, otherwise set to now plus a day
49- tokenExpires : auth . authTokenExpires ? new Date ( auth . authTokenExpires ) : new Date ( Date . now ( ) + 24 * 60 * 60 * 1000 )
66+ tokenExpires : auth . authTokenExpires ? new Date ( auth . authTokenExpires ) : new Date ( Date . now ( ) + 24 * 60 * 60 * 1000 )
5067 } )
5168 return identityManager
5269 }
@@ -68,7 +85,8 @@ const authConfigMap: { [key: string]: ArcGISIdentityManagerFactory } = {
6885
6986export function getIdentityManager (
7087 config : FeatureServiceConfig ,
71- httpClient : HttpClient // TODO remove in favor of an open source lib like axios
88+ httpClient : HttpClient , // TODO remove in favor of an open source lib like axios
89+ processor : ObservationProcessor
7290) : Promise < ArcGISIdentityManager > {
7391 const auth = config . auth
7492 const authType = config . auth ?. type
@@ -79,7 +97,7 @@ export function getIdentityManager(
7997 if ( ! factory ) {
8098 throw new Error ( `No factory found for type ${ authType } ` )
8199 }
82- return factory . create ( getPortalUrl ( config . url ) , getServerUrl ( config . url ) , auth , httpClient )
100+ return factory . create ( getPortalUrl ( config . url ) , getServerUrl ( config . url ) , auth , httpClient , processor )
83101}
84102
85103
0 commit comments