Search in sources :

Example 46 with AccessTokenResponse

use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-addons by openhab.

the class MieleBridgeHandlerTest method setUpOAuthFactory.

private void setUpOAuthFactory() throws Exception {
    AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
    accessTokenResponse.setAccessToken(ACCESS_TOKEN);
    oauthClientServiceMock = mock(OAuthClientService.class);
    when(oauthClientServiceMock.getAccessTokenResponse()).thenReturn(accessTokenResponse);
    OAuthFactory oAuthFactory = mock(OAuthFactory.class);
    Mockito.when(oAuthFactory.getOAuthClientService(SERVICE_HANDLE)).thenReturn(getOAuthClientServiceMock());
    oauthFactoryMock = oAuthFactory;
    OpenHabOAuthTokenRefresher tokenRefresher = getService(OAuthTokenRefresher.class, OpenHabOAuthTokenRefresher.class);
    assertNotNull(tokenRefresher);
    setPrivate(Objects.requireNonNull(tokenRefresher), "oauthFactory", oAuthFactory);
}
Also used : OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) OpenHabOAuthTokenRefresher(org.openhab.binding.mielecloud.internal.auth.OpenHabOAuthTokenRefresher) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse)

Example 47 with AccessTokenResponse

use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse in project openhab-addons by openhab.

the class AbstractMieleThingHandlerTest method setUpBridge.

private void setUpBridge() throws Exception {
    AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
    accessTokenResponse.setAccessToken(ACCESS_TOKEN);
    OAuthClientService oAuthClientService = mock(OAuthClientService.class);
    when(oAuthClientService.getAccessTokenResponse()).thenReturn(accessTokenResponse);
    OAuthFactory oAuthFactory = mock(OAuthFactory.class);
    when(oAuthFactory.getOAuthClientService(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString())).thenReturn(oAuthClientService);
    OpenHabOAuthTokenRefresher tokenRefresher = getService(OAuthTokenRefresher.class, OpenHabOAuthTokenRefresher.class);
    assertNotNull(tokenRefresher);
    setPrivate(Objects.requireNonNull(tokenRefresher), "oauthFactory", oAuthFactory);
    bridge = BridgeBuilder.create(MieleCloudBindingConstants.THING_TYPE_BRIDGE, MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID).withLabel("Miele@home Account").withConfiguration(new Configuration(Collections.singletonMap(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL, MieleCloudBindingIntegrationTestConstants.EMAIL))).build();
    assertNotNull(bridge);
    getThingRegistry().add(getBridge());
    // then:
    waitForAssert(() -> {
        assertNotNull(getBridge().getHandler());
        assertTrue(getBridge().getHandler() instanceof MieleBridgeHandler, "Handler type is wrong");
    });
    MieleBridgeHandler bridgeHandler = (MieleBridgeHandler) getBridge().getHandler();
    assertNotNull(bridgeHandler);
    waitForAssert(() -> {
        assertNotNull(bridgeHandler.getThing());
    });
    bridgeHandler.initialize();
    bridgeHandler.onConnectionAlive();
    setPrivate(bridgeHandler, "discoveryService", null);
    this.bridgeHandler = bridgeHandler;
}
Also used : Configuration(org.openhab.core.config.core.Configuration) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) OpenHabOAuthTokenRefresher(org.openhab.binding.mielecloud.internal.auth.OpenHabOAuthTokenRefresher) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse)

Example 48 with AccessTokenResponse

use of com.tremolosecurity.proxy.auth.oauth2.AccessTokenResponse 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()));
    }
}
Also used : OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) AbstractTestAgent(org.openhab.core.auth.oauth2client.test.internal.AbstractTestAgent) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) IOException(java.io.IOException) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse)

Example 49 with AccessTokenResponse

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

Aggregations

AccessTokenResponse (org.openhab.core.auth.client.oauth2.AccessTokenResponse)36 OAuthException (org.openhab.core.auth.client.oauth2.OAuthException)17 IOException (java.io.IOException)15 AccessTokenResponse (org.eclipse.smarthome.core.auth.client.oauth2.AccessTokenResponse)12 OAuthResponseException (org.openhab.core.auth.client.oauth2.OAuthResponseException)12 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)8 OAuthClientService (org.openhab.core.auth.client.oauth2.OAuthClientService)8 Nullable (org.eclipse.jdt.annotation.Nullable)7 ExecutionException (java.util.concurrent.ExecutionException)6 OAuthException (org.eclipse.smarthome.core.auth.client.oauth2.OAuthException)6 TimeoutException (java.util.concurrent.TimeoutException)5 Request (org.eclipse.jetty.client.api.Request)5 OAuthFactory (org.openhab.core.auth.client.oauth2.OAuthFactory)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 GeneralSecurityException (java.security.GeneralSecurityException)4 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)3 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)3 Test (org.junit.jupiter.api.Test)3 PrivilegedActionException (java.security.PrivilegedActionException)2 Collections (java.util.Collections)2