File: test_authorization_code_iss_parameter.py

package info (click to toggle)
python-authlib 1.6.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,024 kB
  • sloc: python: 27,412; makefile: 53; sh: 14
file content (85 lines) | stat: -rw-r--r-- 2,669 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pytest

from authlib.oauth2.rfc6749.grants import (
    AuthorizationCodeGrant as _AuthorizationCodeGrant,
)
from authlib.oauth2.rfc9207 import IssuerParameter as _IssuerParameter

from .models import CodeGrantMixin
from .models import save_authorization_code

authorize_url = "/oauth/authorize?response_type=code&client_id=client-id"


class AuthorizationCodeGrant(CodeGrantMixin, _AuthorizationCodeGrant):
    TOKEN_ENDPOINT_AUTH_METHODS = ["client_secret_basic", "client_secret_post", "none"]

    def save_authorization_code(self, code, request):
        return save_authorization_code(code, request)


class IssuerParameter(_IssuerParameter):
    def get_issuer(self) -> str:
        return "https://auth.test"


@pytest.fixture(autouse=True)
def server(server):
    server.register_grant(AuthorizationCodeGrant)
    return server


@pytest.fixture(autouse=True)
def client(client, db):
    client.set_client_metadata(
        {
            "redirect_uris": ["https://client.test"],
            "scope": "profile address",
            "token_endpoint_auth_method": "client_secret_basic",
            "response_types": ["code"],
            "grant_types": ["authorization_code"],
        }
    )
    db.session.add(client)
    db.session.commit()
    return client


def test_rfc9207_enabled_success(test_client, server):
    """Check that when RFC9207 is implemented,
    the authorization response has an ``iss`` parameter."""

    server.register_extension(IssuerParameter())
    url = authorize_url + "&state=bar"
    rv = test_client.post(url, data={"user_id": "1"})
    assert "iss=https%3A%2F%2Fauth.test" in rv.location


def test_rfc9207_disabled_success_no_iss(test_client):
    """Check that when RFC9207 is not implemented,
    the authorization response contains no ``iss`` parameter."""

    url = authorize_url + "&state=bar"
    rv = test_client.post(url, data={"user_id": "1"})
    assert "iss=" not in rv.location


def test_rfc9207_enabled_error(test_client, server):
    """Check that when RFC9207 is implemented,
    the authorization response has an ``iss`` parameter,
    even when an error is returned."""

    server.register_extension(IssuerParameter())
    rv = test_client.post(authorize_url)
    assert "error=access_denied" in rv.location
    assert "iss=https%3A%2F%2Fauth.test" in rv.location


def test_rfc9207_disbled_error_no_iss(test_client):
    """Check that when RFC9207 is not implemented,
    the authorization response contains no ``iss`` parameter,
    even when an error is returned."""

    rv = test_client.post(authorize_url)
    assert "error=access_denied" in rv.location
    assert "iss=" not in rv.location