-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Samir Musali
committed
Jan 17, 2024
1 parent
941f05e
commit 567c7e7
Showing
8 changed files
with
378 additions
and
171 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""GitLab Type tests""" | ||
from django.test import TestCase | ||
|
||
from authentik.sources.oauth.models import OAuthSource | ||
from authentik.sources.oauth.types.gitlab import GitLabOAuthCallback | ||
|
||
GITLAB_USER = { | ||
"username": "dev_gitlab", | ||
"email": "dev@gitlab.com", | ||
"name": "Dev", | ||
"surname": "Gitlab", | ||
} | ||
|
||
|
||
class TestTypeGitLab(TestCase): | ||
"""OAuth Source tests for GitLab""" | ||
|
||
def setUp(self): | ||
self.source = OAuthSource.objects.create( | ||
name="gitlab_test", | ||
slug="gitlab_test", | ||
provider_type="gitlab", | ||
) | ||
|
||
def test_enroll_context(self): | ||
"""Test GitLab Enrollment context""" | ||
ak_context = GitLabOAuthCallback().get_user_enroll_context(GITLAB_USER) | ||
self.assertEqual(ak_context["username"], GITLAB_USER["username"]) | ||
self.assertEqual(ak_context["email"], GITLAB_USER["email"]) | ||
self.assertEqual(ak_context["name"], f"{GITLAB_USER['name']} {GITLAB_USER['surname']}") |
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,49 @@ | ||
"""GitLab OAuth Views""" | ||
from typing import Any | ||
|
||
from authentik.sources.oauth.clients.oauth2 import UserprofileHeaderAuthClient | ||
from authentik.sources.oauth.models import OAuthSource | ||
from authentik.sources.oauth.types.registry import SourceType, registry | ||
from authentik.sources.oauth.views.callback import OAuthCallback | ||
from authentik.sources.oauth.views.redirect import OAuthRedirect | ||
|
||
|
||
class GitLabOAuthRedirect(OAuthRedirect): | ||
"""GitLab OAuth2 Redirect""" | ||
|
||
def get_additional_parameters(self, source: OAuthSource): # pragma: no cover | ||
return { | ||
"scope": ["read_user", "openid", "profile", "email"], | ||
} | ||
|
||
|
||
class GitLabOAuthCallback(OAuthCallback): | ||
"""GitLab OAuth2 Callback""" | ||
|
||
client_class: UserprofileHeaderAuthClient | ||
|
||
def get_user_enroll_context( | ||
self, | ||
info: dict[str, Any], | ||
) -> dict[str, Any]: | ||
return { | ||
"username": info.get("username"), | ||
"email": info.get("email"), | ||
"name": f"{info.get('name')} {info.get('surname')}", | ||
} | ||
|
||
|
||
@registry.register() | ||
class GitLabType(SourceType): | ||
"""GitLab Type definition""" | ||
|
||
callback_view = GitLabOAuthCallback | ||
redirect_view = GitLabOAuthRedirect | ||
verbose_name = "GitLab" | ||
name = "gitlab" | ||
|
||
urls_customizable = True | ||
|
||
authorization_url = "https://gitlab.com/oauth/authorize" | ||
access_token_url = "https://gitlab.com/oauth/token" # nosec | ||
profile_url = "https://gitlab.com/api/v4/user" |
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 |
---|---|---|
|
@@ -5427,6 +5427,7 @@ | |
"discord", | ||
"facebook", | ||
"github", | ||
"gitlab", | ||
"google", | ||
"mailcow", | ||
"okta", | ||
|
Oops, something went wrong.