use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project smarthome by eclipse.
the class AbstractTestAgent method testGetAccessTokenByResourceOwnerPasswordCredentials.
@Override
public AccessTokenResponse testGetAccessTokenByResourceOwnerPasswordCredentials() throws OAuthException, IOException, OAuthResponseException {
logger.debug("test getOAuthTokenByResourceOwnerPasswordCredentials");
if (handle == null) {
logger.debug("Creating new oauth service");
oauthClientService = testCreateClient();
} else {
logger.debug("getting oauth client by handle: {}", handle);
oauthClientService = oauthFactory.getOAuthClientService(handle);
}
AccessTokenResponse accessTokenResponse = oauthClientService.getAccessTokenByResourceOwnerPasswordCredentials(username, password, scope);
logger.debug("Token: {}", accessTokenResponse);
return accessTokenResponse;
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project smarthome by eclipse.
the class ConsoleOAuthCommandExtension method execute.
@Override
public void execute(String[] args, Console console) {
this.console = console;
if (args.length < 2) {
console.println("Argument expected. Please check usage.");
return;
}
AbstractTestAgent agent = getTestAgent(args[0]);
if (agent == null) {
console.println("Unexpected test agent:" + args[0]);
return;
}
AccessTokenResponse response;
try {
switch(args[1]) {
case "create":
OAuthClientService newService = agent.testCreateClient();
console.println("handle: " + agent.handle + ", service: " + newService);
break;
case "getAccessTokenByResourceOwnerPassword":
response = agent.testGetAccessTokenByResourceOwnerPasswordCredentials();
consolePrintAccessToken(response);
break;
case "getClient":
OAuthClientService service = agent.testGetClient(args[2]);
console.println("OAuthClientService: " + service);
break;
case "refresh":
response = agent.testRefreshToken();
consolePrintAccessToken(response);
break;
case "getAccessTokenByCode":
console.println("using authorization code: " + args[2]);
response = agent.testGetAccessTokenByAuthorizationCode(args[2]);
consolePrintAccessToken(response);
break;
case "getAuthorizationUrl":
String authURL;
if (args.length >= 3) {
authURL = agent.testGetAuthorizationUrl(args[2]);
console.println("Authorization URL: " + authURL + " state: " + args[2]);
} else {
authURL = agent.testGetAuthorizationUrl(null);
console.println("Authorization URL: " + authURL + " state: null");
}
break;
case "getCachedAccessToken":
response = agent.testGetCachedAccessToken();
consolePrintAccessToken(response);
break;
case "close":
console.println("Closing test agent client service...");
agent.close();
break;
case "delete":
console.println("Delete by handle: " + args[2]);
agent.delete(args[2]);
break;
default:
console.println("Commands are case-sensitive. Unknown command: " + args[1]);
break;
}
} catch (OAuthException | IOException | OAuthResponseException e) {
console.print(String.format("%s %s, cause %s", e.getClass(), e.getMessage(), e.getCause()));
}
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-core by openhab.
the class OAuthClientServiceImpl method getAccessTokenByResourceOwnerPasswordCredentials.
@Override
public AccessTokenResponse getAccessTokenByResourceOwnerPasswordCredentials(String username, String password, @Nullable String scope) throws OAuthException, IOException, OAuthResponseException {
if (isClosed()) {
throw new OAuthException(EXCEPTION_MESSAGE_CLOSED);
}
String tokenUrl = persistedParams.tokenUrl;
if (tokenUrl == null) {
throw new OAuthException("Missing token url");
}
OAuthConnector connector = new OAuthConnector(httpClientFactory, persistedParams.deserializerClassName);
AccessTokenResponse accessTokenResponse = connector.grantTypePassword(tokenUrl, username, password, persistedParams.clientId, persistedParams.clientSecret, scope, Boolean.TRUE.equals(persistedParams.supportsBasicAuth));
// store it
storeHandler.saveAccessTokenResponse(handle, accessTokenResponse);
return accessTokenResponse;
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-core by openhab.
the class OAuthClientServiceImpl method getAccessTokenResponseByAuthorizationCode.
@Override
public AccessTokenResponse getAccessTokenResponseByAuthorizationCode(String authorizationCode, @Nullable String redirectURI) throws OAuthException, IOException, OAuthResponseException {
if (isClosed()) {
throw new OAuthException(EXCEPTION_MESSAGE_CLOSED);
}
if (persistedParams.redirectUri != null && !persistedParams.redirectUri.equals(redirectURI)) {
// check parameter redirectURI in #getAuthorizationUrl are the same as given
throw new OAuthException(String.format("redirectURI should be the same from previous call #getAuthorizationUrl. Expected: %s Found: %s", persistedParams.redirectUri, redirectURI));
}
String tokenUrl = persistedParams.tokenUrl;
if (tokenUrl == null) {
throw new OAuthException("Missing token url");
}
String clientId = persistedParams.clientId;
if (clientId == null) {
throw new OAuthException("Missing client ID");
}
OAuthConnector connector = new OAuthConnector(httpClientFactory, persistedParams.deserializerClassName);
AccessTokenResponse accessTokenResponse = connector.grantTypeAuthorizationCode(tokenUrl, authorizationCode, clientId, persistedParams.clientSecret, redirectURI, Boolean.TRUE.equals(persistedParams.supportsBasicAuth));
// store it
storeHandler.saveAccessTokenResponse(handle, accessTokenResponse);
return accessTokenResponse;
}
use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-core by openhab.
the class OAuthClientServiceImpl method refreshToken.
@Override
public AccessTokenResponse refreshToken() throws OAuthException, IOException, OAuthResponseException {
if (isClosed()) {
throw new OAuthException(EXCEPTION_MESSAGE_CLOSED);
}
AccessTokenResponse lastAccessToken;
try {
lastAccessToken = storeHandler.loadAccessTokenResponse(handle);
} catch (GeneralSecurityException e) {
throw new OAuthException("Cannot decrypt access token from store", e);
}
if (lastAccessToken == null) {
throw new OAuthException("Cannot refresh token because last access token is not available from handle: " + handle);
}
if (lastAccessToken.getRefreshToken() == null) {
throw new OAuthException("Cannot refresh token because last access token did not have a refresh token");
}
String tokenUrl = persistedParams.tokenUrl;
if (tokenUrl == null) {
throw new OAuthException("tokenUrl is required but null");
}
OAuthConnector connector = new OAuthConnector(httpClientFactory, persistedParams.deserializerClassName);
AccessTokenResponse accessTokenResponse = connector.grantTypeRefreshToken(tokenUrl, lastAccessToken.getRefreshToken(), persistedParams.clientId, persistedParams.clientSecret, persistedParams.scope, Boolean.TRUE.equals(persistedParams.supportsBasicAuth));
// The service may not return the refresh token so use the last refresh token otherwise it's not stored.
String refreshToken = accessTokenResponse.getRefreshToken();
if (refreshToken == null || refreshToken.isBlank()) {
accessTokenResponse.setRefreshToken(lastAccessToken.getRefreshToken());
}
// store it
storeHandler.saveAccessTokenResponse(handle, accessTokenResponse);
accessTokenRefreshListeners.forEach(l -> l.onAccessTokenResponse(accessTokenResponse));
return accessTokenResponse;
}
Aggregations