File: test_client_configuration_endpoint.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 (495 lines) | stat: -rw-r--r-- 17,764 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import pytest
from flask import json

from authlib.oauth2.rfc7592 import (
    ClientConfigurationEndpoint as _ClientConfigurationEndpoint,
)

from .models import Client
from .models import Token
from .models import db


class ClientConfigurationEndpoint(_ClientConfigurationEndpoint):
    software_statement_alg_values_supported = ["RS256"]

    def authenticate_token(self, request):
        auth_header = request.headers.get("Authorization")
        if auth_header:
            access_token = auth_header.split()[1]
            return Token.query.filter_by(access_token=access_token).first()

    def update_client(self, client, client_metadata, request):
        client.set_client_metadata(client_metadata)
        db.session.add(client)
        db.session.commit()
        return client

    def authenticate_client(self, request):
        client_id = request.uri.split("/")[-1]
        return Client.query.filter_by(client_id=client_id).first()

    def revoke_access_token(self, request, token):
        token.revoked = True
        db.session.add(token)
        db.session.commit()

    def check_permission(self, client, request):
        client_id = request.uri.split("/")[-1]
        return client_id != "unauthorized_client_id"

    def delete_client(self, client, request):
        db.session.delete(client)
        db.session.commit()

    def generate_client_registration_info(self, client, request):
        return {
            "registration_client_uri": request.uri,
            "registration_access_token": request.headers["Authorization"].split(" ")[1],
        }


@pytest.fixture
def metadata():
    return {}


@pytest.fixture(autouse=True)
def server(server, app, metadata):
    @app.route("/configure_client/<client_id>", methods=["PUT", "GET", "DELETE"])
    def configure_client(client_id):
        return server.create_endpoint_response(
            ClientConfigurationEndpoint.ENDPOINT_NAME
        )

    class MyClientConfiguration(ClientConfigurationEndpoint):
        def get_server_metadata(test_client):
            return metadata

    server.register_endpoint(MyClientConfiguration)
    return server


@pytest.fixture(autouse=True)
def client(client, db):
    client.set_client_metadata(
        {
            "client_name": "Authlib",
            "scope": "openid profile",
        }
    )
    db.session.add(client)
    db.session.commit()
    return client


