@POST
  @Path("authorize")
  @Consumes(MediaType.APPLICATION_JSON)
  public Response authorize(UserCredentials credentials) {
    // Perform all validation here to control the exact error message returned to comply with the Oauth2 standard

    // The client has been authenticated already in the interceptor

    if (!PASSWORD_GRANT_TYPE.equals(credentials.getGrant_type())) {
      // Unsupported grant type
      throw new BadRequestRestException(ImmutableMap.of("error", Oauth2ClientAuthenticationFilter.ERROR_UNSUPPORTED_GRANT_TYPE));
    }

    if (null == credentials.getUsername() ||
        null == credentials.getPassword()) {
      throw new BadRequestRestException(ImmutableMap.of("error", Oauth2ClientAuthenticationFilter.ERROR_INVALID_REQUEST));
    }

    DOAuth2User oauth2User = authenticationProvider.authenticate(credentials.getUsername(), credentials.getPassword());
    if (null != oauth2User) {

      // load connection from db async style (likely case is new token for existing user)
      final Iterable<DConnection> existingConnections = connectionDao.queryByProviderUserId(oauth2User.getId().toString());

      DConnection connection = generateConnection(oauth2User, null, null);

      connectionDao.putWithCacheKey(connection.getAccessToken(), connection);

      // Remove expired connections for the user
      removeExpiredConnections(FactoryResource.PROVIDER_ID_SELF, existingConnections);

      return Response.ok(ImmutableMap.builder()
          .put("access_token", connection.getAccessToken())
          .put("refresh_token", connection.getRefreshToken())
          .put("expires_in", tokenExpiresIn)
          .put("token_type", TOKEN_TYPE_BEARER)
          .build())
          .cookie(createCookie(connection.getAccessToken(), tokenExpiresIn))
          .build();

    } else {
      // authentication failed
      throw new BadRequestRestException(ImmutableMap.of("error", Oauth2ClientAuthenticationFilter.ERROR_INVALID_GRANT));
    }

  }
@ POST @ Path ( "authorize" ) @ Consumes ( MediaType . APPLICATION_JSON ) public Response authorize ( UserCredentials credentials ) { if ( ! PASSWORD_GRANT_TYPE . equals ( credentials . getGrant_type ( ) ) ) { throw new BadRequestRestException ( ImmutableMap . of ( "error" , Oauth2ClientAuthenticationFilter . ERROR_UNSUPPORTED_GRANT_TYPE ) ) ; } if ( null == credentials . getUsername ( ) || null == credentials . getPassword ( ) ) { throw new BadRequestRestException ( ImmutableMap . of ( "error" , Oauth2ClientAuthenticationFilter . ERROR_INVALID_REQUEST ) ) ; } DOAuth2User oauth2User = authenticationProvider . authenticate ( credentials . getUsername ( ) , credentials . getPassword ( ) ) ; if ( null != oauth2User ) { final Iterable < DConnection > existingConnections = connectionDao . queryByProviderUserId ( oauth2User . getId ( ) . toString ( ) ) ; DConnection connection = generateConnection ( oauth2User , null , null ) ; connectionDao . putWithCacheKey ( connection . getAccessToken ( ) , connection ) ; removeExpiredConnections ( FactoryResource . PROVIDER_ID_SELF , existingConnections ) ; return Response . ok ( ImmutableMap . builder ( ) . put ( "access_token" , connection . getAccessToken ( ) ) . put ( "refresh_token" , connection . getRefreshToken ( ) ) . put ( "expires_in" , tokenExpiresIn ) . put ( "token_type" , TOKEN_TYPE_BEARER ) . build ( ) ) . cookie ( createCookie ( connection . getAccessToken ( ) , tokenExpiresIn ) ) . build ( ) ; } else { throw new BadRequestRestException ( ImmutableMap . of ( "error" , Oauth2ClientAuthenticationFilter . ERROR_INVALID_GRANT ) ) ; } }
Authorize a user (basically a sign in)

@param credentials user credentials
@return access_token and refresh token if successful.
Success response http://tools.ietf.org/html/rfc6749#section-5.1
Failure response http://tools.ietf.org/html/rfc6749#section-5.2