Examples of OAuthTokenRequest


Examples of org.apache.oltu.oauth2.as.request.OAuthTokenRequest

    final HttpServletRequest request,
    final HttpServletResponse response)
    throws OAuthSystemException, IOException {
   
    // Attempt to build an OAuth request from the HTTP request.
    OAuthTokenRequest oauthRequest;
    try {
      oauthRequest = new OAuthTokenRequest(request);
      }
    // If the HTTP request was not a valid OAuth token request, then we
    // have no other choice but to reject it as a bad request.
    catch(OAuthProblemException e) {
      // Build the OAuth response.
          OAuthResponse oauthResponse =
            OAuthResponse
                .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
                .error(e)
                .buildJSONMessage();

          // Set the HTTP response status code from the OAuth response.
          response.setStatus(oauthResponse.getResponseStatus());
         
          // Return the error message.
          return oauthResponse.getBody();
      }
   
    // Attempt to get the client.
    ThirdParty thirdParty =
      ThirdPartyBin
        .getInstance().getThirdParty(oauthRequest.getClientId());
    // If the client is unknown, respond as such.
    if(thirdParty == null) {
      // Create the OAuth response.
      OAuthResponse oauthResponse =
        OAuthASResponse
          .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
          .setError(TokenResponse.INVALID_CLIENT)
          .setErrorDescription(
            "The client is unknown: " + oauthRequest.getClientId())
          .buildJSONMessage();
     
      // Set the status and return the error message.
      response.setStatus(oauthResponse.getResponseStatus());
      return oauthResponse.getBody();
    }
   
    // Get the given client secret.
    String thirdPartySecret = oauthRequest.getClientSecret();
    if(thirdPartySecret == null) {
      // Create the OAuth response.
      OAuthResponse oauthResponse =
        OAuthASResponse
          .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
          .setError(TokenResponse.INVALID_CLIENT)
          .setErrorDescription("The client secret is required.")
          .buildJSONMessage();
     
      // Set the status and return the error message.
      response.setStatus(oauthResponse.getResponseStatus());
      return oauthResponse.getBody();
    }
    // Make sure the client gave the right secret.
    else if(! thirdPartySecret.equals(thirdParty.getSecret())) {
      // Create the OAuth response.
      OAuthResponse oauthResponse =
        OAuthASResponse
          .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
          .setError(TokenResponse.INVALID_CLIENT)
          .setErrorDescription("The client secret is incorrect.")
          .buildJSONMessage();
     
      // Set the status and return the error message.
      response.setStatus(oauthResponse.getResponseStatus());
      return oauthResponse.getBody();
    }
   
    // Get the grant-type.
    GrantType grantType;
    String grantTypeString = oauthRequest.getGrantType();
    if(GrantType.AUTHORIZATION_CODE.toString().equals(grantTypeString)) {
      grantType = GrantType.AUTHORIZATION_CODE;
    }
    else if(GrantType.CLIENT_CREDENTIALS.toString().equals(grantTypeString)) {
      grantType = GrantType.CLIENT_CREDENTIALS;
    }
    else if(GrantType.PASSWORD.toString().equals(grantTypeString)) {
      grantType = GrantType.PASSWORD;
    }
    else if(GrantType.REFRESH_TOKEN.toString().equals(grantTypeString)) {
      grantType = GrantType.REFRESH_TOKEN;
    }
    else {
      // Create the OAuth response.
      OAuthResponse oauthResponse =
        OAuthASResponse
          .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
          .setError(TokenResponse.INVALID_GRANT)
          .setErrorDescription(
            "The grant type is unknown: " + grantTypeString)
          .buildJSONMessage();
     
      // Set the status and return the error message.
      response.setStatus(oauthResponse.getResponseStatus());
      return oauthResponse.getBody();
    }
   
    // Handle the different types of token requests.
    AuthorizationToken token;
    if(GrantType.AUTHORIZATION_CODE.equals(grantType)) {
      // Attempt to get the code.
      String codeString = oauthRequest.getCode();
      if(codeString == null) {
        // Create the OAuth response.
        OAuthResponse oauthResponse =
          OAuthASResponse
            .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
            .setError(TokenResponse.INVALID_REQUEST)
            .setErrorDescription(
              "An authorization code must be given to be " +
                "exchanged for an authorization token.")
            .buildJSONMessage();
       
        // Set the status and return the error message.
        response.setStatus(oauthResponse.getResponseStatus());
        return oauthResponse.getBody();
      }
     
      // Attempt to lookup the actual AuthorizationCode object.
      AuthorizationCode code =
        AuthorizationCodeBin.getInstance().getCode(codeString);
      // If the code doesn't exist, reject the request.
      if(code == null) {
        // Create the OAuth response.
        OAuthResponse oauthResponse =
          OAuthASResponse
            .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
            .setError(TokenResponse.INVALID_REQUEST)
            .setErrorDescription(
              "The given authorization code is unknown: " +
                codeString)
            .buildJSONMessage();
       
        // Set the status and return the error message.
        response.setStatus(oauthResponse.getResponseStatus());
        return oauthResponse.getBody();
      }
     
      // Verify that the client asking for a token is the same as the one
      // that requested the code.
      if(! code.getThirdParty().getId().equals(thirdParty.getId())) {
        // Create the OAuth response.
        OAuthResponse oauthResponse =
          OAuthASResponse
            .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
            .setError(TokenResponse.INVALID_REQUEST)
            .setErrorDescription(
              "This client is not allowed to reference this " +
                "code: " +
                codeString)
            .buildJSONMessage();
       
        // Set the status and return the error message.
        response.setStatus(oauthResponse.getResponseStatus());
        return oauthResponse.getBody();
      }

      // If the code has expired, reject the request.
      if(System.currentTimeMillis() > code.getExpirationTime()) {
        // Create the OAuth response.
        OAuthResponse oauthResponse =
          OAuthASResponse
            .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
            .setError(TokenResponse.INVALID_REQUEST)
            .setErrorDescription(
              "The given authorization code has expired: " +
                codeString)
            .buildJSONMessage();
       
        // Set the status and return the error message.
        response.setStatus(oauthResponse.getResponseStatus());
        return oauthResponse.getBody();
      }
     
      // Use the code to lookup the response information and error out if
      // a user has not yet verified it.
      AuthorizationCodeResponse codeResponse =
        AuthorizationCodeResponseBin
          .getInstance().getResponse(code.getCode());
      if(codeResponse == null) {
        // Create the OAuth response.
        OAuthResponse oauthResponse =
          OAuthASResponse
            .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
            .setError(TokenResponse.INVALID_REQUEST)
            .setErrorDescription(
              "A user has not yet verified the code: " +
                codeString)
            .buildJSONMessage();
       
        // Set the status and return the error message.
        response.setStatus(oauthResponse.getResponseStatus());
        return oauthResponse.getBody();
      }
     
      // Determine if the user granted access and, if not, error out.
      if(! codeResponse.getGranted()) {
        // Create the OAuth response.
        OAuthResponse oauthResponse =
          OAuthASResponse
            .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
            .setError(TokenResponse.INVALID_REQUEST)
            .setErrorDescription(
              "The user denied the authorization: " + codeString)
            .buildJSONMessage();
       
        // Set the status and return the error message.
        response.setStatus(oauthResponse.getResponseStatus());
        return oauthResponse.getBody();
      }
     
      // Create a new token.
      token = new AuthorizationToken(codeResponse);
    }
    // Handle a third-party refreshing an existing token.
    else if(GrantType.REFRESH_TOKEN.equals(grantType)) {
      // Get the refresh token from the request.
      String refreshToken = oauthRequest.getRefreshToken();
      if(refreshToken == null) {
        // Create the OAuth response.
        OAuthResponse oauthResponse =
          OAuthASResponse
            .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
View Full Code Here

Examples of org.apache.oltu.oauth2.as.request.OAuthTokenRequest

    @POST
    @Consumes("application/x-www-form-urlencoded")
    @Produces("application/json")
    public Response authorize(@Context HttpServletRequest request) throws OAuthSystemException {

        OAuthTokenRequest oauthRequest = null;

        OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());

        try {
            oauthRequest = new OAuthTokenRequest(request);

            // check if clientid is valid
            if (!Common.CLIENT_ID.equals(oauthRequest.getClientId())) {
                OAuthResponse response =
                    OAuthASResponse.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
                        .setError(OAuthError.TokenResponse.INVALID_CLIENT).setErrorDescription(INVALID_CLIENT_DESCRIPTION)
                        .buildJSONMessage();
                return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
            }

            // check if client_secret is valid
            if (!Common.CLIENT_SECRET.equals(oauthRequest.getClientSecret())) {
                OAuthResponse response =
                    OAuthASResponse.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
                        .setError(OAuthError.TokenResponse.UNAUTHORIZED_CLIENT).setErrorDescription(INVALID_CLIENT_DESCRIPTION)
                        .buildJSONMessage();
                return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
            }

            // do checking for different grant types
            if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE)
                .equals(GrantType.AUTHORIZATION_CODE.toString())) {
                if (!Common.AUTHORIZATION_CODE.equals(oauthRequest.getParam(OAuth.OAUTH_CODE))) {
                    OAuthResponse response = OAuthASResponse
                        .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
                        .setError(OAuthError.TokenResponse.INVALID_GRANT)
                        .setErrorDescription("invalid authorization code")
                        .buildJSONMessage();
                    return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
                }
            } else if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE)
                .equals(GrantType.PASSWORD.toString())) {
                if (!Common.PASSWORD.equals(oauthRequest.getPassword())
                    || !Common.USERNAME.equals(oauthRequest.getUsername())) {
                    OAuthResponse response = OAuthASResponse
                        .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
                        .setError(OAuthError.TokenResponse.INVALID_GRANT)
                        .setErrorDescription("invalid username or password")
                        .buildJSONMessage();
                    return Response.status(response.getResponseStatus()).entity(response.getBody()).build();
                }
            } else if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE)
                .equals(GrantType.REFRESH_TOKEN.toString())) {
                // refresh token is not supported in this implementation
                OAuthResponse response = OAuthASResponse
                    .errorResponse(HttpServletResponse.SC_BAD_REQUEST)
                    .setError(OAuthError.TokenResponse.INVALID_GRANT)
View Full Code Here

Examples of org.apache.oltu.oauth2.as.request.OAuthTokenRequest

        assertInvalidTokenRequest(request);
    }

    private void assertInvalidTokenRequest(HttpServletRequest request) throws OAuthSystemException {
        try {
            new OAuthTokenRequest(request);
            fail("Exception expected");
        } catch (OAuthProblemException e) {
            assertEquals(OAuthError.TokenResponse.INVALID_REQUEST, e.getError());
        }
View Full Code Here

Examples of org.apache.oltu.oauth2.as.request.OAuthTokenRequest

                .expectAccessGrant(ACCESS_GRANT)
                .expectRedirectUri(REDIRECT_URI)
                .build();
        replay(request);

        OAuthTokenRequest req = null;
        try {
            req = new OAuthTokenRequest(request);

        } catch (OAuthProblemException e) {
            fail("Exception not expected");
        }
        assertEquals(GrantType.AUTHORIZATION_CODE.toString(), req.getGrantType());
        assertEquals(CLIENT_ID, req.getClientId());
        assertEquals(REDIRECT_URI, req.getRedirectURI());
        assertEquals(ACCESS_GRANT, req.getCode());

        verify(request);

        request = new OauthMockRequestBuilder()
                .expectGrantType(GrantType.PASSWORD.toString())
                .expectHttpMethod(OAuth.HttpMethod.POST)
                .expectContentType(OAuth.ContentType.URL_ENCODED)
                .expectClientId(CLIENT_ID)
                .expectClientSecret(SECRET)
                .expectBasicAuthHeader(null)
                .expectOauthUsername(USERNAME)
                .expectOauthPassword(PASSWORD)
                .build();
        replay(request);

        try {
            req = new OAuthTokenRequest(request);

        } catch (OAuthProblemException e) {
            fail("Exception not expected");
        }
        assertEquals(CLIENT_ID, req.getClientId());
        assertEquals(USERNAME, req.getUsername());
        assertEquals(PASSWORD, req.getPassword());

        verify(request);

        request = new OauthMockRequestBuilder()
                .expectGrantType(GrantType.CLIENT_CREDENTIALS.toString())
                .expectHttpMethod(OAuth.HttpMethod.POST)
                .expectContentType(OAuth.ContentType.URL_ENCODED)
                .expectBasicAuthHeader(createBasicAuthHeader(CLIENT_ID, SECRET))
                .build();
        replay(request);

        try {
            req = new OAuthTokenRequest(request);

        } catch (OAuthProblemException e) {
            fail("Exception not expected");
        }

        verify(request);

        request = new OauthMockRequestBuilder()
                .expectGrantType(GrantType.REFRESH_TOKEN.toString())
                .expectHttpMethod(OAuth.HttpMethod.POST)
                .expectContentType(OAuth.ContentType.URL_ENCODED)
                .expectClientId(CLIENT_ID)
                .expectClientSecret(SECRET)
                .expectBasicAuthHeader(null)
                .expectOauthRefreshToken(REFRESH_TOKEN)
                .build();
        replay(request);

        try {
            req = new OAuthTokenRequest(request);

        } catch (OAuthProblemException e) {
            fail("Exception not expected");
        }
        assertEquals(CLIENT_ID, req.getClientId());
        assertEquals(REFRESH_TOKEN, req.getRefreshToken());
        assertEquals(SECRET, req.getClientSecret());

        verify(request);

        request = new OauthMockRequestBuilder()
                .expectGrantType(GrantType.REFRESH_TOKEN.toString())
                .expectHttpMethod(OAuth.HttpMethod.POST)
                .expectContentType(OAuth.ContentType.URL_ENCODED)
                .expectClientId("")
                .expectClientSecret("")
                .expectBasicAuthHeader(createBasicAuthHeader(CLIENT_ID, SECRET))
                .expectOauthRefreshToken(REFRESH_TOKEN)
                .build();
        replay(request);

        try {
            req = new OAuthTokenRequest(request);

        } catch (OAuthProblemException e) {
            fail("Exception not expected");
        }
        assertEquals(CLIENT_ID, req.getClientId());
        assertEquals(REFRESH_TOKEN, req.getRefreshToken());
        assertEquals(SECRET, req.getClientSecret());

        verify(request);
    }
