Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<failOnWarnings>true</failOnWarnings>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ public CertificateWithPrivateKey(PrivateKey privateKey, X509Certificate certific
}

/**
* Gets the X.509 certificate.
*
* @return Certificate
*/
public X509Certificate getCertificate() {
return certificate;
}

/**
* Gets the private key associated with the certificate.
*
* @return Private key
*/
public PrivateKey getPrivateKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public ClientCredentialsGrantTokenClient(String tokenEndpointUri, ClientCredenti
* @param scopes OAuth2 scopes to request
* @return Access token
* @throws IOException Exception during token endpoint communication
* @throws InterruptedException if the thread is interrupted during token retrieval
*/
@SuppressWarnings("UnusedReturnValue")
public String getToken(final String... scopes) throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@
import java.util.ArrayList;
import java.util.List;

/**
* Abstract base class for OAuth2 custom grant token clients.
* Provides common functionality for obtaining access tokens using custom grant types.
*/
public abstract class CustomGrantTokenClient {
private final ClientCredentials clientCredentials;
private final TokenEndpointHttpClient tokenEndpointHttpClient;
private final String partialCacheKey;
private final TokenCache cache;

/**
* Constructs a custom grant token client.
*
* @param tokenEndpointUri URI of the OAuth2 token endpoint
* @param clientCredentials client credentials for authentication
* @param cache token cache for storing and retrieving tokens
*/
public CustomGrantTokenClient(String tokenEndpointUri, ClientCredentials clientCredentials, TokenCache cache) {
this.tokenEndpointHttpClient = new TokenEndpointHttpClient(tokenEndpointUri);
this.clientCredentials = clientCredentials;
Expand All @@ -24,6 +35,7 @@ public CustomGrantTokenClient(String tokenEndpointUri, ClientCredentials clientC
* @param scopes OAuth2 scopes to request
* @return Access token
* @throws IOException Exception during token endpoint communication
* @throws InterruptedException if the thread is interrupted during token retrieval
*/
protected String getTokenInternal(final List<NameValuePair> parameters, final String... scopes) throws IOException, InterruptedException {

Expand Down Expand Up @@ -55,6 +67,8 @@ public ExpiringToken get() throws IOException, InterruptedException {
}

/**
* Gets the OAuth2 grant type for this client.
*
* @return Grant type (i.e. "client_credentials")
*/
protected abstract String getGrantType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ public class ExpiringToken {
}

/**
* Gets the access token string.
*
* @return Token
*/
public String getToken() {
return token;
}

/**
* Gets the token expiration time in seconds.
*
* @return Expiration in seconds
*/
public int getExpiresInSeconds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
public class InMemoryTokenCache implements TokenCache {
private final ConcurrentMap<String, CachedToken> cache = new ConcurrentHashMap<>();

/**
* Constructs a new in-memory token cache.
*/
public InMemoryTokenCache() {
// Default constructor
}

/**
* Wrapper class to store token with its expiration time
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class NameValuePair {
final String value;

/**
* Constructs a name-value pair.
*
* @param name Name
* @param value Value
*/
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/scalepoint/oauth_token_client/NoCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
*/
@SuppressWarnings("unused")
public class NoCache implements TokenCache {

/**
* Constructs a new no-cache implementation.
*/
public NoCache() {
// Default constructor
}

/**
* @param cacheKey Cache key
* @param underlyingSource Underlying token source to invoke
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,47 @@ public ResourceScopedAccessGrantParameters(String scope, String resource, String
}

/**
* Gets the OAuth2 scope for the access request.
*
* @return OAuth2 scope
*/
public String getScope() {
return scope;
}

/**
* Gets the specific resource identifier for the access request.
*
* @return Specific resource identifier
*/
public String getResource() {
return resource;
}

/**
* Gets the resource tenant identifier.
*
* @return Resource tenant identifier
*/
public String getTenantId() {
return tenantId;
}

/**
* Gets the original authentication method references.
*
* @return Original authentication method references
*/
@SuppressWarnings("unused")
public String[] getAmr() {
return amr;
}

/**
* Gets the authentication method references as a space-separated string.
*
* @return AMR values as a space-separated string, or null if no AMR values are set
*/
protected String getAmrString() {
return (amr == null || amr.length < 1)
? null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
@SuppressWarnings("WeakerAccess")
public class ResourceScopedAccessGrantTokenClient extends CustomGrantTokenClient {

/**
* Constructs a resource-scoped access grant token client.
*
* @param tokenEndpointUri URI of the OAuth2 token endpoint
* @param clientCredentials client credentials for authentication
*/
@SuppressWarnings("SameParameterValue")
public ResourceScopedAccessGrantTokenClient(String tokenEndpointUri, ClientCredentials clientCredentials) {
super(tokenEndpointUri, clientCredentials, new NoCache());
Expand All @@ -21,6 +27,7 @@ public ResourceScopedAccessGrantTokenClient(String tokenEndpointUri, ClientCrede
* @param parameters Custom grant parameters
* @return Access token
* @throws IOException Exception during token endpoint communication
* @throws InterruptedException if the thread is interrupted during token retrieval
*/
@SuppressWarnings("UnusedReturnValue")
public String getToken(ResourceScopedAccessGrantParameters parameters) throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
*/
public interface TokenCache {
/**
* Gets a token from the cache or retrieves it from the underlying source if not cached.
*
* @param cacheKey Cache key
* @param underlyingSource Underlying token source to invoke on cache miss
* @return Token from either cache or underlying source
* @throws IOException Exception from underlying cache
* @throws InterruptedException if the thread is interrupted during token retrieval
*/
String get(String cacheKey, TokenSource underlyingSource) throws IOException, InterruptedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
*/
public interface TokenSource {
/**
* Gets an expiring token.
*
* @return Token
* @throws IOException
* @throws IOException if an I/O error occurs during token retrieval
* @throws InterruptedException if the thread is interrupted during token retrieval
*/
ExpiringToken get() throws IOException, InterruptedException;
}