UNPKG

@bowtie/sls

Version:

Serverless helpers & utilities

104 lines (84 loc) 2.05 kB
const qs = require('qs') const https = require('https') module.exports.authorize = (event, context, callback) => { console.log('AUTHORIZE EVENT', event) const { state, scope } = event.queryStringParameters const { client_id, // client_secret, redirect_uri } = event.service.github const params = { state, scope, client_id, redirect_uri } const response = { statusCode: 302, headers: { Location: 'https://github.com/login/oauth/authorize?' + qs.stringify(params) }, body: null } callback(null, response) // Use this code if you don't use the http event with the LAMBDA-PROXY integration // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event }); } const doRequest = (body, options, callback, redirect_uri) => { const req = https.request(options, (resp) => { resp.setEncoding('utf8') if (resp.statusCode === 200) { resp.on('data', (chunk) => { const response = { statusCode: 302, headers: { Location: redirect_uri + '?' + qs.stringify(JSON.parse(chunk)) }, body: null } callback(null, response) }) } else { callback(new Error('non 200 from gh')) } }) req.on('error', callback) req.write(body) req.end() } module.exports.callback = (event, context, callback) => { const { state, scope } = event.queryStringParameters const { client_id, client_secret, redirect_uri } = event.service.github const data = { state, scope, client_id, client_secret, redirect_uri } const body = JSON.stringify(data) const headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) } const options = { headers: headers, port: 443, method: 'POST', hostname: 'github.com', path: '/login/oauth/access_token/' } doRequest(body, options, callback, redirect_uri) }