Java Code Examples for org.scribe.exceptions.OAuthException

The following examples show how to use org.scribe.exceptions.OAuthException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: jpa-invoicer   Source File: Google2Api.java    License: The Unlicense 6 votes vote down vote up
@Override
public AccessTokenExtractor getAccessTokenExtractor() {
    return new AccessTokenExtractor() {
        
        @Override
        public Token extract(String response) {
            Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
 
            Matcher matcher = Pattern.compile("\"access_token\" : \"([^&\"]+)\"").matcher(response);
            if (matcher.find())
            {
              String token = OAuthEncoder.decode(matcher.group(1));
              return new Token(token, "", response);
            } 
            else
            {
              throw new OAuthException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null);
            }
        }
    };
}
 
Example 2
Source Project: secure-data-service   Source File: BasicRESTClient.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Response connect(final String authorizationCode) throws OAuthException, MalformedURLException,
        URISyntaxException {
    OAuthService service = new ServiceBuilder().provider(SliApi.class).apiKey(config.getApiKey())
            .apiSecret(config.getApiSecret()).callback(config.getCallback()).build();
    Verifier verifier = new Verifier(authorizationCode);
    SliApi.TokenResponse r = ((SliApi.SLIOauth20ServiceImpl) service).getAccessToken(
            new Token(config.getApiSecret(), authorizationCode), verifier, null);

    if (r != null && r.getToken() != null) {
        accessToken = r.getToken();
        sessionToken = accessToken.getToken();
    }

    ResponseBuilder builder = Response.status(r.getOauthResponse().getCode());
    for (Map.Entry<String, String> entry : r.getOauthResponse().getHeaders().entrySet()) {
        if (entry.getKey() == null) {
            builder.header("Status", entry.getValue());
        } else {
            builder.header(entry.getKey(), entry.getValue());
        }
    }
    builder.entity(r.getOauthResponse().getBody());

    return builder.build();
}
 
Example 3
@Override
public void signRequest(final Token accessToken, final OAuthRequest request) {
    request.addQuerystringParameter(OAuthConstants.ACCESS_TOKEN, accessToken.getToken());
    String response = accessToken.getRawResponse();
    Matcher matcher = openIdPattern.matcher(response);
    if (matcher.find()) {
        request.addQuerystringParameter("openid", matcher.group(1));
    } else {
        throw new OAuthException("微信接口返回数据miss openid: " + response);
    }
}
 
Example 4
public Token extract(String response){
    Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
    Matcher matcher = accessTokenPattern.matcher(response);
    if(matcher.find()){
        return new Token(matcher.group(1), "", response);
    }
    else{
        throw new OAuthException("Cannot extract an acces token. Response was: " + response);
    }
}
 
Example 5
Source Project: secure-data-service   Source File: SliTokenExtractor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Token extract(String response) {
    Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
    JsonParser parser = new JsonParser();
    JsonObject json = parser.parse(response).getAsJsonObject();

    if (json.has("error")) {
        throw new OAuthException(json.get("error_description").getAsString());
    }

    return new Token(json.get("access_token").getAsString(), "", response);
}
 
Example 6
Source Project: secure-data-service   Source File: BasicClientFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SLIClient getClientWithAuthCode(String authorizationCode) throws OAuthException, MalformedURLException,
        URISyntaxException {
    BasicRESTClient restClient = new BasicRESTClient(apiServerURL, clientId, clientSecret, callbackURL);
    restClient.connect(authorizationCode);
    SLIClient client = new BasicClient(restClient);
    return client;
}
 
Example 7
Source Project: EverMemo   Source File: EvernoteAuthToken.java    License: MIT License 5 votes vote down vote up
private String extract(String response, Pattern p) {
  Matcher matcher = p.matcher(response);
  if (matcher.find() && matcher.groupCount() >= 1) {
    return OAuthEncoder.decode(matcher.group(1));
  } else {
    throw new OAuthException("Response body is incorrect. " +
        "Can't extract token and secret from this: '" + response + "'", null);
  }
}
 
