Skip to content

Commit 96a8881

Browse files
committed
- merges CoreAuthProvider and auth provider interfaces
1 parent 67ab2f9 commit 96a8881

10 files changed

Lines changed: 61 additions & 92 deletions

File tree

‎src/main/java/com/microsoft/graph/authentication/IAuthenticationProvider.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@
55
import com.microsoft.graph.exceptions.AuthenticationException;
66
import com.microsoft.graph.httpcore.IHttpRequest;
77

8-
public interface IAuthenticationProvider {
98

9+
/**
10+
* Authenticates requests to be sent to the API
11+
* @param <NativeRequestType> the type of the http native http request from the http client library
12+
*/
13+
public interface IAuthenticationProvider<NativeRequestType> {
1014
/**
1115
* Authenticates the request
1216
*
1317
* @param request the request to authenticate
1418
*/
1519
void authenticateRequest(@Nonnull final IHttpRequest request) throws AuthenticationException;
16-
20+
/**
21+
* Authenticates the request
22+
*
23+
* @param request the request to authenticate
24+
* @return Request with Authorization header added to it
25+
*/
26+
@Nonnull
27+
NativeRequestType authenticateRequest(@Nonnull final NativeRequestType request) throws AuthenticationException;
1728
}

‎src/main/java/com/microsoft/graph/authentication/ICoreAuthenticationProvider.java‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎src/main/java/com/microsoft/graph/authentication/TokenCredentialAuthProvider.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import javax.annotation.Nonnull;
2222

23-
public class TokenCredentialAuthProvider implements ICoreAuthenticationProvider , IAuthenticationProvider {
23+
public class TokenCredentialAuthProvider implements IAuthenticationProvider<Request> {
2424

2525
//TokenCredential expected from user
2626
private TokenCredential tokenCredential;
@@ -29,7 +29,7 @@ public class TokenCredentialAuthProvider implements ICoreAuthenticationProvider
2929
// maximum delay to wait for token obtention
3030
private Duration tokenBlockTimeout;
3131

32-
32+
3333
//TODO: Upon further review from Peter, this should include a Null check in the case that this is the first request
3434
//There should be just one constructor as scopes should probably not be optional
3535
/**
@@ -38,7 +38,7 @@ public class TokenCredentialAuthProvider implements ICoreAuthenticationProvider
3838
* @param tokenCredential Credential object inheriting the TokenCredential interface used to instantiate the Auth Provider
3939
* @throws AuthenticationException exception occurs if the TokenCredential parameter is null
4040
*/
41-
public TokenCredentialAuthProvider(@Nonnull final TokenCredential tokenCredential) throws AuthenticationException {
41+
public TokenCredentialAuthProvider(@Nonnull final TokenCredential tokenCredential) throws AuthenticationException {
4242
if(tokenCredential == null) {
4343
throw new AuthenticationException(new Error(Codes.InvalidArgument,
4444
String.format(Messages.NullParameter, "TokenCredential"))
@@ -115,7 +115,6 @@ private boolean ShouldAuthenticateRequest(@Nonnull final String requestUrl) {
115115
} catch (MalformedURLException ex) {
116116
return false;
117117
}
118-
119118
}
120119

121120
/**

‎src/main/java/com/microsoft/graph/core/BaseClient.java‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import com.microsoft.graph.http.CoreHttpProvider;
2727
import com.microsoft.graph.http.IHttpProvider;
2828
import com.microsoft.graph.httpcore.HttpClients;
29-
import com.microsoft.graph.authentication.ICoreAuthenticationProvider;
29+
import com.microsoft.graph.authentication.IAuthenticationProvider;
3030
import com.microsoft.graph.logger.DefaultLogger;
3131
import com.microsoft.graph.logger.ILogger;
3232
import com.microsoft.graph.serializer.DefaultSerializer;
@@ -36,6 +36,7 @@
3636
import javax.annotation.Nonnull;
3737

3838
import okhttp3.OkHttpClient;
39+
import okhttp3.Request;
3940

4041
/**
4142
* A client that communications with an OData service
@@ -104,23 +105,25 @@ public CustomRequestBuilder<JsonObject> customRequest(@Nonnull final String url)
104105
* @return builder to start configuring the client
105106
*/
106107
@Nonnull
107-
public static Builder<OkHttpClient> builder() {
108-
return new Builder<OkHttpClient>();
108+
public static Builder<OkHttpClient, Request> builder() {
109+
return new Builder<>();
109110
}
110111

111112
/**
112113
* Builder to help configure the Graph service client
114+
* @param <httpClientType> type of the native http library client
115+
* @param <httpRequestType> type of the native http library request
113116
*/
114-
public static class Builder<httpClientType> {
117+
public static class Builder<httpClientType, httpRequestType> {
115118
private ISerializer serializer;
116119
private IHttpProvider httpProvider;
117120
private ILogger logger;
118121
private httpClientType httpClient;
119-
private ICoreAuthenticationProvider auth;
122+
private IAuthenticationProvider<httpRequestType> auth;
120123

121-
private ICoreAuthenticationProvider getAuthenticationProvider() {
124+
private IAuthenticationProvider<httpRequestType> getAuthenticationProvider() {
122125
if(auth == null) {
123-
throw new NullPointerException("auth"); // TODO initialize to default once moved to core
126+
throw new NullPointerException("auth");
124127
} else {
125128
return auth;
126129
}
@@ -142,7 +145,7 @@ private ISerializer getSerializer() {
142145
@SuppressWarnings("unchecked")
143146
private httpClientType getHttpClient() {
144147
if(httpClient == null) {
145-
return (httpClientType)HttpClients.createDefault(getAuthenticationProvider());
148+
return (httpClientType)HttpClients.createDefault((IAuthenticationProvider<Request>)getAuthenticationProvider());
146149
} else {
147150
return httpClient;
148151
}
@@ -163,7 +166,7 @@ private IHttpProvider getHttpProvider() {
163166
* @return the instance of this builder
164167
*/
165168
@Nonnull
166-
public Builder<httpClientType> serializer(@Nonnull final ISerializer serializer) {
169+
public Builder<httpClientType, httpRequestType> serializer(@Nonnull final ISerializer serializer) {
167170
checkNotNull(serializer, "serializer");
168171
this.serializer = serializer;
169172
return this;
@@ -177,7 +180,7 @@ public Builder<httpClientType> serializer(@Nonnull final ISerializer serializer)
177180
* @return the instance of this builder
178181
*/
179182
@Nonnull
180-
public Builder<httpClientType> httpProvider(@Nonnull final IHttpProvider httpProvider) {
183+
public Builder<httpClientType, httpRequestType> httpProvider(@Nonnull final IHttpProvider httpProvider) {
181184
checkNotNull(httpProvider, "httpProvider");
182185
this.httpProvider = httpProvider;
183186
return this;
@@ -191,7 +194,7 @@ public Builder<httpClientType> httpProvider(@Nonnull final IHttpProvider httpPro
191194
* @return the instance of this builder
192195
*/
193196
@Nonnull
194-
public Builder<httpClientType> logger(@Nonnull final ILogger logger) {
197+
public Builder<httpClientType, httpRequestType> logger(@Nonnull final ILogger logger) {
195198
checkNotNull(logger, "logger");
196199
this.logger = logger;
197200
return this;
@@ -205,7 +208,7 @@ public Builder<httpClientType> logger(@Nonnull final ILogger logger) {
205208
* @return the instance of this builder
206209
*/
207210
@Nonnull
208-
public Builder<httpClientType> httpClient(@Nonnull final httpClientType client) {
211+
public Builder<httpClientType, httpRequestType> httpClient(@Nonnull final httpClientType client) {
209212
checkNotNull(client, "client");
210213
this.httpClient = client;
211214
return this;
@@ -218,7 +221,7 @@ public Builder<httpClientType> httpClient(@Nonnull final httpClientType client)
218221
* @return the instance of this builder
219222
*/
220223
@Nonnull
221-
public Builder<httpClientType> authenticationProvider(@Nonnull final ICoreAuthenticationProvider auth) {
224+
public Builder<httpClientType, httpRequestType> authenticationProvider(@Nonnull final IAuthenticationProvider<httpRequestType> auth) {
222225
checkNotNull(auth, "auth");
223226
this.auth = auth;
224227
return this;

‎src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.io.IOException;
44

5-
import com.microsoft.graph.authentication.ICoreAuthenticationProvider;
5+
import com.microsoft.graph.authentication.IAuthenticationProvider;
66
import com.microsoft.graph.exceptions.AuthenticationException;
77

88
import javax.annotation.Nullable;
@@ -25,13 +25,13 @@ public class AuthenticationHandler implements Interceptor {
2525
*/
2626
public final MiddlewareType MIDDLEWARE_TYPE = MiddlewareType.AUTHENTICATION;
2727

28-
private ICoreAuthenticationProvider authProvider;
28+
private IAuthenticationProvider<Request> authProvider;
2929

3030
/**
3131
* Initialize a the handler with a authentication provider
3232
* @param authProvider the authentication provider to use
3333
*/
34-
public AuthenticationHandler(@Nonnull final ICoreAuthenticationProvider authProvider) {
34+
public AuthenticationHandler(@Nonnull final IAuthenticationProvider<Request> authProvider) {
3535
this.authProvider = authProvider;
3636
}
3737

‎src/main/java/com/microsoft/graph/httpcore/HttpClients.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.microsoft.graph.httpcore;
22

3-
import com.microsoft.graph.authentication.ICoreAuthenticationProvider;
3+
import com.microsoft.graph.authentication.IAuthenticationProvider;
44

55
import java.util.Arrays;
66

@@ -10,6 +10,7 @@
1010
import okhttp3.Interceptor;
1111
import okhttp3.OkHttpClient;
1212
import okhttp3.Protocol;
13+
import okhttp3.Request;
1314
import okhttp3.OkHttpClient.Builder;
1415

1516
/**
@@ -42,7 +43,7 @@ public static Builder custom() {
4243
* @return OkHttpClient build with authentication provider given, default redirect and default retry handlers
4344
*/
4445
@Nonnull
45-
public static OkHttpClient createDefault(@Nonnull final ICoreAuthenticationProvider auth) {
46+
public static OkHttpClient createDefault(@Nonnull final IAuthenticationProvider<Request> auth) {
4647
return custom()
4748
.addInterceptor(new AuthenticationHandler(auth))
4849
.addInterceptor(new RetryHandler())

‎src/test/java/com/microsoft/graph/core/GraphServiceClientTest.java‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
5+
import static org.mockito.Mockito.mock;
56

67
import java.util.List;
78
import java.util.Map;
@@ -14,20 +15,16 @@
1415
import com.microsoft.graph.http.IHttpProvider;
1516
import com.microsoft.graph.http.IHttpRequest;
1617
import com.microsoft.graph.http.IStatefulResponseHandler;
17-
import com.microsoft.graph.authentication.ICoreAuthenticationProvider;
18+
import com.microsoft.graph.authentication.IAuthenticationProvider;
1819
import com.microsoft.graph.logger.ILogger;
1920
import com.microsoft.graph.logger.LoggerLevel;
2021
import com.microsoft.graph.serializer.DefaultSerializer;
2122
import com.microsoft.graph.serializer.ISerializer;
2223

2324
public class GraphServiceClientTest {
24-
private ICoreAuthenticationProvider getAuthProvider() {
25-
return new ICoreAuthenticationProvider() {
26-
@Override
27-
public Request authenticateRequest(Request request) {
28-
return request;
29-
}
30-
};
25+
@SuppressWarnings("unchecked")
26+
private IAuthenticationProvider<Request> getAuthProvider() {
27+
return mock(IAuthenticationProvider.class);
3128
}
3229

3330
@Test
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
package com.microsoft.graph.httpcore;
22

3-
import static org.junit.Assert.assertTrue;
4-
import static org.junit.Assert.fail;
53

6-
import com.microsoft.graph.authentication.ICoreAuthenticationProvider;
4+
import com.microsoft.graph.authentication.IAuthenticationProvider;
75
import static org.mockito.Mockito.mock;
8-
import static org.mockito.Mockito.when;
9-
import static org.mockito.Mockito.anyObject;
6+
import static org.mockito.Mockito.any;
107
import static org.mockito.Mockito.times;
118
import static org.mockito.Mockito.verify;
129

1310
import org.junit.Test;
1411

1512
import okhttp3.OkHttpClient;
1613
import okhttp3.Request;
17-
import okhttp3.Response;
1814

1915
public class AuthenticationHandlerTest {
2016
@Test
17+
@SuppressWarnings("unchecked")
2118
public void testAuthenticationHandler() throws Exception {
22-
ICoreAuthenticationProvider authProvider = mock(ICoreAuthenticationProvider.class);
19+
IAuthenticationProvider<Request> authProvider = mock(IAuthenticationProvider.class);
2320
AuthenticationHandler authHandler = new AuthenticationHandler(authProvider);
2421
Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me/").build();
2522
OkHttpClient client = new OkHttpClient().newBuilder().addInterceptor(authHandler).build();
26-
Response response = client.newCall(request).execute();
27-
verify(authProvider, times(1)).authenticateRequest(anyObject());
23+
client.newCall(request).execute();
24+
verify(authProvider, times(1)).authenticateRequest(any(Request.class));
2825
}
2926

3027
}

‎src/test/java/com/microsoft/graph/httpcore/HttpClientsTest.java‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
11
package com.microsoft.graph.httpcore;
22

33
import static org.junit.Assert.assertTrue;
4+
import static org.mockito.Mockito.mock;
45

5-
import com.microsoft.graph.authentication.ICoreAuthenticationProvider;
6+
import com.microsoft.graph.authentication.IAuthenticationProvider;
67
import org.junit.Test;
78

89
import okhttp3.Interceptor;
910
import okhttp3.OkHttpClient;
10-
import okhttp3.Request;
1111

1212
public class HttpClientsTest {
1313

1414
@Test
15+
@SuppressWarnings("unchecked")
1516
public void testHttpClientCreation() {
16-
ICoreAuthenticationProvider authprovider = new ICoreAuthenticationProvider() {
17-
public Request authenticateRequest(Request request) {
18-
Request newRequest = request.newBuilder().addHeader("Authorization", "Bearer " + "TOKEN").build();
19-
return newRequest;
20-
}
21-
};
22-
23-
OkHttpClient httpclient = HttpClients.createDefault(authprovider);
17+
OkHttpClient httpclient = HttpClients.createDefault(mock(IAuthenticationProvider.class));
2418
assertTrue(httpclient != null);
2519
}
2620

2721
@Test
22+
@SuppressWarnings("unchecked")
2823
public void arrayInterceptorsTest() {
29-
AuthenticationHandler authenticationHandler = new AuthenticationHandler(new ICoreAuthenticationProvider() {
30-
@Override
31-
public Request authenticateRequest(Request request) {
32-
return request;
33-
}
34-
});
24+
AuthenticationHandler authenticationHandler = new AuthenticationHandler(mock(IAuthenticationProvider.class));
3525
Interceptor[] interceptors = {new RetryHandler(), new RedirectHandler(), authenticationHandler};
3626
OkHttpClient client = HttpClients.createFromInterceptors(interceptors);
3727
assertTrue(client.interceptors().size()==4);

‎src/test/java/com/microsoft/graph/httpcore/TelemetryHandlerTest.java‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import static org.junit.Assert.assertNotNull;
44
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.Mockito.mock;
56

67
import java.io.IOException;
78

8-
import com.microsoft.graph.authentication.ICoreAuthenticationProvider;
9+
import com.microsoft.graph.authentication.IAuthenticationProvider;
910
import org.junit.Test;
1011

1112
import okhttp3.Interceptor;
@@ -21,15 +22,11 @@ public void telemetryInitTest() {
2122
}
2223

2324
@Test
25+
@SuppressWarnings("unchecked")
2426
public void interceptTest() throws IOException {
2527
final String expectedHeader = TelemetryHandler.GRAPH_VERSION_PREFIX +"/"
2628
+TelemetryHandler.VERSION;
27-
final OkHttpClient client = HttpClients.createDefault(new ICoreAuthenticationProvider() {
28-
@Override
29-
public Request authenticateRequest(Request request) {
30-
return request;
31-
}
32-
});
29+
final OkHttpClient client = HttpClients.createDefault(mock(IAuthenticationProvider.class));
3330
final Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/users/").build();
3431
final Response response = client.newCall(request).execute();
3532
assertNotNull(response);
@@ -39,15 +36,10 @@ public Request authenticateRequest(Request request) {
3936
}
4037

4138
@Test
39+
@SuppressWarnings("unchecked")
4240
public void arrayInterceptorsTest() throws IOException {
4341

44-
final AuthenticationHandler authenticationHandler = new AuthenticationHandler(new ICoreAuthenticationProvider() {
45-
46-
@Override
47-
public Request authenticateRequest(Request request) {
48-
return request;
49-
}
50-
});
42+
final AuthenticationHandler authenticationHandler = new AuthenticationHandler(mock(IAuthenticationProvider.class));
5143
final Interceptor[] interceptors = {new RetryHandler(), new RedirectHandler(), authenticationHandler};
5244
final OkHttpClient client = HttpClients.createFromInterceptors(interceptors);
5345
final String expectedHeader = TelemetryHandler.GRAPH_VERSION_PREFIX +"/"

0 commit comments

Comments
 (0)