Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hkarthik7 committed Aug 27, 2021
1 parent 5b651f4 commit 10b46cd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion azd/src/main/java/org/azd/git/GitApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
50 changes: 48 additions & 2 deletions azd/src/main/java/org/azd/oauth/OAuthApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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<String, Object>(){{
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
}
Expand Down

0 comments on commit 10b46cd

Please sign in to comment.