Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit f6c9220

Browse files
author
Nakul Sabharwal
committed
Added Client Credential Provider
1 parent 7bacb81 commit f6c9220

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.microsoft.graph.auth.confidentialClient;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
10+
import org.junit.Test;
11+
12+
import com.microsoft.graph.auth.enums.NationalCloud;
13+
import com.microsoft.graph.httpcore.IAuthenticationProvider;
14+
15+
public class ClientCredentialProviderTests {
16+
public static String CLIENT_ID = "CLIENT_ID";
17+
public static String SCOPE = "https://graph.microsoft.com/.default";
18+
public static List<String > SCOPES = Arrays.asList(SCOPE);
19+
public static String CLIENT_SECRET = "CLIENT_SECRET";
20+
public static String CLIENT_ASSERTION = "CLIENT_ASSERTION";
21+
public static String TENANT = "TENANT_GUID_OR_DOMAIN_NAME";
22+
public static NationalCloud NATIONAL_CLOUD = NationalCloud.Global;
23+
24+
@Test
25+
public void createInstanceClientSecretTest() {
26+
IAuthenticationProvider authenticationProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT, NATIONAL_CLOUD);
27+
assertNotNull(authenticationProvider);
28+
}
29+
30+
@Test
31+
public void getTokenRequestMessageTest() throws OAuthSystemException {
32+
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT, NATIONAL_CLOUD);
33+
String expected = "grant_type=client_credentials&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default+&client_secret=CLIENT_SECRET&client_id=CLIENT_ID";
34+
String actual = authenticationProvider.getTokenRequestMessage().getBody();
35+
String expectedLocationUri = "https://login.microsoftonline.com/TENANT_GUID_OR_DOMAIN_NAME/oauth2/v2.0/token";
36+
String actualLocationUri = authenticationProvider.getTokenRequestMessage().getLocationUri();
37+
assertEquals(expected, actual);
38+
assertEquals(expectedLocationUri, actualLocationUri);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)