Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added runs.json
Empty file.
Empty file added runs2.json
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,9 @@ public void setOtherClaims(String name, Object value) {

}

@Override
public Response retrieveToken(KeycloakSession session, FederatedIdentityModel identity) {
protected void refreshStoredToken(KeycloakSession session, FederatedIdentityModel identity) {
try {
if (identity.getToken().startsWith("{")) {
if (identity.getToken() != null && identity.getToken().startsWith("{")) {
OAuthResponse previousResponse = JsonSerialization.readValue(identity.getToken(), OAuthResponse.class);
Long exp = previousResponse.getAccessTokenExpiration();
if (needsRefresh(exp) && previousResponse.getRefreshToken() != null) {
Expand All @@ -322,6 +321,11 @@ public Response retrieveToken(KeycloakSession session, FederatedIdentityModel id
throw new WebApplicationException("Unable to refresh token", e,
Response.status(Response.Status.BAD_GATEWAY).entity(error).type(MediaType.APPLICATION_JSON).build());
}
}

@Override
public Response retrieveToken(KeycloakSession session, FederatedIdentityModel identity) {
refreshStoredToken(session, identity);
return Response.ok(identity.getToken()).type(MediaType.APPLICATION_JSON).build();
}

Expand Down Expand Up @@ -371,6 +375,11 @@ public Response retrieveToken(KeycloakSession session, FederatedIdentityModel id
}

if (Booleans.isTrue(getConfig().isStoreToken())) {
String originalToken = identity.getToken();
refreshStoredToken(session, identity);
if (!java.util.Objects.equals(originalToken, identity.getToken())) {
session.users().updateFederatedIdentity(session.getContext().getRealm(), user, identity);
Comment on lines +378 to +381
}
response = exchangeStoredToken(uriInfo, null, null, userSession, user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ private Response getTokenV2(String providerAlias) {
}

// now it is OK to retrieve the token from the session or the database
String oldToken = identity.getToken();
try {
Response response = identityProvider.retrieveToken(session, identity, userSession, authResult.user());
event.success();
Expand All @@ -599,6 +600,10 @@ private Response getTokenV2(String providerAlias) {
event.detail(Details.REASON, e.getMessage());
event.error(Errors.INVALID_REQUEST);
throw new CorsErrorResponseException(cors, OAuthErrorException.INVALID_REQUEST, "Failed to retrieve token from identity provider", Response.Status.BAD_REQUEST);
} finally {
if (Booleans.isTrue(model.isStoreToken()) && !Objects.equals(oldToken, identity.getToken())) {
session.users().updateFederatedIdentity(session.getContext().getRealm(), authResult.user(), identity);
}
}
}

Expand Down