mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-11-09 23:40:05 +00:00
fix azure null refresh token issue (#20725)
This commit is contained in:
parent
5b94056fb5
commit
6db8454649
@ -771,36 +771,51 @@ public class AuthenticationCodeFlowHandler {
|
|||||||
|
|
||||||
private void refreshAccessTokenAzureAd2Token(
|
private void refreshAccessTokenAzureAd2Token(
|
||||||
AzureAd2OidcConfiguration azureConfig, OidcCredentials azureAdProfile) {
|
AzureAd2OidcConfiguration azureConfig, OidcCredentials azureAdProfile) {
|
||||||
|
|
||||||
HttpURLConnection connection = null;
|
HttpURLConnection connection = null;
|
||||||
try {
|
try {
|
||||||
|
RefreshToken refreshToken = azureAdProfile.getRefreshToken();
|
||||||
|
if (refreshToken == null || refreshToken.getValue() == null) {
|
||||||
|
throw new TechnicalException("No refresh token available to request new access token.");
|
||||||
|
}
|
||||||
|
|
||||||
Map<String, String> headers = new HashMap<>();
|
Map<String, String> headers = new HashMap<>();
|
||||||
headers.put(
|
headers.put(
|
||||||
HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.APPLICATION_FORM_ENCODED_HEADER_VALUE);
|
HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.APPLICATION_FORM_ENCODED_HEADER_VALUE);
|
||||||
headers.put(HttpConstants.ACCEPT_HEADER, HttpConstants.APPLICATION_JSON);
|
headers.put(HttpConstants.ACCEPT_HEADER, HttpConstants.APPLICATION_JSON);
|
||||||
// get the token endpoint from discovery URI
|
|
||||||
URL tokenEndpointURL = azureConfig.findProviderMetadata().getTokenEndpointURI().toURL();
|
URL tokenEndpointURL = azureConfig.findProviderMetadata().getTokenEndpointURI().toURL();
|
||||||
connection = HttpUtils.openPostConnection(tokenEndpointURL, headers);
|
connection = HttpUtils.openPostConnection(tokenEndpointURL, headers);
|
||||||
|
|
||||||
BufferedWriter out =
|
String requestBody = azureConfig.makeOauth2TokenRequest(refreshToken.getValue());
|
||||||
|
byte[] bodyBytes = requestBody.getBytes(StandardCharsets.UTF_8);
|
||||||
|
connection.setFixedLengthStreamingMode(bodyBytes.length);
|
||||||
|
|
||||||
|
try (BufferedWriter out =
|
||||||
new BufferedWriter(
|
new BufferedWriter(
|
||||||
new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8));
|
new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8))) {
|
||||||
out.write(azureConfig.makeOauth2TokenRequest(azureAdProfile.getRefreshToken().getValue()));
|
out.write(requestBody);
|
||||||
out.close();
|
}
|
||||||
|
|
||||||
int responseCode = connection.getResponseCode();
|
int responseCode = connection.getResponseCode();
|
||||||
if (responseCode != 200) {
|
if (responseCode != 200) {
|
||||||
throw new TechnicalException(
|
String error = HttpUtils.buildHttpErrorMessage(connection);
|
||||||
"request for access token failed: " + HttpUtils.buildHttpErrorMessage(connection));
|
LOG.warn("Token refresh failed ({}): {}", responseCode, error);
|
||||||
|
throw new TechnicalException("Token refresh failed with status: " + responseCode);
|
||||||
}
|
}
|
||||||
var body = HttpUtils.readBody(connection);
|
|
||||||
|
String body = HttpUtils.readBody(connection);
|
||||||
Map<String, Object> res = JsonUtils.readValue(body, new TypeReference<>() {});
|
Map<String, Object> res = JsonUtils.readValue(body, new TypeReference<>() {});
|
||||||
|
|
||||||
// Populate Tokens
|
|
||||||
azureAdProfile.setAccessToken(new BearerAccessToken((String) res.get("access_token")));
|
azureAdProfile.setAccessToken(new BearerAccessToken((String) res.get("access_token")));
|
||||||
azureAdProfile.setRefreshToken(new RefreshToken((String) res.get("refresh_token")));
|
azureAdProfile.setRefreshToken(new RefreshToken((String) res.get("refresh_token")));
|
||||||
azureAdProfile.setIdToken(SignedJWT.parse((String) res.get("id_token")));
|
|
||||||
} catch (final IOException | ParseException e) {
|
if (res.containsKey("id_token")) {
|
||||||
throw new TechnicalException(e);
|
azureAdProfile.setIdToken(SignedJWT.parse((String) res.get("id_token")));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException | ParseException e) {
|
||||||
|
throw new TechnicalException("Exception while refreshing Azure AD token", e);
|
||||||
} finally {
|
} finally {
|
||||||
HttpUtils.closeConnection(connection);
|
HttpUtils.closeConnection(connection);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user