Example 8
Source Project: EverMemo-EverNote   Source File: EvernoteAuthToken.java    License: MIT License 5 votes vote down vote up
private String extract(String response, Pattern p) {
  Matcher matcher = p.matcher(response);
  if (matcher.find() && matcher.groupCount() >= 1) {
    return OAuthEncoder.decode(matcher.group(1));
  } else {
    throw new OAuthException("Response body is incorrect. " +
        "Can't extract token and secret from this: '" + response + "'", null);
  }
}
 
Example 9
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
        throws IOException, ServletException {

    HttpSession session = request.getSession();

    try {

        SliApi.setBaseUrl(apiUrl);

        // Setup OAuth service
        OAuthService service = new ServiceBuilder().provider(SliApi.class)
                .apiKey(propDecryptor.getDecryptedClientId()).apiSecret(propDecryptor.getDecryptedClientSecret())
                .callback(callbackUrl).build();

        // Check cookies for token, if found insert into session
        boolean cookieFound = checkCookiesForToken(request, session);

        Object token = session.getAttribute(OAUTH_TOKEN);

        if (token == null && request.getParameter(OAUTH_CODE) == null) {
            // Initiate authentication
            initiatingAuthentication(request, response, session, service);
        } else if (token == null && request.getParameter(OAUTH_CODE) != null) {
            // Verify authentication
            verifyingAuthentication(request, response, session, service);
        } else {
            // Complete authentication
            completeAuthentication(request, response, session, token, cookieFound);
        }
    } catch (OAuthException ex) {
        session.invalidate();
        LOG.error(LOG_MESSAGE_AUTH_EXCEPTION, new Object[] { ex.getMessage() });
        response.sendError(HttpServletResponse.SC_FORBIDDEN, ex.getMessage());
        return;
    } /* catch (Exception ex) {
        session.invalidate();
        LOG.error(LOG_MESSAGE_AUTH_EXCEPTION, new Object[] { ex.getMessage() });
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
        return;
    }*/
}
 
Example 10
Source Project: secure-data-service   Source File: SDKAPIClient.java    License: Apache License 2.0 4 votes vote down vote up
public SLIClient getClient(String sessionToken) throws OAuthException, MalformedURLException, URISyntaxException {
    return clientFactory.getClientWithSessionToken(sessionToken);
}
 
Example 11
Source Project: secure-data-service   Source File: RESTClient.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Connect to the SLI ReSTful API web service passing the authentication token provided by
 * the IDP. The IDP will redirect successful login attempts to the callbackURL and include
 * an authorization token in the response. You must then pass the authorization token to
 * this call.
 *
 * If the code is invalid, an exception is thrown.
 *
 * @requestCode Code provided to the callbackURL by the IDP.
 * @param authorizationCode
 *            Authorization request code returned by oauth to the callbackURL.
 * @return HTTP Response to the request.
 * @throws OAuthException
 * @throws MalformedURLException
 * @throws URISyntaxException
 */
public abstract Response connect(final String authorizationCode)
        throws OAuthException, MalformedURLException, URISyntaxException;
 
Example 12
Source Project: secure-data-service   Source File: SLIClientFactory.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Get an SLIClient with an authorization request code
 *
 * @param authorizationCode
 *            Authorization request code returned by oauth to the callbackURL.
 * @return a connected SLIClient
 */
SLIClient getClientWithAuthCode(String authorizationCode) throws OAuthException, MalformedURLException, URISyntaxException;
 
Example 13
Source Project: secure-data-service   Source File: SLIClientFactory.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Get an SLIClient with a session token
 *
 * @param sessionToken
 *            SAML token
 * @return a connected SLIClient
 */
SLIClient getClientWithSessionToken(String sessionToken) throws OAuthException, MalformedURLException, URISyntaxException;