From cce53b3d88272d9d79996b2ea6174e56e3f276c6 Mon Sep 17 00:00:00 2001 From: ramsessanchez Date: Wed, 15 Jul 2020 13:38:15 -0700 Subject: [PATCH 001/213] adding files need to update auth --- Scripts/getLatestVersion.ps1 | 6 +- Scripts/validateMavenVersion.ps1 | 7 +- build.gradle | 2 + .../graph/authentication/AuthConstants.java | 14 ++ .../IAuthenticationProvider.java | 15 ++ .../TokenCredntialAuthProvider.java | 41 ++++++ .../microsoft/graph/httpcore/HttpMethod.java | 28 ++++ .../graph/httpcore/IHttpRequest.java | 135 ++++++++++++++++++ .../microsoft/graph/options/HeaderOption.java | 17 +++ .../com/microsoft/graph/options/Option.java | 46 ++++++ 10 files changed, 305 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/microsoft/graph/authentication/AuthConstants.java create mode 100644 src/main/java/com/microsoft/graph/authentication/IAuthenticationProvider.java create mode 100644 src/main/java/com/microsoft/graph/authentication/TokenCredntialAuthProvider.java create mode 100644 src/main/java/com/microsoft/graph/httpcore/HttpMethod.java create mode 100644 src/main/java/com/microsoft/graph/httpcore/IHttpRequest.java create mode 100644 src/main/java/com/microsoft/graph/options/HeaderOption.java create mode 100644 src/main/java/com/microsoft/graph/options/Option.java diff --git a/Scripts/getLatestVersion.ps1 b/Scripts/getLatestVersion.ps1 index ca1806b62..bcf3475ec 100644 --- a/Scripts/getLatestVersion.ps1 +++ b/Scripts/getLatestVersion.ps1 @@ -6,10 +6,10 @@ Retrieve the latest version of the library .Description Retrieves the latest version specified in the Gradle.Properties file - Uses the retrieved values to update the enviornment variable VERSION_STRING -#> - + Uses the retrieved values to update the environment variable VERSION_STRING .Parameter propertiesPath + The path pointing to the gradle.properties file. +#> Param( [parameter(Mandatory = $true)] diff --git a/Scripts/validateMavenVersion.ps1 b/Scripts/validateMavenVersion.ps1 index f591d5e1e..bd3dd66c7 100644 --- a/Scripts/validateMavenVersion.ps1 +++ b/Scripts/validateMavenVersion.ps1 @@ -8,11 +8,12 @@ .Description Retrieves the local, Maven, and Bintray versions of the Java-Core build. Checks that the Maven and Bintray versions are aligned, trigger warning if not. - Checks that the current local version is greater than those currently deployed. -#> - + Checks that the current local version is greater than those currently deployed. .Parameter packageName + The package name to search in the Maven and JCenter repositories. .Parameter propertiesPath + The path pointing to the Gradle.Properties file. +#> Param( [parameter(Mandatory = $true)] diff --git a/build.gradle b/build.gradle index 261f884bc..ee44ab9bd 100644 --- a/build.gradle +++ b/build.gradle @@ -37,6 +37,8 @@ dependencies { // https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1' + + implementation 'com.azure:azure-identity:1.0.8' } def pomConfig = { diff --git a/src/main/java/com/microsoft/graph/authentication/AuthConstants.java b/src/main/java/com/microsoft/graph/authentication/AuthConstants.java new file mode 100644 index 000000000..1a0233df3 --- /dev/null +++ b/src/main/java/com/microsoft/graph/authentication/AuthConstants.java @@ -0,0 +1,14 @@ +package com.microsoft.graph.authentication; + +public class AuthConstants { + + public static class Tenants + { + public static final String Common = "common"; + public static final String Organizations = "organizations"; + public static final String Consumers = "consumers"; + } + public static final String BEARER = "Bearer "; + public static final String TOKEN_ENDPOINT = "/oauth2/v2.0/token"; + public static final String AUTHORIZATION_HEADER = "Authorization"; +} \ No newline at end of file diff --git a/src/main/java/com/microsoft/graph/authentication/IAuthenticationProvider.java b/src/main/java/com/microsoft/graph/authentication/IAuthenticationProvider.java new file mode 100644 index 000000000..15ed5bd82 --- /dev/null +++ b/src/main/java/com/microsoft/graph/authentication/IAuthenticationProvider.java @@ -0,0 +1,15 @@ +package com.microsoft.graph.authentication; + +//This should be deleted later once IHttpRequest is moved to the core library +import com.microsoft.graph.httpcore.IHttpRequest; + +public interface IAuthenticationProvider { + + /** + * Authenticates the request + * + * @param request the request to authenticate + */ + void authenticateRequest(final IHttpRequest request); + +} diff --git a/src/main/java/com/microsoft/graph/authentication/TokenCredntialAuthProvider.java b/src/main/java/com/microsoft/graph/authentication/TokenCredntialAuthProvider.java new file mode 100644 index 000000000..c96b96477 --- /dev/null +++ b/src/main/java/com/microsoft/graph/authentication/TokenCredntialAuthProvider.java @@ -0,0 +1,41 @@ +package com.microsoft.graph.authentication; + +import com.azure.core.credential.TokenCredential; +import com.azure.core.credential.TokenRequestContext; +import com.azure.core.http.HttpRequest; +import com.microsoft.graph.httpcore.IHttpRequest; +import com.microsoft.graph.httpcore.ICoreAuthenticationProvider; +import okhttp3.Request; + +import java.util.List; + +public class TokenCredntialAuthProvider implements ICoreAuthenticationProvider , IAuthenticationProvider { + + private TokenCredential tokenCredential; + private HttpRequest request; + private TokenRequestContext context; + + public TokenCredntialAuthProvider(TokenCredential tokenCredential, List scopes) { + this.tokenCredential = tokenCredential; + context = new TokenRequestContext().setScopes(scopes); + } + + @Override + public void authenticateRequest(IHttpRequest request) { + String accessToken = tokenCredential.getToken(context).toString(); + request.addHeader(AuthConstants.AUTHORIZATION_HEADER, AuthConstants.BEARER + accessToken); + } + + @Override + public Request authenticateRequest(Request request) { + String accessToken = "";//getAccessToken(); + return request.newBuilder() + .addHeader(AuthConstants.AUTHORIZATION_HEADER, AuthConstants.BEARER + accessToken) + .build(); + } + +// void getAccessToken(String credential) { +// String accessToken = tokenCredential.getToken(context).toString(); +// +// } +} diff --git a/src/main/java/com/microsoft/graph/httpcore/HttpMethod.java b/src/main/java/com/microsoft/graph/httpcore/HttpMethod.java new file mode 100644 index 000000000..878e487ac --- /dev/null +++ b/src/main/java/com/microsoft/graph/httpcore/HttpMethod.java @@ -0,0 +1,28 @@ +package com.microsoft.graph.httpcore; + +public enum HttpMethod { + /** + * Get + */ + GET, + + /** + * Post + */ + POST, + + /** + * Patch + */ + PATCH, + + /** + * Delete + */ + DELETE, + + /** + * Put + */ + PUT, +} diff --git a/src/main/java/com/microsoft/graph/httpcore/IHttpRequest.java b/src/main/java/com/microsoft/graph/httpcore/IHttpRequest.java new file mode 100644 index 000000000..44c88134d --- /dev/null +++ b/src/main/java/com/microsoft/graph/httpcore/IHttpRequest.java @@ -0,0 +1,135 @@ +package com.microsoft.graph.httpcore; + +import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect; +import com.microsoft.graph.httpcore.middlewareoption.IShouldRetry; +import com.microsoft.graph.options.HeaderOption; +import com.microsoft.graph.options.Option; + +import java.net.URL; +import java.util.List; + +/** + * An HTTP request + */ +public interface IHttpRequest { + + /** + * Gets the request URL + * + * @return the request URL + */ + URL getRequestUrl(); + + /** + * Gets the HTTP method + * + * @return the HTTP method + */ + HttpMethod getHttpMethod(); + + /** + * Gets the headers + * + * @return the headers + */ + List getHeaders(); + + /** + * Gets the options + * + * @return the options + */ + List