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
|
import pytest
from flask import json
from authlib.oauth2.rfc6749.grants import ClientCredentialsGrant
from authlib.oauth2.rfc7523 import JWTBearerClientAssertion
from authlib.oauth2.rfc7523 import client_secret_jwt_sign
from authlib.oauth2.rfc7523 import private_key_jwt_sign
from tests.util import read_file_path
@pytest.fixture(autouse=True)
def server(server):
class JWTClientCredentialsGrant(ClientCredentialsGrant):
TOKEN_ENDPOINT_AUTH_METHODS = [
JWTBearerClientAssertion.CLIENT_AUTH_METHOD,
]
server.register_grant(JWTClientCredentialsGrant)
return server
@pytest.fixture(autouse=True)
def client(client, db):
client.set_client_metadata(
{
"scope": "profile",
"redirect_uris": ["https://client.test/authorized"],
"grant_types": ["client_credentials"],
"token_endpoint_auth_method": JWTBearerClientAssertion.CLIENT_AUTH_METHOD,
}
)
db.session.add(client)
db.session.commit()
return client
def register_jwt_client_auth(server, validate_jti=True):
class JWTClientAuth(JWTBearerClientAssertion):
def validate_jti(self, claims, jti):
return True
def resolve_client_public_key(self, client, headers):
if headers["alg"] == "RS256":
return read_file_path("jwk_public.json")
return client.client_secret
server.register_client_auth_method(
JWTClientAuth.CLIENT_AUTH_METHOD,
JWTClientAuth("https://provider.test/oauth/token", validate_jti),
)
def test_invalid_client(test_client, server):
register_jwt_client_auth(server)
rv = test_client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
},
)
resp = json.loads(rv.data)
assert resp["error"] == "invalid_client"
def test_invalid_jwt(test_client, server):
register_jwt_client_auth(server)
rv = test_client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": client_secret_jwt_sign(
client_secret="invalid-secret",
client_id="client-id",
token_endpoint="https://provider.test/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert resp["error"] == "invalid_client"
def test_not_found_client(test_client, server):
register_jwt_client_auth(server)
rv = test_client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": client_secret_jwt_sign(
client_secret="client-secret",
client_id="invalid-client",
token_endpoint="https://provider.test/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert resp["error"] == "invalid_client"
def test_not_supported_auth_method(test_client, server, client, db):
register_jwt_client_auth(server)
client.set_client_metadata(
{
"scope": "profile",
"redirect_uris": ["https://client.test/authorized"],
"grant_types": ["client_credentials"],
"token_endpoint_auth_method": "invalid",
}
)
db.session.add(client)
db.session.commit()
rv = test_client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": client_secret_jwt_sign(
client_secret="client-secret",
client_id="client-id",
token_endpoint="https://provider.test/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert resp["error"] == "invalid_client"
def test_client_secret_jwt(test_client, server):
register_jwt_client_auth(server)
rv = test_client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": client_secret_jwt_sign(
client_secret="client-secret",
client_id="client-id",
token_endpoint="https://provider.test/oauth/token",
claims={"jti": "nonce"},
),
},
)
resp = json.loads(rv.data)
assert "access_token" in resp
def test_private_key_jwt(test_client, server):
register_jwt_client_auth(server)
rv = test_client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": private_key_jwt_sign(
private_key=read_file_path("jwk_private.json"),
client_id="client-id",
token_endpoint="https://provider.test/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert "access_token" in resp
def test_not_validate_jti(test_client, server):
register_jwt_client_auth(server, validate_jti=False)
rv = test_client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": client_secret_jwt_sign(
client_secret="client-secret",
client_id="client-id",
token_endpoint="https://provider.test/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert "access_token" in resp
|