Created
March 29, 2018 14:09
-
-
Save swinton/da5a16b0295e1b6ba04d83855ce2c398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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