Skip to content

Instantly share code, notes, and snippets.

@Ridermansb
Created October 3, 2017 11:38
Show Gist options
  • Save Ridermansb/1b4ebc5ca9b141213fb7c24c4b76a9da to your computer and use it in GitHub Desktop.
Save Ridermansb/1b4ebc5ca9b141213fb7c24c4b76a9da to your computer and use it in GitHub Desktop.
WebTask Github Proxy
/* eslint-disable import/no-extraneous-dependencies */
/**
* Make request to Github API to get all repositories
* Headers: { Authorization: Bearer <AUTH0 USER API TOKEN> }
*
* On WebTask
* Secrets required:
* 1. auth0_domain - Auth0 Client Domain
* 2. client_id
* 3. client_secret
* 4. auth0_audience - Auth0 Client Audience
* 5. gh_client_id - Github App Client ID
* 6. gh_client_secret - Github App Client Secret
*
* @type {request}
*/
const request = require('request');
const ManagementClient = require('auth0').ManagementClient;
const GITHUB_URL = 'https://api.github.com';
function Management(secrets) {
return new ManagementClient({
domain: secrets.auth0_domain,
clientId: secrets.client_id,
clientSecret: secrets.client_secret,
audience: secrets.auth0_audience,
scope: 'read:users read:user_idp_tokens',
});
}
module.exports = function (context, cb) {
function getGithubRepositories(githubProvider) {
request({
method: 'GET',
url: `${GITHUB_URL}/user/repos?client_id=${context.secrets.gh_client_id}&client_secret=${context.secrets.gh_client_secret}`,
headers: {
Authorization: `token ${githubProvider.access_token}`,
Accept: 'application/vnd.github.mercy-preview+json',
'User-Agent': 'WebTask/0.0.1',
},
json: true,
}
, (error, response, body) => {
if (error) throw new Error(error);
cb(null, body);
});
}
function getUserIdpTokens(error, user) {
if (error) throw new Error(error);
const githubIdp = user[0].identities.filter(it => it.provider === 'github');
if (githubIdp.length > 0) {
return getGithubRepositories(githubIdp[0]);
}
return cb('Not found');
}
const managementClient = new Management(context.secrets);
managementClient.getUser({ access_token: context.token }, getUserIdpTokens);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment