Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sources/oauth: add gitlab type [AUTH-323] #8195

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions authentik/sources/oauth/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"authentik.sources.oauth.types.discord",
"authentik.sources.oauth.types.facebook",
"authentik.sources.oauth.types.github",
"authentik.sources.oauth.types.gitlab",
"authentik.sources.oauth.types.google",
"authentik.sources.oauth.types.mailcow",
"authentik.sources.oauth.types.oidc",
Expand Down
9 changes: 9 additions & 0 deletions authentik/sources/oauth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ class Meta:
verbose_name_plural = _("GitHub OAuth Sources")


class GitLabOAuthSource(OAuthSource):
"""Social Login using GitLab.com or a GitLab Instance."""

class Meta:
abstract = True
verbose_name = _("GitLab OAuth Source")
verbose_name_plural = _("GitLab OAuth Sources")


class TwitchOAuthSource(OAuthSource):
"""Social Login using Twitch."""

Expand Down
30 changes: 30 additions & 0 deletions authentik/sources/oauth/tests/test_type_gitlab.py
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 = {
"preferred_username": "dev_gitlab",
"email": "dev@gitlab.com",
"name": "Dev",
}


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["preferred_username"])
self.assertEqual(ak_context["email"], GITLAB_USER["email"])
self.assertEqual(ak_context["name"], GITLAB_USER["name"])
54 changes: 54 additions & 0 deletions authentik/sources/oauth/types/gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
GitLab OAuth Views

See https://docs.gitlab.com/ee/integration/oauth_provider.html
and https://docs.gitlab.com/ee/integration/openid_connect_provider.html
"""

from typing import Any

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):
return {
"scope": ["read_user", "openid", "profile", "email"],
}


class GitLabOAuthCallback(OAuthCallback):
"""GitLab OAuth2 Callback"""

def get_user_enroll_context(
self,
info: dict[str, Any],
) -> dict[str, Any]:
return {
"username": info.get("preferred_username"),
"email": info.get("email"),
"name": info.get("name"),
}


@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/oauth/userinfo"
oidc_well_known_url = "https://gitlab.com/.well-known/openid-configuration"
oidc_jwks_url = "https://gitlab.com/oauth/discovery/keys"
1 change: 1 addition & 0 deletions blueprints/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4444,6 +4444,7 @@
"discord",
"facebook",
"github",
"gitlab",
"google",
"mailcow",
"okta",
Expand Down
4 changes: 3 additions & 1 deletion schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29699,7 +29699,7 @@ components:
* `authentik.events` - authentik Events
AppleChallengeResponseRequest:
type: object
description: Pseudo class for plex response
description: Pseudo class for apple response
properties:
component:
type: string
Expand Down Expand Up @@ -41406,6 +41406,7 @@ components:
- discord
- facebook
- github
- gitlab
- google
- mailcow
- okta
Expand All @@ -41421,6 +41422,7 @@ components:
* `discord` - Discord
* `facebook` - Facebook
* `github` - GitHub
* `gitlab` - GitLab
* `google` - Google
* `mailcow` - Mailcow
* `okta` - Okta
Expand Down
2 changes: 2 additions & 0 deletions web/src/admin/sources/oauth/OAuthSourceViewPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export function ProviderToLabel(provider?: ProviderTypeEnum): string {
return "Facebook";
case ProviderTypeEnum.Github:
return "GitHub";
case ProviderTypeEnum.Gitlab:
return "GitLab";
case ProviderTypeEnum.Google:
return "Google";
case ProviderTypeEnum.Mailcow:
Expand Down
Loading