|
|
@ -16,6 +16,7 @@ package org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization.client;
|
|
|
|
|
|
|
|
|
|
|
|
import feign.Client;
|
|
|
|
import feign.Client;
|
|
|
|
import feign.Feign;
|
|
|
|
import feign.Feign;
|
|
|
|
|
|
|
|
import feign.FeignException;
|
|
|
|
import feign.Logger;
|
|
|
|
import feign.Logger;
|
|
|
|
import feign.Request;
|
|
|
|
import feign.Request;
|
|
|
|
import feign.RequestInterceptor;
|
|
|
|
import feign.RequestInterceptor;
|
|
|
@ -63,6 +64,7 @@ public class OAuthRequestInterceptor implements RequestInterceptor {
|
|
|
|
private ApiApplicationRegistrationService apiApplicationRegistrationService;
|
|
|
|
private ApiApplicationRegistrationService apiApplicationRegistrationService;
|
|
|
|
private TokenIssuerService tokenIssuerService;
|
|
|
|
private TokenIssuerService tokenIssuerService;
|
|
|
|
private static Log log = LogFactory.getLog(OAuthRequestInterceptor.class);
|
|
|
|
private static Log log = LogFactory.getLog(OAuthRequestInterceptor.class);
|
|
|
|
|
|
|
|
private ApiApplicationKey apiApplicationKey;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Creates an interceptor that authenticates all requests.
|
|
|
|
* Creates an interceptor that authenticates all requests.
|
|
|
@ -82,29 +84,39 @@ public class OAuthRequestInterceptor implements RequestInterceptor {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void apply(RequestTemplate template) {
|
|
|
|
public void apply(RequestTemplate template) {
|
|
|
|
if (tokenInfo == null) {
|
|
|
|
if (tokenInfo == null) {
|
|
|
|
//had to do on demand initialization due to start up error.
|
|
|
|
if (apiApplicationKey == null) {
|
|
|
|
ApiRegistrationProfile apiRegistrationProfile = new ApiRegistrationProfile();
|
|
|
|
ApiRegistrationProfile apiRegistrationProfile = new ApiRegistrationProfile();
|
|
|
|
apiRegistrationProfile.setApplicationName(APPLICATION_NAME);
|
|
|
|
apiRegistrationProfile.setApplicationName(APPLICATION_NAME);
|
|
|
|
apiRegistrationProfile.setIsAllowedToAllDomains(false);
|
|
|
|
apiRegistrationProfile.setIsAllowedToAllDomains(false);
|
|
|
|
apiRegistrationProfile.setIsMappingAnExistingOAuthApp(false);
|
|
|
|
apiRegistrationProfile.setIsMappingAnExistingOAuthApp(false);
|
|
|
|
apiRegistrationProfile.setTags(DEVICE_MANAGEMENT_SERVICE_TAG);
|
|
|
|
apiRegistrationProfile.setTags(DEVICE_MANAGEMENT_SERVICE_TAG);
|
|
|
|
ApiApplicationKey apiApplicationKey = apiApplicationRegistrationService.register(apiRegistrationProfile);
|
|
|
|
apiApplicationKey = apiApplicationRegistrationService.register(apiRegistrationProfile);
|
|
|
|
|
|
|
|
}
|
|
|
|
String consumerKey = apiApplicationKey.getConsumerKey();
|
|
|
|
String consumerKey = apiApplicationKey.getConsumerKey();
|
|
|
|
String consumerSecret = apiApplicationKey.getConsumerSecret();
|
|
|
|
String consumerSecret = apiApplicationKey.getConsumerSecret();
|
|
|
|
String username = AuthorizationConfigurationManager.getInstance().getUsername();
|
|
|
|
String username = AuthorizationConfigurationManager.getInstance().getUsername();
|
|
|
|
String password = AuthorizationConfigurationManager.getInstance().getPassword();
|
|
|
|
String password = AuthorizationConfigurationManager.getInstance().getPassword();
|
|
|
|
tokenIssuerService = Feign.builder().client(getSSLClient()).logger(new Slf4jLogger()).logLevel(Logger.Level.FULL)
|
|
|
|
if (tokenIssuerService == null) {
|
|
|
|
.requestInterceptor(new BasicAuthRequestInterceptor(consumerKey, consumerSecret))
|
|
|
|
tokenIssuerService = Feign.builder().client(getSSLClient()).logger(new Slf4jLogger()).logLevel(
|
|
|
|
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
|
|
|
|
Logger.Level.FULL)
|
|
|
|
.target(TokenIssuerService.class,
|
|
|
|
.requestInterceptor(new BasicAuthRequestInterceptor(consumerKey, consumerSecret))
|
|
|
|
AuthorizationConfigurationManager.getInstance().getTokenEndpoint());
|
|
|
|
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
|
|
|
|
|
|
|
|
.target(TokenIssuerService.class,
|
|
|
|
|
|
|
|
AuthorizationConfigurationManager.getInstance().getTokenEndpoint());
|
|
|
|
|
|
|
|
}
|
|
|
|
tokenInfo = tokenIssuerService.getToken(PASSWORD_GRANT_TYPE, username, password, REQUIRED_SCOPE);
|
|
|
|
tokenInfo = tokenIssuerService.getToken(PASSWORD_GRANT_TYPE, username, password, REQUIRED_SCOPE);
|
|
|
|
tokenInfo.setExpires_in(System.currentTimeMillis() + (tokenInfo.getExpires_in() * 1000));
|
|
|
|
tokenInfo.setExpires_in(System.currentTimeMillis() + (tokenInfo.getExpires_in() * 1000));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
synchronized (this) {
|
|
|
|
synchronized (this) {
|
|
|
|
if (System.currentTimeMillis() + refreshTimeOffset > tokenInfo.getExpires_in()) {
|
|
|
|
if (System.currentTimeMillis() + refreshTimeOffset > tokenInfo.getExpires_in()) {
|
|
|
|
tokenInfo = tokenIssuerService.getToken(REFRESH_GRANT_TYPE, tokenInfo.getRefresh_token());
|
|
|
|
try {
|
|
|
|
tokenInfo.setExpires_in(System.currentTimeMillis() + tokenInfo.getExpires_in());
|
|
|
|
tokenInfo = tokenIssuerService.getToken(REFRESH_GRANT_TYPE, tokenInfo.getRefresh_token());
|
|
|
|
|
|
|
|
tokenInfo.setExpires_in(System.currentTimeMillis() + tokenInfo.getExpires_in());
|
|
|
|
|
|
|
|
} catch (FeignException e) {
|
|
|
|
|
|
|
|
tokenInfo = null;
|
|
|
|
|
|
|
|
apply(template);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String headerValue = "Bearer " + tokenInfo.getAccess_token();
|
|
|
|
String headerValue = "Bearer " + tokenInfo.getAccess_token();
|
|
|
|