Skip to content

Commit

Permalink
feat(provider): Add Mail.ru OAuth Service Provider and Callback snipp…
Browse files Browse the repository at this point in the history
…et (#522)

* Update callback.js

- Fix Mail.ru bug (missing request parameter: access_token)

Note: setGetAccessTokenProfileUrl should be added to Mail.ru provider to enable support.

* Add Mail.ru OAuth Service Provider

* Update callbacks.md

- Fix broken callbacks snippet.

* Update callback.js

- Bug fix #522 (comment)
- Minor refactoring.

* Fix: Code linting.

* Update callback.js

Improve approach for building of URL based review recommendation.

* Feat: Reduce API surface expansion

Make use of provider.id === "mailru" as suggested in review discussion in place of setGetAccessTokenProfileUrl.

* Fix: Code linting
  • Loading branch information
nyedidikeke committed Jan 1, 2021
1 parent f2ad693 commit a8362ec
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import GitLab from './gitlab'
import Google from './google'
import IdentityServer4 from './identity-server4'
import LinkedIn from './linkedin'
import MailRu from './mailru'
import Mixer from './mixer'
import Netlify from './netlify'
import Okta from './okta'
Expand Down Expand Up @@ -49,6 +50,7 @@ export default {
Google,
IdentityServer4,
LinkedIn,
MailRu,
Mixer,
Netlify,
Okta,
Expand Down
25 changes: 25 additions & 0 deletions src/providers/mailru.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default (options) => {
return {
id: 'mailru',
name: 'Mail.ru',
type: 'oauth',
version: '2.0',
scope: 'userinfo',
params: {
grant_type: 'authorization_code'
},
accessTokenUrl: 'https://oauth.mail.ru/token',
requestTokenUrl: 'https://oauth.mail.ru/token',
authorizationUrl: 'https://oauth.mail.ru/login?response_type=code',
profileUrl: 'https://oauth.mail.ru/userinfo',
profile: (profile) => {
return {
id: profile.id,
name: profile.name,
email: profile.email,
image: profile.image
}
},
...options
}
}
7 changes: 7 additions & 0 deletions src/server/lib/oauth/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ function _get (provider, accessToken, results, callback) {
if (this._useAuthorizationHeaderForGET) {
headers.Authorization = this.buildAuthHeader(accessToken)

// Mail.ru requires 'access_token' as URL request parameter
if (provider.id === 'mailru') {
const safeAccessTokenURL = new URL(url)
safeAccessTokenURL.searchParams.append('access_token', accessToken)
url = safeAccessTokenURL.href
}

// This line is required for Twitch
headers['Client-ID'] = provider.clientId
accessToken = null
Expand Down
3 changes: 2 additions & 1 deletion www/docs/configuration/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ NextAuth.js is designed to work with any OAuth service, it supports OAuth 1.0, 1
* [Google](/providers/google)
* [IdentityServer4](/providers/identity-server4)
* [LinkedIn](/providers/LinkedIn)
* [Mail.ru](/providers/Mail.ru)
* [Mixer](/providers/Mixer)
* [Netlify](/providers/Netlify)
* [Okta](/providers/Okta)
Expand Down Expand Up @@ -225,7 +226,7 @@ The Credentials provider can only be used if JSON Web Tokens are enabled for ses
:::

<!-- React Image Component -->
export const Image = ({ children, src, alt = '' }) => (
export const Image = ({ children, src, alt = '' }) => (
<div
style={{
padding: '0.2rem',
Expand Down
25 changes: 25 additions & 0 deletions www/docs/providers/mailru.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
id: Mail.ru
title: Mail.ru
---

## Documentation

https://o2.mail.ru/docs

## Configuration

https://o2.mail.ru/app/

## Example

```js
import Providers from `next-auth/providers`
...
providers: [
Providers.MailRu({
clientId: process.env.MAILRU_CLIENT_ID,
clientSecret: process.env.MAILRU_CLIENT_SECRET
})
]
...
1 change: 1 addition & 0 deletions www/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = {
'providers/google',
'providers/identity-server4',
'providers/linkedin',
'providers/Mail.ru',
'providers/mixer',
'providers/okta',
'providers/slack',
Expand Down

0 comments on commit a8362ec

Please sign in to comment.