use of edu.uiuc.ncsa.security.util.jwk.JSONWebKeys in project OA4MP by ncsa.
the class SciTokensCommands method list_key_ids.
public void list_key_ids(InputLine inputLine) throws Exception {
if (showHelp(inputLine)) {
printListKeyIDs();
return;
}
JSONWebKeys jsonWebKeys = null;
if (1 < inputLine.size()) {
jsonWebKeys = JSONWebKeyUtil.fromJSON(new File(inputLine.getArg(1)));
} else {
if (keys == null) {
if (getBooleanInput("Do you want to enter a file name?")) {
String x = getInput("Enter path and name of the key file");
jsonWebKeys = JSONWebKeyUtil.fromJSON(new File(x));
} else {
return;
}
} else {
jsonWebKeys = keys;
}
}
String defaultWebKey = null;
if (jsonWebKeys.hasDefaultKey()) {
defaultWebKey = jsonWebKeys.getDefaultKeyID();
} else {
defaultWebKey = defaultKeyID;
}
for (String keyID : jsonWebKeys.keySet()) {
JSONWebKey webKey = jsonWebKeys.get(keyID);
boolean isDefault = webKey.id.equals(defaultWebKey);
say("id=" + keyID + ", alg=" + webKey.algorithm + ", type=" + webKey.type + ", use=" + webKey.use + (isDefault ? " (default)" : ""));
}
}
use of edu.uiuc.ncsa.security.util.jwk.JSONWebKeys in project OA4MP by ncsa.
the class OA2DiscoveryServlet method doIt.
@Override
protected void doIt(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Throwable {
String requestUri = httpServletRequest.getRequestURI();
// normalize the uri
if (requestUri.endsWith("/")) {
requestUri = requestUri.substring(0, requestUri.length() - 1);
}
if (requestUri.endsWith("/certs")) {
JSONWebKeys publicKeys = JSONWebKeyUtil.makePublic(((OA2SE) getServiceEnvironment()).getJsonWebKeys());
JSONObject json = JSONWebKeyUtil.toJSON(publicKeys);
String out = JSONUtils.valueToString(json, 1, 0);
httpServletResponse.setHeader("Content-Type", "application/json;charset=UTF-8");
PrintWriter printWriter = httpServletResponse.getWriter();
printWriter.write(out);
printWriter.flush();
printWriter.close();
return;
}
super.doIt(httpServletRequest, httpServletResponse);
}
use of edu.uiuc.ncsa.security.util.jwk.JSONWebKeys in project OA4MP by ncsa.
the class OA2ConfigurationLoader method getJSONWebKeys.
protected JSONWebKeys getJSONWebKeys() {
ConfigurationNode node = getFirstNode(cn, "JSONWebKey");
if (node == null) {
warn("Error: No signing keys in the configuration file. Signing is not available");
// throw new IllegalStateException();
return new JSONWebKeys(null);
}
// if the whole thing is included
String json = getNodeValue(node, "json", null);
JSONWebKeys keys = null;
try {
if (json != null) {
keys = JSONWebKeyUtil.fromJSON(json);
}
// points to a file that contains it all
String path = getNodeValue(node, "path", null);
if (path != null) {
keys = JSONWebKeyUtil.fromJSON(new File(path));
}
} catch (Throwable t) {
throw new GeneralException("Error reading signing keys", t);
}
if (keys == null) {
throw new IllegalStateException("Error: Could not load signing keys");
}
keys.setDefaultKeyID(getFirstAttribute(node, "defaultKeyID"));
return keys;
}
use of edu.uiuc.ncsa.security.util.jwk.JSONWebKeys in project OA4MP by ncsa.
the class SciTokensCommands method list_keys.
public void list_keys(InputLine inputLine) throws Exception {
if (showHelp(inputLine)) {
listKeysHelp();
return;
}
JSONWebKeys localKeys = null;
if (1 == inputLine.size()) {
// try to use the defined keys
if (keys == null || keys.isEmpty()) {
say("Sorry, there are no keys specified. Either use setkeys or specify a key file.");
return;
}
localKeys = keys;
} else {
File publicKeyFile = new File(inputLine.getArg(1));
localKeys = readKeys(publicKeyFile);
}
boolean hasDefault = localKeys.hasDefaultKey();
String defaultKey = null;
if (hasDefault) {
defaultKey = localKeys.getDefaultKeyID();
}
for (String key : localKeys.keySet()) {
if (hasDefault) {
if (key.equals(defaultKey)) {
say("key id=" + key + " (default)");
} else {
say("key id=" + key);
}
} else {
say("key id=" + key);
}
say(KeyUtil.toX509PEM(localKeys.get(key).publicKey));
}
}
use of edu.uiuc.ncsa.security.util.jwk.JSONWebKeys in project OA4MP by ncsa.
the class SciTokensCommands method create_token.
public void create_token(InputLine inputLine) throws Exception {
if (showHelp(inputLine)) {
createTokenHelp();
return;
}
// pull off the command line arguments
JSONWebKeys localKeys = null;
if (inputLine.hasArg("-keys")) {
String fileName = getArgValue(inputLine, "-keys");
File f = new File(fileName);
if (!f.exists()) {
say("Sorry, that file does not seem to exist");
return;
}
if (!f.isFile()) {
say("Sorry, the thing yo specified is not a file.");
return;
}
localKeys = readKeys(f);
} else {
if (keys == null || keys.isEmpty()) {
if (getBooleanInput("No keys set. Would you like to specify keys for signing?")) {
String x = getInput("Enter fully qualified path and file name");
if (isEmpty(x)) {
say("no file entered, exiting...");
return;
}
localKeys = readKeys(new File(x));
}
} else {
localKeys = keys;
}
}
String localDefaultID = null;
if (inputLine.hasArg("-id")) {
localDefaultID = getArgValue(inputLine, "-id");
} else {
if (defaultKeyID != null) {
localDefaultID = defaultKeyID;
} else {
if (getBooleanInput("No key id found. Do you want to enter one?")) {
localDefaultID = getInput("Enter key id:");
} else {
return;
}
}
}
JSONObject claims = null;
if (inputLine.hasArg("-file")) {
claims = JSONObject.fromObject(readFile(getArgValue(inputLine, "-file")));
} else {
String x = getInput("Enter the name of the file containing the JSON object to use:");
if (isEmpty(x)) {
say("No argument, exiting...");
return;
}
claims = JSONObject.fromObject(readFile(x));
}
String signedToken = JWTUtil.createJWT(claims, localKeys.get(localDefaultID));
lastToken = signedToken;
say(signedToken);
}
Aggregations