@atomist/sdm
Version:
Atomist Software Delivery Machine SDK
56 lines • 2 kB
JavaScript
;
/*
* Copyright © 2020 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.JwtGoalSigningAlgorithm = void 0;
const crypto = require("crypto");
const _ = require("lodash");
/**
* JWT based GoalSigningAlgorithm
*
* Generate keys:
*
* openssl ecparam -genkey -name secp521r1 -noout -out es512-private.pem
* openssl ec -in es512-private.pem -pubout -out es512-public.pem
*/
exports.JwtGoalSigningAlgorithm = {
name: "jwt-es512",
sign: async (goal, key) => {
const privateKey = crypto.createPrivateKey({
key: key.privateKey,
passphrase: key.passphrase,
});
const { default: CompactSign } = require("jose/jws/compact/sign");
const jws = await new CompactSign(Buffer.from(JSON.stringify(goal)))
.setProtectedHeader({ alg: "ES512" })
.sign(privateKey);
return jws;
},
verify: async (goal, signatureString, key) => {
const publicKey = crypto.createPublicKey({
key: key.publicKey,
});
const { default: compactVerify } = require("jose/jws/compact/verify");
try {
const { payload } = await compactVerify(signatureString, publicKey);
return _.merge({}, goal, JSON.parse(Buffer.from(payload).toString()));
}
catch (e) {
return undefined;
}
},
};
//# sourceMappingURL=jwtGoalSigning.js.map