-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1231 from saily/gitlab-provider
Gitlab provider
- Loading branch information
Showing
9 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# -*- coding: utf-8 -*- | ||
from allauth.socialaccount import providers | ||
from allauth.socialaccount.providers.base import ProviderAccount | ||
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider | ||
|
||
|
||
class GitLabAccount(ProviderAccount): | ||
|
||
def get_profile_url(self): | ||
return self.account.extra_data.get('html_url') | ||
|
||
def get_avatar_url(self): | ||
return self.account.extra_data.get('avatar_url') | ||
|
||
def to_str(self): | ||
dflt = super(GitLabAccount, self).to_str() | ||
return self.account.extra_data.get('name', dflt) | ||
|
||
|
||
class GitLabProvider(OAuth2Provider): | ||
id = 'gitlab' | ||
name = 'GitLab' | ||
package = 'allauth.socialaccount.providers.gitlab' | ||
account_class = GitLabAccount | ||
|
||
def extract_uid(self, data): | ||
return str(data['id']) | ||
|
||
def extract_common_fields(self, data): | ||
return dict( | ||
email=data.get('email'), | ||
username=data.get('username'), | ||
name=data.get('name'), | ||
) | ||
|
||
|
||
providers.registry.register(GitLabProvider) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# -*- coding: utf-8 -*- | ||
from allauth.socialaccount.providers.gitlab.provider import GitLabProvider | ||
from allauth.socialaccount.tests import OAuth2TestsMixin | ||
from allauth.tests import MockedResponse | ||
from allauth.tests import TestCase | ||
|
||
|
||
class GitLabTests(OAuth2TestsMixin, TestCase): | ||
provider_id = GitLabProvider.id | ||
|
||
def get_mocked_response(self): | ||
return MockedResponse(200, """ | ||
{ | ||
"avatar_url": "https://secure.gravatar.com/avatar/123", | ||
"bio": "", | ||
"can_create_group": "true", | ||
"can_create_project": "true", | ||
"color_scheme_id": 2, | ||
"created_at": "2015-12-14T23:40:33+0100", | ||
"current_sign_in_at": "2015-12-14T23:40:33+0100", | ||
"email": "[email protected]", | ||
"id": 2, | ||
"identities": [], | ||
"is_admin": "false", | ||
"linkedin": "", | ||
"name": "Mr Bob", | ||
"private_token": "123", | ||
"projects_limit": 10, | ||
"skype": "mr.bob", | ||
"state": "active", | ||
"theme_id": 6, | ||
"twitter": "mrbob", | ||
"two_factor_enabled": "false", | ||
"username": "mr.bob", | ||
"web_url": "https://your.gitlab.server.tld/u/mr.bob", | ||
"website_url": "http://mr.bob" | ||
} | ||
""") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
from allauth.socialaccount.providers.gitlab.provider import GitLabProvider | ||
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns | ||
|
||
|
||
urlpatterns = default_urlpatterns(GitLabProvider) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
from allauth.socialaccount import app_settings | ||
from allauth.socialaccount.providers.gitlab.provider import GitLabProvider | ||
from allauth.socialaccount.providers.oauth2.views import OAuth2Adapter | ||
from allauth.socialaccount.providers.oauth2.views import OAuth2CallbackView | ||
from allauth.socialaccount.providers.oauth2.views import OAuth2LoginView | ||
|
||
import requests | ||
|
||
|
||
class GitLabOAuth2Adapter(OAuth2Adapter): | ||
provider_id = GitLabProvider.id | ||
provider_default_url = 'https://gitlab.com' | ||
provider_api_version = 'v3' | ||
|
||
settings = app_settings.PROVIDERS.get(provider_id, {}) | ||
provider_base_url = settings.get('GITLAB_URL', provider_default_url) | ||
|
||
access_token_url = '{0}/oauth/token'.format(provider_base_url) | ||
authorize_url = '{0}/oauth/authorize'.format(provider_base_url) | ||
profile_url = '{0}/api/{1}/user'.format( | ||
provider_base_url, provider_api_version | ||
) | ||
|
||
def complete_login(self, request, app, token, response): | ||
extra_data = requests.get(self.profile_url, params={ | ||
'access_token': token.token | ||
}) | ||
|
||
return self.get_provider().sociallogin_from_response( | ||
request, | ||
extra_data.json() | ||
) | ||
|
||
oauth2_login = OAuth2LoginView.adapter_view(GitLabOAuth2Adapter) | ||
oauth2_callback = OAuth2CallbackView.adapter_view(GitLabOAuth2Adapter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters