Compare commits
4 Commits
v2.0.6-cxf
...
develop-v2
Author | SHA1 | Date | |
---|---|---|---|
2cd9cc9484 | |||
eb654c5488 | |||
d29ccbef79 | |||
f84279a148 |
5
pom.xml
5
pom.xml
@@ -74,6 +74,11 @@
|
|||||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||||
<version>${jackson.version}</version>
|
<version>${jackson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.14.0</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
@@ -20,6 +20,8 @@ import java.util.Base64;
|
|||||||
import javax.ws.rs.client.ClientRequestContext;
|
import javax.ws.rs.client.ClientRequestContext;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements a simple 2-credential (username & password) based
|
* This class implements a simple 2-credential (username & password) based
|
||||||
* authorization filter.
|
* authorization filter.
|
||||||
@@ -36,8 +38,8 @@ public class BasicAuthorizationFilter implements AuthorizationFilter {
|
|||||||
* @param password A password or secret key.
|
* @param password A password or secret key.
|
||||||
*/
|
*/
|
||||||
public BasicAuthorizationFilter(String username, String password) {
|
public BasicAuthorizationFilter(String username, String password) {
|
||||||
this.username = username;
|
this.username = StringUtils.trimToNull(username);
|
||||||
this.password = password;
|
this.password = StringUtils.trimToNull(password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -14,6 +14,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.inteligr8.rs;
|
package com.inteligr8.rs;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.ws.rs.client.ClientBuilder;
|
import javax.ws.rs.client.ClientBuilder;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import javax.ws.rs.client.WebTarget;
|
||||||
|
|
||||||
@@ -83,6 +85,11 @@ public abstract class Client {
|
|||||||
.register(provider)
|
.register(provider)
|
||||||
.register(new LoggingFilter());
|
.register(new LoggingFilter());
|
||||||
|
|
||||||
|
if (this.getConfig().getConnectTimeoutInMillis() != null)
|
||||||
|
clientBuilder.connectTimeout(this.getConfig().getConnectTimeoutInMillis(), TimeUnit.MILLISECONDS);
|
||||||
|
if (this.getConfig().getResponseTimeoutInMillis() != null)
|
||||||
|
clientBuilder.readTimeout(this.getConfig().getResponseTimeoutInMillis(), TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
if (authFilter == null)
|
if (authFilter == null)
|
||||||
authFilter = this.getConfig().createAuthorizationFilter();
|
authFilter = this.getConfig().createAuthorizationFilter();
|
||||||
if (authFilter != null)
|
if (authFilter != null)
|
||||||
|
@@ -120,6 +120,16 @@ public interface ClientConfiguration {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
default Integer getConnectTimeoutInMillis() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
default Integer getResponseTimeoutInMillis() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true to enable Jackson UNWRAP_ROOT_VALUE feature; false otherwise.
|
* @return true to enable Jackson UNWRAP_ROOT_VALUE feature; false otherwise.
|
||||||
*/
|
*/
|
||||||
|
@@ -16,6 +16,8 @@ package com.inteligr8.rs;
|
|||||||
|
|
||||||
import javax.ws.rs.client.ClientRequestContext;
|
import javax.ws.rs.client.ClientRequestContext;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is the base for implementations of client authorization similar
|
* This class is the base for implementations of client authorization similar
|
||||||
* to OAuth-based flows.
|
* to OAuth-based flows.
|
||||||
@@ -45,8 +47,8 @@ public class ClientEnforcementAuthorizationFilter implements AuthorizationFilter
|
|||||||
* @param clientSecret A secret corresponding to the client ID.
|
* @param clientSecret A secret corresponding to the client ID.
|
||||||
*/
|
*/
|
||||||
public ClientEnforcementAuthorizationFilter(String clientId, String clientSecret) {
|
public ClientEnforcementAuthorizationFilter(String clientId, String clientSecret) {
|
||||||
this.clientId = clientId;
|
this.clientId = StringUtils.trimToNull(clientId);
|
||||||
this.clientSecret = clientSecret;
|
this.clientSecret = StringUtils.trimToNull(clientSecret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -19,8 +19,6 @@ import javax.ws.rs.client.ClientBuilder;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that provides pre-configured JAX-RS Client & WebTarget objects
|
* A class that provides pre-configured JAX-RS Client & WebTarget objects
|
||||||
@@ -28,7 +26,6 @@ import org.springframework.stereotype.Component;
|
|||||||
*
|
*
|
||||||
* @author brian@inteligr8.com
|
* @author brian@inteligr8.com
|
||||||
*/
|
*/
|
||||||
@Component("client.jaxrs")
|
|
||||||
public class ClientImpl extends Client {
|
public class ClientImpl extends Client {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(ClientImpl.class);
|
private final Logger logger = LoggerFactory.getLogger(ClientImpl.class);
|
||||||
@@ -39,7 +36,6 @@ public class ClientImpl extends Client {
|
|||||||
* This constructor is for Spring or POJO use.
|
* This constructor is for Spring or POJO use.
|
||||||
* @param config The client configuration.
|
* @param config The client configuration.
|
||||||
*/
|
*/
|
||||||
@Autowired
|
|
||||||
public ClientImpl(ClientConfiguration config) {
|
public ClientImpl(ClientConfiguration config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,10 @@ import java.net.URI;
|
|||||||
|
|
||||||
import javax.ws.rs.core.Form;
|
import javax.ws.rs.core.Form;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements the OAuth Authorization Code flow as an authorization
|
* This class implements the OAuth Authorization Code flow as an authorization
|
||||||
* filter.
|
* filter.
|
||||||
@@ -26,6 +30,8 @@ import javax.ws.rs.core.Form;
|
|||||||
*/
|
*/
|
||||||
public class OAuthAuthorizationCodeAuthorizationFilter extends OAuthAuthorizationFilter {
|
public class OAuthAuthorizationCodeAuthorizationFilter extends OAuthAuthorizationFilter {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
private final URI redirectUri;
|
private final URI redirectUri;
|
||||||
|
|
||||||
@@ -68,12 +74,13 @@ public class OAuthAuthorizationCodeAuthorizationFilter extends OAuthAuthorizatio
|
|||||||
public OAuthAuthorizationCodeAuthorizationFilter(String tokenUrl, String clientId, String clientSecret, String code, URI redirectUri) {
|
public OAuthAuthorizationCodeAuthorizationFilter(String tokenUrl, String clientId, String clientSecret, String code, URI redirectUri) {
|
||||||
super(tokenUrl, clientId, clientSecret);
|
super(tokenUrl, clientId, clientSecret);
|
||||||
|
|
||||||
this.code = code;
|
this.code = StringUtils.trimToNull(code);
|
||||||
this.redirectUri = redirectUri;
|
this.redirectUri = redirectUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Form createForm() {
|
protected Form createForm() {
|
||||||
|
this.logger.debug("Using OAuth grant_type 'authorization_code'");
|
||||||
Form form = new Form().param("grant_type", "authorization_code");
|
Form form = new Form().param("grant_type", "authorization_code");
|
||||||
if (this.redirectUri != null)
|
if (this.redirectUri != null)
|
||||||
form.param("redirect_uri", this.redirectUri.toString());
|
form.param("redirect_uri", this.redirectUri.toString());
|
||||||
|
@@ -27,6 +27,7 @@ import javax.ws.rs.core.HttpHeaders;
|
|||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.Response.Status.Family;
|
import javax.ws.rs.core.Response.Status.Family;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -48,6 +49,7 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
private String accessToken;
|
private String accessToken;
|
||||||
private long expiration;
|
private long expiration;
|
||||||
private String refreshToken;
|
private String refreshToken;
|
||||||
|
private Long refreshTokenExpiration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This constructor creates an OAuth-based authorization filter using the
|
* This constructor creates an OAuth-based authorization filter using the
|
||||||
@@ -87,9 +89,9 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
*/
|
*/
|
||||||
public OAuthAuthorizationFilter(String tokenUrl, String clientId, String clientSecret, String scope) {
|
public OAuthAuthorizationFilter(String tokenUrl, String clientId, String clientSecret, String scope) {
|
||||||
this.tokenUrl = tokenUrl;
|
this.tokenUrl = tokenUrl;
|
||||||
this.clientId = clientId;
|
this.clientId = StringUtils.trimToNull(clientId);
|
||||||
this.clientSecret = clientSecret;
|
this.clientSecret = StringUtils.trimToNull(clientSecret);
|
||||||
this.scope = scope;
|
this.scope = StringUtils.trimToNull(scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -99,8 +101,29 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void filter(ClientRequestContext requestContext) {
|
public void filter(ClientRequestContext requestContext) {
|
||||||
if (this.accessToken == null || System.currentTimeMillis() > this.expiration)
|
if (this.accessToken == null) {
|
||||||
this.requestToken();
|
this.requestToken();
|
||||||
|
} else if (System.currentTimeMillis() >= this.expiration) {
|
||||||
|
this.logger.trace("Access token expired; retrieving new one with refresh token");
|
||||||
|
|
||||||
|
if (this.refreshTokenExpiration != null && System.currentTimeMillis() >= this.refreshTokenExpiration.longValue()) {
|
||||||
|
this.logger.debug("Refresh token expired; performing full authentication");
|
||||||
|
this.refreshToken = null;
|
||||||
|
this.requestToken();
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
this.requestToken();
|
||||||
|
} catch (WebApplicationException wae) {
|
||||||
|
if (wae.getResponse().getStatusInfo().getFamily() == Family.CLIENT_ERROR) {
|
||||||
|
this.logger.debug("Received OAuth response {} using refresh token; performing full authentication", wae.getResponse().getStatus());
|
||||||
|
this.refreshToken = null;
|
||||||
|
this.requestToken();
|
||||||
|
} else {
|
||||||
|
throw wae;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, "Bearer " + this.accessToken);
|
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, "Bearer " + this.accessToken);
|
||||||
}
|
}
|
||||||
@@ -140,6 +163,8 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
.build();
|
.build();
|
||||||
WebTarget target = client.target(this.tokenUrl);
|
WebTarget target = client.target(this.tokenUrl);
|
||||||
|
|
||||||
|
long requestSendTime = System.currentTimeMillis();
|
||||||
|
|
||||||
Response response = target.request().post(entity);
|
Response response = target.request().post(entity);
|
||||||
|
|
||||||
this.logger.debug("Received OAuth response: {}", response.getStatus());
|
this.logger.debug("Received OAuth response: {}", response.getStatus());
|
||||||
@@ -160,8 +185,10 @@ public abstract class OAuthAuthorizationFilter implements AuthorizationFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.accessToken = (String)responseMap.get("access_token");
|
this.accessToken = (String)responseMap.get("access_token");
|
||||||
this.expiration = System.currentTimeMillis() + ((Number)responseMap.get("expires_in")).longValue() * 1000L;
|
this.expiration = requestSendTime + ((Number)responseMap.get("expires_in")).longValue() * 1000L;
|
||||||
this.refreshToken = (String)responseMap.get("refresh_token");
|
this.refreshToken = (String)responseMap.get("refresh_token");
|
||||||
|
if (responseMap.containsKey("refresh_token_expires_in"))
|
||||||
|
this.refreshTokenExpiration = requestSendTime + ((Number)responseMap.get("refresh_token_expires_in")).longValue() * 1000L;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Form createRefreshForm() {
|
protected Form createRefreshForm() {
|
||||||
|
@@ -16,6 +16,9 @@ package com.inteligr8.rs;
|
|||||||
|
|
||||||
import javax.ws.rs.core.Form;
|
import javax.ws.rs.core.Form;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements the OAuth Client Credential flow as an authorization
|
* This class implements the OAuth Client Credential flow as an authorization
|
||||||
* filter.
|
* filter.
|
||||||
@@ -24,6 +27,8 @@ import javax.ws.rs.core.Form;
|
|||||||
*/
|
*/
|
||||||
public class OAuthClientCredentialAuthorizationFilter extends OAuthAuthorizationFilter {
|
public class OAuthClientCredentialAuthorizationFilter extends OAuthAuthorizationFilter {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param tokenUrl The URL to the OAuth IdP token service.
|
* @param tokenUrl The URL to the OAuth IdP token service.
|
||||||
* @param clientId The ID provided by the OAuth IdP administrator.
|
* @param clientId The ID provided by the OAuth IdP administrator.
|
||||||
@@ -35,6 +40,7 @@ public class OAuthClientCredentialAuthorizationFilter extends OAuthAuthorization
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Form createForm() {
|
protected Form createForm() {
|
||||||
|
this.logger.debug("Using OAuth grant_type 'client_credentials'");
|
||||||
return new Form().param("grant_type", "client_credentials");
|
return new Form().param("grant_type", "client_credentials");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,6 +16,10 @@ package com.inteligr8.rs;
|
|||||||
|
|
||||||
import javax.ws.rs.core.Form;
|
import javax.ws.rs.core.Form;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class implements the OAuth Password Grant flow as an authorization
|
* This class implements the OAuth Password Grant flow as an authorization
|
||||||
* filter.
|
* filter.
|
||||||
@@ -24,6 +28,8 @@ import javax.ws.rs.core.Form;
|
|||||||
*/
|
*/
|
||||||
public class OAuthPasswordGrantAuthorizationFilter extends OAuthAuthorizationFilter {
|
public class OAuthPasswordGrantAuthorizationFilter extends OAuthAuthorizationFilter {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
private final String username;
|
private final String username;
|
||||||
private final String password;
|
private final String password;
|
||||||
|
|
||||||
@@ -46,12 +52,13 @@ public class OAuthPasswordGrantAuthorizationFilter extends OAuthAuthorizationFil
|
|||||||
*/
|
*/
|
||||||
public OAuthPasswordGrantAuthorizationFilter(String tokenUrl, String clientId, String clientSecret, String username, String password) {
|
public OAuthPasswordGrantAuthorizationFilter(String tokenUrl, String clientId, String clientSecret, String username, String password) {
|
||||||
super(tokenUrl, clientId, clientSecret);
|
super(tokenUrl, clientId, clientSecret);
|
||||||
this.username = username;
|
this.username = StringUtils.trimToNull(username);
|
||||||
this.password = password;
|
this.password = StringUtils.trimToNull(password);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Form createForm() {
|
protected Form createForm() {
|
||||||
|
this.logger.debug("Using OAuth grant_type 'password': {}", this.username);
|
||||||
return new Form().param("grant_type", "password")
|
return new Form().param("grant_type", "password")
|
||||||
.param("username", this.username);
|
.param("username", this.username);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user