Skip to content

Commit

Permalink
feat(provider): Add Bungie (#589)
Browse files Browse the repository at this point in the history
* Add Bungie provider

* Use absolute URL for images

* Correct image URL and use consistent formatting

Co-authored-by: Nico Domino <[email protected]>
  • Loading branch information
2 people authored and balazsorban44 committed Feb 1, 2021
1 parent f47f5c6 commit 958c31a
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 3 deletions.
44 changes: 44 additions & 0 deletions src/providers/bungie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export default (options) => {
return {
id: 'bungie',
name: 'Bungie',
type: 'oauth',
version: '2.0',
scope: '',
params: { reauth: 'true', grant_type: 'authorization_code' },
accessTokenUrl: 'https://www.bungie.net/platform/app/oauth/token/',
requestTokenUrl: 'https://www.bungie.net/platform/app/oauth/token/',
authorizationUrl: 'https://www.bungie.net/en/OAuth/Authorize?response_type=code',
profileUrl: 'https://www.bungie.net/platform/User/GetBungieAccount/{membershipId}/254/',
prepareProfileRequest: ({ provider, url, headers, results }) => {
if (!results.membership_id) {
// internal error
// @TODO: handle better
throw new Error('Expected membership_id to be passed.')
}

if (!provider.apiKey) {
throw new Error('The Bungie provider requires the apiKey option to be present.')
}

headers['X-API-Key'] = provider.apiKey
url = url.replace('{membershipId}', results.membership_id)

return url
},
profile: (profile) => {
const { bungieNetUser: user } = profile.Response

return {
id: user.membershipId,
name: user.displayName,
image: `https://www.bungie.net${user.profilePicturePath.startsWith('/') ? '' : '/'}${user.profilePicturePath}`,
email: null
}
},
apiKey: null,
clientId: null,
clientSecret: null,
...options
}
}
2 changes: 2 additions & 0 deletions src/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Auth0 from './auth0'
import Basecamp from './basecamp'
import BattleNet from './battlenet'
import Box from './box'
import Bungie from './bungie'
import Credentials from './credentials'
import Cognito from './cognito'
import Discord from './discord'
Expand Down Expand Up @@ -31,6 +32,7 @@ export default {
Basecamp,
BattleNet,
Box,
Bungie,
Credentials,
Cognito,
Discord,
Expand Down
17 changes: 14 additions & 3 deletions src/server/lib/oauth/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default async (req, provider, csrfToken, callback) => {
client.get(
provider,
accessToken,
results,
async (error, profileData) => {
const { profile, account, OAuthProfile } = await _getProfile(error, profileData, accessToken, refreshToken, provider)
callback(error, profile, account, OAuthProfile)
Expand Down Expand Up @@ -253,9 +254,14 @@ async function _getOAuthAccessToken (code, provider, callback) {
)
}

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

if (this._useAuthorizationHeaderForGET) {
Expand All @@ -266,6 +272,11 @@ function _get (provider, accessToken, callback) {
accessToken = null
}

const prepareRequest = provider.prepareProfileRequest
if (prepareRequest) {
url = prepareRequest({ provider, url, headers, results }) || url
}

this._request('GET', url, headers, null, accessToken, callback)
}

Expand Down
132 changes: 132 additions & 0 deletions www/docs/providers/bungie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
---
id: bungie
title: Bungie
---

## Documentation

https://github.com/Bungie-net/api/wiki/OAuth-Documentation

## Configuration

https://www.bungie.net/en/Application

## Example

```js
import Providers from `next-auth/providers`
...
providers: [
Providers.Bungie({
clientId: process.env.BUNGIE_CLIENT_ID,
clientSecret: process.env.BUNGIE_SECRET,
apiKey: process.env.BUNGIE_API_KEY
}),
}
...
```

## Instructions

### Configuration

:::tip
Bungie require all sites to run HTTPS (including local development instances).
:::

:::tip
Bungie doesn't allow you to use localhost as the website URL, instead you need to use https://127.0.0.1:3000
:::

Navigate to https://www.bungie.net/en/Application and fill in the required details:

* Application name
* Application Status
* Website
* OAuth Client Type
- Confidential
* Redirect URL
- https://localhost:3000/api/auth/callback/bungie
* Scope
- `Access items like your Bungie.net notifications, memberships, and recent Bungie.Net forum activity.`
* Origin Header

The following guide may be helpful:

* [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)

### Example server

You will need to edit your host file and point your site at `127.0.0.1`

[How to edit my host file?](https://phoenixnap.com/kb/how-to-edit-hosts-file-in-windows-mac-or-linux)

On Windows (Run Powershell as administrator)

```ps
Add-Content -Path C:\Windows\System32\drivers\etc\hosts -Value "127.0.0.1`tdev.example.com" -Force
```

```
127.0.0.1 dev.example.com
```

#### Create certificate


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.

```bash
openssl req -x509 -out localhost.crt -keyout localhost.key \
-newkey rsa:2048 -nodes -sha256 \
-subj '/CN=localhost' -extensions EXT -config <( \
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
```

:::tip
**Windows**

The OpenSSL executable is distributed with [Git](https://git-scm.com/download/win]9) for Windows.
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.

Add environment variable `OPENSSL_CONF=C:/Program Files/Git/mingw64/ssl/openssl.cnf`

```bash
req -x509 -out localhost.crt -keyout localhost.key \
-newkey rsa:2048 -nodes -sha256 \
-subj '/CN=localhost'
```

:::

Create directory `certificates` and place `localhost.key` and `localhost.crt`


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:


```js
const { createServer } = require('https')
const { parse } = require('url')
const next = require('next')
const fs = require('fs')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

const httpsOptions = {
key: fs.readFileSync('./certificates/localhost.key'),
cert: fs.readFileSync('./certificates/localhost.crt')
}

app.prepare().then(() => {
createServer(httpsOptions, (req, res) => {
const parsedUrl = parse(req.url, true)
handle(req, res, parsedUrl)
}).listen(3000, err => {
if (err) throw err
console.log('> Ready on https://localhost:3000')
})
})
```
1 change: 1 addition & 0 deletions www/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
'providers/basecamp',
'providers/battle.net',
'providers/box',
'providers/bungie',
'providers/cognito',
'providers/discord',
'providers/email',
Expand Down

0 comments on commit 958c31a

Please sign in to comment.