|
| 1 | +package com.microsoft.graph.auth.confidentialClient; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.apache.oltu.oauth2.client.OAuthClient; |
| 6 | +import org.apache.oltu.oauth2.client.URLConnectionClient; |
| 7 | +import org.apache.oltu.oauth2.client.request.OAuthClientRequest; |
| 8 | +import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder; |
| 9 | +import org.apache.oltu.oauth2.common.exception.OAuthProblemException; |
| 10 | +import org.apache.oltu.oauth2.common.exception.OAuthSystemException; |
| 11 | +import org.apache.oltu.oauth2.common.message.types.GrantType; |
| 12 | + |
| 13 | +import com.microsoft.graph.auth.BaseAuthentication; |
| 14 | +import com.microsoft.graph.auth.enums.NationalCloud; |
| 15 | +import com.microsoft.graph.httpcore.IAuthenticationProvider; |
| 16 | + |
| 17 | +public class ClientCredentialProvider extends BaseAuthentication implements IAuthenticationProvider{ |
| 18 | + |
| 19 | + public ClientCredentialProvider(String clientId, |
| 20 | + List<String> scopes, |
| 21 | + String clientSecret, |
| 22 | + String tenant, |
| 23 | + NationalCloud nationalCloud) { |
| 24 | + super( scopes, |
| 25 | + clientId, |
| 26 | + GetAuthority(nationalCloud == null? NationalCloud.Global: nationalCloud, tenant), |
| 27 | + null, |
| 28 | + nationalCloud == null? NationalCloud.Global: nationalCloud, |
| 29 | + tenant, |
| 30 | + clientSecret); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String getAccessToken() { |
| 35 | + if(super.response != null) { |
| 36 | + long duration = System.currentTimeMillis() - super.startTime; |
| 37 | + if(duration > 0 && duration < super.response.getExpiresIn()*1000) { |
| 38 | + return super.response.getAccessToken(); |
| 39 | + } |
| 40 | + } |
| 41 | + String accessToken = null; |
| 42 | + try { |
| 43 | + OAuthClientRequest request = getTokenRequestMessage(); |
| 44 | + accessToken = getAccessTokenNewRequest(request); |
| 45 | + } catch (Exception e) { |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | + return accessToken; |
| 49 | + } |
| 50 | + |
| 51 | + protected OAuthClientRequest getTokenRequestMessage() throws OAuthSystemException { |
| 52 | + String tokenUrl = super.authority + "/oauth2/v2.0/token"; |
| 53 | + TokenRequestBuilder token = OAuthClientRequest. |
| 54 | + tokenLocation(tokenUrl) |
| 55 | + .setClientId(super.ClientId) |
| 56 | + .setGrantType(GrantType.CLIENT_CREDENTIALS) |
| 57 | + .setScope(getScopesAsString()); |
| 58 | + if(super.ClientSecret != null) { |
| 59 | + token.setClientSecret(this.ClientSecret); |
| 60 | + } |
| 61 | + return token.buildBodyMessage(); |
| 62 | + } |
| 63 | + |
| 64 | + protected String getAccessTokenNewRequest(OAuthClientRequest request) throws OAuthSystemException, OAuthProblemException { |
| 65 | + OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); |
| 66 | + super.startTime = System.currentTimeMillis(); |
| 67 | + super.response = oAuthClient.accessToken(request); |
| 68 | + return super.response.getAccessToken(); |
| 69 | + } |
| 70 | +} |
0 commit comments