Skip to content

Commit edc8222

Browse files
committed
New files for Azure Active Directory OAuth Api + updates to OAuthConstants class.
1 parent dc5f6ac commit edc8222

File tree

4 files changed

+205
-1
lines changed

4 files changed

+205
-1
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.apis.service.AzureActiveDirectoryService;
4+
import com.github.scribejava.core.builder.api.DefaultApi20;
5+
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
6+
import com.github.scribejava.core.extractors.TokenExtractor;
7+
import com.github.scribejava.core.model.OAuth2AccessToken;
8+
import com.github.scribejava.core.model.OAuthConfig;
9+
import com.github.scribejava.core.model.Verb;
10+
import com.github.scribejava.core.utils.OAuthEncoder;
11+
12+
import java.util.Map;
13+
14+
/**
15+
* Microsoft Azure Active Directory Api
16+
*
17+
* Some helpful links
18+
* https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code
19+
* https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-webapp-java
20+
* https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/signed-in-user-operations
21+
* https://portal.azure.com
22+
*/
23+
public class AzureActiveDirectoryApi extends DefaultApi20 {
24+
25+
private static final String MSFT_GRAPH_URL = "https://graph.windows.net";
26+
27+
private static final String AUTHORIZE_URL = "?client_id=%s&redirect_uri=%s&response_type=code&resource="
28+
+ MSFT_GRAPH_URL;
29+
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
30+
31+
private static final String MSFT_LOGIN_URL = "https://login.microsoftonline.com";
32+
private static final String SLASH = "/";
33+
private static final String COMMON = "common";
34+
private static final String TOKEN_URI = "oauth2/token";
35+
private static final String AUTH_URI = "oauth2/authorize";
36+
37+
private static class InstanceHolder {
38+
39+
private static final AzureActiveDirectoryApi INSTANCE = new AzureActiveDirectoryApi();
40+
}
41+
42+
public static AzureActiveDirectoryApi instance() {
43+
return AzureActiveDirectoryApi.InstanceHolder.INSTANCE;
44+
}
45+
46+
@Override
47+
public String getAccessTokenEndpoint() {
48+
return MSFT_LOGIN_URL + SLASH + COMMON + SLASH + TOKEN_URI;
49+
}
50+
51+
@Override
52+
protected String getAuthorizationBaseUrl() {
53+
return MSFT_LOGIN_URL + SLASH + COMMON + SLASH + AUTH_URI;
54+
}
55+
56+
@Override
57+
public Verb getAccessTokenVerb() {
58+
return Verb.POST;
59+
}
60+
61+
@Override
62+
public AzureActiveDirectoryService createService(OAuthConfig config) {
63+
return new AzureActiveDirectoryService(this, config);
64+
}
65+
66+
@Override
67+
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
68+
return OAuth2AccessTokenJsonExtractor.instance();
69+
}
70+
71+
@Override
72+
public String getAuthorizationUrl(OAuthConfig config, Map<String, String> additionalParams) {
73+
74+
String scope = config.getScope();
75+
76+
if ( scope == null ) {
77+
return MSFT_LOGIN_URL + SLASH + COMMON + SLASH + AUTH_URI + String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(),
78+
OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));
79+
} else {
80+
return MSFT_LOGIN_URL + SLASH + COMMON + SLASH + AUTH_URI
81+
+ String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
82+
}
83+
}
84+
85+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.scribejava.apis.service;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
import com.github.scribejava.core.model.OAuthConfig;
5+
import com.github.scribejava.core.model.OAuthConstants;
6+
import com.github.scribejava.core.model.OAuthRequest;
7+
import com.github.scribejava.core.oauth.OAuth20Service;
8+
9+
public class AzureActiveDirectoryService extends OAuth20Service {
10+
11+
private final DefaultApi20 api;
12+
13+
public AzureActiveDirectoryService(DefaultApi20 api, OAuthConfig config) {
14+
super(api, config);
15+
this.api = api;
16+
}
17+
18+
@Override
19+
public OAuthRequest createAccessTokenRequest(String code) {
20+
final OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
21+
final OAuthConfig config = getConfig();
22+
23+
request.addHeader(OAuthConstants.CONTENT_TYPE, OAuthConstants.APPLICATION_X_WWW_FORM_URLENCODED);
24+
25+
request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
26+
final String apiSecret = config.getApiSecret();
27+
if (apiSecret != null) {
28+
request.addBodyParameter(OAuthConstants.CLIENT_SECRET, apiSecret);
29+
}
30+
request.addBodyParameter(OAuthConstants.CODE, code);
31+
request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
32+
final String scope = config.getScope();
33+
if (scope != null) {
34+
request.addBodyParameter(OAuthConstants.SCOPE, scope);
35+
}
36+
request.addBodyParameter(OAuthConstants.GRANT_TYPE, OAuthConstants.AUTHORIZATION_CODE);
37+
38+
return request;
39+
}
40+
41+
@Override
42+
public void signRequest(String accessToken, OAuthRequest request) {
43+
request.addHeader(OAuthConstants.AUTHORIZATION, OAuthConstants.BEARER + accessToken);
44+
request.addHeader(OAuthConstants.ACCEPT,
45+
OAuthConstants.APPLICATION_JSON_ODATA_MINIMALMETADATA_STREAMING_TRUE_CHARSET_UTF_8);
46+
}
47+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import com.github.scribejava.apis.AzureActiveDirectoryApi;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.core.model.OAuth2AccessToken;
6+
import com.github.scribejava.core.model.OAuthRequest;
7+
import com.github.scribejava.core.model.Response;
8+
import com.github.scribejava.core.model.Verb;
9+
import com.github.scribejava.core.oauth.OAuth20Service;
10+
11+
import java.io.IOException;
12+
import java.util.Scanner;
13+
import java.util.concurrent.ExecutionException;
14+
15+
16+
17+
public class MicrosoftAzureExample {
18+
19+
private static final String NETWORK_NAME = "Microsoft Azure Active Directory";
20+
private static final String PROTECTED_RESOURCE_URL = "https://graph.windows.net/me?api-version=1.6";
21+
22+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
23+
// Replace these with your client id and secret
24+
final String clientId = "client id here";
25+
final String clientSecret = "client secret here";
26+
final OAuth20Service service = new ServiceBuilder(clientId)
27+
.apiSecret(clientSecret)
28+
.scope( "openid" )
29+
.callback("http://www.example.com/oauth_callback/")
30+
.build(AzureActiveDirectoryApi.instance());
31+
final Scanner in = new Scanner(System.in, "UTF-8");
32+
33+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
34+
System.out.println();
35+
36+
// Obtain the Authorization URL
37+
System.out.println("Fetching the Authorization URL...");
38+
final String authorizationUrl = service.getAuthorizationUrl();
39+
System.out.println("Got the Authorization URL!");
40+
System.out.println("Now go and authorize ScribeJava here:");
41+
System.out.println(authorizationUrl);
42+
System.out.println("And paste the authorization code here");
43+
System.out.print(">>");
44+
final String code = in.nextLine();
45+
System.out.println();
46+
47+
// Trade the Request Token and Verfier for the Access Token
48+
System.out.println("Trading the Request Token for an Access Token...");
49+
final OAuth2AccessToken accessToken = service.getAccessToken(code);
50+
System.out.println("Got the Access Token!");
51+
System.out.println("(if your curious the raw answer looks like this: " + accessToken.getRawResponse() + "')");
52+
System.out.println();
53+
54+
// Now let's go and ask for a protected resource!
55+
System.out.println("Now we're going to access a protected resource...");
56+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
57+
service.signRequest(accessToken, request);
58+
final Response response = service.execute(request);
59+
System.out.println("Got it! Lets see what we found...");
60+
System.out.println();
61+
System.out.println(response.getCode());
62+
System.out.println(response.getBody());
63+
64+
System.out.println();
65+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
66+
}
67+
}

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthConstants.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@ public interface OAuthConstants {
4141

4242
//not OAuth specific
4343
String USER_AGENT_HEADER_NAME = "User-Agent";
44-
44+
String CONTENT_TYPE = "Content-Type";
45+
String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
46+
String AUTHORIZATION = "Authorization";
47+
String BEARER = "Bearer ";
48+
String ACCEPT = "Accept";
49+
String APPLICATION_JSON_ODATA_MINIMALMETADATA_STREAMING_TRUE_CHARSET_UTF_8 = "application/json; odata=minimalmetadata; streaming=true; charset=utf-8";
4550
}

0 commit comments

Comments
 (0)