Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
ldclakmal committed Jan 10, 2021
1 parent b390e57 commit 2a3feeb
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions oauth2-ballerina/client_oauth2_provider.bal
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,14 @@ isolated function getOAuth2TokenForPasswordGrant(PasswordGrantConfig grantConfig
if (cachedAccessToken == "") {
return getAccessTokenFromAuthorizationRequest(grantConfig, tokenCache);
} else {
if (isOAuth2CacheEntryValid(tokenCache)) {
if (isCachedTokenExpired(tokenCache.expTime)) {
return cachedAccessToken;
} else {
lock {
if (isOAuth2CacheEntryValid(tokenCache)) {
if (isCachedTokenExpired(tokenCache.expTime)) {
return tokenCache.accessToken;
} else {
return getAccessTokenFromRefreshRequest(grantConfig, tokenCache);
}
return getAccessTokenFromRefreshRequest(grantConfig, tokenCache);
}
}
}
Expand All @@ -251,16 +250,14 @@ isolated function getOAuth2TokenForClientCredentialsGrant(ClientCredentialsGrant
if (cachedAccessToken == "") {
return getAccessTokenFromAuthorizationRequest(grantConfig, tokenCache);
} else {
if (isOAuth2CacheEntryValid(tokenCache)) {
if (isCachedTokenExpired(tokenCache.expTime)) {
return cachedAccessToken;
} else {
lock {
if (isOAuth2CacheEntryValid(tokenCache)) {
cachedAccessToken = tokenCache.accessToken;
return cachedAccessToken;
} else {
return getAccessTokenFromAuthorizationRequest(grantConfig, tokenCache);
if (isCachedTokenExpired(tokenCache.expTime)) {
return tokenCache.accessToken;
}
return getAccessTokenFromAuthorizationRequest(grantConfig, tokenCache);
}
}
}
Expand All @@ -274,29 +271,25 @@ isolated function getOAuth2TokenForDirectTokenMode(DirectTokenConfig grantConfig
string? directAccessToken = grantConfig?.accessToken;
if (directAccessToken is string && directAccessToken != "") {
return directAccessToken;
} else {
return getAccessTokenFromRefreshRequest(grantConfig, tokenCache);
}
return getAccessTokenFromRefreshRequest(grantConfig, tokenCache);
} else {
if (isOAuth2CacheEntryValid(tokenCache)) {
if (isCachedTokenExpired(tokenCache.expTime)) {
return cachedAccessToken;
} else {
lock {
if (isOAuth2CacheEntryValid(tokenCache)) {
cachedAccessToken = tokenCache.accessToken;
return cachedAccessToken;
} else {
return getAccessTokenFromRefreshRequest(grantConfig, tokenCache);
if (isCachedTokenExpired(tokenCache.expTime)) {
return tokenCache.accessToken;
}
return getAccessTokenFromRefreshRequest(grantConfig, tokenCache);
}
}
}
}

// Checks the validity of the access token, which is in the cache. If the expiry time is 0, that means no expiry time is
// returned with the authorization request. This implies that the token is valid forever.
isolated function isOAuth2CacheEntryValid(TokenCache tokenCache) returns boolean {
int expTime = tokenCache.expTime;
isolated function isCachedTokenExpired(int expTime) returns boolean {
if (expTime == 0) {
return true;
}
Expand Down Expand Up @@ -503,5 +496,4 @@ isolated function updateOAuth2CacheEntry(json responsePayload, TokenCache tokenC
string refreshToken = (checkpanic (responsePayload.refresh_token)).toJsonString();
tokenCache.refreshToken = refreshToken;
}
return ();
}

0 comments on commit 2a3feeb

Please sign in to comment.