Search in sources :

Example 1 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project SeriesGuide by UweTrottmann.

the class AnalyticsTree method log.

@Override
protected void log(int priority, String tag, String message, Throwable t) {
    if (priority == Log.ERROR) {
        // remove any stack trace attached by Timber
        if (message != null) {
            int newLine = message.indexOf('\n');
            if (newLine > 0) {
                message = message.substring(0, newLine);
            }
        }
        // special treatment for some exceptions
        if (t instanceof TvdbException) {
            TvdbException e = (TvdbException) t;
            Utils.trackCustomEvent(context, CATEGORY_THETVDB_ERROR, tag + ": " + message, e.getMessage());
            return;
        } else if (t instanceof OAuthProblemException) {
            // log trakt OAuth failures
            OAuthProblemException e = (OAuthProblemException) t;
            StringBuilder exceptionMessage = new StringBuilder();
            if (!TextUtils.isEmpty(e.getError())) {
                exceptionMessage.append(e.getError());
            }
            if (!TextUtils.isEmpty(e.getDescription())) {
                exceptionMessage.append(", ").append(e.getDescription());
            }
            if (!TextUtils.isEmpty(e.getUri())) {
                exceptionMessage.append(", ").append(e.getUri());
            }
            Utils.trackCustomEvent(context, "OAuth Error", tag + ": " + message, exceptionMessage.toString());
            return;
        } else if (t instanceof OAuthSystemException) {
            // log trakt OAuth failures
            OAuthSystemException e = (OAuthSystemException) t;
            Utils.trackCustomEvent(context, "OAuth Error", tag + ": " + message, e.getMessage());
            return;
        }
    }
    // drop empty messages
    if (message == null) {
        return;
    }
    // drop debug and verbose logs
    if (priority == Log.DEBUG || priority == Log.VERBOSE) {
        return;
    }
    // transform priority into string
    String level = null;
    switch(priority) {
        case Log.INFO:
            level = "INFO";
            break;
        case Log.WARN:
            level = "WARN";
            break;
        case Log.ERROR:
            level = "ERROR";
            break;
    }
    // finally log to crashlytics
    Crashlytics.log(level + "/" + tag + ": " + message);
    // track some non-fatal exceptions with crashlytics
    if (priority == Log.ERROR) {
        if (t instanceof SQLiteException) {
            Crashlytics.logException(t);
        }
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) TvdbException(com.battlelancer.seriesguide.thetvdbapi.TvdbException) SQLiteException(android.database.sqlite.SQLiteException)

Example 2 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project BIMserver by opensourceBIM.

the class JsonHandler method getServiceInterface.

private <T extends PublicInterface> T getServiceInterface(HttpServletRequest httpRequest, BimServer bimServer, Class<T> interfaceClass, String methodName, String token, String oAuthCode) throws UserException, ServerException {
    if (methodName.equals("login") || methodName.equals("autologin")) {
        return bimServer.getServiceFactory().get(AccessMethod.JSON).get(interfaceClass);
    }
    OAuthAccessResourceRequest oauthRequest;
    try {
        oauthRequest = new OAuthAccessResourceRequest(httpRequest, ParameterStyle.HEADER);
        token = oauthRequest.getAccessToken();
    } catch (OAuthSystemException e) {
    } catch (OAuthProblemException e) {
    }
    if (token == null) {
        token = httpRequest == null ? null : (String) httpRequest.getSession().getAttribute("token");
    }
    if (token == null) {
        token = oAuthCode;
    }
    if (token == null) {
        return bimServer.getServiceFactory().get(AccessMethod.JSON).get(interfaceClass);
    }
    T service = bimServer.getServiceFactory().get(token, AccessMethod.JSON).get(interfaceClass);
    if (service == null) {
        service = bimServer.getServiceFactory().get(AccessMethod.JSON).get(interfaceClass);
        if (httpRequest != null) {
            httpRequest.getSession().setAttribute("token", token);
        }
    }
    return service;
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthAccessResourceRequest(org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException)

Example 3 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project entando-core by entando.

the class EntandoOauth2Interceptor method extractOAuthParameters.

protected void extractOAuthParameters(HttpServletRequest request, String permission) {
    try {
        logger.info("Permission required: {}", permission);
        OAuthAccessResourceRequest requestMessage = new OAuthAccessResourceRequest(request, ParameterStyle.HEADER);
        String accessToken = requestMessage.getAccessToken();
        if (StringUtils.isBlank(accessToken)) {
            throw new EntandoTokenException("no access token found", request, null);
        }
        final OAuth2Token token = oAuth2TokenManager.getApiOAuth2Token(accessToken);
        this.validateToken(request, accessToken, token);
        String username = token.getClientId();
        this.checkAuthorization(username, permission, request);
    } catch (OAuthSystemException | ApsSystemException | OAuthProblemException ex) {
        logger.error("System exception {}", ex.getMessage());
        throw new EntandoTokenException("error parsing OAuth parameters", request, "guest");
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) EntandoTokenException(org.entando.entando.web.common.exceptions.EntandoTokenException) OAuthAccessResourceRequest(org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuth2Token(org.entando.entando.aps.system.services.oauth2.model.OAuth2Token) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException)

Example 4 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project entando-core by entando.

the class TokenEndpointServlet method validateClientWithAuthorizationCode.

private OAuthResponse validateClientWithAuthorizationCode(HttpServletRequest request) throws Throwable {
    try {
        final OAuthTokenRequest oauthRequest = new OAuthTokenRequest(request);
        IOAuthConsumerManager consumerManager = (IOAuthConsumerManager) ApsWebApplicationUtils.getBean(SystemConstants.OAUTH_CONSUMER_MANAGER, request);
        IApiOAuthorizationCodeManager codeManager = (IApiOAuthorizationCodeManager) ApsWebApplicationUtils.getBean(SystemConstants.OAUTH2_AUTHORIZATION_CODE_MANAGER, request);
        if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.AUTHORIZATION_CODE.toString()) || oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.REFRESH_TOKEN.toString())) {
            final String clientId = oauthRequest.getClientId();
            final String oauthType = GrantType.AUTHORIZATION_CODE.toString();
            final String authCode = oauthRequest.getParam(OAuth.OAUTH_CODE);
            final String clientSecret = oauthRequest.getClientSecret();
            boolean checkVerifyAccess = codeManager.verifyAccess(clientId, clientSecret, consumerManager);
            if (!checkVerifyAccess) {
                _logger.error(ERROR_AUTHENTICATION_FAILED);
                return null;
            } else if (!codeManager.verifyCode(authCode, request.getRemoteAddr())) {
                _logger.error("OAuth2 authcode does not match or the source of client is different");
                return null;
            }
            return this.registerToken(request, clientId, oauthType, null);
        } else if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.PASSWORD.toString())) {
            final String username = oauthRequest.getUsername();
            final String password = oauthRequest.getPassword();
            final String oauthType = GrantType.PASSWORD.toString();
            IUserManager userManager = (IUserManager) ApsWebApplicationUtils.getBean(SystemConstants.USER_MANAGER, request);
            UserDetails user = userManager.getUser(username, password);
            if (user == null) {
                _logger.error(ERROR_AUTHENTICATION_FAILED);
                return null;
            }
            return this.registerToken(request, username, oauthType, null);
        } else {
            return null;
        }
    } catch (OAuthSystemException e) {
        _logger.error("OAuthSystemException - {} ", e);
        return null;
    } catch (OAuthProblemException e) {
        _logger.error("OAuthProblemException - {} ", e.getError().concat(" ").concat(e.getDescription()));
        _logger.debug("OAuthProblemException - {} ", e);
        return null;
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) UserDetails(com.agiletec.aps.system.services.user.UserDetails) IOAuthConsumerManager(org.entando.entando.aps.system.services.oauth2.IOAuthConsumerManager) IUserManager(com.agiletec.aps.system.services.user.IUserManager) IApiOAuthorizationCodeManager(org.entando.entando.aps.system.services.oauth2.IApiOAuthorizationCodeManager) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthTokenRequest(org.apache.oltu.oauth2.as.request.OAuthTokenRequest)

