com.clouway.oauth2.PublicCertsController Maven / Gradle / Ivy
package com.clouway.oauth2;
import com.clouway.friendlyserve.Request;
import com.clouway.friendlyserve.Response;
import com.clouway.friendlyserve.RsJson;
import com.clouway.friendlyserve.Take;
import com.clouway.oauth2.client.IdentityKeyPair;
import com.clouway.oauth2.jws.Pem;
import com.clouway.oauth2.jws.Pem.Block;
import com.google.gson.JsonObject;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/**
* @author Ianislav Nachev
*/
public class PublicCertsController implements Take {
private final KeyStore keyStore;
public PublicCertsController(KeyStore keyStore) {
this.keyStore = keyStore;
}
@Override
public Response ack(Request request) throws IOException {
List keyMap = keyStore.getKeys();
JsonObject o = new JsonObject();
if (keyMap.isEmpty()) {
return new RsJson(o);
}
Pem pem = new Pem();
for (IdentityKeyPair each : keyMap) {
String certificateAsText = pem.format(
new Block("CERTIFICATE", Collections.emptyMap(), each.publicKey.getEncoded())
);
o.addProperty(each.keyId, certificateAsText);
}
return new RsJson(o);
}
}