Skip to content

Commit 1e65e2e

Browse files
authored
Merge pull request #510 from AzureAD/SJAIN/obo-service-principal
changes for obo flow to work with Service Principal
2 parents 96457fc + e683d2b commit 1e65e2e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/main/java/com/microsoft/aad/msal4j/TokenCache.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ void saveTokens(TokenRequestExecutor tokenRequestExecutor, AuthenticationResult
207207
idTokens.put(idTokenEntity.getKey(), idTokenEntity);
208208

209209
AccountCacheEntity accountCacheEntity = authenticationResult.accountCacheEntity();
210-
accountCacheEntity.environment(environment);
211-
accounts.put(accountCacheEntity.getKey(), accountCacheEntity);
210+
if(accountCacheEntity!=null) {
211+
accountCacheEntity.environment(environment);
212+
accounts.put(accountCacheEntity.getKey(), accountCacheEntity);
213+
}
212214
}
213215
} finally {
214216
lock.writeLock().unlock();
@@ -533,6 +535,18 @@ private Optional<IdTokenCacheEntity> getIdTokenCacheEntity(
533535
).findAny();
534536
}
535537

538+
private Optional<RefreshTokenCacheEntity> getRefreshTokenCacheEntity(
539+
String clientId,
540+
Set<String> environmentAliases,
541+
String userAssertionHash) {
542+
return refreshTokens.values().stream().filter(
543+
refreshToken ->
544+
userAssertionHashMatches(refreshToken, userAssertionHash) &&
545+
environmentAliases.contains(refreshToken.environment) &&
546+
refreshToken.clientId.equals(clientId)
547+
).findAny();
548+
}
549+
536550
private Optional<RefreshTokenCacheEntity> getRefreshTokenCacheEntity(
537551
IAccount account,
538552
String clientId,
@@ -683,6 +697,11 @@ AuthenticationResult getCachedAuthenticationResult(
683697
getIdTokenCacheEntity(authority, clientId, environmentAliases, userAssertionHash);
684698

685699
idTokenCacheEntity.ifPresent(tokenCacheEntity -> builder.idToken(tokenCacheEntity.secret));
700+
701+
Optional<RefreshTokenCacheEntity> rtCacheEntity = getRefreshTokenCacheEntity(clientId, environmentAliases, userAssertionHash);
702+
703+
rtCacheEntity.ifPresent(refreshTokenCacheEntity ->
704+
builder.refreshToken(refreshTokenCacheEntity.secret));
686705
} finally {
687706
lock.readLock().unlock();
688707
}

0 commit comments

Comments
 (0)