From 1085b183296a1a943da40f2c1355be0a9c7d1cd4 Mon Sep 17 00:00:00 2001 From: Manjunath Reddy Date: Sun, 2 Sep 2018 16:50:14 +0800 Subject: [PATCH 1/6] User Github rest client --- package-lock.json | 124 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 125 insertions(+) diff --git a/package-lock.json b/package-lock.json index 8dc383b..918f6f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,41 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@gimenete/type-writer": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/@gimenete/type-writer/-/type-writer-0.1.3.tgz", + "integrity": "sha512-vhpvVfM/fYqb1aAnkgOvtDKoOgU3ZYIvDnKSDAFSoBvallmGURMlHOE0/VG/gqunUZVXGCFBGHxI8swjBh+sIA==", + "requires": { + "camelcase": "^5.0.0", + "prettier": "^1.13.7" + } + }, + "@octokit/rest": { + "version": "15.10.0", + "resolved": "https://registry.npmjs.org/@octokit/rest/-/rest-15.10.0.tgz", + "integrity": "sha512-xZ4ejCZoqvKrIN3tQOKZlJ6nDQxaOdLcjRsamDnbckU7V5YTn2xheIqFXnQ2vLvxqVwyI8+2dfsODYbHxtwtSw==", + "requires": { + "@gimenete/type-writer": "^0.1.3", + "before-after-hook": "^1.1.0", + "btoa-lite": "^1.0.0", + "debug": "^3.1.0", + "http-proxy-agent": "^2.1.0", + "https-proxy-agent": "^2.2.0", + "lodash": "^4.17.4", + "node-fetch": "^2.1.1", + "url-template": "^2.0.8" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "@types/body-parser": { "version": "1.17.0", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.17.0.tgz", @@ -96,6 +131,14 @@ "negotiator": "0.6.1" } }, + "agent-base": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-4.2.1.tgz", + "integrity": "sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg==", + "requires": { + "es6-promisify": "^5.0.0" + } + }, "ajv": { "version": "5.5.2", "resolved": "https://registry.npmjs.org/ajv/-/ajv-5.5.2.tgz", @@ -163,6 +206,11 @@ "tweetnacl": "^0.14.3" } }, + "before-after-hook": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-1.1.0.tgz", + "integrity": "sha512-VOMDtYPwLbIncTxNoSzRyvaMxtXmLWLUqr8k5AfC1BzLk34HvBXaQX8snOwQZ4c0aX8aSERqtJSiI9/m2u5kuA==" + }, "body-parser": { "version": "1.18.3", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.3.tgz", @@ -187,6 +235,11 @@ } } }, + "btoa-lite": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/btoa-lite/-/btoa-lite-1.0.0.tgz", + "integrity": "sha1-M3dm2hWAEhD92VbCLpxokaudAzc=" + }, "buffer-equal-constant-time": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", @@ -197,6 +250,11 @@ "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=" }, + "camelcase": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.0.0.tgz", + "integrity": "sha512-faqwZqnWxbxn+F1d399ygeamQNy3lPp/H9H6rNrqYh4FSVCtcY+3cub1MxA8o9mDd55mM8Aghuu/kuyYA6VTsA==" + }, "caseless": { "version": "0.12.0", "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", @@ -308,6 +366,19 @@ "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" }, + "es6-promise": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.4.tgz", + "integrity": "sha512-/NdNZVJg+uZgtm9eS3O6lrOLYmQag2DjdEXuPaHlZ6RuVqgqaVZfgYCepEIKsLqwdQArOPtC3XzRLqGGfT8KQQ==" + }, + "es6-promisify": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz", + "integrity": "sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=", + "requires": { + "es6-promise": "^4.0.3" + } + }, "escape-html": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", @@ -539,6 +610,25 @@ "statuses": ">= 1.4.0 < 2" } }, + "http-proxy-agent": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-2.1.0.tgz", + "integrity": "sha512-qwHbBLV7WviBl0rQsOzH6o5lwyOIvwp/BdFnvVxXORldu5TmjFfjzBcWUWS5kWAZhmv+JtiDhSuQCp4sBfbIgg==", + "requires": { + "agent-base": "4", + "debug": "3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "http-signature": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz", @@ -549,6 +639,25 @@ "sshpk": "^1.7.0" } }, + "https-proxy-agent": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-2.2.1.tgz", + "integrity": "sha512-HPCTS1LW51bcyMYbxUIOO4HEOlQ1/1qRaFWcyxvwaqUS9TY88aoEuHUY33kuAh1YhVVaDQhLZsnPd+XNARWZlQ==", + "requires": { + "agent-base": "^4.1.0", + "debug": "^3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "iconv-lite": { "version": "0.4.23", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz", @@ -787,6 +896,11 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz", "integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk=" }, + "node-fetch": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.2.0.tgz", + "integrity": "sha512-OayFWziIxiHY8bCUyLX6sTpDH8Jsbp4FfYd1j1f7vZyfgkcOnAyM4oQR16f8a0s7Gl/viMGRey8eScYk4V4EZA==" + }, "oauth-sign": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz", @@ -820,6 +934,11 @@ "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" }, + "prettier": { + "version": "1.14.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.14.2.tgz", + "integrity": "sha512-McHPg0n1pIke+A/4VcaS2en+pTNjy4xF+Uuq86u/5dyDO59/TtFZtQ708QIRkEZ3qwKz3GVkVa6mpxK/CpB8Rg==" + }, "proxy-addr": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.4.tgz", @@ -1008,6 +1127,11 @@ "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" }, + "url-template": { + "version": "2.0.8", + "resolved": "https://registry.npmjs.org/url-template/-/url-template-2.0.8.tgz", + "integrity": "sha1-/FZaPMy/93MMd19WQflVV5FDnyE=" + }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", diff --git a/package.json b/package.json index 36d04c3..44f092e 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "homepage": "https://github.com/manjufy/nodejs-github-auth#readme", "dependencies": { + "@octokit/rest": "^15.10.0", "axios": "^0.18.0", "body-parser": "^1.18.3", "cors": "^2.8.4", From 96111798518c8edbe280c2933c31764eeea45f18 Mon Sep 17 00:00:00 2001 From: Manjunath Reddy Date: Sun, 2 Sep 2018 16:53:45 +0800 Subject: [PATCH 2/6] Re-orgainise libs --- src/server.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/server.js b/src/server.js index 9fbd68e..6b06969 100644 --- a/src/server.js +++ b/src/server.js @@ -1,12 +1,12 @@ -const jwt = require('express-jwt') -const jwks = require('jwks-rsa') -const cors = require('cors') +const app = express() +const axios = require('axios') const bodyParser = require('body-parser') +const cors = require('cors') const express = require('express') -const axios = require('axios') -const app = express() +const jwt = require('express-jwt') +const jwks = require('jwks-rsa') const util = require('util') - +const octokit = require('@octokit/rest') app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cors()); From 1f7c87bef49a3d3a8f42b9aa6d4e40622998efd2 Mon Sep 17 00:00:00 2001 From: Manjunath Reddy Date: Sun, 2 Sep 2018 23:06:09 +0800 Subject: [PATCH 3/6] OAuth flow explained --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4ab84d7..9584efb 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,9 @@ Make sure to install nodemon locally in global mode and -`npm run dev` to start \ No newline at end of file +`npm run dev` to start + + +### Github OAuth flow explained + +https://github.com/octokit/octokit.net/blob/master/docs/oauth-flow.md \ No newline at end of file From 224b232cbf95de328fd27c7211f354c2eb3161f5 Mon Sep 17 00:00:00 2001 From: Manjunath Reddy Date: Mon, 3 Sep 2018 00:48:21 +0800 Subject: [PATCH 4/6] Format the github auth response to object --- src/server.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/server.js b/src/server.js index 6b06969..ad52a15 100644 --- a/src/server.js +++ b/src/server.js @@ -1,10 +1,12 @@ +const express = require('express') + const app = express() const axios = require('axios') const bodyParser = require('body-parser') const cors = require('cors') -const express = require('express') const jwt = require('express-jwt') const jwks = require('jwks-rsa') +const queryStr = require('querystring') const util = require('util') const octokit = require('@octokit/rest') app.use(bodyParser.json()); @@ -30,9 +32,14 @@ app.get('/auth', async (req, res) => { 'crossDomain': true } }) + + + // Response is in the form of access_token=50d935da34d5bf48d0560e30d1f052ee56d030bc&scope=user%3Aemail&token_type=bearer const token = response.data - res.send(token) + res.json( + queryStr.parse(token) + ) }) app.listen(3333) From 1bcf60b916b3525d4920007a2caacad3d0ee1898 Mon Sep 17 00:00:00 2001 From: Manjunath Reddy Date: Tue, 4 Sep 2018 21:52:47 +0800 Subject: [PATCH 5/6] Update readme --- README.md | 6 +++++- src/server.js | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9584efb..144f5ac 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,8 @@ and ### Github OAuth flow explained -https://github.com/octokit/octokit.net/blob/master/docs/oauth-flow.md \ No newline at end of file +https://github.com/octokit/octokit.net/blob/master/docs/oauth-flow.md + +### TODO + +Implement a package to consume Github auth code and return access token \ No newline at end of file diff --git a/src/server.js b/src/server.js index ad52a15..d752902 100644 --- a/src/server.js +++ b/src/server.js @@ -20,6 +20,7 @@ app.get('/', (req, res) => { app.get('/auth', async (req, res) => { const code = req.query.code + // Make a post request to Github const response = await axios.post('https://github.com/login/oauth/access_token', { client_id: '13713e448956673736bb', From 0fab1968ffaae93acb59539f3ebec8882d85a348 Mon Sep 17 00:00:00 2001 From: Manjunath Reddy Date: Sat, 8 Sep 2018 22:21:03 +0800 Subject: [PATCH 6/6] Documentation --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 144f5ac..07c7748 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,15 @@ https://github.com/octokit/octokit.net/blob/master/docs/oauth-flow.md ### TODO -Implement a package to consume Github auth code and return access token \ No newline at end of file +Implement a package to consume Github auth code and return access token + +### Errors + +``` +{ + "message": "Bad credentials", + "documentation_url": "https://developer.github.com/v3" +} +``` + +Make sure to include `Authorization: Bearer ` In your header \ No newline at end of file