net.oauth.client.OAuthClient Java Examples

The following examples show how to use net.oauth.client.OAuthClient. 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 File: OAuthServiceImplRobotTest.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the case when the user has not started the authorization process (no
 * request token).
 */
public final void testCheckAuthorizationNoRequestToken() {
  // Setup.
  LoginFormHandler loginForm = mock(LoginFormHandler.class);
  OAuthClient client = mock(OAuthClient.class);
  PersistenceManager pm = mock(PersistenceManager.class);
  PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class);

  OAuthAccessor accessor = buildAccessor(CONSUMER_KEY, CONSUMER_SECRET,
      REQUEST_TOKEN_URL, AUTHORIZE_URL, CALLBACK_URL, ACCESS_TOKEN_URL);
  accessor.requestToken = REQUEST_TOKEN_STRING;
  oauthService = new OAuthServiceImpl(accessor, client, pmf,
      USER_RECORD_KEY);
  OAuthUser userWithRequestToken = new OAuthUser(USER_RECORD_KEY, REQUEST_TOKEN_STRING);

  // Expectations.
  when(pmf.getPersistenceManager()).thenReturn(pm);
  when(pm.getObjectById(OAuthUser.class, USER_RECORD_KEY)).thenReturn(null, userWithRequestToken,
      userWithRequestToken);

  assertFalse(oauthService.checkAuthorization(null, loginForm));

  String authUrl = userWithRequestToken.getAuthUrl();
  try {
    new URL(authUrl);
  } catch (MalformedURLException e) {
    fail("Malformed authUrl");
  }

  assertTrue(Pattern.matches(".+(oauth_token){1}.+", authUrl));
  assertTrue(Pattern.matches(".+(oauth_callback){1}.+", authUrl));
}
 
Example #2
Source File: OAuthServiceImplRobotTest.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the case when the user has not started the authorization process (no
 * request token).
 */
public final void testCheckAuthorizationNoRequestToken() {
  // Setup.
  LoginFormHandler loginForm = mock(LoginFormHandler.class);
  OAuthClient client = mock(OAuthClient.class);
  PersistenceManager pm = mock(PersistenceManager.class);
  PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class);

  OAuthAccessor accessor = buildAccessor(CONSUMER_KEY, CONSUMER_SECRET,
      REQUEST_TOKEN_URL, AUTHORIZE_URL, CALLBACK_URL, ACCESS_TOKEN_URL);
  accessor.requestToken = REQUEST_TOKEN_STRING;
  oauthService = new OAuthServiceImpl(accessor, client, pmf,
      USER_RECORD_KEY);
  OAuthUser userWithRequestToken = new OAuthUser(USER_RECORD_KEY, REQUEST_TOKEN_STRING);

  // Expectations.
  when(pmf.getPersistenceManager()).thenReturn(pm);
  when(pm.getObjectById(OAuthUser.class, USER_RECORD_KEY)).thenReturn(null, userWithRequestToken,
      userWithRequestToken);

  assertFalse(oauthService.checkAuthorization(null, loginForm));

  String authUrl = userWithRequestToken.getAuthUrl();
  try {
    new URL(authUrl);
  } catch (MalformedURLException e) {
    fail("Malformed authUrl");
  }

  assertTrue(Pattern.matches(".+(oauth_token){1}.+", authUrl));
  assertTrue(Pattern.matches(".+(oauth_callback){1}.+", authUrl));
}
 
Example #3
Source File: TokenRequestController.java    From cxf with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/tokenRequest")
protected ModelAndView handleRequest(@ModelAttribute("oAuthParams") OAuthParams oAuthParams,
                                     HttpServletRequest request)
    throws Exception {

    String oauthToken = oAuthParams.getOauthToken();

    String tokenRequestEndpoint = oAuthParams.getTokenRequestEndpoint();
    String clientID = oAuthParams.getClientID();

    if (tokenRequestEndpoint == null || "".equals(tokenRequestEndpoint)) {
        oAuthParams.setErrorMessage("Missing token request URI");
    }

    if (clientID == null || "".equals(clientID)) {
        oAuthParams.setErrorMessage("Missing consumer key");
    }

    if (oauthToken == null || "".equals(oauthToken)) {
        oAuthParams.setErrorMessage("Missing oauth token");
    }

    String verifier = oAuthParams.getOauthVerifier();
    if (verifier == null || "".equals(verifier)) {
        oAuthParams.setErrorMessage("Missing oauth verifier");
    }

    if (oAuthParams.getErrorMessage() == null) {
        OAuthClient client = new OAuthClient(new URLConnectionClient());
        OAuthServiceProvider provider = new OAuthServiceProvider(
            oAuthParams.getTemporaryCredentialsEndpoint(),
            oAuthParams.getResourceOwnerAuthorizationEndpoint(), tokenRequestEndpoint);

        OAuthConsumer consumer = new OAuthConsumer(null, clientID,
            oAuthParams.getClientSecret(),
            provider);
        OAuthAccessor accessor = new OAuthAccessor(consumer);
        accessor.requestToken = oauthToken;
        accessor.tokenSecret = Common.findCookieValue(request, "tokenSec");

        Map<String, String> parameters = new HashMap<>();
        parameters.put(OAuth.OAUTH_SIGNATURE_METHOD, oAuthParams.getSignatureMethod());
        parameters.put(OAuth.OAUTH_NONCE, UUID.randomUUID().toString());
        parameters.put(OAuth.OAUTH_TIMESTAMP, String.valueOf(System.currentTimeMillis() / 1000));
        parameters.put(OAuth.OAUTH_TOKEN, oauthToken);
        parameters.put(OAuth.OAUTH_VERIFIER, oAuthParams.getOauthVerifier());


        try {
            client.getAccessToken(accessor, OAuthMessage.GET, parameters.entrySet());
            oAuthParams.setOauthToken(accessor.accessToken);
        } catch (Exception e) {
            oAuthParams.setErrorMessage(e.toString());
            oAuthParams.setOauthToken(oauthToken);
            return new ModelAndView("tokenRequest");
        }
        oAuthParams.setOauthTokenSecret(accessor.tokenSecret);
    }

    oAuthParams.setClientID(Common.findCookieValue(request, "clientID"));
    oAuthParams.setClientSecret(Common.findCookieValue(request, "clientSecret"));

    return new ModelAndView("accessToken");
}
 
Example #4
Source File: GetProtectedResourceController.java    From cxf with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/getProtectedResource")
protected ModelAndView handleRequest(@ModelAttribute("oAuthParams") OAuthParams oAuthParams,
                                     HttpServletRequest request)
    throws Exception {

    OAuthServiceProvider provider = new OAuthServiceProvider(
        oAuthParams.getTemporaryCredentialsEndpoint(),
        oAuthParams.getResourceOwnerAuthorizationEndpoint(), null);

    OAuthConsumer consumer = new OAuthConsumer(null, oAuthParams.getClientID(),
        oAuthParams.getClientSecret(),
        provider);
    OAuthAccessor accessor = new OAuthAccessor(consumer);
    accessor.requestToken = oAuthParams.getOauthToken();
    accessor.tokenSecret = oAuthParams.getOauthTokenSecret();

    Map<String, String> parameters = new HashMap<>();
    parameters.put(OAuth.OAUTH_SIGNATURE_METHOD, oAuthParams.getSignatureMethod());
    parameters.put(OAuth.OAUTH_NONCE, UUID.randomUUID().toString());
    parameters.put(OAuth.OAUTH_TIMESTAMP, String.valueOf(System.currentTimeMillis() / 1000));
    parameters.put(OAuth.OAUTH_TOKEN, oAuthParams.getOauthToken());
    parameters.put(OAuth.OAUTH_CONSUMER_KEY, oAuthParams.getClientID());

    OAuthMessage msg = null;
    String method = request.getParameter("op");


    if ("GET".equals(method)) {
        msg = accessor
            .newRequestMessage(OAuthMessage.GET, oAuthParams.getGetResourceURL(), parameters.entrySet());
    } else {
        msg = accessor
            .newRequestMessage(OAuthMessage.POST, oAuthParams.getPostResourceURL(),
                parameters.entrySet());
    }


    OAuthClient client = new OAuthClient(new URLConnectionClient());

    msg = client.access(msg, ParameterStyle.QUERY_STRING);

    StringBuilder bodyBuffer = readBody(msg);

    oAuthParams.setResourceResponse(bodyBuffer.toString());
    String authHeader = msg.getHeader("WWW-Authenticate");
    String oauthHeader = msg.getHeader("OAuth");
    String header = "";

    if (authHeader != null) {
        header += "WWW-Authenticate:" + authHeader;
    }

    if (oauthHeader != null) {
        header += "OAuth:" + oauthHeader;
    }

    oAuthParams.setHeader(header);
    oAuthParams.setResponseCode(((OAuthResponseMessage)msg).getHttpResponse().getStatusCode());

    return new ModelAndView("accessToken");
}
 
Example #5
Source File: OAuthServiceImpl.java    From swellrt with Apache License 2.0 3 votes vote down vote up
/**
 * Factory method. Initializes OAuthServiceProvider with necessary tokens and
 * urls.
 * 
 * @param userRecordKey key consisting of user id and wave id.
 * @param consumerKey service provider OAuth consumer key.
 * @param consumerSecret service provider OAuth consumer secret.
 * @param requestTokenUrl url to get service provider request token.
 * @param authorizeUrl url to service provider authorize page.
 * @param callbackUrl url to callback page.
 * @param accessTokenUrl url to get service provider access token.
 * @return OAuthService instance.
 */
