Skip to content

Instantly share code, notes, and snippets.

@swinton
Created March 29, 2018 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swinton/da5a16b0295e1b6ba04d83855ce2c398 to your computer and use it in GitHub Desktop.
Save swinton/da5a16b0295e1b6ba04d83855ce2c398 to your computer and use it in GitHub Desktop.
/**
* See: https://support.insomnia.rest/article/26-plugins#template-tags
*/
const fs = require('fs')
const jwt = require('jsonwebtoken')
//
const createApp = function ({id, cert}) {
return function () {
const payload = {
iat: Math.floor(new Date() / 1000), // Issued at time
exp: Math.floor(new Date() / 1000) + 60, // JWT expiration time
iss: id // GitHub App ID
}
// Sign with RSA SHA256
return jwt.sign(payload, cert, {algorithm: 'RS256'})
}
}
// module.exports.requestHooks = [
// context => {
// context.request.setHeader('Authorization', `bearer ${app()}`);
// context.request.setHeader('Accept', 'application/vnd.github.machine-man-preview+json');
// }
// ];
module.exports.templateTags = [{
name: 'jwt',
displayName: 'JSON Web Token',
description: 'https://developer.github.com/apps/building-github-apps/authentication-options-for-github-apps/#authenticating-as-a-github-app',
args: [
{
displayName: 'ID',
description: 'ID of GitHub App',
type: 'number'
},
{
displayName: 'Private key file',
description: 'Path to private key file',
type: 'string'
}
],
async run (context, id, path) {
const cert = fs.readFileSync(path)
const app = createApp({
id: id,
cert: cert
})
return app()
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment