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

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

The newest version!
package com.clouway.oauth2.exampleapp.storage;

import com.clouway.oauth2.client.Client;
import com.clouway.oauth2.client.ClientFinder;
import com.clouway.oauth2.client.JwtKeyStore;
import com.clouway.oauth2.client.ClientRegistrationRequest;
import com.clouway.oauth2.exampleapp.ClientRegistry;
import com.clouway.oauth2.jws.Pem;
import com.clouway.oauth2.jws.Pem.Block;
import com.clouway.oauth2.jwt.Jwt.ClaimSet;
import com.clouway.oauth2.jwt.Jwt.Header;
import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import com.google.inject.Inject;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @author Ivan Stefanov 
 */
class InMemoryClientRepository implements ClientRegistry, ClientFinder, JwtKeyStore {
  private final Map clients = Maps.newHashMap();
  private final Map serviceAccountKeys = Maps.newHashMap();
  private final Pem pem = new Pem();

  @Inject
  public InMemoryClientRepository() {
  }

  @Override
  public Client register(Client client) {
    clients.put(client.id, client);
    return client;
  }

  @Override
  public Client register(ClientRegistrationRequest request) {
    String randomId = UUID.randomUUID().toString();
    Client client =
            new Client(randomId, request.secret, request.description, request.redirectURLs, false);
    clients.put(client.id, client);
    return client;
  }

  @Override
  public Optional findClient(String clientId) {
    return Optional.fromNullable(clients.get(clientId));
  }

  @Override
  public Optional findKey(Header header, ClaimSet claimSet) {
    return Optional.fromNullable(serviceAccountKeys.get(claimSet.iss));
  }

  public void registerServiceAccount(String clientEmail, String privateKeyAsPem) {
    try {
      serviceAccountKeys.put(clientEmail, pem.parse(new ByteArrayInputStream(privateKeyAsPem.getBytes())));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy