Package org.apache.shindig.gadgets.oauth2

Examples of org.apache.shindig.gadgets.oauth2.OAuth2Message


        if (ret == null) {
          final long issuedAt = System.currentTimeMillis();

          final String contentType = response.getHeader("Content-Type");
          final String responseString = response.getResponseAsString();
          final OAuth2Message msg = this.oauth2MessageProvider.get();

          if (isLogging) {
            TokenAuthorizationResponseHandler.LOG.log("Content-Type {0}", contentType);
            TokenAuthorizationResponseHandler.LOG.log("Response String {0}", responseString);
          }
          if (contentType.startsWith("application/json")) {
            // Google does this
            final JSONObject responseJson = new JSONObject(responseString);
            msg.parseJSON(responseJson.toString());
          } else {
            // Default assume it is application/x-www-form-urlencoded
            // Facebook has a content type of text/plain
            // GitHub has a content type of application/x-www-form-urlencoded
            msg.parseQuery('?' + responseString);
          }

          final OAuth2Error error = msg.getError();
          if (error != null) {
            ret = getError("error parsing request", null, msg.getErrorUri(),
                    msg.getErrorDescription());
          } else if (error == null && accessor != null) {
            final String accessToken = msg.getAccessToken();
            final String refreshToken = msg.getRefreshToken();
            final String expiresIn = msg.getExpiresIn();
            final String tokenType = msg.getTokenType();
            final String providerName = accessor.getServiceName();
            final String gadgetUri = accessor.getGadgetUri();
            final String scope = accessor.getScope();
            final String user = accessor.getUser();
            final String macAlgorithm = msg.getMacAlgorithm();
            final String macSecret = msg.getMacSecret();
            final Map<String, String> unparsedProperties = msg.getUnparsedProperties();

            if (accessToken != null) {
              final OAuth2Token storedAccessToken = this.store.createToken();
              storedAccessToken.setIssuedAt(issuedAt);
              if (expiresIn != null) {
View Full Code Here


              "grant_type is not code", null);
    }

    if (ret == null) {
      try {
        final OAuth2Message msg = this.oauth2MessageProvider.get();
        msg.parseRequest(request);
        if (msg.getError() != null) {
          ret = new OAuth2HandlerError(msg.getError(), "error parsing authorization response",
                  null, msg.getErrorUri(), msg.getErrorDescription());
        } else {
          ret = this.setAuthorizationCode(msg.getAuthorization(), accessor);
        }
      } catch (final Exception e) {
        if (CodeAuthorizationResponseHandler.LOG.isLoggable()) {
          CodeAuthorizationResponseHandler.LOG.log(
                  "Exception exchanging authorization code for access_token", e);
View Full Code Here

                "error exchanging code for access_token", e);
      }

      if (ret == null && response != null) {
        if (response.getHttpStatusCode() != HttpResponse.SC_OK) {
          final OAuth2Message msg = this.oauth2MessageProvider.get();
          msg.parseJSON(response.getResponseAsString());
          if (msg.getError() != null) {
            ret = new OAuth2HandlerError(msg.getError(), "error exchanging code for access_token",
                    null, msg.getErrorUri(), msg.getErrorDescription());
          }
        }

        if (ret == null) {
          for (final TokenEndpointResponseHandler tokenEndpointResponseHandler : this.tokenEndpointResponseHandlers) {
View Full Code Here

        if (ret == null) {
          final long issuedAt = System.currentTimeMillis();

          final String contentType = response.getHeader("Content-Type");
          final String responseString = response.getResponseAsString();
          final OAuth2Message msg = this.oauth2MessageProvider.get();

          if (contentType.startsWith("text/plain")) {
            // Facebook does this
            msg.parseQuery('?' + responseString);
          } else if (contentType.startsWith("application/json")) {
            // Google does this
            final JSONObject responseJson = new JSONObject(responseString);
            msg.parseJSON(responseJson.toString());
          } else {
            if (isLogging) {
              TokenAuthorizationResponseHandler.LOG.log("Unhandled Content-Type {0}", contentType);
              TokenAuthorizationResponseHandler.LOG.exiting(
                      TokenAuthorizationResponseHandler.LOG_CLASS, "handleResponse", null);
            }
            ret = TokenAuthorizationResponseHandler.getError("Unhandled Content-Type "
                    + contentType);
          }

          final OAuth2Error error = msg.getError();
          if (error != null) {
            ret = getError("error parsing request", null, msg.getErrorUri(),
                    msg.getErrorDescription());
          } else if (error == null && accessor != null) {
            final String accessToken = msg.getAccessToken();
            final String refreshToken = msg.getRefreshToken();
            final String expiresIn = msg.getExpiresIn();
            final String tokenType = msg.getTokenType();
            final String providerName = accessor.getServiceName();
            final String gadgetUri = accessor.getGadgetUri();
            final String scope = accessor.getScope();
            final String user = accessor.getUser();
            final String macAlgorithm = msg.getMacAlgorithm();
            final String macSecret = msg.getMacSecret();
            final Map<String, String> unparsedProperties = msg.getUnparsedProperties();

            if (accessToken != null) {
              final OAuth2Token storedAccessToken = this.store.createToken();
              storedAccessToken.setIssuedAt(issuedAt);
              if (expiresIn != null) {
View Full Code Here

  protected void doGet(final HttpServletRequest request, final HttpServletResponse resp)
          throws IOException {

    OAuth2Accessor accessor = null;
    try {
      final OAuth2Message msg = this.oauth2MessageProvider.get();
      msg.parseRequest(request);
      final OAuth2Error error = msg.getError();
      final String encRequestStateKey = msg.getState();
      if (encRequestStateKey == null) {
        if (error != null) {
          OAuth2CallbackServlet.sendError(error, "encRequestStateKey is null", msg.getErrorUri(),
                  msg.getErrorDescription(), null, resp, null, this.sendTraceToClient);
        } else {
          OAuth2CallbackServlet.sendError(OAuth2Error.CALLBACK_PROBLEM,
                  "OAuth2CallbackServlet requestStateKey is null.", "", "", null, resp, null,
                  this.sendTraceToClient);
        }
        return;
      }

      final OAuth2CallbackState state = new OAuth2CallbackState(this.stateCrypter,
              encRequestStateKey);

      accessor = this.store.getOAuth2Accessor(state);

      if (error != null) {
        OAuth2CallbackServlet.sendError(error, "error parsing request", msg.getErrorDescription(),
                msg.getErrorUri(), accessor, resp, null, this.sendTraceToClient);
        return;
      }

      if (accessor == null || !accessor.isValid() || accessor.isErrorResponse()) {
        String message;
View Full Code Here

    Assert.assertNotNull(this.cache.getClient(MockUtils.CLIENT_INDEX2));
  }

  @Test
  public void testStoreOAuth2Accessor_1() throws Exception {
    final OAuth2Store store = MockUtils.getDummyStore(this.cache, null, null);
    OAuth2Accessor accessor = new BasicOAuth2Accessor("XXX", "YYY", "ZZZ", "", false, store, "AAA");

    final Integer result = this.cache.storeOAuth2Accessor(accessor);

    Assert.assertEquals(-1664180105, result.intValue());
View Full Code Here

    final OAuth2HandlerError result = TokenAuthorizationResponseHandlerTest.tarh.handleResponse(
        accessor, response);

    Assert.assertNull(result);

    final OAuth2Token accessToken = TokenAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.ACCESS);
    Assert.assertNotNull(accessToken);
    Assert.assertEquals("xxx", new String(accessToken.getSecret(), "UTF-8"));
    Assert.assertEquals(OAuth2Message.BEARER_TOKEN_TYPE, accessToken.getTokenType());
    Assert.assertTrue(accessToken.getExpiresAt() > 1000);

    final OAuth2Token refreshToken = TokenAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.REFRESH);
    Assert.assertNotNull(refreshToken);
    Assert.assertEquals("yyy", new String(refreshToken.getSecret(), "UTF-8"));
  }
View Full Code Here

    final OAuth2HandlerError result = TokenAuthorizationResponseHandlerTest.tarh.handleResponse(
        accessor, response);

    Assert.assertNull(result);

    final OAuth2Token accessToken = TokenAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.ACCESS);
    Assert.assertNotNull(accessToken);
    Assert.assertEquals("xxx", new String(accessToken.getSecret(), "UTF-8"));
    Assert.assertEquals(OAuth2Message.BEARER_TOKEN_TYPE, accessToken.getTokenType());
    Assert.assertTrue(accessToken.getExpiresAt() > 1000);

    final OAuth2Token refreshToken = TokenAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.REFRESH);
    Assert.assertNotNull(refreshToken);
    Assert.assertEquals("yyy", new String(refreshToken.getSecret(), "UTF-8"));
  }
View Full Code Here

    final OAuth2HandlerError result = fixture.handleRequest(accessor, request);

    Assert.assertNull(result);

    final OAuth2Token accessToken = CodeAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.ACCESS);
    Assert.assertNotNull(accessToken);
    Assert.assertEquals("xxx", new String(accessToken.getSecret(), "UTF-8"));
    Assert.assertEquals(OAuth2Message.BEARER_TOKEN_TYPE, accessToken.getTokenType());
    Assert.assertTrue(accessToken.getExpiresAt() > 1000);

    final OAuth2Token refreshToken = CodeAuthorizationResponseHandlerTest.store.getToken(
        accessor.getGadgetUri(), accessor.getServiceName(), accessor.getUser(),
        accessor.getScope(), OAuth2Token.Type.REFRESH);
    Assert.assertNotNull(refreshToken);
    Assert.assertEquals("yyy", new String(refreshToken.getSecret(), "UTF-8"));
  }
View Full Code Here

    Assert.assertEquals(MockUtils.ACCESSOR_INDEX1, result);
  }

  @Test
  public void testGetToken_1() throws Exception {
    final OAuth2Token result = this.cache.getToken(MockUtils.ACCESS_TOKEN_INDEX);

    Assert.assertNotNull(result);
    Assert.assertEquals(MockUtils.ACCESS_SECRET, new String(result.getSecret(), "UTF-8"));
  }
View Full Code Here

TOP

Related Classes of org.apache.shindig.gadgets.oauth2.OAuth2Message

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.