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

🐛 (backend) gitlab oicd userinfo endpoint #232

Merged
merged 1 commit into from
Sep 23, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to

## [Unreleased]

## Fixed

- 🐛 (backend) gitlab oicd userinfo endpoint #232


## [1.4.0] - 2024-09-17

Expand Down
12 changes: 11 additions & 1 deletion src/backend/core/authentication/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ def get_userinfo(self, access_token, id_token, payload):
proxies=self.get_settings("OIDC_PROXY", None),
)
user_response.raise_for_status()
userinfo = self.verify_token(user_response.text)

try:
userinfo = user_response.json()
except ValueError:
try:
userinfo = self.verify_token(user_response.text)
except Exception as e:
raise SuspiciousOperation(
_("Invalid response format or token verification failed")
) from e

return userinfo

def get_or_create_user(self, access_token, id_token, payload):
Expand Down
67 changes: 66 additions & 1 deletion src/backend/core/tests/authentication/test_backends.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""Unit tests for the Authentication Backends."""

import re

from django.core.exceptions import SuspiciousOperation
from django.test.utils import override_settings

import pytest
import responses

from core import models
from core.authentication.backends import OIDCAuthenticationBackend
Expand Down Expand Up @@ -81,7 +85,7 @@ def get_userinfo_mocked(*args):
assert models.User.objects.count() == 1


def test_models_oidc_user_getter_invalid_token(django_assert_num_queries, monkeypatch):
def test_authentication_getter_invalid_token(django_assert_num_queries, monkeypatch):
"""The user's info doesn't contain a sub."""
klass = OIDCAuthenticationBackend()

Expand All @@ -102,3 +106,64 @@ def get_userinfo_mocked(*args):
klass.get_or_create_user(access_token="test-token", id_token=None, payload=None)

assert models.User.objects.exists() is False


@override_settings(OIDC_OP_USER_ENDPOINT="http://oidc.endpoint.test/userinfo")
@responses.activate
def test_authentication_get_userinfo_json_response():
"""Test get_userinfo method with a JSON response."""

responses.add(
responses.GET,
re.compile(r".*/userinfo"),
json={"name": "John Doe", "email": "john.doe@example.com"},
status=200,
)

oidc_backend = OIDCAuthenticationBackend()
result = oidc_backend.get_userinfo("fake_access_token", None, None)

assert result["name"] == "John Doe"
assert result["email"] == "john.doe@example.com"


@override_settings(OIDC_OP_USER_ENDPOINT="http://oidc.endpoint.test/userinfo")
@responses.activate
def test_authentication_get_userinfo_token_response(monkeypatch):
"""Test get_userinfo method with a token response."""

responses.add(
responses.GET, re.compile(r".*/userinfo"), body="fake.jwt.token", status=200
)

def mock_verify_token(self, token): # pylint: disable=unused-argument
return {"name": "Jane Doe", "email": "jane.doe@example.com"}

monkeypatch.setattr(OIDCAuthenticationBackend, "verify_token", mock_verify_token)

oidc_backend = OIDCAuthenticationBackend()
result = oidc_backend.get_userinfo("fake_access_token", None, None)

assert result["name"] == "Jane Doe"
assert result["email"] == "jane.doe@example.com"


@override_settings(OIDC_OP_USER_ENDPOINT="http://oidc.endpoint.test/userinfo")
@responses.activate
def test_authentication_get_userinfo_invalid_response():
"""
Test get_userinfo method with an invalid JWT response that
causes verify_token to raise an error.
"""

responses.add(
responses.GET, re.compile(r".*/userinfo"), body="fake.jwt.token", status=200
)

oidc_backend = OIDCAuthenticationBackend()

with pytest.raises(
SuspiciousOperation,
match="Invalid response format or token verification failed",
):
oidc_backend.get_userinfo("fake_access_token", None, None)
Loading