Skip to content

Commit

Permalink
backends: Add MusicBrainz OAuth2
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Mar 16, 2022
1 parent 32d7bcf commit f27461d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
28 changes: 28 additions & 0 deletions social_core/backends/musicbrainz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from social_core.backends.oauth import BaseOAuth2

class MusicBrainzOAuth2(BaseOAuth2):
"""MusicBrainz OAuth authentication backend"""
name = 'musicbrainz'
AUTHORIZATION_URL = 'https://musicbrainz.org/oauth2/authorize'
ACCESS_TOKEN_URL = 'https://musicbrainz.org/oauth2/token'
ACCESS_TOKEN_METHOD = 'POST'
ID_KEY = 'metabrainz_user_id'
DEFAULT_SCOPE = ['profile', 'email']
SCOPE_SEPARATOR = ' '
REDIRECT_STATE = False
EXTRA_DATA = [
('metabrainz_user_id', 'id'),
('expires_in', 'expires'),
]

def get_user_details(self, response):
"""Return user details from MusicBrainz account"""
return {'username': response.get('sub'),
'email': response.get('email') or '',
'first_name': response.get('sub')}

def user_data(self, access_token, *args, **kwargs):
"""Loads user data from service"""
return self.get_json('https://musicbrainz.org/oauth2/userinfo', params={
'access_token': access_token
})
28 changes: 28 additions & 0 deletions social_core/tests/backends/test_musicbrainz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json

from httpretty import HTTPretty

from ...exceptions import AuthFailed
from .oauth import OAuth2Test


class MusicBrainzAuth2Test(OAuth2Test):
backend_path = 'social_core.backends.musicbrainz.MusicBrainzOAuth2'
user_data_url = 'https://musicbrainz.org/oauth2/userinfo'
expected_username = 'foobar'
access_token_body = json.dumps({
'access_token': 'GjtKfJS6G4lupbQcCOiTKo4HcLXUgI1p',
'expires_in': 3600,
'token_type': 'Bearer',
'refresh_token': 'GjSCBBjp4fnbE0AKo3uFu9qq9K2fFm4u'
})
user_data_body = json.dumps({
'sub': 'foobar',
'email': 'foo@bar.com',
})

def test_login(self):
self.do_login()

def test_partial_pipeline(self):
self.do_partial_pipeline()

0 comments on commit f27461d

Please sign in to comment.