Skip to content

Commit

Permalink
Merge pull request #1231 from saily/gitlab-provider
Browse files Browse the repository at this point in the history
Gitlab provider
  • Loading branch information
pennersr committed Dec 28, 2015
2 parents d4b9105 + d679ce0 commit 6bd6139
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Chris Beaven
Chris Davis
Christopher Grebs
Daniel Eriksson
Daniel Widerin
David Evans
David Friedman
Eric Delord
Expand Down
Empty file.
1 change: 1 addition & 0 deletions allauth/socialaccount/providers/gitlab/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your models here.
37 changes: 37 additions & 0 deletions allauth/socialaccount/providers/gitlab/provider.py
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)
38 changes: 38 additions & 0 deletions allauth/socialaccount/providers/gitlab/tests.py
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"
}
""")
6 changes: 6 additions & 0 deletions allauth/socialaccount/providers/gitlab/urls.py
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)
36 changes: 36 additions & 0 deletions allauth/socialaccount/providers/gitlab/views.py
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)
19 changes: 19 additions & 0 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,25 @@ App registration
https://github.com/settings/applications/new


GitLab
------

The GitLab provider works by default with https://gitlab.com. It allows you
to connect to your private GitLab server and use GitLab as an OAuth2
authentication provider as described in GitLab docs here:

http://doc.gitlab.com/ce/integration/oauth_provider.html

Following GitLab settings are available, it unset https://gitlab.com will
be used.

GITLAB_URL:
Override endpoint to request an authorization and access token. For your
private GitLab server you use:

https://your.gitlab.server.tld


Google
------

Expand Down
1 change: 1 addition & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
'allauth.socialaccount.providers.foursquare',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.github',
'allauth.socialaccount.providers.gitlab',
'allauth.socialaccount.providers.hubic',
'allauth.socialaccount.providers.instagram',
'allauth.socialaccount.providers.linkedin',
Expand Down

0 comments on commit 6bd6139

Please sign in to comment.