Search in sources :

Example 31 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project lusid-sdk-java by finbourne.

the class OAuthOkHttpClient method execute.

@Override
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers, String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {
    MediaType mediaType = MediaType.parse("application/json");
    Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());
    if (headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("Content-Type")) {
                mediaType = MediaType.parse(entry.getValue());
            } else {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }
    }
    RequestBody body = request.getBody() != null ? RequestBody.create(request.getBody(), mediaType) : null;
    requestBuilder.method(requestMethod, body);
    try {
        Response response = client.newCall(requestBuilder.build()).execute();
        return OAuthClientResponseFactory.createCustomResponse(response.body().string(), response.body().contentType().toString(), response.code(), responseClass);
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
}
Also used : OAuthClientResponse(org.apache.oltu.oauth2.client.response.OAuthClientResponse) Response(okhttp3.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) Request(okhttp3.Request) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) MediaType(okhttp3.MediaType) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 32 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project aos-Video by nova-video-player.

the class TraktSigninDialogPreference method onClick.

@Override
public void onClick() {
    try {
        OAuthClientRequest t = Trakt.getAuthorizationRequest(getSharedPreferences());
        final OAuthData oa = new OAuthData();
        OAuthCallback codeCallBack = new OAuthCallback() {

            @Override
            public void onFinished(final OAuthData data) {
                // TODO Auto-generated method stub
                if (data.code != null) {
                    final ProgressDialog mProgress = new ProgressDialog(getContext());
                    AsyncTask t = new AsyncTask() {

                        @Override
                        protected void onPreExecute() {
                            mProgress.show();
                        }

                        @Override
                        protected Object doInBackground(Object... params) {
                            OAuthAccessTokenResponse res = Trakt.getAccessToken(oa.code);
                            return res;
                        }

                        @Override
                        protected void onPostExecute(Object result) {
                            mProgress.dismiss();
                            if (result != null && result instanceof OAuthAccessTokenResponse) {
                                OAuthAccessTokenResponse res = (OAuthAccessTokenResponse) result;
                                if (res.getAccessToken() != null) {
                                    Trakt.setAccessToken(getSharedPreferences(), res.getAccessToken());
                                    Trakt.setRefreshToken(getSharedPreferences(), res.getRefreshToken());
                                    TraktSigninDialogPreference.this.notifyChanged();
                                }
                            }
                        }
                    };
                    t.execute();
                } else {
                    new AlertDialog.Builder(getContext()).setNegativeButton(android.R.string.ok, null).setMessage(R.string.dialog_subloader_nonetwork_title).setIcon(android.R.drawable.ic_dialog_alert).show();
                }
            }
        };
        od = new OAuthDialog(getContext(), codeCallBack, oa, t);
        od.show();
        od.setOnDismissListener(mOnDismissListener);
        od.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialogInterface) {
                mOnDismissListener.onDismiss(dialogInterface);
            }
        });
    } catch (OAuthSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : DialogInterface(android.content.DialogInterface) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) AsyncTask(android.os.AsyncTask) ProgressDialog(android.app.ProgressDialog) OAuthAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 33 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project pyramus by otavanopisto.

the class SecurityFilter method hasOAuthApiAccess.

