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

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

package com.clouway.oauth2;

import com.clouway.friendlyserve.Request;
import com.clouway.friendlyserve.Response;
import com.clouway.friendlyserve.RsEmpty;
import com.clouway.oauth2.client.Client;
import com.clouway.oauth2.client.ClientFinder;
import com.clouway.oauth2.token.BearerToken;
import com.clouway.oauth2.token.Tokens;
import com.google.common.base.Optional;

/**
 * @author Miroslav Genov ([email protected])
 */
class RevokeTokenController implements ClientRequest {

  private final ClientFinder clientFinder;
  private final Tokens tokens;

  RevokeTokenController(ClientFinder clientFinder, Tokens tokens) {
    this.clientFinder = clientFinder;
    this.tokens = tokens;
  }

  @Override
  public Response handleAsOf(Request request, ClientCredentials credentials, DateTime instant) {
    String token = request.param("token");

    Optional possibleToken = tokens.findTokenAvailableAt(token, instant);
    if (!possibleToken.isPresent()) {
      return OAuthError.invalidRequest();
    }

    Optional possibleClient = clientFinder.findClient(possibleToken.get().clientId);
    if (!possibleClient.isPresent()) {
      return OAuthError.invalidClient();
    }

    Client client = possibleClient.get();

    if (!client.credentialsMatch(credentials)) {
      return OAuthError.invalidClient();
    }

    tokens.revokeToken(token);

    return new RsEmpty();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy