Search in sources :

Example 1 with JSONWebKeys

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)" : ""));
    }
}
Also used : JSONWebKey(edu.uiuc.ncsa.security.util.jwk.JSONWebKey) JSONWebKeys(edu.uiuc.ncsa.security.util.jwk.JSONWebKeys) File(java.io.File)

Example 2 with JSONWebKeys

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);
}
Also used : JSONObject(net.sf.json.JSONObject) JSONWebKeys(edu.uiuc.ncsa.security.util.jwk.JSONWebKeys) PrintWriter(java.io.PrintWriter)

Example 3 with JSONWebKeys

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;
}
Also used : GeneralException(edu.uiuc.ncsa.security.core.exceptions.GeneralException) ConfigurationNode(org.apache.commons.configuration.tree.ConfigurationNode) JSONWebKeys(edu.uiuc.ncsa.security.util.jwk.JSONWebKeys) File(java.io.File)

Example 4 with JSONWebKeys

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));
    }
}
Also used : JSONWebKeys(edu.uiuc.ncsa.security.util.jwk.JSONWebKeys) File(java.io.File)

Example 5 with JSONWebKeys

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);
}
Also used : JSONObject(net.sf.json.JSONObject) JSONWebKeys(edu.uiuc.ncsa.security.util.jwk.JSONWebKeys) File(java.io.File)

Aggregations

JSONWebKeys (edu.uiuc.ncsa.security.util.jwk.JSONWebKeys)6 File (java.io.File)5 JSONObject (net.sf.json.JSONObject)3 GeneralException (edu.uiuc.ncsa.security.core.exceptions.GeneralException)2 JSONWebKey (edu.uiuc.ncsa.security.util.jwk.JSONWebKey)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 ConfigurationNode (org.apache.commons.configuration.tree.ConfigurationNode)1