@@ -22,6 +22,7 @@ import { IdTokenEntity } from "../unifiedCache/entities/IdTokenEntity";
22
22
import { AccessTokenEntity } from "../unifiedCache/entities/AccessTokenEntity" ;
23
23
import { RefreshTokenEntity } from "../unifiedCache/entities/RefreshTokenEntity" ;
24
24
import { InteractionRequiredAuthError } from "../error/InteractionRequiredAuthError" ;
25
+ import { CacheRecord } from "../unifiedCache/entities/CacheRecord" ;
25
26
26
27
/**
27
28
* Class that handles response parsing.
@@ -104,81 +105,32 @@ export class ResponseHandler {
104
105
* @param state
105
106
*/
106
107
generateAuthenticationResult ( serverTokenResponse : ServerAuthorizationTokenResponse , authority : Authority ) : AuthenticationResult {
107
- // Retrieve current account if in Cache
108
- // TODO: add this once the req for cache look up for tokens is confirmed
109
108
110
- const authenticationResult = this . processTokenResponse ( serverTokenResponse , authority ) ;
111
-
112
- const environment = authority . canonicalAuthorityUrlComponents . HostNameAndPort ;
113
- this . addCredentialsToCache ( authenticationResult , environment , serverTokenResponse . refresh_token ) ;
114
-
115
- return authenticationResult ;
116
- }
117
-
118
- /**
119
- * Returns a new AuthenticationResult with the data from original result filled with the relevant data.
120
- * @param authenticationResult
121
- * @param idTokenString(raw idToken in the server response)
122
- */
123
- processTokenResponse ( serverTokenResponse : ServerAuthorizationTokenResponse , authority : Authority ) : AuthenticationResult {
124
- const authenticationResult : AuthenticationResult = {
125
- uniqueId : "" ,
126
- tenantId : "" ,
127
- tokenType : "" ,
128
- idToken : null ,
129
- idTokenClaims : null ,
130
- accessToken : "" ,
131
- scopes : [ ] ,
132
- expiresOn : null ,
133
- familyId : null
134
- } ;
135
-
136
- // IdToken
109
+ // create an idToken object (not entity)
137
110
const idTokenObj = new IdToken ( serverTokenResponse . id_token , this . cryptoObj ) ;
138
111
139
- // if account is not in cache, append it to the cache
140
- this . addAccountToCache ( serverTokenResponse , idTokenObj , authority ) ;
141
-
142
- // TODO: Check how this changes for auth code response
143
- const expiresSeconds = Number ( idTokenObj . claims . exp ) ;
144
- if ( expiresSeconds && ! authenticationResult . expiresOn ) {
145
- authenticationResult . expiresOn = new Date ( expiresSeconds * 1000 ) ;
146
- }
112
+ // save the response tokens
113
+ const cacheRecord = this . generateCacheRecord ( serverTokenResponse , idTokenObj , authority ) ;
114
+ this . uCacheManager . saveCacheRecord ( cacheRecord ) ;
147
115
148
116
// Expiration calculation
149
117
const expiresInSeconds = TimeUtils . nowSeconds ( ) + serverTokenResponse . expires_in ;
150
118
const extendedExpiresInSeconds = expiresInSeconds + serverTokenResponse . ext_expires_in ;
151
- // Set consented scopes in response
152
- const responseScopes = ScopeSet . fromString ( serverTokenResponse . scope , this . clientId , true ) ;
153
119
154
- return {
155
- ... authenticationResult ,
120
+ const responseScopes = ScopeSet . fromString ( serverTokenResponse . scope , this . clientId , true ) ;
121
+ const authenticationResult : AuthenticationResult = {
156
122
uniqueId : idTokenObj . claims . oid || idTokenObj . claims . sub ,
157
123
tenantId : idTokenObj . claims . tid ,
124
+ scopes : responseScopes . asArray ( ) ,
158
125
idToken : idTokenObj . rawIdToken ,
159
126
idTokenClaims : idTokenObj . claims ,
160
127
accessToken : serverTokenResponse . access_token ,
161
128
expiresOn : new Date ( expiresInSeconds ) ,
162
129
extExpiresOn : new Date ( extendedExpiresInSeconds ) ,
163
- scopes : responseScopes . asArray ( ) ,
164
- familyId : serverTokenResponse . foci ,
130
+ familyId : serverTokenResponse . foci || null ,
165
131
} ;
166
- }
167
132
168
- /**
169
- * if Account is not in the cache, generateAccount and append it to the cache
170
- * @param serverTokenResponse
171
- * @param idToken
172
- * @param authority
173
- */
174
- addAccountToCache ( serverTokenResponse : ServerAuthorizationTokenResponse , idToken : IdToken , authority : Authority ) : void {
175
- const environment = authority . canonicalAuthorityUrlComponents . HostNameAndPort ;
176
- let accountEntity : AccountEntity ;
177
- const cachedAccount : AccountEntity = this . uCacheManager . getAccount ( this . homeAccountIdentifier , environment , idToken . claims . tid ) ;
178
- if ( ! cachedAccount ) {
179
- accountEntity = this . generateAccountEntity ( serverTokenResponse , idToken , authority ) ;
180
- this . uCacheManager . addAccountEntity ( accountEntity ) ;
181
- }
133
+ return authenticationResult ;
182
134
}
183
135
184
136
/**
@@ -205,35 +157,53 @@ export class ResponseHandler {
205
157
}
206
158
207
159
/**
208
- * Appends the minted tokens to the in-memory cache
209
- * @param authenticationResult
160
+ * Generates CacheRecord
161
+ * @param serverTokenResponse
162
+ * @param idTokenObj
210
163
* @param authority
211
164
*/
212
- addCredentialsToCache (
213
- authenticationResult : AuthenticationResult ,
214
- authority : string ,
215
- refreshToken : string
216
- ) : void {
217
- const idTokenEntity = IdTokenEntity . createIdTokenEntity (
165
+ generateCacheRecord ( serverTokenResponse : ServerAuthorizationTokenResponse , idTokenObj : IdToken , authority : Authority ) : CacheRecord {
166
+
167
+ const cacheRecord = new CacheRecord ( ) ;
168
+
169
+ // Account
170
+ cacheRecord . account = this . generateAccountEntity (
171
+ serverTokenResponse ,
172
+ idTokenObj ,
173
+ authority
174
+ ) ;
175
+
176
+ // IdToken
177
+ cacheRecord . idToken = IdTokenEntity . createIdTokenEntity (
218
178
this . homeAccountIdentifier ,
219
- authenticationResult ,
179
+ authority . canonicalAuthorityUrlComponents . HostNameAndPort ,
180
+ serverTokenResponse . id_token ,
220
181
this . clientId ,
221
- authority
182
+ idTokenObj . claims . tid
222
183
) ;
223
- const accessTokenEntity = AccessTokenEntity . createAccessTokenEntity (
184
+
185
+ // AccessToken
186
+ const responseScopes = ScopeSet . fromString ( serverTokenResponse . scope , this . clientId , true ) ;
187
+ cacheRecord . accessToken = AccessTokenEntity . createAccessTokenEntity (
224
188
this . homeAccountIdentifier ,
225
- authenticationResult ,
189
+ authority . canonicalAuthorityUrlComponents . HostNameAndPort ,
190
+ serverTokenResponse . access_token ,
226
191
this . clientId ,
227
- authority
192
+ idTokenObj . claims . tid ,
193
+ responseScopes . asArray ( ) . join ( " " ) ,
194
+ serverTokenResponse . expires_in ,
195
+ serverTokenResponse . ext_expires_in
228
196
) ;
229
- const refreshTokenEntity = RefreshTokenEntity . createRefreshTokenEntity (
197
+
198
+ // refreshToken
199
+ cacheRecord . refreshToken = RefreshTokenEntity . createRefreshTokenEntity (
230
200
this . homeAccountIdentifier ,
231
- authenticationResult ,
232
- refreshToken ,
201
+ authority . canonicalAuthorityUrlComponents . HostNameAndPort ,
202
+ serverTokenResponse . refresh_token ,
233
203
this . clientId ,
234
- authority
204
+ serverTokenResponse . foci
235
205
) ;
236
206
237
- this . uCacheManager . addCredentialCache ( accessTokenEntity , idTokenEntity , refreshTokenEntity ) ;
207
+ return cacheRecord ;
238
208
}
239
209
}
0 commit comments