tests/lib.js
/**
 * Dynamically registers a backend service with the given options. Then it
 * immediately authorizes with that client and returns a promise that gets
 * resolved with the access token response.
 * @param {Object} options
 * @param {import("jsonwebtoken").Algorithm} [options.alg]
 * @param {any} [options.err]
 * @param {any} [options.dur]
 * @param {string} [options.scope = "system/*.*"]
 * @returns {Promise<Object>}
 */
Dynamically registers a backend service with the given options. Then it
immediately authorizes with that client and returns a promise that gets
resolved with the access token response.
Dynamically registers a backend service with the given options . Then it immediately authorizes with that client and returns a promise that gets resolved with the access token response .
function authorize(options = {}) {
    let state = {};
    const tokenUrl = buildUrl(["auth", "token"]);
    const alg      = options.alg || "RS384"
    return requestPromise({
        url : buildUrl(["generator", "jwks"]),
        qs  : { alg },
        json: true
    })
    .then(res => state.jwks = res.body)
    .then(() => state.keys = findKeyPair(state.jwks.keys))
    .then(() => {
        let form = { jwks: JSON.stringify(state.jwks) };
        if (options.err) form.err = options.err;
        if (options.dur) form.dur = options.dur;
        return requestPromise({
            method: "POST",
            url   : buildUrl(["auth", "register"]),
            json  : true,
            form
        });
    })
    .then(res => state.clientId = res.body)
    .then(() => {
        let jwtToken = {
            iss: state.clientId,
            sub: state.clientId,
            aud: tokenUrl,
            exp: Date.now()/1000 + 300, 
            jti: crypto.randomBytes(32).toString("hex")
        };
        let privateKey = jwkToPem(state.keys.privateKey, { private: true });
        let signed = jwt.sign(jwtToken, privateKey, {
            algorithm: alg,
            keyid: state.keys.privateKey.kid,
            header: {
                kty: state.keys.privateKey.kty
            }
        });
        return requestPromise({
            method: "POST",
            uri   : tokenUrl,
            json  : true,
            form  : {
                scope: "scope" in options ? options.scope : "system/*.*",
                grant_type: "client_credentials",
                client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
                client_assertion: signed
            }
        });
    })
    .then(res => {return res.body})
    .catch(result => {
        return Promise.reject(result.outcome || result.error || result)
    });
}
function authorize ( options = { } ) { let state = { } ; const tokenUrl = buildUrl ( [ "auth" , "token" ] ) ; const alg = options . alg || "RS384" return requestPromise ( { url : buildUrl ( [ "generator" , "jwks" ] ) , qs : { alg } , json : true } ) . then ( res => state . jwks = res . body ) . then ( ( ) => state . keys = findKeyPair ( state . jwks . keys ) ) . then ( ( ) => { let form = { jwks : JSON . stringify ( state . jwks ) } ; if ( options . err ) form . err = options . err ; if ( options . dur ) form . dur = options . dur ; return requestPromise ( { method : "POST" , url : buildUrl ( [ "auth" , "register" ] ) , json : true , form } ) ; } ) . then ( res => state . clientId = res . body ) . then ( ( ) => { let jwtToken = { iss : state . clientId , sub : state . clientId , aud : tokenUrl , exp : Date . now ( ) / 1000 + 300 , jti : crypto . randomBytes ( 32 ) . toString ( "hex" ) } ; let privateKey = jwkToPem ( state . keys . privateKey , { private : true } ) ; let signed = jwt . sign ( jwtToken , privateKey , { algorithm : alg , keyid : state . keys . privateKey . kid , header : { kty : state . keys . privateKey . kty } } ) ; return requestPromise ( { method : "POST" , uri : tokenUrl , json : true , form : { scope : "scope" in options ? options . scope : "system/*.*" , grant_type : "client_credentials" , client_assertion_type : "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" , client_assertion : signed } } ) ; } ) . then ( res => { return res . body } ) . catch ( result => { return Promise . reject ( result . outcome || result . error || result ) } ) ; }
Dynamically registers a backend service with the given options.
Dynamically registers a backend service with the given options .
// Save the JWKS to the state object // Save the keys to the state object // console.log(form) // 5 min // Convert the private JWK to PEM private key to sign with // Sign the jwt with our private key // console.log(result.response.body)