diff --git a/azd/src/main/java/org/azd/git/GitApi.java b/azd/src/main/java/org/azd/git/GitApi.java index b9803cd2..6c2d3cd8 100644 --- a/azd/src/main/java/org/azd/git/GitApi.java +++ b/azd/src/main/java/org/azd/git/GitApi.java @@ -40,7 +40,7 @@ public class GitApi implements GitDetails { * @throws ConnectionException A connection object should be created with Azure DevOps organization name, personal access token * and project. This validates the connection object and throws exception if it is not provided. * @throws AzDException Default Api Exception handler. - * @return git repository object + * @return git repository object {@link Repository} */ @Override public Repository createRepository(String repositoryName, String projectId) throws ConnectionException, AzDException { diff --git a/azd/src/main/java/org/azd/oauth/OAuthApi.java b/azd/src/main/java/org/azd/oauth/OAuthApi.java index c61307b0..90a03a74 100644 --- a/azd/src/main/java/org/azd/oauth/OAuthApi.java +++ b/azd/src/main/java/org/azd/oauth/OAuthApi.java @@ -10,8 +10,14 @@ import java.util.LinkedHashMap; +/*** + * OAuth Api class to authorize access to REST API + */ public class OAuthApi { + /*** + * Deserialize JSON response to POJO + */ private static final JsonMapper MAPPER = new JsonMapper(); private static final String AREA = "accounts"; @@ -20,13 +26,24 @@ public class OAuthApi { static { try { VSTS_BASE_URL = Client.getLocationUrl(AREA,null); - } catch (ConnectionException |AzDException e) { + } catch (ConnectionException | AzDException e) { } } + /*** + * Default constructor + */ public OAuthApi() {} + /*** + * Generate the authorization endpoint with client id, state, scope and redirection url. + * @param clientId The ID assigned to your app when it was registered + * @param state Can be any value. Typically a generated string value that correlates the callback with its associated authorization request. + * @param scope Scopes registered with the app. Space separated. See https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops#scopes + * @param redirectUrl Callback URL for your app. Must exactly match the URL registered with the app. + * @return The authorization endpoint to authorize your app + */ public static String getAuthorizationEndpoint(String clientId, String state, String scope, String redirectUrl) { var queryString = new LinkedHashMap(){{ @@ -48,11 +65,25 @@ public static String getAuthorizationEndpoint(String clientId, String state, Str return stringBuilder.toString(); } + /** + * Helps to create a query string from given key and value + * @param key pass the key of the HashMap + * @param value pass the value of the HasMap + * @return query string + */ private static String getQueryString(String key, Object value) { return "&" + key + "=" + value; } - + /*** + * Now you use the authorization code to request an access token for the user. + * Your service must make a service-to-service HTTP request to Azure DevOps Services. + * @param appSecret URL encoded client secret acquired when the app was registered + * @param authCode URL encoded "code" provided via the code query parameter to your callback URL + * @param callbackUrl callback URL registered with the app + * @return AuthorizedToken object {@link AuthorizedToken} + * @throws AzDException Default Api Exception handler. + */ public static AuthorizedToken getAccessToken(String appSecret, String authCode, String callbackUrl) throws AzDException { StringBuilder stringBuilder = new StringBuilder(); @@ -77,6 +108,16 @@ public static AuthorizedToken getAccessToken(String appSecret, String authCode, return res; } + /*** + * If a user's access token expires, you can use the refresh token that they acquired in the authorization flow to get a new access token. + * @param appSecret URL encoded client secret acquired when the app was registered + * @param authCode URL encoded "code" provided via the code query parameter to your callback URL + * @param callbackUrl callback URL registered with the app + * @return AuthorizedToken object {@link AuthorizedToken} + * @throws ConnectionException A connection object should be created with Azure DevOps organization name, personal access token + * and project. This validates the connection object and throws exception if it is not provided. + * @throws AzDException Default Api Exception handler. + */ public static AuthorizedToken getRefreshToken(String appSecret, String authCode, String callbackUrl) throws AzDException, ConnectionException { StringBuilder stringBuilder = new StringBuilder(); @@ -99,6 +140,11 @@ public static AuthorizedToken getRefreshToken(String appSecret, String authCode, return res; } + /*** + * Check if the access token has expired. + * @param authorizedToken authorized token object {@link AuthorizedToken} + * @return True if the token has expired. {@link Boolean} + */ public static boolean hasTokenExpired(AuthorizedToken authorizedToken) { return authorizedToken.getReceivedTimestamp() < 1629897097271L || (authorizedToken.getReceivedTimestamp() + authorizedToken.getExpiresIn()*1000) < System.currentTimeMillis(); }