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
|
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
from .models import Client
from .models import User
from .models import db
from .oauth2_server import TestCase
from .oauth2_server import create_authorization_server
class JWTClientCredentialsGrant(ClientCredentialsGrant):
TOKEN_ENDPOINT_AUTH_METHODS = [
JWTBearerClientAssertion.CLIENT_AUTH_METHOD,
]
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
class ClientCredentialsTest(TestCase):
def prepare_data(self, auth_method, validate_jti=True):
server = create_authorization_server(self.app)
server.register_grant(JWTClientCredentialsGrant)
server.register_client_auth_method(
JWTClientAuth.CLIENT_AUTH_METHOD,
JWTClientAuth("https://localhost/oauth/token", validate_jti),
)
user = User(username="foo")
db.session.add(user)
db.session.commit()
client = Client(
user_id=user.id,
client_id="credential-client",
client_secret="credential-secret",
)
client.set_client_metadata(
{
"scope": "profile",
"redirect_uris": ["http://localhost/authorized"],
"grant_types": ["client_credentials"],
"token_endpoint_auth_method": auth_method,
}
)
db.session.add(client)
db.session.commit()
def test_invalid_client(self):
self.prepare_data(JWTBearerClientAssertion.CLIENT_AUTH_METHOD)
rv = self.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(self):
self.prepare_data(JWTBearerClientAssertion.CLIENT_AUTH_METHOD)
rv = self.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="credential-client",
token_endpoint="https://localhost/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert resp["error"] == "invalid_client"
def test_not_found_client(self):
self.prepare_data(JWTBearerClientAssertion.CLIENT_AUTH_METHOD)
rv = self.client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": client_secret_jwt_sign(
client_secret="credential-secret",
client_id="invalid-client",
token_endpoint="https://localhost/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert resp["error"] == "invalid_client"
def test_not_supported_auth_method(self):
self.prepare_data("invalid")
rv = self.client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": client_secret_jwt_sign(
client_secret="credential-secret",
client_id="credential-client",
token_endpoint="https://localhost/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert resp["error"] == "invalid_client"
def test_client_secret_jwt(self):
self.prepare_data(JWTBearerClientAssertion.CLIENT_AUTH_METHOD)
rv = self.client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": client_secret_jwt_sign(
client_secret="credential-secret",
client_id="credential-client",
token_endpoint="https://localhost/oauth/token",
claims={"jti": "nonce"},
),
},
)
resp = json.loads(rv.data)
assert "access_token" in resp
def test_private_key_jwt(self):
self.prepare_data(JWTBearerClientAssertion.CLIENT_AUTH_METHOD)
rv = self.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="credential-client",
token_endpoint="https://localhost/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert "access_token" in resp
def test_not_validate_jti(self):
self.prepare_data(JWTBearerClientAssertion.CLIENT_AUTH_METHOD, False)
rv = self.client.post(
"/oauth/token",
data={
"grant_type": "client_credentials",
"client_assertion_type": JWTBearerClientAssertion.CLIENT_ASSERTION_TYPE,
"client_assertion": client_secret_jwt_sign(
client_secret="credential-secret",
client_id="credential-client",
token_endpoint="https://localhost/oauth/token",
),
},
)
resp = json.loads(rv.data)
assert "access_token" in resp
|