Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #70 from AzureAD/dev
Browse files Browse the repository at this point in the history
Dev to master
  • Loading branch information
Kanishk Panwar committed Nov 6, 2015
2 parents 5af0c35 + d7601f1 commit 8e402d8
Show file tree
Hide file tree
Showing 31 changed files with 721 additions and 628 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.microsoft.azure</groupId>
<artifactId>adal4j</artifactId>

<version>1.1.1</version>
<version>1.1.2</version>
<packaging>jar</packaging>
<name>adal4j</name>
<description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AdalAuthorizatonGrant {

private final AuthorizationGrant grant;
private final Map<String, String> params;

/**
*
* @param grant
Expand Down Expand Up @@ -62,16 +62,15 @@ Map<String, String> toParameters() {
if (this.params != null) {
outParams.putAll(this.params);
}

outParams.put("scope", "openid");
outParams.putAll(grant.toParameters());
return outParams;
}
AuthorizationGrant getAuthorizationGrant(){

AuthorizationGrant getAuthorizationGrant() {
return this.grant;
}


Map<String, String> getCustomParameters() {
return params;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/microsoft/aad/adal4j/AdalJWTClaimsSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public JSONObject toJSONObject() {
final JSONArray arr = (JSONArray) jo.get(AUDIENCE_CLAIM);
if (!arr.isEmpty()) {
jo.put(AUDIENCE_CLAIM, arr.get(0));
} else {
}
else {
jo.remove(AUDIENCE_CLAIM);
}
}
Expand Down
41 changes: 27 additions & 14 deletions src/main/java/com/microsoft/aad/adal4j/AdalOAuthRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.Collections;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -46,6 +50,8 @@ class AdalOAuthRequest extends HTTPRequest {

private final Map<String, String> extraHeaderParams;
private final Logger log = LoggerFactory.getLogger(AdalOAuthRequest.class);
private final Proxy proxy;
private final SSLSocketFactory sslSocketFactory;

/**
*
Expand All @@ -54,9 +60,12 @@ class AdalOAuthRequest extends HTTPRequest {
* @param correlationId
*/
AdalOAuthRequest(final Method method, final URL url,
final Map<String, String> extraHeaderParams) {
final Map<String, String> extraHeaderParams, final Proxy proxy,
final SSLSocketFactory sslSocketFactory) {
super(method, url);
this.extraHeaderParams = extraHeaderParams;
this.proxy = proxy;
this.sslSocketFactory = sslSocketFactory;
}

Map<String, String> getReadOnlyExtraHeaderParameters() {
Expand All @@ -69,7 +78,8 @@ Map<String, String> getReadOnlyExtraHeaderParameters() {
@Override
public HTTPResponse send() throws IOException {

final HttpURLConnection conn = HttpHelper.openConnection(this.getURL());
final HttpsURLConnection conn = HttpHelper.openConnection(this.getURL(),
this.proxy, this.sslSocketFactory);
this.configureHeaderAndExecuteOAuthCall(conn);
final String out = this.processAndReadResponse(conn);
HttpHelper.verifyReturnedCorrelationId(log, conn,
Expand All @@ -88,7 +98,8 @@ HTTPResponse createResponse(final HttpURLConnection conn, final String out)

try {
response.setContentType(conn.getContentType());
} catch (final ParseException e) {
}
catch (final ParseException e) {
throw new IOException("Couldn't parse Content-Type header: "
+ e.getMessage(), e);
}
Expand All @@ -102,7 +113,7 @@ HTTPResponse createResponse(final HttpURLConnection conn, final String out)
return response;
}

void configureHeaderAndExecuteOAuthCall(final HttpURLConnection conn)
void configureHeaderAndExecuteOAuthCall(final HttpsURLConnection conn)
throws IOException {

if (this.getAuthorization() != null) {
Expand All @@ -111,14 +122,15 @@ void configureHeaderAndExecuteOAuthCall(final HttpURLConnection conn)

Map<String, String> params = new java.util.HashMap<>();
if (this.extraHeaderParams != null && !this.extraHeaderParams.isEmpty()) {
for (java.util.Map.Entry<String, String> entry : this.extraHeaderParams.entrySet()) {
for (java.util.Map.Entry<String, String> entry : this.extraHeaderParams
.entrySet()) {
if (entry.getValue() == null || entry.getValue().isEmpty()) {
continue;
}
params.put(entry.getKey(), entry.getValue());
}
}

HttpHelper.configureAdditionalHeaders(conn, params);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
Expand All @@ -139,13 +151,13 @@ String processAndReadResponse(final HttpURLConnection conn)
final int responseCode = conn.getResponseCode();
if (responseCode == 200) {
inReader = new InputStreamReader(conn.getInputStream());
} else {
InputStream stream = conn.getErrorStream();
if(stream == null && responseCode == 404)
{
stream = conn.getInputStream();
}
}
else {
InputStream stream = conn.getErrorStream();
if (stream == null && responseCode == 404) {
stream = conn.getInputStream();
}

inReader = new InputStreamReader(stream);
}
final BufferedReader reader = new BufferedReader(inReader);
Expand All @@ -159,7 +171,8 @@ String processAndReadResponse(final HttpURLConnection conn)
}
out.append(buffer, 0, rsz);
}
} finally {
}
finally {
reader.close();
}
return out.toString();
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/com/microsoft/aad/adal4j/AdalTokenRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
package com.microsoft.aad.adal4j;

import java.io.IOException;
import java.net.Proxy;
import java.net.URL;
import java.util.Map;

import javax.net.ssl.SSLSocketFactory;

import com.nimbusds.oauth2.sdk.ParseException;
import com.nimbusds.oauth2.sdk.SerializeException;
import com.nimbusds.oauth2.sdk.TokenErrorResponse;
Expand All @@ -42,14 +45,19 @@ class AdalTokenRequest {
private final ClientAuthentication clientAuth;
private final AdalAuthorizatonGrant authzGrant;
private final Map<String, String> headerMap;
private final Proxy proxy;
private final SSLSocketFactory sslSocketFactory;

AdalTokenRequest(final URL uri, final ClientAuthentication clientAuth,
final AdalAuthorizatonGrant authzGrant,
final Map<String, String> headerMap) {
final Map<String, String> headerMap, final Proxy proxy,
final SSLSocketFactory sslSocketFactory) {
this.clientAuth = clientAuth;
this.authzGrant = authzGrant;
this.uri = uri;
this.headerMap = headerMap;
this.proxy = proxy;
this.sslSocketFactory = sslSocketFactory;
}

/**
Expand Down Expand Up @@ -86,15 +94,14 @@ AuthenticationResult executeOAuthRequestAndProcessResponse()
.getJWTClaimsSet());
}

result = new AuthenticationResult(
response.getAccessToken().getType().getValue(),
response.getAccessToken().getValue(),
refreshToken,
response.getAccessToken().getLifetime(),
response.getIDTokenString(),
info,
!StringHelper.isBlank(response.getResource()));
} else {
result = new AuthenticationResult(response.getAccessToken()
.getType().getValue(),
response.getAccessToken().getValue(), refreshToken,
response.getAccessToken().getLifetime(),
response.getIDTokenString(), info,
!StringHelper.isBlank(response.getResource()));
}
else {
final TokenErrorResponse errorResponse = TokenErrorResponse
.parse(httpResponse);
throw new AuthenticationException(errorResponse.toJSONObject()
Expand All @@ -116,7 +123,8 @@ AdalOAuthRequest toOAuthRequest() throws SerializeException {
}

final AdalOAuthRequest httpRequest = new AdalOAuthRequest(
HTTPRequest.Method.POST, this.uri, headerMap);
HTTPRequest.Method.POST, this.uri, headerMap, this.proxy,
this.sslSocketFactory);
httpRequest.setContentType(CommonContentTypes.APPLICATION_URLENCODED);
final Map<String, String> params = this.authzGrant.toParameters();
httpRequest.setQuery(URLUtils.serializeParameters(params));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public String getPublicCertificateHash()
* @throws CertificateEncodingException
* @throws NoSuchAlgorithmException
*/
public String getPublicCertificate()
throws CertificateEncodingException, NoSuchAlgorithmException {
public String getPublicCertificate() throws CertificateEncodingException,
NoSuchAlgorithmException {
return Base64.encodeBase64String(this.publicCertificate.getEncoded());
}

/**
* Returns private key of the credential.
*
Expand Down
32 changes: 15 additions & 17 deletions src/main/java/com/microsoft/aad/adal4j/AuthenticationAuthority.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Arrays;
import java.util.Map;

import javax.net.ssl.SSLSocketFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,7 +37,8 @@ class AuthenticationAuthority {
.getLogger(AuthenticationAuthority.class);

private final static String[] TRUSTED_HOST_LIST = { "login.windows.net",
"login.chinacloudapi.cn", "login.cloudgovapi.us", "login.microsoftonline.com" };
"login.chinacloudapi.cn", "login.cloudgovapi.us",
"login.microsoftonline.com" };
private final static String TENANTLESS_TENANT_NAME = "common";
private final static String AUTHORIZE_ENDPOINT_TEMPLATE = "https://{host}/{tenant}/oauth2/authorize";
private final static String DISCOVERY_ENDPOINT = "common/discovery/instance";
Expand Down Expand Up @@ -63,8 +66,6 @@ class AuthenticationAuthority {
private final URL authorityUrl;
private final boolean validateAuthority;

private Proxy proxy;

AuthenticationAuthority(final URL authorityUrl,
final boolean validateAuthority) {

Expand All @@ -75,14 +76,6 @@ class AuthenticationAuthority {
setupAuthorityProperties();
}

public Proxy getProxy() {
return proxy;
}

public void setProxy(Proxy proxy) {
this.proxy = proxy;
}

String getHost() {
return host;
}
Expand All @@ -98,11 +91,11 @@ String getAuthority() {
String getTokenEndpoint() {
return tokenEndpoint;
}

String getUserRealmEndpoint(String username) {
return String.format(userRealmEndpointFormat, host, username);
}

AuthorityType getAuthorityType() {
return authorityType;
}
Expand All @@ -123,7 +116,8 @@ void setSelfSignedJwtAudience(final String selfSignedJwtAudience) {
this.selfSignedJwtAudience = selfSignedJwtAudience;
}

void doInstanceDiscovery(final Map<String, String> headers)
void doInstanceDiscovery(final Map<String, String> headers,
final Proxy proxy, final SSLSocketFactory sslSocketFactory)
throws Exception {

// instance discovery should be executed only once per context instance.
Expand All @@ -132,7 +126,9 @@ void doInstanceDiscovery(final Map<String, String> headers)
if (!doStaticInstanceDiscovery()) {
// if authority must be validated and dynamic discovery request
// as a fall back is success
if (validateAuthority && !doDynamicInstanceDiscovery(headers)) {
if (validateAuthority
&& !doDynamicInstanceDiscovery(headers, proxy,
sslSocketFactory)) {
throw new AuthenticationException(
AuthenticationErrorMessage.AUTHORITY_NOT_IN_VALID_LIST);
}
Expand All @@ -144,9 +140,11 @@ void doInstanceDiscovery(final Map<String, String> headers)
}
}

boolean doDynamicInstanceDiscovery(final Map<String, String> headers)
boolean doDynamicInstanceDiscovery(final Map<String, String> headers,
final Proxy proxy, final SSLSocketFactory sslSocketFactory)
throws Exception {
final String json = HttpHelper.executeHttpGet(log, instanceDiscoveryEndpoint, headers, proxy);
final String json = HttpHelper.executeHttpGet(log,
instanceDiscoveryEndpoint, headers, proxy, sslSocketFactory);
final InstanceDiscoveryResponse discoveryResponse = JsonHelper
.convertJsonToObject(json, InstanceDiscoveryResponse.class);
return !StringHelper.isBlank(discoveryResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
******************************************************************************/
package com.microsoft.aad.adal4j;


/**
* Authentication callback Interface that can be implemented by the developer.
*/
Expand Down
Loading

0 comments on commit 8e402d8

Please sign in to comment.