Search in sources :

Example 26 with OAuthException

use of org.openhab.core.auth.client.oauth2.OAuthException in project openhab-core by openhab.

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;
}
Also used : AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse)

Example 27 with OAuthException

use of org.openhab.core.auth.client.oauth2.OAuthException in project openhab-core by openhab.

the class AbstractTestAgent method testGetCachedAccessToken.

@Override
public AccessTokenResponse testGetCachedAccessToken() throws OAuthException, IOException, OAuthResponseException {
    logger.debug("test getCachedAccessToken");
    AccessTokenResponse oldRefreshedToken = oauthClientService.getAccessTokenResponse();
    return oldRefreshedToken;
}
Also used : AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse)

Example 28 with OAuthException

use of org.openhab.core.auth.client.oauth2.OAuthException in project smarthome by eclipse.

the class OAuthClientServiceImpl method extractAuthCodeFromAuthResponse.

@Override
public String extractAuthCodeFromAuthResponse(@NonNull String redirectURLwithParams) throws OAuthException {
    // parse the redirectURL
    try {
        URL redirectURLObject = new URL(redirectURLwithParams);
        UrlEncoded urlEncoded = new UrlEncoded(redirectURLObject.getQuery());
        // may contain multiple...
        String stateFromRedirectURL = urlEncoded.getValue(STATE, 0);
        if (stateFromRedirectURL == null) {
            if (persistedParams.state == null) {
                // This should not happen as the state is usually set
                return urlEncoded.getValue(CODE, 0);
            }
            // else
            throw new OAuthException(String.format("state from redirectURL is incorrect.  Expected: %s Found: %s", persistedParams.state, stateFromRedirectURL));
        } else {
            if (stateFromRedirectURL.equals(persistedParams.state)) {
                return urlEncoded.getValue(CODE, 0);
            }
            // else
            throw new OAuthException(String.format("state from redirectURL is incorrect.  Expected: %s Found: %s", persistedParams.state, stateFromRedirectURL));
        }
    } catch (MalformedURLException e) {
        throw new OAuthException("Redirect URL is malformed", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) OAuthException(org.eclipse.smarthome.core.auth.client.oauth2.OAuthException) UrlEncoded(org.eclipse.jetty.util.UrlEncoded) URL(java.net.URL)

Example 29 with OAuthException

use of org.openhab.core.auth.client.oauth2.OAuthException in project smarthome by eclipse.

the class OAuthFactoryImpl method deleteServiceAndAccessToken.

@Override
public void deleteServiceAndAccessToken(String handle) {
    OAuthClientService clientImpl = oauthClientServiceCache.get(handle);
    if (clientImpl != null) {
        try {
            clientImpl.remove();
        } catch (OAuthException e) {
        // client was already closed, does not matter
        }
        oauthClientServiceCache.remove(handle);
    }
    oAuthStoreHandler.remove(handle);
}
Also used : OAuthClientService(org.eclipse.smarthome.core.auth.client.oauth2.OAuthClientService) OAuthException(org.eclipse.smarthome.core.auth.client.oauth2.OAuthException)

Example 30 with OAuthException

use of org.openhab.core.auth.client.oauth2.OAuthException in project openhab-core by openhab.

the class OAuthClientServiceImpl method extractAuthCodeFromAuthResponse.

@Override
public String extractAuthCodeFromAuthResponse(String redirectURLwithParams) throws OAuthException {
    // parse the redirectURL
    try {
        URL redirectURLObject = new URL(redirectURLwithParams);
        UrlEncoded urlEncoded = new UrlEncoded(redirectURLObject.getQuery());
        // may contain multiple...
        String stateFromRedirectURL = urlEncoded.getValue(STATE, 0);
        if (stateFromRedirectURL == null) {
            if (persistedParams.state == null) {
                // This should not happen as the state is usually set
                return urlEncoded.getValue(CODE, 0);
            }
            // else
            throw new OAuthException(String.format("state from redirectURL is incorrect.  Expected: %s Found: %s", persistedParams.state, stateFromRedirectURL));
        } else {
            if (stateFromRedirectURL.equals(persistedParams.state)) {
                return urlEncoded.getValue(CODE, 0);
            }
            // else
            throw new OAuthException(String.format("state from redirectURL is incorrect.  Expected: %s Found: %s", persistedParams.state, stateFromRedirectURL));
        }
    } catch (MalformedURLException e) {
        throw new OAuthException("Redirect URL is malformed", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) UrlEncoded(org.eclipse.jetty.util.UrlEncoded) URL(java.net.URL)

Aggregations

AccessTokenResponse (org.openhab.core.auth.client.oauth2.AccessTokenResponse)25 OAuthException (org.openhab.core.auth.client.oauth2.OAuthException)25 IOException (java.io.IOException)22 OAuthResponseException (org.openhab.core.auth.client.oauth2.OAuthResponseException)21 OAuthClientService (org.openhab.core.auth.client.oauth2.OAuthClientService)15 OAuthException (org.eclipse.smarthome.core.auth.client.oauth2.OAuthException)8 Nullable (org.eclipse.jdt.annotation.Nullable)7 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)7 Test (org.junit.jupiter.api.Test)7 OAuthFactory (org.openhab.core.auth.client.oauth2.OAuthFactory)7 AccessTokenResponse (org.eclipse.smarthome.core.auth.client.oauth2.AccessTokenResponse)6 ExecutionException (java.util.concurrent.ExecutionException)5 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)5 Request (org.eclipse.jetty.client.api.Request)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 List (java.util.List)4 TimeoutException (java.util.concurrent.TimeoutException)4 Configuration (org.openhab.core.config.core.Configuration)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4