public static OAuthService newInstance(String userRecordKey, String consumerKey,
    String consumerSecret, String requestTokenUrl, String authorizeUrl, String callbackUrl,
    String accessTokenUrl) {
  OAuthServiceProvider provider =
      new OAuthServiceProvider(requestTokenUrl, authorizeUrl, accessTokenUrl);
  OAuthConsumer consumer = new OAuthConsumer(callbackUrl, consumerKey, consumerSecret, provider);
  OAuthAccessor accessor = new OAuthAccessor(consumer);
  OAuthClient client = new OAuthClient(new OpenSocialHttpClient());
  PersistenceManagerFactory pmf = SingletonPersistenceManagerFactory.get();
  return new OAuthServiceImpl(accessor, client, pmf, userRecordKey);
}
 
Example #6
Source File: OAuthServiceImpl.java    From swellrt with Apache License 2.0 3 votes vote down vote up
/**
 * Initializes necessary OAuthClient and accessor objects for OAuth handling.
 * 
 * @param accessor Used to store tokesn for OAuth authorization.
 * @param client Handles OAuth authorization.
 * @param pmf Manages datastore fetching and storing.
 * @param recordKey User id for datastore object.
 */
OAuthServiceImpl(OAuthAccessor accessor, OAuthClient client,
    PersistenceManagerFactory pmf, String recordKey) {
  this.userRecordKey = recordKey;
  this.pmf = pmf;
  this.accessor = accessor;
  this.oauthClient = client;
}
 
Example #7
Source File: OAuthServiceImpl.java    From incubator-retired-wave with Apache License 2.0 3 votes vote down vote up
/**
 * Factory method. Initializes OAuthServiceProvider with necessary tokens and
 * urls.
 * 
 * @param userRecordKey key consisting of user id and wave id.
 * @param consumerKey service provider OAuth consumer key.
 * @param consumerSecret service provider OAuth consumer secret.
 * @param requestTokenUrl url to get service provider request token.
 * @param authorizeUrl url to service provider authorize page.
 * @param callbackUrl url to callback page.
 * @param accessTokenUrl url to get service provider access token.
 * @return OAuthService instance.
 */
public static OAuthService newInstance(String userRecordKey, String consumerKey,
    String consumerSecret, String requestTokenUrl, String authorizeUrl, String callbackUrl,
    String accessTokenUrl) {
  OAuthServiceProvider provider =
      new OAuthServiceProvider(requestTokenUrl, authorizeUrl, accessTokenUrl);
  OAuthConsumer consumer = new OAuthConsumer(callbackUrl, consumerKey, consumerSecret, provider);
  OAuthAccessor accessor = new OAuthAccessor(consumer);
  OAuthClient client = new OAuthClient(new OpenSocialHttpClient());
  PersistenceManagerFactory pmf = SingletonPersistenceManagerFactory.get();
  return new OAuthServiceImpl(accessor, client, pmf, userRecordKey);
}
 
Example #8
Source File: OAuthServiceImpl.java    From incubator-retired-wave with Apache License 2.0 3 votes vote down vote up
/**
 * Initializes necessary OAuthClient and accessor objects for OAuth handling.
 * 
 * @param accessor Used to store tokesn for OAuth authorization.
 * @param client Handles OAuth authorization.
 * @param pmf Manages datastore fetching and storing.
 * @param recordKey User id for datastore object.
 */
OAuthServiceImpl(OAuthAccessor accessor, OAuthClient client,
    PersistenceManagerFactory pmf, String recordKey) {
  this.userRecordKey = recordKey;
  this.pmf = pmf;
  this.accessor = accessor;
  this.oauthClient = client;
}
 
Example #9
Source File: OAuthMessage.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Construct an HTTP request from this OAuth message.
 * 
 * @param style
 *            where to put the OAuth parameters, within the HTTP request
 * @deprecated use HttpMessage.newRequest
 */
public HttpMessage toHttpRequest(OAuthClient.ParameterStyle style) throws IOException {
    return HttpMessage.newRequest(this, style.getReplacement());
}
 
Example #10
Source File: OAuthMessage.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Construct an HTTP request from this OAuth message.
 * 
 * @param style
 *            where to put the OAuth parameters, within the HTTP request
 * @deprecated use HttpMessage.newRequest
 */
public HttpMessage toHttpRequest(OAuthClient.ParameterStyle style) throws IOException {
    return HttpMessage.newRequest(this, style.getReplacement());
}