Last active
May 13, 2017 17:35
-
-
Save jcready/6dff3db44fe9e3465d39c2d4f5295478 to your computer and use it in GitHub Desktop.
Node.js example of GitHub Integration auth process
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
// With async/await support | |
const fs = require('fs') | |
const jwt = require('jwt-simple') | |
const pem_file = '/path/to/github.pem' // the absolute path to your Application Pem Certificate issued by GitHub | |
const integration_id = 0 // GitHub Application Integration ID | |
const installation_id = 0 // once installed on an organization. The Organization Integration ID | |
const expire_seconds = 60 // number of seconds the jwt token expires (max ~600 but not designated by GitHub) | |
const slug = 'owner/repo' // name of repo for demo purposes | |
const privateKey = fs.readFileSync(pem_file) | |
(async function get_repo_details () { | |
// Step 1) Create an integrations access token | |
const now = Math.round(Date.now() / 1000) | |
const payload = { iat: now, exp: now + expire_seconds, iss: integration_id } | |
const token = jwt.encode(payload, privateKey, 'RS256') | |
const headers = new Headers({ Accept: 'application/vnd.github.machine-man-preview+json', Authorization: `Bearer ${token}` }) | |
const res = await fetch(`https://api.github.com/installations/${id}/access_tokens`, { method: 'POST', headers }) | |
const result = await res.json() | |
console.log(res.status, result) | |
// Step 2) use token to interact with github api | |
headers.set('Authorization', `Bearer ${result.token}`) | |
const repo = await fetch(`https://api.github.com/repos/${slug}`, { headers }) | |
console.log(repo.status, await repo.json()) | |
})() |
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
// Without async/await support | |
const fs = require('fs') | |
const jwt = require('jwt-simple') | |
const pem_file = '/path/to/github.pem' // the absolute path to your Application Pem Certificate issued by GitHub | |
const integration_id = 0 // GitHub Application Integration ID | |
const installation_id = 0 // once installed on an organization. The Organization Integration ID | |
const expire_seconds = 60 // number of seconds the jwt token expires (max ~600 but not designated by GitHub) | |
const slug = 'owner/repo' // name of repo for demo purposes | |
const privateKey = fs.readFileSync(pem_file) | |
// Step 1) Create an integrations access token | |
get_access_token(installation_id).then((access_token) => | |
// Step 2) use token to interact with github api | |
fetch(`https://api.github.com/repos/${slug}`, { | |
headers: new Headers({ | |
Accept: 'application/vnd.github.machine-man-preview+json', | |
Authorization: `Bearer ${access_token}` | |
}) | |
}).then((res) => res.json()) | |
).then(console.log) | |
function get_access_token (id) { | |
return fetch(`https://api.github.com/installations/${id}/access_tokens`, { | |
method: 'POST', | |
headers: new Headers({ | |
Accept: 'application/vnd.github.machine-man-preview+json', | |
Authorization: `Bearer ${integration_token()}` | |
}) | |
}).then((res) => res.json()) | |
.then((json) => json.token) | |
} | |
function integration_token () { | |
const now = Math.round(Date.now() / 1000) | |
return jwt.encode({ | |
iat: now, | |
exp: now + expire_seconds, | |
iss: integration_id | |
}, privateKey, 'RS256') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment