All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clouway.oauth2.AuthCodeAuthorization Maven / Gradle / Ivy

package com.clouway.oauth2;

import com.clouway.friendlyserve.Request;
import com.clouway.friendlyserve.Response;
import com.clouway.oauth2.authorization.Authorization;
import com.clouway.oauth2.authorization.ClientAuthorizationRepository;
import com.clouway.oauth2.client.Client;
import com.clouway.oauth2.token.GrantType;
import com.clouway.oauth2.user.IdentityFinder;
import com.clouway.oauth2.util.Params;
import com.google.common.base.Optional;

import java.util.Map;

/**
 * @author Miroslav Genov ([email protected])
 */
class AuthCodeAuthorization implements ClientActivity {

  private final ClientAuthorizationRepository clientAuthorizationRepository;
  private final IdentityFinder identityFinder;
  private final AuthorizedClientActivity authorizedClientActivity;

  AuthCodeAuthorization(ClientAuthorizationRepository clientAuthorizationRepository, IdentityFinder identityFinder, AuthorizedClientActivity authorizedClientActivity) {
    this.clientAuthorizationRepository = clientAuthorizationRepository;
    this.identityFinder = identityFinder;
    this.authorizedClientActivity = authorizedClientActivity;
  }

  @Override
  public Response execute(Client client, Request request, DateTime instant) {
    String authCode = request.param("code");

    Optional possibleAuthorization = clientAuthorizationRepository.findAuthorization(client, authCode, instant);

    if (!possibleAuthorization.isPresent()) {
      return OAuthError.invalidGrant();
    }

    Authorization authorization = possibleAuthorization.get();

    Map params  = new Params().parse(request, "code");

    Optional possibleIdentity = identityFinder.findIdentity(authorization.identityId, GrantType.AUTHORIZATION_CODE, instant, params);
    if (!possibleIdentity.isPresent()) {
      return OAuthError.invalidGrant("identity was not found");
    }

    Identity identity = possibleIdentity.get();
    return authorizedClientActivity.execute(client, identity, authorization.scopes, request, instant, params);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy