use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-core by openhab.
the class OAuthConnector method doRequest.
private AccessTokenResponse doRequest(final String grantType, HttpClient httpClient, final Request request, Fields fields) throws OAuthResponseException, OAuthException, IOException {
int statusCode = 0;
String content = "";
try {
final FormContentProvider entity = new FormContentProvider(fields);
final ContentResponse response = AccessController.doPrivileged((PrivilegedExceptionAction<ContentResponse>) () -> {
Request requestWithContent = request.content(entity);
return requestWithContent.send();
});
statusCode = response.getStatus();
content = response.getContentAsString();
if (statusCode == HttpStatus.OK_200) {
AccessTokenResponse jsonResponse = gson.fromJson(content, AccessTokenResponse.class);
// this is not supplied by the response
jsonResponse.setCreatedOn(LocalDateTime.now());
logger.debug("grant type {} to URL {} success", grantType, request.getURI());
return jsonResponse;
} else if (statusCode == HttpStatus.BAD_REQUEST_400) {
OAuthResponseException errorResponse = gson.fromJson(content, OAuthResponseException.class);
logger.error("grant type {} to URL {} failed with error code {}, description {}", grantType, request.getURI(), errorResponse.getError(), errorResponse.getErrorDescription());
throw errorResponse;
} else {
logger.error("grant type {} to URL {} failed with HTTP response code {}", grantType, request.getURI(), statusCode);
throw new OAuthException("Bad http response, http code " + statusCode);
}
} catch (PrivilegedActionException pae) {
Exception underlyingException = pae.getException();
if (underlyingException instanceof InterruptedException || underlyingException instanceof TimeoutException || underlyingException instanceof ExecutionException) {
throw new IOException("Exception in oauth communication, grant type " + grantType, underlyingException);
}
// Dont know what exception it is, wrap it up and throw it out
throw new OAuthException("Exception in oauth communication, grant type " + grantType, underlyingException);
} catch (JsonSyntaxException e) {
throw new OAuthException(String.format("Unable to deserialize json into AccessTokenResponse/ OAuthResponseException. httpCode: %i json: %s", statusCode, content), e);
}
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-core by openhab.
the class AbstractTestAgent method testRefreshToken.
@Override
public AccessTokenResponse testRefreshToken() throws OAuthException, IOException, OAuthResponseException {
logger.debug("test RefreshToken");
AccessTokenResponse newRefreshedToken = oauthClientService.refreshToken();
return newRefreshedToken;
}
use of org.openhab.core.auth.client.oauth2.OAuthResponseException in project openhab-core by openhab.
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()));
}
}
Aggregations