Skip to content

Commit 3abb0c8

Browse files
feat(provider): Add Bungie (#589)
* Add Bungie provider * Use absolute URL for images * Correct image URL and use consistent formatting Co-authored-by: Nico Domino <yo@ndo.dev>
1 parent 8c56e13 commit 3abb0c8

File tree

5 files changed

+193
-3
lines changed

5 files changed

+193
-3
lines changed

src/providers/bungie.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export default (options) => {
2+
return {
3+
id: 'bungie',
4+
name: 'Bungie',
5+
type: 'oauth',
6+
version: '2.0',
7+
scope: '',
8+
params: { reauth: 'true', grant_type: 'authorization_code' },
9+
accessTokenUrl: 'https://www.bungie.net/platform/app/oauth/token/',
10+
requestTokenUrl: 'https://www.bungie.net/platform/app/oauth/token/',
11+
authorizationUrl: 'https://www.bungie.net/en/OAuth/Authorize?response_type=code',
12+
profileUrl: 'https://www.bungie.net/platform/User/GetBungieAccount/{membershipId}/254/',
13+
prepareProfileRequest: ({ provider, url, headers, results }) => {
14+
if (!results.membership_id) {
15+
// internal error
16+
// @TODO: handle better
17+
throw new Error('Expected membership_id to be passed.')
18+
}
19+
20+
if (!provider.apiKey) {
21+
throw new Error('The Bungie provider requires the apiKey option to be present.')
22+
}
23+
24+
headers['X-API-Key'] = provider.apiKey
25+
url = url.replace('{membershipId}', results.membership_id)
26+
27+
return url
28+
},
29+
profile: (profile) => {
30+
const { bungieNetUser: user } = profile.Response
31+
32+
return {
33+
id: user.membershipId,
34+
name: user.displayName,
35+
image: `https://www.bungie.net${user.profilePicturePath.startsWith('/') ? '' : '/'}${user.profilePicturePath}`,
36+
email: null
37+
}
38+
},
39+
apiKey: null,
40+
clientId: null,
41+
clientSecret: null,
42+
...options
43+
}
44+
}

src/providers/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Auth0 from './auth0'
44
import Basecamp from './basecamp'
55
import BattleNet from './battlenet'
66
import Box from './box'
7+
import Bungie from './bungie'
78
import Credentials from './credentials'
89
import Cognito from './cognito'
910
import Discord from './discord'
@@ -31,6 +32,7 @@ export default {
3132
Basecamp,
3233
BattleNet,
3334
Box,
35+
Bungie,
3436
Credentials,
3537
Cognito,
3638
Discord,

src/server/lib/oauth/callback.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export default async (req, provider, csrfToken, callback) => {
9393
client.get(
9494
provider,
9595
accessToken,
96+
results,
9697
async (error, profileData) => {
9798
const { profile, account, OAuthProfile } = await _getProfile(error, profileData, accessToken, refreshToken, provider)
9899
callback(error, profile, account, OAuthProfile)
@@ -253,9 +254,14 @@ async function _getOAuthAccessToken (code, provider, callback) {
253254
)
254255
}
255256

256-
// Ported from https://github.com/ciaranj/node-oauth/blob/a7f8a1e21c362eb4ed2039431fb9ac2ae749f26a/lib/oauth2.js
257-
function _get (provider, accessToken, callback) {
258-
const url = provider.profileUrl
257+
/**
258+
* Ported from https://github.com/ciaranj/node-oauth/blob/a7f8a1e21c362eb4ed2039431fb9ac2ae749f26a/lib/oauth2.js
259+
*
260+
* 18/08/2020 @robertcraigie added results parameter to pass data to an optional request preparer.
261+
* e.g. see providers/bungie
262+
*/
263+
function _get (provider, accessToken, results, callback) {
264+
let url = provider.profileUrl
259265
const headers = provider.headers || {}
260266

261267
if (this._useAuthorizationHeaderForGET) {
@@ -266,6 +272,11 @@ function _get (provider, accessToken, callback) {
266272
accessToken = null
267273
}
268274

275+
const prepareRequest = provider.prepareProfileRequest
276+
if (prepareRequest) {
277+
url = prepareRequest({ provider, url, headers, results }) || url
278+
}
279+
269280
this._request('GET', url, headers, null, accessToken, callback)
270281
}
271282

www/docs/providers/bungie.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
id: bungie
3+
title: Bungie
4+
---
5+
6+
## Documentation
7+
8+
https://github.com/Bungie-net/api/wiki/OAuth-Documentation
9+
10+
## Configuration
11+
12+
https://www.bungie.net/en/Application
13+
14+
## Example
15+
16+
```js
17+
import Providers from `next-auth/providers`
18+
...
19+
providers: [
20+
Providers.Bungie({
21+
clientId: process.env.BUNGIE_CLIENT_ID,
22+
clientSecret: process.env.BUNGIE_SECRET,
23+
apiKey: process.env.BUNGIE_API_KEY
24+
}),
25+
}
26+
...
27+
```
28+
29+
## Instructions
30+
31+
### Configuration
32+
33+
:::tip
34+
Bungie require all sites to run HTTPS (including local development instances).
35+
:::
36+
37+
:::tip
38+
Bungie doesn't allow you to use localhost as the website URL, instead you need to use https://127.0.0.1:3000
39+
:::
40+
41+
Navigate to https://www.bungie.net/en/Application and fill in the required details:
42+
43+
* Application name
44+
* Application Status
45+
* Website
46+
* OAuth Client Type
47+
- Confidential
48+
* Redirect URL
49+
- https://localhost:3000/api/auth/callback/bungie
50+
* Scope
51+
- `Access items like your Bungie.net notifications, memberships, and recent Bungie.Net forum activity.`
52+
* Origin Header
53+
54+
The following guide may be helpful:
55+
56+
* [How to setup localhost with HTTPS with a Next.js app](https://medium.com/@anMagpie/secure-your-local-development-server-with-https-next-js-81ac6b8b3d68)
57+
58+
### Example server
59+
60+
You will need to edit your host file and point your site at `127.0.0.1`
61+
62+
[How to edit my host file?](https://phoenixnap.com/kb/how-to-edit-hosts-file-in-windows-mac-or-linux)
63+
64+
On Windows (Run Powershell as administrator)
65+
66+
```ps
67+
Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value "127.0.0.1`tdev.example.com" -Force
68+
```
69+
70+
```
71+
127.0.0.1 dev.example.com
72+
```
73+
74+
#### Create certificate
75+
76+
77+
Creating a certificate for localhost is easy with openssl . Just put the following command in the terminal. The output will be two files: localhost.key and localhost.crt.
78+
79+
```bash
80+
openssl req -x509 -out localhost.crt -keyout localhost.key \
81+
-newkey rsa:2048 -nodes -sha256 \
82+
-subj '/CN=localhost' -extensions EXT -config <( \
83+
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
84+
```
85+
86+
:::tip
87+
**Windows**
88+
89+
The OpenSSL executable is distributed with [Git](https://git-scm.com/download/win]9) for Windows.
90+
Once installed you will find the openssl.exe file in `C:/Program Files/Git/mingw64/bin` which you can add to the system PATH environment variable if it’s not already done.
91+
92+
Add environment variable `OPENSSL_CONF=C:/Program Files/Git/mingw64/ssl/openssl.cnf`
93+
94+
```bash
95+
req -x509 -out localhost.crt -keyout localhost.key \
96+
-newkey rsa:2048 -nodes -sha256 \
97+
-subj '/CN=localhost'
98+
```
99+
100+
:::
101+
102+
Create directory `certificates` and place `localhost.key` and `localhost.crt`
103+
104+
105+
You can create a `server.js` in the root of your project and run it with `node server.js` to test Sign in with Bungie integration locally:
106+
107+
108+
```js
109+
const { createServer } = require('https')
110+
const { parse } = require('url')
111+
const next = require('next')
112+
const fs = require('fs')
113+
114+
const dev = process.env.NODE_ENV !== 'production'
115+
const app = next({ dev })
116+
const handle = app.getRequestHandler()
117+
118+
const httpsOptions = {
119+
key: fs.readFileSync('./certificates/localhost.key'),
120+
cert: fs.readFileSync('./certificates/localhost.crt')
121+
}
122+
123+
app.prepare().then(() => {
124+
createServer(httpsOptions, (req, res) => {
125+
const parsedUrl = parse(req.url, true)
126+
handle(req, res, parsedUrl)
127+
}).listen(3000, err => {
128+
if (err) throw err
129+
console.log('> Ready on https://localhost:3000')
130+
})
131+
})
132+
```

www/sidebars.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = {
2929
'providers/basecamp',
3030
'providers/battle.net',
3131
'providers/box',
32+
'providers/bungie',
3233
'providers/cognito',
3334
'providers/discord',
3435
'providers/email',

0 commit comments

Comments
 (0)