|
| 1 | +import axios from 'axios' |
| 2 | +import bodyParser from 'body-parser' |
| 3 | + |
1 | 4 | // eslint-disable-next-line no-unused-vars |
2 | 5 | const NuxtModule = function (moduleOptions = {}) { |
3 | | - const options = this.options.druxt || {} |
| 6 | + const options = { |
| 7 | + ...this.options.druxt || {}, |
| 8 | + auth: { |
| 9 | + ...(this.options.druxt || {}).auth || {}, |
| 10 | + clientId: undefined, |
| 11 | + clientSecret: undefined, |
| 12 | + ...moduleOptions, |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + // Check if cliend ID is provided. |
| 17 | + if (!options.auth.clientId) { |
| 18 | + throw new Error('DruxtAuth requires a clientId to be provided.') |
| 19 | + } |
| 20 | + |
4 | 21 | const { baseUrl } = options |
5 | 22 |
|
6 | 23 | // @nuxtjs/auth-next module settings. |
@@ -28,10 +45,93 @@ const NuxtModule = function (moduleOptions = {}) { |
28 | 45 | codeChallengeMethod: 'S256', |
29 | 46 | }, |
30 | 47 |
|
| 48 | + // Password grant with API secret. |
| 49 | + 'drupal-password': { |
| 50 | + scheme: 'refresh', |
| 51 | + token: { |
| 52 | + property: 'access_token', |
| 53 | + type: 'Bearer', |
| 54 | + name: 'Authorization', |
| 55 | + maxAge: 60 * 60 * 24 * 365 |
| 56 | + }, |
| 57 | + refreshToken: { |
| 58 | + property: 'refresh_token', |
| 59 | + data: 'refresh_token', |
| 60 | + maxAge: 60 * 60 * 24 * 30 |
| 61 | + }, |
| 62 | + endpoints: { |
| 63 | + token: baseUrl + '/oauth/token', |
| 64 | + login: { |
| 65 | + baseURL: '', |
| 66 | + url: '/_auth/drupal-password/token' |
| 67 | + }, |
| 68 | + logout: false, |
| 69 | + refresh: { |
| 70 | + baseURL: '', |
| 71 | + url: '/_auth/drupal-password/token' |
| 72 | + }, |
| 73 | + user: { |
| 74 | + url: baseUrl + '/oauth/userinfo', |
| 75 | + method: 'post' |
| 76 | + }, |
| 77 | + }, |
| 78 | + user: { |
| 79 | + property: false |
| 80 | + }, |
| 81 | + grantType: 'password' |
| 82 | + }, |
| 83 | + |
31 | 84 | ...(this.options.auth || {}).strategies |
32 | 85 | }, |
33 | 86 | } |
34 | 87 |
|
| 88 | + // Add password grant server middleware. |
| 89 | + this.options.serverMiddleware.unshift({ |
| 90 | + path: '/_auth/drupal-password/token', |
| 91 | + handler: async (req, res, next) => { |
| 92 | + if (req.method !== 'POST') { |
| 93 | + return next() |
| 94 | + } |
| 95 | + |
| 96 | + const formMiddleware = bodyParser.json() |
| 97 | + await formMiddleware(req, res, async () => { |
| 98 | + const data = req.body |
| 99 | + |
| 100 | + if (data.grant_type === 'password' && (!data.username || !data.password)) { |
| 101 | + return next(new Error('Invalid username or password')) |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + // Build POST data string. |
| 106 | + const postData = new URLSearchParams({ |
| 107 | + client_id: (options.auth || {}).clientId || process.env.DRUXT_AUTH_CLIENT_ID, |
| 108 | + client_secret: (options.auth || {}).clientSecret || process.env.DRUXT_AUTH_CLIENT_SECRET, |
| 109 | + ...data |
| 110 | + }).toString() |
| 111 | + |
| 112 | + // Request token, |
| 113 | + const response = await axios.post( |
| 114 | + this.options.auth.strategies['drupal-password'].endpoints.token, |
| 115 | + postData, |
| 116 | + { |
| 117 | + headers: { |
| 118 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 119 | + }, |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + // Return response data. |
| 124 | + res.end(JSON.stringify(response.data)) |
| 125 | + } catch(err) { |
| 126 | + // Handle error. |
| 127 | + console.error(err) |
| 128 | + res.statusCode = (err.response || {}).statusCode || 500 |
| 129 | + res.end(JSON.stringify({ ...((err.response || {}).data || {}) })) |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | + }) |
| 134 | + |
35 | 135 | // Enable Vuex Store. |
36 | 136 | this.options.store = true |
37 | 137 |
|
|
0 commit comments