This repository was archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathClientCredentialProvider.java
More file actions
105 lines (94 loc) · 3.6 KB
/
Copy pathClientCredentialProvider.java
File metadata and controls
105 lines (94 loc) · 3.6 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
package com.microsoft.graph.auth.confidentialClient;
import java.util.List;
import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import com.microsoft.graph.auth.AuthConstants;
import com.microsoft.graph.auth.BaseAuthentication;
import com.microsoft.graph.auth.enums.NationalCloud;
import com.microsoft.graph.authentication.IAuthenticationProvider;
import com.microsoft.graph.http.IHttpRequest;
import com.microsoft.graph.httpcore.ICoreAuthenticationProvider;
import okhttp3.Request;
public class ClientCredentialProvider extends BaseAuthentication implements IAuthenticationProvider, ICoreAuthenticationProvider{
/*
* Client credential provider instance using client secret
*
* @param clientId Client ID of application
* @param scopes Scopes that application need to access protected resources
* @param clientSecret Client secret of application
* @param tenant The tenant GUID or friendly name format or common
*
*/
public ClientCredentialProvider(String clientId,
List<String> scopes,
String clientSecret,
String tenant,
NationalCloud nationalCloud) {
super( scopes,
clientId,
GetAuthority(nationalCloud == null? NationalCloud.Global: nationalCloud, tenant),
null,
nationalCloud == null? NationalCloud.Global: nationalCloud,
tenant,
clientSecret);
}
@Override
public void authenticateRequest(IHttpRequest request) {
String accessToken = getAcccessToken();
request.addHeader(AuthConstants.AUTHORIZATION_HEADER, AuthConstants.BEARER + accessToken);
}
@Override
public Request authenticateRequest(Request request) {
String accessToken = getAcccessToken();
return request.newBuilder().addHeader(AuthConstants.AUTHORIZATION_HEADER, AuthConstants.BEARER + accessToken).build();
}
String getAcccessToken() {
String accessToken = "";
try {
long duration = System.currentTimeMillis() - getStartTime();
if(getResponse()!=null && duration>0 && duration< getResponse().getExpiresIn()*1000) {
accessToken = getResponse().getAccessToken();
} else {
OAuthClientRequest authRequest = getTokenRequestMessage();
accessToken = getAccessTokenNewRequest(authRequest);
}
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
/*
* Create the token request message
*
* @return The token request message
*/
OAuthClientRequest getTokenRequestMessage() throws OAuthSystemException {
String tokenUrl = getAuthority() + AuthConstants.TOKEN_ENDPOINT;
TokenRequestBuilder token = OAuthClientRequest.
tokenLocation(tokenUrl)
.setClientId(getClientId())
.setGrantType(GrantType.CLIENT_CREDENTIALS)
.setScope(getScopesAsString());
if(getClientSecret() != null) {
token.setClientSecret(getClientSecret());
}
return token.buildBodyMessage();
}
/*
* Call using request to get response containing access token
*
* @param request The request to execute
* @return The access token in response
*/
String getAccessTokenNewRequest(OAuthClientRequest request) throws OAuthSystemException, OAuthProblemException {
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
setStartTime(System.currentTimeMillis());
setResponse(oAuthClient.accessToken(request));
return getResponse().getAccessToken();
}
}