def test_read_client(test_client, client, token):
    assert client.client_name == "Authlib"
    headers = {"Authorization": f"bearer {token.access_token}"}
    rv = test_client.get("/configure_client/client-id", headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 200
    assert resp["client_id"] == client.client_id
    assert resp["client_name"] == "Authlib"
    assert (
        resp["registration_client_uri"] == "http://localhost/configure_client/client-id"
    )
    assert resp["registration_access_token"] == token.access_token


def test_read_access_denied(test_client):
    rv = test_client.get("/configure_client/client-id")
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "access_denied"

    headers = {"Authorization": "bearer invalid_token"}
    rv = test_client.get("/configure_client/client-id", headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "access_denied"

    headers = {"Authorization": "bearer unauthorized_token"}
    rv = test_client.get(
        "/configure_client/client-id",
        json={"client_id": "client-id", "client_name": "new client_name"},
        headers=headers,
    )
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "access_denied"


def test_read_invalid_client(test_client, token):
    # If the client does not exist on this server, the server MUST respond
    # with HTTP 401 Unauthorized, and the registration access token used to
    # make this request SHOULD be immediately revoked.

    headers = {"Authorization": f"bearer {token.access_token}"}
    rv = test_client.get("/configure_client/invalid_client_id", headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 401
    assert resp["error"] == "invalid_client"


def test_read_unauthorized_client(test_client, token):
    # If the client does not have permission to read its record, the server
    # MUST return an HTTP 403 Forbidden.

    client = Client(
        client_id="unauthorized_client_id",
        client_secret="unauthorized_client_secret",
    )
    db.session.add(client)

    headers = {"Authorization": f"bearer {token.access_token}"}
    rv = test_client.get("/configure_client/unauthorized_client_id", headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 403
    assert resp["error"] == "unauthorized_client"


def test_update_client(test_client, client, token):
    # Valid values of client metadata fields in this request MUST replace,
    # not augment, the values previously associated with this client.
    # Omitted fields MUST be treated as null or empty values by the server,
    # indicating the client's request to delete them from the client's
    # registration.  The authorization server MAY ignore any null or empty
    # value in the request just as any other value.

    assert client.client_name == "Authlib"
    headers = {"Authorization": f"bearer {token.access_token}"}
    body = {
        "client_id": client.client_id,
        "client_name": "NewAuthlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 200
    assert resp["client_id"] == client.client_id
    assert resp["client_name"] == "NewAuthlib"
    assert client.client_name == "NewAuthlib"
    assert client.scope == ""


def test_update_access_denied(test_client):
    rv = test_client.put("/configure_client/client-id", json={})
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "access_denied"

    headers = {"Authorization": "bearer invalid_token"}
    rv = test_client.put("/configure_client/client-id", json={}, headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "access_denied"

    headers = {"Authorization": "bearer unauthorized_token"}
    rv = test_client.put(
        "/configure_client/client-id",
        json={"client_id": "client-id", "client_name": "new client_name"},
        headers=headers,
    )
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "access_denied"


def test_update_invalid_request(test_client, token):
    headers = {"Authorization": f"bearer {token.access_token}"}

    # The client MUST include its 'client_id' field in the request...
    rv = test_client.put("/configure_client/client-id", json={}, headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "invalid_request"

    # ... and it MUST be the same as its currently issued client identifier.
    rv = test_client.put(
        "/configure_client/client-id",
        json={"client_id": "invalid_client_id"},
        headers=headers,
    )
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "invalid_request"

    # The updated client metadata fields request MUST NOT include the
    # 'registration_access_token', 'registration_client_uri',
    # 'client_secret_expires_at', or 'client_id_issued_at' fields
    rv = test_client.put(
        "/configure_client/client-id",
        json={
            "client_id": "client-id",
            "registration_client_uri": "https://client.test",
        },
        headers=headers,
    )
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "invalid_request"

    # If the client includes the 'client_secret' field in the request,
    # the value of this field MUST match the currently issued client
    # secret for that client.
    rv = test_client.put(
        "/configure_client/client-id",
        json={"client_id": "client-id", "client_secret": "invalid_secret"},
        headers=headers,
    )
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "invalid_request"


def test_update_invalid_client(test_client, token):
    # If the client does not exist on this server, the server MUST respond
    # with HTTP 401 Unauthorized, and the registration access token used to
    # make this request SHOULD be immediately revoked.

    headers = {"Authorization": f"bearer {token.access_token}"}
    rv = test_client.put(
        "/configure_client/invalid_client_id",
        json={"client_id": "invalid_client_id", "client_name": "new client_name"},
        headers=headers,
    )
    resp = json.loads(rv.data)
    assert rv.status_code == 401
    assert resp["error"] == "invalid_client"


def test_update_unauthorized_client(test_client, token):
    # If the client does not have permission to read its record, the server
    # MUST return an HTTP 403 Forbidden.

    client = Client(
        client_id="unauthorized_client_id",
        client_secret="unauthorized_client_secret",
    )
    db.session.add(client)

    headers = {"Authorization": f"bearer {token.access_token}"}
    rv = test_client.put(
        "/configure_client/unauthorized_client_id",
        json={
            "client_id": "unauthorized_client_id",
            "client_name": "new client_name",
        },
        headers=headers,
    )
    resp = json.loads(rv.data)
    assert rv.status_code == 403
    assert resp["error"] == "unauthorized_client"


def test_update_invalid_metadata(test_client, metadata, client, token):
    metadata["token_endpoint_auth_methods_supported"] = ["client_secret_basic"]
    headers = {"Authorization": f"bearer {token.access_token}"}

    # For all metadata fields, the authorization server MAY replace any
    # invalid values with suitable default values, and it MUST return any
    # such fields to the client in the response.
    # If the client attempts to set an invalid metadata field and the
    # authorization server does not set a default value, the authorization
    # server responds with an error as described in [RFC7591].

    body = {
        "client_id": client.client_id,
        "client_name": "NewAuthlib",
        "token_endpoint_auth_method": "invalid_auth_method",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "invalid_client_metadata"


def test_update_scopes_supported(test_client, metadata, token):
    metadata["scopes_supported"] = ["profile", "email"]

    headers = {"Authorization": f"bearer {token.access_token}"}
    body = {
        "client_id": "client-id",
        "scope": "profile email",
        "client_name": "Authlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert resp["client_id"] == "client-id"
    assert resp["client_name"] == "Authlib"
    assert resp["scope"] == "profile email"

    headers = {"Authorization": f"bearer {token.access_token}"}
    body = {
        "client_id": "client-id",
        "scope": "",
        "client_name": "Authlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert resp["client_id"] == "client-id"
    assert resp["client_name"] == "Authlib"

    body = {
        "client_id": "client-id",
        "scope": "profile email address",
        "client_name": "Authlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert resp["error"] in "invalid_client_metadata"


def test_update_response_types_supported(test_client, metadata, token):
    metadata["response_types_supported"] = ["code"]

    headers = {"Authorization": f"bearer {token.access_token}"}
    body = {
        "client_id": "client-id",
        "response_types": ["code"],
        "client_name": "Authlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert resp["client_id"] == "client-id"
    assert resp["client_name"] == "Authlib"
    assert resp["response_types"] == ["code"]

    # https://datatracker.ietf.org/doc/html/rfc7592#section-2.2
    # If omitted, the default is that the client will use only the "code"
    # response type.
    body = {"client_id": "client-id", "client_name": "Authlib"}
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert "client_id" in resp
    assert resp["client_name"] == "Authlib"
    assert "response_types" not in resp

    body = {
        "client_id": "client-id",
        "response_types": ["code", "token"],
        "client_name": "Authlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert resp["error"] in "invalid_client_metadata"


def test_update_grant_types_supported(test_client, metadata, token):
    metadata["grant_types_supported"] = ["authorization_code", "password"]

    headers = {"Authorization": f"bearer {token.access_token}"}
    body = {
        "client_id": "client-id",
        "grant_types": ["password"],
        "client_name": "Authlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert resp["client_id"] == "client-id"
    assert resp["client_name"] == "Authlib"
    assert resp["grant_types"] == ["password"]

    # https://datatracker.ietf.org/doc/html/rfc7592#section-2.2
    # If omitted, the default behavior is that the client will use only
    # the "authorization_code" Grant Type.
    body = {"client_id": "client-id", "client_name": "Authlib"}
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert "client_id" in resp
    assert resp["client_name"] == "Authlib"
    assert "grant_types" not in resp

    body = {
        "client_id": "client-id",
        "grant_types": ["client_credentials"],
        "client_name": "Authlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert resp["error"] in "invalid_client_metadata"


def test_update_token_endpoint_auth_methods_supported(test_client, metadata, token):
    metadata["token_endpoint_auth_methods_supported"] = ["client_secret_basic"]

    headers = {"Authorization": f"bearer {token.access_token}"}
    body = {
        "client_id": "client-id",
        "token_endpoint_auth_method": "client_secret_basic",
        "client_name": "Authlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert resp["client_id"] == "client-id"
    assert resp["client_name"] == "Authlib"
    assert resp["token_endpoint_auth_method"] == "client_secret_basic"

    body = {
        "client_id": "client-id",
        "token_endpoint_auth_method": "none",
        "client_name": "Authlib",
    }
    rv = test_client.put("/configure_client/client-id", json=body, headers=headers)
    resp = json.loads(rv.data)
    assert resp["error"] in "invalid_client_metadata"


def test_delete_client(test_client, client, token):
    assert client.client_name == "Authlib"
    headers = {"Authorization": f"bearer {token.access_token}"}
    rv = test_client.delete("/configure_client/client-id", headers=headers)
    assert rv.status_code == 204
    assert not rv.data


def test_delete_access_denied(test_client):
    rv = test_client.delete("/configure_client/client-id")
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "access_denied"

    headers = {"Authorization": "bearer invalid_token"}
    rv = test_client.delete("/configure_client/client-id", headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "access_denied"

    headers = {"Authorization": "bearer unauthorized_token"}
    rv = test_client.delete(
        "/configure_client/client-id",
        json={"client_id": "client-id", "client_name": "new client_name"},
        headers=headers,
    )
    resp = json.loads(rv.data)
    assert rv.status_code == 400
    assert resp["error"] == "access_denied"


def test_delete_invalid_client(test_client, token):
    # If the client does not exist on this server, the server MUST respond
    # with HTTP 401 Unauthorized, and the registration access token used to
    # make this request SHOULD be immediately revoked.

    headers = {"Authorization": f"bearer {token.access_token}"}
    rv = test_client.delete("/configure_client/invalid_client_id", headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 401
    assert resp["error"] == "invalid_client"


def test_delete_unauthorized_client(test_client, token):
    # If the client does not have permission to read its record, the server
    # MUST return an HTTP 403 Forbidden.

    client = Client(
        client_id="unauthorized_client_id",
        client_secret="unauthorized_client_secret",
    )
    db.session.add(client)

    headers = {"Authorization": f"bearer {token.access_token}"}
    rv = test_client.delete("/configure_client/unauthorized_client_id", headers=headers)
    resp = json.loads(rv.data)
    assert rv.status_code == 403
    assert resp["error"] == "unauthorized_client"