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
|
from flask import json
from authlib.common.urls import url_decode
from authlib.common.urls import urlparse
from authlib.jose import jwt
from authlib.oauth2.rfc6749.grants import (
AuthorizationCodeGrant as _AuthorizationCodeGrant,
)
from authlib.oidc.core import HybridIDToken
from authlib.oidc.core.grants import OpenIDCode as _OpenIDCode
from authlib.oidc.core.grants import OpenIDHybridGrant as _OpenIDHybridGrant
from .models import Client
from .models import CodeGrantMixin
from .models import User
from .models import db
from .models import exists_nonce
from .models import save_authorization_code
from .oauth2_server import TestCase
from .oauth2_server import create_authorization_server
JWT_CONFIG = {"iss": "Authlib", "key": "secret", "alg": "HS256", "exp": 3600}
class AuthorizationCodeGrant(CodeGrantMixin, _AuthorizationCodeGrant):
def save_authorization_code(self, code, request):
return save_authorization_code(code, request)
class OpenIDCode(_OpenIDCode):
def get_jwt_config(self, grant):
return dict(JWT_CONFIG)
def exists_nonce(self, nonce, request):
return exists_nonce(nonce, request)
def generate_user_info(self, user, scopes):
return user.generate_user_info(scopes)
class OpenIDHybridGrant(_OpenIDHybridGrant):
def save_authorization_code(self, code, request):
return save_authorization_code(code, request)
def get_jwt_config(self):
return dict(JWT_CONFIG)
def exists_nonce(self, nonce, request):
return exists_nonce(nonce, request)
def generate_user_info(self, user, scopes):
return user.generate_user_info(scopes)
class OpenIDCodeTest(TestCase):
def prepare_data(self):
server = create_authorization_server(self.app)
server.register_grant(OpenIDHybridGrant)
server.register_grant(AuthorizationCodeGrant, [OpenIDCode()])
user = User(username="foo")
db.session.add(user)
db.session.commit()
client = Client(
user_id=user.id,
client_id="hybrid-client",
client_secret="hybrid-secret",
)
client.set_client_metadata(
{
"redirect_uris": ["https://a.b"],
"scope": "openid profile address",
"response_types": [
"code id_token",
"code token",
"code id_token token",
],
"grant_types": ["authorization_code"],
}
)
db.session.add(client)
db.session.commit()
def validate_claims(self, id_token, params):
claims = jwt.decode(
id_token, "secret", claims_cls=HybridIDToken, claims_params=params
)
claims.validate()
def test_invalid_client_id(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"response_type": "code token",
"state": "bar",
"nonce": "abc",
"scope": "openid profile",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
resp = json.loads(rv.data)
assert resp["error"] == "invalid_client"
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "invalid-client",
"response_type": "code token",
"state": "bar",
"nonce": "abc",
"scope": "openid profile",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
resp = json.loads(rv.data)
assert resp["error"] == "invalid_client"
def test_require_nonce(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "hybrid-client",
"response_type": "code token",
"scope": "openid profile",
"state": "bar",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
assert "error=invalid_request" in rv.location
assert "nonce" in rv.location
def test_invalid_response_type(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "hybrid-client",
"response_type": "code id_token invalid",
"state": "bar",
"nonce": "abc",
"scope": "profile",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
params = dict(url_decode(urlparse.urlparse(rv.location).query))
assert params["error"] == "unsupported_response_type"
def test_invalid_scope(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "hybrid-client",
"response_type": "code id_token",
"state": "bar",
"nonce": "abc",
"scope": "profile",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
assert "error=invalid_scope" in rv.location
def test_access_denied(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "hybrid-client",
"response_type": "code token",
"state": "bar",
"nonce": "abc",
"scope": "openid profile",
"redirect_uri": "https://a.b",
},
)
assert "error=access_denied" in rv.location
def test_code_access_token(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "hybrid-client",
"response_type": "code token",
"state": "bar",
"nonce": "abc",
"scope": "openid profile",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
assert "code=" in rv.location
assert "access_token=" in rv.location
assert "id_token=" not in rv.location
params = dict(url_decode(urlparse.urlparse(rv.location).fragment))
assert params["state"] == "bar"
code = params["code"]
headers = self.create_basic_header("hybrid-client", "hybrid-secret")
rv = self.client.post(
"/oauth/token",
data={
"grant_type": "authorization_code",
"redirect_uri": "https://a.b",
"code": code,
},
headers=headers,
)
resp = json.loads(rv.data)
assert "access_token" in resp
assert "id_token" in resp
def test_code_id_token(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "hybrid-client",
"response_type": "code id_token",
"state": "bar",
"nonce": "abc",
"scope": "openid profile",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
assert "code=" in rv.location
assert "id_token=" in rv.location
assert "access_token=" not in rv.location
params = dict(url_decode(urlparse.urlparse(rv.location).fragment))
assert params["state"] == "bar"
params["nonce"] = "abc"
params["client_id"] = "hybrid-client"
self.validate_claims(params["id_token"], params)
code = params["code"]
headers = self.create_basic_header("hybrid-client", "hybrid-secret")
rv = self.client.post(
"/oauth/token",
data={
"grant_type": "authorization_code",
"redirect_uri": "https://a.b",
"code": code,
},
headers=headers,
)
resp = json.loads(rv.data)
assert "access_token" in resp
assert "id_token" in resp
def test_code_id_token_access_token(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "hybrid-client",
"response_type": "code id_token token",
"state": "bar",
"nonce": "abc",
"scope": "openid profile",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
assert "code=" in rv.location
assert "id_token=" in rv.location
assert "access_token=" in rv.location
params = dict(url_decode(urlparse.urlparse(rv.location).fragment))
assert params["state"] == "bar"
self.validate_claims(params["id_token"], params)
code = params["code"]
headers = self.create_basic_header("hybrid-client", "hybrid-secret")
rv = self.client.post(
"/oauth/token",
data={
"grant_type": "authorization_code",
"redirect_uri": "https://a.b",
"code": code,
},
headers=headers,
)
resp = json.loads(rv.data)
assert "access_token" in resp
assert "id_token" in resp
def test_response_mode_query(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "hybrid-client",
"response_type": "code id_token token",
"response_mode": "query",
"state": "bar",
"nonce": "abc",
"scope": "openid profile",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
assert "code=" in rv.location
assert "id_token=" in rv.location
assert "access_token=" in rv.location
params = dict(url_decode(urlparse.urlparse(rv.location).query))
assert params["state"] == "bar"
def test_response_mode_form_post(self):
self.prepare_data()
rv = self.client.post(
"/oauth/authorize",
data={
"client_id": "hybrid-client",
"response_type": "code id_token token",
"response_mode": "form_post",
"state": "bar",
"nonce": "abc",
"scope": "openid profile",
"redirect_uri": "https://a.b",
"user_id": "1",
},
)
assert b'name="code"' in rv.data
assert b'name="id_token"' in rv.data
assert b'name="access_token"' in rv.data
|