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

com.clouway.oauth2.exampleapp.storage.InMemoryResourceOwnerRepository Maven / Gradle / Ivy

package com.clouway.oauth2.exampleapp.storage;

import com.clouway.oauth2.ResourceOwner;
import com.clouway.oauth2.exampleapp.ResourceOwnerAuthentication;
import com.clouway.oauth2.ResourceOwnerStore;
import com.clouway.oauth2.Session;
import com.clouway.oauth2.exampleapp.SessionSecurity;
import com.clouway.oauth2.token.TokenGenerator;
import com.google.common.base.Optional;
import com.google.inject.Inject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Ivan Stefanov 
 */
class InMemoryResourceOwnerRepository implements ResourceOwnerStore, ResourceOwnerAuthentication, SessionSecurity {
  private Map resourceOwners = new HashMap() {{
    put("admin", new ResourceOwner("admin", "admin"));
  }};

  private Set sessions = new HashSet();

  private TokenGenerator tokenGenerator;

  @Inject
  InMemoryResourceOwnerRepository(TokenGenerator tokenGenerator) {
    this.tokenGenerator = tokenGenerator;
  }

  @Override
  public void save(ResourceOwner resourceOwner) {
    resourceOwners.put(resourceOwner.username, resourceOwner);
  }

  @Override
  public Optional auth(String username, String password, String remoteAddress) {
    if (resourceOwners.containsKey(username)) {
      if (password.equals(resourceOwners.get(username).password)) {
        Session session = new Session(tokenGenerator.generate());

        sessions.add(session);

        return Optional.of(session);
      }
    }

    return Optional.absent();
  }

  @Override
  public Boolean exists(Session session) {
    return sessions.contains(session);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy