Search in sources :

Example 21 with OAuthClientRequest

use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method registerRemoteApplication.

public SOAuthServer registerRemoteApplication(String redirectUrl, String name, String description) throws UserException {
    try {
        OAuthClientRequest request = OAuthClientRegistrationRequest.location(getBimServer().getServerSettingsCache().getServerSettings().getSiteAddress() + "/oauth/register/", OAuthRegistration.Type.PUSH).setName(name).setUrl(redirectUrl).setDescription(description).setRedirectURL(redirectUrl).buildJSONMessage();
        OAuthRegistrationClient oauthclient = new OAuthRegistrationClient(new org.bimserver.webservices.impl.URLConnectionClient());
        OAuthClientRegistrationResponse response = oauthclient.clientInfo(request);
        SOAuthServer server = new SOAuthServer();
        server.setClientId(response.getClientId());
        server.setClientSecret(response.getClientSecret());
        return server;
    } catch (Exception e) {
        throw new UserException(e);
    }
}
Also used : OAuthRegistrationClient(org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient) OAuthClientRegistrationResponse(org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) UserException(org.bimserver.shared.exceptions.UserException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Example 22 with OAuthClientRequest

use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project BIMserver by opensourceBIM.

the class SendUrl method main.

public static void main(String[] args) {
    try {
        OAuthClientRequest request = OAuthClientRegistrationRequest.location("https://thisisanexperimentalserver.com/oauth/register/", OAuthRegistration.Type.PUSH).setName("Zapier").setUrl("https://zapier.com/dashboard/auth/oauth/return/App56192API").setDescription("App Description").setRedirectURL("https://zapier.com/dashboard/auth/oauth/return/App56192API").buildJSONMessage();
        OAuthRegistrationClient oauthclient = new OAuthRegistrationClient(new org.bimserver.webservices.impl.URLConnectionClient());
        OAuthClientRegistrationResponse response = oauthclient.clientInfo(request);
        System.out.println(response.getClientId());
        System.out.println(response.getClientSecret());
    } catch (OAuthSystemException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (OAuthProblemException e) {
        e.printStackTrace();
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthRegistrationClient(org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient) OAuthClientRegistrationResponse(org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) IOException(java.io.IOException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 23 with OAuthClientRequest

use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project components by Talend.

the class Oauth2ImplicitClient method getAuthorizationCode.

private String getAuthorizationCode() {
    try {
        AuthenticationRequestBuilder builder = OAuthClientRequest.authorizationLocation(authorizationLocation.toString()).setClientId(clientID).setRedirectURI(callbackURL.toString());
        if (responseType != null) {
            builder.setResponseType(responseType);
        }
        OAuthClientRequest request = builder.buildQueryMessage();
        // FIXME : remove those Syso when the studio activate the INFO log by default
        System.out.println(messages.getMessage("msg.info.showAuthorizUrl"));
        System.out.println(request.getLocationUri());
        // --
        logger.info(messages.getMessage("msg.info.showAuthorizUrl"));
        logger.info(request.getLocationUri());
        OAuth2ImplicitGrantServer service = new OAuth2ImplicitGrantServer(callbackURL.getHost(), callbackURL.getPort(), 10 * 60 * 1000);
        // <--- this method wait for 10 minutes maximum to grab authorization code
        service.run();
        String code = service.getAuthorizationCode();
        service.stop();
        return code;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : AuthenticationRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder) OAuth2ImplicitGrantServer(org.talend.components.common.oauth.server.OAuth2ImplicitGrantServer) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException)

Example 24 with OAuthClientRequest

use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project components by Talend.

the class Oauth2ImplicitClient method getToken.

public <T extends OAuthAccessTokenResponse> T getToken(Class<T> tokenResponseClass) {
    try {
        TokenRequestBuilder builder = // 
        OAuthClientRequest.tokenLocation(// 
        tokenLocation.toString()).setGrantType(// 
        grantType).setClientId(// 
        clientID).setClientSecret(clientSecret);
        if (GrantType.AUTHORIZATION_CODE == grantType) {
            builder = // 
            builder.setRedirectURI(callbackURL.toString()).setCode(getAuthorizationCode());
        } else if (GrantType.REFRESH_TOKEN == grantType) {
            builder = builder.setRefreshToken(refreshToken);
        }
        OAuthClientRequest request = builder.buildQueryMessage();
        OAuthClient oauthClient = new OAuthClient(new URLConnectionClient());
        return oauthClient.accessToken(request, tokenResponseClass);
    } catch (OAuthSystemException e) {
        throw new RuntimeException(e);
    } catch (OAuthProblemException e) {
        throw new RuntimeException(e);
    }
}
Also used : TokenRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 25 with OAuthClientRequest

use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project android-client by GenesisVision.

the class OAuth method retryingIntercept.

private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAuthorizationFailure) throws IOException {
    Request request = chain.request();
    // If the request already have an authorization (eg. Basic auth), do nothing
    if (request.header("Authorization") != null) {
        return chain.proceed(request);
    }
    // If first time, get the token
    OAuthClientRequest oAuthRequest;
    if (getAccessToken() == null) {
        updateAccessToken(null);
    }
    if (getAccessToken() != null) {
        // Build the request
        Builder rb = request.newBuilder();
        String requestAccessToken = new String(getAccessToken());
        try {
            oAuthRequest = new OAuthBearerClientRequest(request.url().toString()).setAccessToken(requestAccessToken).buildHeaderMessage();
        } catch (OAuthSystemException e) {
            throw new IOException(e);
        }
        for (Map.Entry<String, String> header : oAuthRequest.getHeaders().entrySet()) {
            rb.addHeader(header.getKey(), header.getValue());
        }
        rb.url(oAuthRequest.getLocationUri());
        // Execute the request
        Response response = chain.proceed(rb.build());
        // 401/403 most likely indicates that access token has expired. Unless it happens two times in a row.
        if (response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure) {
            if (updateAccessToken(requestAccessToken)) {
                return retryingIntercept(chain, false);
            }
        }
        return response;
    } else {
        return chain.proceed(chain.request());
    }
}
Also used : OAuthBearerClientRequest(org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) Response(okhttp3.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) AuthenticationRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder) Builder(okhttp3.Request.Builder) TokenRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder) Request(okhttp3.Request) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) OAuthBearerClientRequest(org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest) IOException(java.io.IOException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) Map(java.util.Map)

Aggregations

OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)34 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)25 IOException (java.io.IOException)20 Request (okhttp3.Request)18 Response (okhttp3.Response)18 Builder (okhttp3.Request.Builder)17 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)13 OAuthBearerClientRequest (org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest)11 Map (java.util.Map)10 AuthenticationRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder)10 TokenRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder)10 MediaType (okhttp3.MediaType)9 RequestBody (okhttp3.RequestBody)9 OAuthClientResponse (org.apache.oltu.oauth2.client.response.OAuthClientResponse)9 URI (java.net.URI)6 URLConnectionClient (org.apache.oltu.oauth2.client.URLConnectionClient)6 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)5 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)5 OAuthRegistrationClient (org.apache.oltu.oauth2.ext.dynamicreg.client.OAuthRegistrationClient)3 OAuthClientRegistrationResponse (org.apache.oltu.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse)3