View Full Code Here

Examples of org.eurekastreams.server.persistence.mappers.requests.opensocial.OAuthTokenRequest

        SecurityToken securityToken = request.getSecurityToken();
        OAuthConsumer consumer = consumerMapper.execute(new OAuthConsumerRequest(request.getServiceName(),
                securityToken.getAppUrl()));
        if (consumer != null)
        {
            tokenDeleteMapper.execute(new OAuthTokenRequest(consumer, securityToken.getViewerId(), securityToken
                    .getOwnerId()));
        }
        else
        {
            throw new ExecutionException("OAuth Consumer was not found.");
View Full Code Here

Examples of org.eurekastreams.server.persistence.mappers.requests.opensocial.OAuthTokenRequest

    @Test
    public void testFindTokenByServiceNameAndGadgetUrl()
    {
        OAuthConsumer consumer = new OAuthConsumer("provider1", "http://www.example.com/gadget2.xml", "key1",
                "secret1", "HMAC-SHA1");
        OAuthToken token = sut.execute(new OAuthTokenRequest(consumer, "123", "456"));

        assertTrue("No Token found for consumer 101 and viewer 123 and owner 456", token != null);

        // verify loaded attributes of token
        assertEquals("Incorrect access token returned", "accesstoken1", token.getAccessToken());
View Full Code Here

Examples of org.eurekastreams.server.persistence.mappers.requests.opensocial.OAuthTokenRequest

    @Test
    public void testFindNoTokenByServiceNameAndGadgetUrl()
    {
        OAuthConsumer consumer = new OAuthConsumer("provider1", "http://www.example.com/gadget2.xml", "key1",
                "secret1", "HMAC-SHA1");
        OAuthToken token = sut.execute(new OAuthTokenRequest(consumer, "111", "111"));

        assertTrue("Token found for consumer 101 and viewer 111 and owner 111", token == null);
    }
View Full Code Here

Examples of org.eurekastreams.server.persistence.mappers.requests.opensocial.OAuthTokenRequest

    @Test
    public void testExpiredToken()
    {
        OAuthConsumer consumer = new OAuthConsumer("provider4", "http://www.example.com/gadget4.xml", "key4",
                "secret4", "HMAC-SHA1");
        OAuthToken token = sut.execute(new OAuthTokenRequest(consumer, "123", "456"));
        assertTrue("Non-expired Token found for consumer 104 and viewer 123 and owner 456", token == null);
    }
View Full Code Here

Examples of org.eurekastreams.server.persistence.mappers.requests.opensocial.OAuthTokenRequest

    @Test
    public void testDeleteByConsumerAndViewerAndOwner()
    {
        OAuthConsumer consumer = new OAuthConsumer("provider3", "http://www.example.com/gadget4.xml", "key3",
                "secret3", "HMAC-SHA1");
        sut.execute(new OAuthTokenRequest(consumer, "123", "456"));

        Query q = getEntityManager().createQuery(
                "from OAuthToken t where t.consumer.serviceProviderName = :serviceName"
                        + " and t.consumer.gadgetUrl = :gadgetUrl and t.viewerId = :viewerId and t.ownerId = :ownerId")
                .setParameter("serviceName", "provider3").setParameter("gadgetUrl",
View Full Code Here

Examples of org.eurekastreams.server.persistence.mappers.requests.opensocial.OAuthTokenRequest

    @Test
    public void testDeleteNonexistentToken()
    {
        OAuthConsumer consumer = new OAuthConsumer("providerX", "http://www.example.com/gadgetX.xml", "keyX",
                "secretX", "HMAC-SHA1");
        Boolean result = sut.execute(new OAuthTokenRequest(consumer, "X", "Y"));

        Assert.assertEquals(true, result);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.