Skip to content

Commit

Permalink
feat(#27): add api proxy support to userinfo (#28)
Browse files Browse the repository at this point in the history
* feat(#27): add api proxy support to userinfo

* chore(#27): cleanup
  • Loading branch information
Decipher committed Feb 17, 2023
1 parent b6ef616 commit a537e4d
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-owls-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"druxt-auth": minor
---

feat(#27): add support for Druxt API Proxy mode.
7 changes: 6 additions & 1 deletion example/nuxt/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export default {
// modules: [],

// DruxtJS: https://druxtjs.org
druxt: { baseUrl },
druxt: {
baseUrl,

// Uncomment to enable API Proxy mode.
// proxy: { api: true }
},

// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
Expand Down
30 changes: 27 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,31 @@ const NuxtModule = function (moduleOptions = {}) {
throw new Error('DruxtAuth requires a clientId to be provided.')
}

const { baseUrl } = options
let { baseUrl } = options

// Nuxt proxy integration.
const proxy = (options.proxy || {}).api
if (proxy) {
if (this.options.proxy) {
if (Array.isArray(this.options.proxy)) {
this.options.proxy = [
...this.options.proxy,
baseUrl + '/oauth/userinfo'
]
}
else {
this.options.proxy = {
...this.options.proxy,
'/oauth/userinfo': baseUrl
}
}
}
else {
this.options.proxy = {
'/oauth/userinfo': baseUrl
}
}
}

// @nuxtjs/auth-next module settings.
this.options.auth = {
Expand All @@ -37,7 +61,7 @@ const NuxtModule = function (moduleOptions = {}) {
endpoints: {
authorization: baseUrl + '/oauth/authorize',
token: baseUrl + '/oauth/token',
userInfo: baseUrl + '/oauth/userinfo',
userInfo: (!proxy ? baseUrl : '') + '/oauth/userinfo',
},
clientId: (options.auth || {}).clientId || process.env.DRUXT_AUTH_CLIENT_ID,
responseType: 'code',
Expand Down Expand Up @@ -71,7 +95,7 @@ const NuxtModule = function (moduleOptions = {}) {
url: '/_auth/drupal-password/token'
},
user: {
url: baseUrl + '/oauth/userinfo',
url: (!proxy ? baseUrl : '') + '/oauth/userinfo',
method: 'post'
},
},
Expand Down
171 changes: 171 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,176 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DruxtAuth Nuxt module API Proxy - Array 1`] = `
Object {
"redirect": Object {
"callback": "/callback",
"logout": "/",
},
"strategies": Object {
"drupal-authorization_code": Object {
"clientId": "mock-client-id",
"codeChallengeMethod": "S256",
"endpoints": Object {
"authorization": "https://demo-api.druxtjs.org/oauth/authorize",
"token": "https://demo-api.druxtjs.org/oauth/token",
"userInfo": "/oauth/userinfo",
},
"grantType": "authorization_code",
"responseType": "code",
"scheme": "oauth2",
},
"drupal-password": Object {
"endpoints": Object {
"login": Object {
"baseURL": "",
"url": "/_auth/drupal-password/token",
},
"logout": false,
"refresh": Object {
"baseURL": "",
"url": "/_auth/drupal-password/token",
},
"token": "https://demo-api.druxtjs.org/oauth/token",
"user": Object {
"method": "post",
"url": "/oauth/userinfo",
},
},
"grantType": "password",
"refreshToken": Object {
"data": "refresh_token",
"maxAge": 2592000,
"property": "refresh_token",
},
"scheme": "refresh",
"token": Object {
"maxAge": 31536000,
"name": "Authorization",
"property": "access_token",
"type": "Bearer",
},
"user": Object {
"property": false,
},
},
},
}
`;

exports[`DruxtAuth Nuxt module API Proxy - Object 1`] = `
Object {
"redirect": Object {
"callback": "/callback",
"logout": "/",
},
"strategies": Object {
"drupal-authorization_code": Object {
"clientId": "mock-client-id",
"codeChallengeMethod": "S256",
"endpoints": Object {
"authorization": "https://demo-api.druxtjs.org/oauth/authorize",
"token": "https://demo-api.druxtjs.org/oauth/token",
"userInfo": "/oauth/userinfo",
},
"grantType": "authorization_code",
"responseType": "code",
"scheme": "oauth2",
},
"drupal-password": Object {
"endpoints": Object {
"login": Object {
"baseURL": "",
"url": "/_auth/drupal-password/token",
},
"logout": false,
"refresh": Object {
"baseURL": "",
"url": "/_auth/drupal-password/token",
},
"token": "https://demo-api.druxtjs.org/oauth/token",
"user": Object {
"method": "post",
"url": "/oauth/userinfo",
},
},
"grantType": "password",
"refreshToken": Object {
"data": "refresh_token",
"maxAge": 2592000,
"property": "refresh_token",
},
"scheme": "refresh",
"token": Object {
"maxAge": 31536000,
"name": "Authorization",
"property": "access_token",
"type": "Bearer",
},
"user": Object {
"property": false,
},
},
},
}
`;

exports[`DruxtAuth Nuxt module API Proxy - default 1`] = `
Object {
"redirect": Object {
"callback": "/callback",
"logout": "/",
},
"strategies": Object {
"drupal-authorization_code": Object {
"clientId": "mock-client-id",
"codeChallengeMethod": "S256",
"endpoints": Object {
"authorization": "https://demo-api.druxtjs.org/oauth/authorize",
"token": "https://demo-api.druxtjs.org/oauth/token",
"userInfo": "/oauth/userinfo",
},
"grantType": "authorization_code",
"responseType": "code",
"scheme": "oauth2",
},
"drupal-password": Object {
"endpoints": Object {
"login": Object {
"baseURL": "",
"url": "/_auth/drupal-password/token",
},
"logout": false,
"refresh": Object {
"baseURL": "",
"url": "/_auth/drupal-password/token",
},
"token": "https://demo-api.druxtjs.org/oauth/token",
"user": Object {
"method": "post",
"url": "/oauth/userinfo",
},
},
"grantType": "password",
"refreshToken": Object {
"data": "refresh_token",
"maxAge": 2592000,
"property": "refresh_token",
},
"scheme": "refresh",
"token": Object {
"maxAge": 31536000,
"name": "Authorization",
"property": "access_token",
"type": "Bearer",
},
"user": Object {
"property": false,
},
},
},
}
`;

exports[`DruxtAuth Nuxt module Defaults 1`] = `
Object {
"redirect": Object {
Expand Down
44 changes: 44 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global beforeEach, describe, expect, jest, test */

import DruxtAuthModule from '../src'

let mock
Expand Down Expand Up @@ -80,4 +82,46 @@ describe('DruxtAuth Nuxt module', () => {
await mock.options.serverMiddleware[0].handler(req, res, next)
expect(res.end).toBeCalledWith('true')
})

test('API Proxy - default', async () => {
mock.options.druxt.proxy = { api: true }

// Call Druxt module with module options.
DruxtAuthModule.call(mock, {
clientId: 'mock-client-id'
})

// Expect the @nuxtjs/auth-next module to be correctly configured.
expect(mock.options.auth).toMatchSnapshot()
})

test('API Proxy - Object', async () => {
mock.options.druxt.proxy = { api: true }
mock.options.proxy = {
'/test': 'https://api.umami.demo.druxtjs.org'
}

// Call Druxt module with module options.
DruxtAuthModule.call(mock, {
clientId: 'mock-client-id'
})

// Expect the @nuxtjs/auth-next module to be correctly configured.
expect(mock.options.auth).toMatchSnapshot()
})

test('API Proxy - Array', async () => {
mock.options.druxt.proxy = { api: true }
mock.options.proxy = [
'https://api.umami.demo.druxtjs.org/test'
]

// Call Druxt module with module options.
DruxtAuthModule.call(mock, {
clientId: 'mock-client-id'
})

// Expect the @nuxtjs/auth-next module to be correctly configured.
expect(mock.options.auth).toMatchSnapshot()
})
})

0 comments on commit a537e4d

Please sign in to comment.