Example 5 with OAuthSystemException

use of org.apache.amber.oauth2.common.exception.OAuthSystemException in project entando-core by entando.

the class AuthenticationProviderManager method registerToken.

private void registerToken(final UserDetails user) {
    OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
    try {
        final String accessToken = oauthIssuerImpl.accessToken();
        final String refreshToken = oauthIssuerImpl.refreshToken();
        user.setAccessToken(accessToken);
        user.setRefreshToken(refreshToken);
        final OAuth2Token oAuth2Token = new OAuth2Token();
        oAuth2Token.setAccessToken(accessToken);
        oAuth2Token.setRefreshToken(refreshToken);
        oAuth2Token.setClientId("LOCAL_USER");
        oAuth2Token.setLocalUser(user.getUsername());
        // gets a calendar using the default time zone and locale.
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 3600);
        oAuth2Token.setExpiresIn(calendar.getTime());
        oAuth2Token.setGrantType(GrantType.IMPLICIT.toString());
        tokenManager.addApiOAuth2Token(oAuth2Token, true);
    } catch (OAuthSystemException e) {
        _logger.error("OAuthSystemException {} ", e.getMessage());
        _logger.debug("OAuthSystemException {} ", e);
    } catch (ApsSystemException e) {
        _logger.error("ApsSystemException {} ", e.getMessage());
        _logger.debug("ApsSystemException {} ", e);
    }
}
Also used : OAuthIssuerImpl(org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) Calendar(java.util.Calendar) OAuth2Token(org.entando.entando.aps.system.services.oauth2.model.OAuth2Token) MD5Generator(org.apache.oltu.oauth2.as.issuer.MD5Generator) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) OAuthIssuer(org.apache.oltu.oauth2.as.issuer.OAuthIssuer)

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