-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathAbstractManagedIdentitySource.java
More file actions
179 lines (144 loc) · 8.55 KB
/
AbstractManagedIdentitySource.java
File metadata and controls
179 lines (144 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.URISyntaxException;
//base class for all sources that support managed identity
abstract class AbstractManagedIdentitySource {
private static final Logger LOG = LoggerFactory.getLogger(AbstractManagedIdentitySource.class);
private static final String MANAGED_IDENTITY_NO_RESPONSE_RECEIVED = "[Managed Identity] Authentication unavailable. No response received from the managed identity endpoint.";
protected final ManagedIdentityRequest managedIdentityRequest;
protected final ServiceBundle serviceBundle;
ManagedIdentitySourceType managedIdentitySourceType;
ManagedIdentityIdType idType;
String userAssignedId;
private boolean isUserAssignedManagedIdentity;
private String managedIdentityUserAssignedClientId;
private String managedIdentityUserAssignedResourceId;
public AbstractManagedIdentitySource(MsalRequest msalRequest, ServiceBundle serviceBundle,
ManagedIdentitySourceType sourceType) {
this.managedIdentityRequest = (ManagedIdentityRequest) msalRequest;
this.managedIdentitySourceType = sourceType;
this.serviceBundle = serviceBundle;
this.idType = ((ManagedIdentityApplication) msalRequest.application()).getManagedIdentityId().getIdType();
this.userAssignedId = ((ManagedIdentityApplication) msalRequest.application()).getManagedIdentityId().getUserAssignedId();
}
public ManagedIdentityResponse getManagedIdentityResponse(
ManagedIdentityParameters parameters) {
createManagedIdentityRequest(parameters.resource);
managedIdentityRequest.addTokenRevocationParametersToQuery(parameters);
IHttpResponse response;
try {
HttpRequest httpRequest = new HttpRequest(managedIdentityRequest.method,
managedIdentityRequest.computeURI().toString(),
managedIdentityRequest.headers);
response = serviceBundle.getHttpHelper().executeHttpRequest(httpRequest, managedIdentityRequest.requestContext(), serviceBundle);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} catch (MsalClientException e) {
if (e.getCause() instanceof SocketException) {
String message = e.getMessage();
String correlationId = managedIdentityRequest.requestContext().correlationId();
LOG.error(LogHelper.createMessage(
"[Managed Identity] Network unreachable: " + message,
correlationId));
throw new MsalServiceException(message, MsalError.MANAGED_IDENTITY_UNREACHABLE_NETWORK,
correlationId);
}
throw e;
}
return handleResponse(parameters, response);
}
public ManagedIdentityResponse handleResponse(
ManagedIdentityParameters parameters,
IHttpResponse response) {
String message;
try {
if (response.statusCode() == HttpURLConnection.HTTP_OK) {
LOG.info("[Managed Identity] Successful response received.");
return getSuccessfulResponse(response);
} else {
message = getMessageFromErrorResponse(response);
String correlationId = managedIdentityRequest.requestContext().correlationId();
LOG.error(LogHelper.createMessage(
String.format("[Managed Identity] request failed, HttpStatusCode: %s, Error message: %s",
response.statusCode(), message),
correlationId));
throw new MsalServiceException(message, AuthenticationErrorCode.MANAGED_IDENTITY_REQUEST_FAILED,
correlationId);
}
} catch (Exception e) {
if (!(e instanceof MsalServiceException)) {
message = String.format("[Managed Identity] Unexpected exception occurred when parsing the response, HttpStatusCode: %s, Error message: %s",
response.statusCode(), e.getMessage());
String correlationId = managedIdentityRequest.requestContext().correlationId();
LOG.error(LogHelper.createMessage(message, correlationId));
throw new MsalServiceException(message, AuthenticationErrorCode.MANAGED_IDENTITY_REQUEST_FAILED,
correlationId);
} else {
throw e;
}
}
}
public abstract void createManagedIdentityRequest(String resource);
protected ManagedIdentityResponse getSuccessfulResponse(IHttpResponse response) {
ManagedIdentityResponse managedIdentityResponse;
try {
managedIdentityResponse = JsonHelper.convertJsonStringToJsonSerializableObject(response.body(), ManagedIdentityResponse::fromJson);
} catch (MsalJsonParsingException e) {
throw new MsalJsonParsingException(String.format(MsalErrorMessage.MANAGED_IDENTITY_RESPONSE_PARSE_FAILURE, response.statusCode(), e.getMessage()), MsalError.MANAGED_IDENTITY_RESPONSE_PARSE_FAILURE, managedIdentitySourceType);
}
if (managedIdentityResponse == null || managedIdentityResponse.getAccessToken() == null
|| managedIdentityResponse.getAccessToken().isEmpty() || managedIdentityResponse.getExpiresOn() == null
|| managedIdentityResponse.getExpiresOn().isEmpty()) {
String message = "[Managed Identity] Response is either null or insufficient for authentication.";
String correlationId = managedIdentityRequest.requestContext().correlationId();
LOG.error(LogHelper.createMessage(message, correlationId));
throw new MsalServiceException(message, MsalError.MANAGED_IDENTITY_REQUEST_FAILED,
correlationId);
}
return managedIdentityResponse;
}
protected String getMessageFromErrorResponse(IHttpResponse response) {
ManagedIdentityErrorResponse managedIdentityErrorResponse;
try {
managedIdentityErrorResponse = JsonHelper.convertJsonStringToJsonSerializableObject(response.body(), ManagedIdentityErrorResponse::fromJson);
} catch (MsalJsonParsingException e) {
throw new MsalJsonParsingException(String.format(MsalErrorMessage.MANAGED_IDENTITY_RESPONSE_PARSE_FAILURE, response.statusCode(), e.getMessage()), MsalError.MANAGED_IDENTITY_RESPONSE_PARSE_FAILURE, managedIdentitySourceType);
}
if (managedIdentityErrorResponse == null) {
return MANAGED_IDENTITY_NO_RESPONSE_RECEIVED;
}
if (managedIdentityErrorResponse.getMessage() != null && !managedIdentityErrorResponse.getMessage().isEmpty()) {
return String.format("[Managed Identity] Error Message: %s Managed Identity Correlation ID: %s Use this Correlation ID for further investigation.",
managedIdentityErrorResponse.getMessage(), managedIdentityErrorResponse.getCorrelationId());
}
return String.format("[Managed Identity] Error Code: %s Error Message: %s",
managedIdentityErrorResponse.getError(), managedIdentityErrorResponse.getErrorDescription());
}
protected static IEnvironmentVariables getEnvironmentVariables() {
return ManagedIdentityApplication.environmentVariables == null ?
new EnvironmentVariables() : ManagedIdentityApplication.environmentVariables;
}
public boolean isUserAssignedManagedIdentity() {
return this.isUserAssignedManagedIdentity;
}
public String getManagedIdentityUserAssignedClientId() {
return this.managedIdentityUserAssignedClientId;
}
public String getManagedIdentityUserAssignedResourceId() {
return this.managedIdentityUserAssignedResourceId;
}
public void setUserAssignedManagedIdentity(boolean isUserAssignedManagedIdentity) {
this.isUserAssignedManagedIdentity = isUserAssignedManagedIdentity;
}
public void setManagedIdentityUserAssignedClientId(String managedIdentityUserAssignedClientId) {
this.managedIdentityUserAssignedClientId = managedIdentityUserAssignedClientId;
}
public void setManagedIdentityUserAssignedResourceId(String managedIdentityUserAssignedResourceId) {
this.managedIdentityUserAssignedResourceId = managedIdentityUserAssignedResourceId;
}
}