private boolean hasOAuthApiAccess() {
    try {
        OAuthAccessResourceRequest oauthRequest = new OAuthAccessResourceRequest(request, ParameterStyle.HEADER);
        String accessToken = oauthRequest.getAccessToken();
        ClientApplicationAccessToken clientApplicationAccessToken = oauthController.findByAccessToken(accessToken);
        if (clientApplicationAccessToken == null) {
            return false;
        } else {
            Long currentTime = System.currentTimeMillis() / 1000L;
            if (currentTime > clientApplicationAccessToken.getExpires()) {
                return false;
            } else {
                return true;
            }
        }
    } catch (OAuthProblemException e) {
        return false;
    } catch (OAuthSystemException e) {
        return false;
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthAccessResourceRequest(org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest) ClientApplicationAccessToken(fi.otavanopisto.pyramus.domainmodel.clientapplications.ClientApplicationAccessToken) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException)

Example 34 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project pyramus by otavanopisto.

the class AbstractRESTPermissionsTest method createAccessTokens.

@Before
public void createAccessTokens() {
    OAuthClientRequest tokenRequest = null;
    if (!Role.EVERYONE.name().equals(role)) {
        try {
            tokenRequest = OAuthClientRequest.tokenLocation("https://dev.pyramus.fi:8443/1/oauth/token").setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(fi.otavanopisto.pyramus.Common.CLIENT_ID).setClientSecret(fi.otavanopisto.pyramus.Common.CLIENT_SECRET).setRedirectURI(fi.otavanopisto.pyramus.Common.REDIRECT_URL).setCode(fi.otavanopisto.pyramus.Common.getRoleAuth(Common.strToRole(role))).buildBodyMessage();
        } catch (OAuthSystemException e) {
            e.printStackTrace();
        }
        Response response = given().contentType("application/x-www-form-urlencoded").body(tokenRequest.getBody()).post("/oauth/token");
        String accessToken = response.body().jsonPath().getString("access_token");
        setAccessToken(accessToken);
    } else {
        setAccessToken("");
    }
    /**
     * AdminAccessToken
     */
    if (!Role.ADMINISTRATOR.name().equals(role)) {
        tokenRequest = null;
        try {
            tokenRequest = OAuthClientRequest.tokenLocation("https://dev.pyramus.fi:8443/1/oauth/token").setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(fi.otavanopisto.pyramus.Common.CLIENT_ID).setClientSecret(fi.otavanopisto.pyramus.Common.CLIENT_SECRET).setRedirectURI(fi.otavanopisto.pyramus.Common.REDIRECT_URL).setCode(fi.otavanopisto.pyramus.Common.getRoleAuth(Role.ADMINISTRATOR)).buildBodyMessage();
        } catch (OAuthSystemException e) {
            e.printStackTrace();
        }
        Response response = given().contentType("application/x-www-form-urlencoded").body(tokenRequest.getBody()).post("/oauth/token");
        String adminAccessToken = response.body().jsonPath().getString("access_token");
        setAdminAccessToken(adminAccessToken);
    } else {
        setAdminAccessToken(accessToken);
    }
}
Also used : Response(io.restassured.response.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) Before(org.junit.Before)

Example 35 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project pyramus by otavanopisto.

the class AbstractRESTPermissionsTestJUnit5 method getOauthToken.

protected String getOauthToken(Role role) {
    if (!Role.EVERYONE.equals(role)) {
        OAuthClientRequest tokenRequest = null;
        try {
            tokenRequest = OAuthClientRequest.tokenLocation("https://dev.pyramus.fi:8443/1/oauth/token").setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(fi.otavanopisto.pyramus.Common.CLIENT_ID).setClientSecret(fi.otavanopisto.pyramus.Common.CLIENT_SECRET).setRedirectURI(fi.otavanopisto.pyramus.Common.REDIRECT_URL).setCode(fi.otavanopisto.pyramus.Common.getRoleAuth(role)).buildBodyMessage();
        } catch (OAuthSystemException e) {
            e.printStackTrace();
        }
        Response response = given().contentType("application/x-www-form-urlencoded").body(tokenRequest.getBody()).post("/oauth/token");
        return response.body().jsonPath().getString("access_token");
    }
    return "";
}
Also used : Response(io.restassured.response.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Aggregations

OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)100 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)47 IOException (java.io.IOException)37 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)36 Request (okhttp3.Request)27 Response (okhttp3.Response)27 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)20 Builder (okhttp3.Request.Builder)17 OAuthBearerClientRequest (org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest)17 Map (java.util.Map)15 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)15 OAuthClientResponse (org.apache.oltu.oauth2.client.response.OAuthClientResponse)14 MediaType (okhttp3.MediaType)13 RequestBody (okhttp3.RequestBody)13 TokenRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder)12 AuthenticationRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder)11 Path (javax.ws.rs.Path)10 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)9 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)9 HashMap (java.util.HashMap)8