From 11432f073b35f4ac1e9448f96241fe184a37d3fb Mon Sep 17 00:00:00 2001 From: Joel Schneider Date: Thu, 26 Apr 2018 16:16:17 -0400 Subject: [PATCH 1/6] Add a laravel passport provider --- lib/providers/passport.js | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lib/providers/passport.js diff --git a/lib/providers/passport.js b/lib/providers/passport.js new file mode 100644 index 000000000..b64788ee4 --- /dev/null +++ b/lib/providers/passport.js @@ -0,0 +1,79 @@ +const axios = require('axios') +const bodyParser = require('body-parser') +const { assignDefaults } = require('./_utils') + +module.exports = function passport (strategy) { + assignDefaults(strategy, { + _scheme: 'oauth2', + _name: 'passport', + authorization_endpoint: `${strategy.url}/oauth/authorize`, + token_endpoint: `${strategy.url}/oauth/token`, + token_key: 'access_token', + token_type: 'Bearer', + response_type: 'code', + grant_type: 'authorization_code', + scope: '*' + }) + + // Get client_secret, client_id and token_endpoint + const clientSecret = strategy.client_secret + const clientID = strategy.client_id + const tokenEndpoint = strategy.token_endpoint + + // IMPORTANT: remove client_secret from generated bundle + delete strategy.client_secret + + // Endpoint + const endpoint = `/_auth/oauth/${strategy._name}/authorize` + strategy.access_token_endpoint = endpoint + + // Set response_type to code + strategy.response_type = 'code' + + // Form parser + const formMiddleware = bodyParser.urlencoded() + + // Register endpoint + this.options.serverMiddleware.unshift({ + path: endpoint, + handler: (req, res, next) => { + if (req.method !== 'POST') { + return next() + } + + formMiddleware(req, res, () => { + const { + code, + redirect_uri: redirectUri = strategy.redirect_uri, + response_type: responseType = strategy.response_type, + grant_type: grantType = strategy.grant_type + } = req.body + + if (!code) { + return next() + } + + axios + .request({ + method: 'post', + url: tokenEndpoint, + data: { + client_id: clientID, + client_secret: clientSecret, + grant_type: grantType, + response_type: responseType, + redirect_uri: redirectUri, + code + }, + headers: { + Accept: 'application/json' + } + }) + .then(response => { + res.end(JSON.stringify(response.data)) + }) + .catch(error => next(error)) + }) + } + }) +} From d7b74797a139d8adb386d6a9b259c7f62ba2a27c Mon Sep 17 00:00:00 2001 From: Joel Schneider Date: Thu, 26 Apr 2018 16:35:09 -0400 Subject: [PATCH 2/6] Ignore axios base url for authorization code --- lib/schemes/oauth2.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/schemes/oauth2.js b/lib/schemes/oauth2.js index 9bef344fd..6e21a5954 100644 --- a/lib/schemes/oauth2.js +++ b/lib/schemes/oauth2.js @@ -99,6 +99,7 @@ export default class Oauth2Scheme { const data = await this.$auth.request({ method: 'post', url: this.options.access_token_endpoint, + baseURL: false, data: encodeQuery({ code: parsedQuery.code, client_id: this.options.client_id, From 83215fd14b751b7e33f4e3f794ae8aaeb93f5f53 Mon Sep 17 00:00:00 2001 From: Joel Schneider Date: Thu, 26 Apr 2018 16:38:58 -0400 Subject: [PATCH 3/6] Add documentation for passport provider --- docs/providers/passport.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 docs/providers/passport.md diff --git a/docs/providers/passport.md b/docs/providers/passport.md new file mode 100644 index 000000000..93f77a47d --- /dev/null +++ b/docs/providers/passport.md @@ -0,0 +1,32 @@ +# Github + +[Source Code](https://github.com/nuxt-community/auth-module/blob/dev/lib/providers/passport.js) + +## Usage + +```js +auth: { + strategies: { + passport: { + url: '...', + client_id: '...', + client_secret: '...' + }, + } +} +``` + +## Usage + +Anywhere in your application logic: + +```js +this.$auth.loginWith('passport') +``` + +💁 This provider is based on [oauth2 scheme](../schemes/oauth2.md) and supports all scheme options. + +### Obtaining `url`, `client_id` and `client_secret` + +These options are **REQUIRED**. The `url` is the location of your Laravel application. To obtain the `client_id` and `client_secret`, create a new client app in your [Laravel app](https://laravel.com/docs/5.6/passport#managing-clients). + From 8d68f8e3781b875c55e3104ff1896d3d78eb1ba8 Mon Sep 17 00:00:00 2001 From: Joel Schneider Date: Thu, 26 Apr 2018 16:47:05 -0400 Subject: [PATCH 4/6] Fix title --- docs/providers/passport.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/providers/passport.md b/docs/providers/passport.md index 93f77a47d..4a5f3073d 100644 --- a/docs/providers/passport.md +++ b/docs/providers/passport.md @@ -1,4 +1,4 @@ -# Github +# Laravel Passport [Source Code](https://github.com/nuxt-community/auth-module/blob/dev/lib/providers/passport.js) From c8881b4cad1da4b669062206666322334e062647 Mon Sep 17 00:00:00 2001 From: Joel Schneider Date: Thu, 26 Apr 2018 22:05:09 -0400 Subject: [PATCH 5/6] Add passport link --- docs/SUMMARY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index e21cef9f5..6fb8804fd 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -14,6 +14,7 @@ * [Facebook](providers/facebook.md) * [Github](providers/github.md) * [Google](providers/google.md) + * [Laravel Passport](providers/passport.md) * API * [Auth](api/auth.md) * [Storage](api/storage.md) From 34303fbfaec28404357c32346963061ef1b768e7 Mon Sep 17 00:00:00 2001 From: Joel Schneider Date: Fri, 27 Apr 2018 09:43:04 -0400 Subject: [PATCH 6/6] Change provider name to laravel.passport --- docs/providers/passport.md | 2 +- lib/providers/{passport.js => laravel.passport.js} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename lib/providers/{passport.js => laravel.passport.js} (96%) diff --git a/docs/providers/passport.md b/docs/providers/passport.md index 4a5f3073d..f2529fc36 100644 --- a/docs/providers/passport.md +++ b/docs/providers/passport.md @@ -7,7 +7,7 @@ ```js auth: { strategies: { - passport: { + 'laravel.passport': { url: '...', client_id: '...', client_secret: '...' diff --git a/lib/providers/passport.js b/lib/providers/laravel.passport.js similarity index 96% rename from lib/providers/passport.js rename to lib/providers/laravel.passport.js index b64788ee4..765348d19 100644 --- a/lib/providers/passport.js +++ b/lib/providers/laravel.passport.js @@ -2,10 +2,10 @@ const axios = require('axios') const bodyParser = require('body-parser') const { assignDefaults } = require('./_utils') -module.exports = function passport (strategy) { +module.exports = function laravelPassport (strategy) { assignDefaults(strategy, { _scheme: 'oauth2', - _name: 'passport', + _name: 'laravel.passport', authorization_endpoint: `${strategy.url}/oauth/authorize`, token_endpoint: `${strategy.url}/oauth/token`, token_key: 'access_token',