Skip to content

Commit f105494

Browse files
tony2001simo5
authored andcommitted
Add support for 'scope' claim with multiple scopes
+ add tests
1 parent cabac91 commit f105494

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

jwcrypto/jwt.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ def _check_check_claims(self, check_claims):
479479
self._check_string_claim('iss', check_claims)
480480
self._check_string_claim('sub', check_claims)
481481
self._check_array_or_string_claim('aud', check_claims)
482+
self._check_string_claim('scope', check_claims)
482483
self._check_integer_claim('exp', check_claims)
483484
self._check_integer_claim('nbf', check_claims)
484485
self._check_integer_claim('iat', check_claims)
@@ -556,7 +557,26 @@ def _check_provided_claims(self):
556557
"'%s'" % (name,
557558
claims[name],
558559
value))
560+
elif name == 'scope':
561+
if value is not None:
562+
if not isinstance(claims[name], str):
563+
raise JWTInvalidClaimValue(
564+
"Invalid '%s' value. Scope list has to be "
565+
"a string, got a %s instead: %s" % (
566+
name, type(claims[name]), str(claims[name])))
559567

568+
found = False
569+
got_scopes = claims[name].split()
570+
for s in got_scopes:
571+
if s == value:
572+
found = True
573+
break
574+
575+
if not found:
576+
raise JWTInvalidClaimValue(
577+
"Invalid '%s' value. Scope list '%s' does not "
578+
"contain the required scope '%s'" % (
579+
name, claims[name], value))
560580
else:
561581
if value is not None and value != claims[name]:
562582
raise JWTInvalidClaimValue(

jwcrypto/tests.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,68 @@ def test_unexpected(self):
19771977
jwt.JWT(jwt=enctok, key=key)
19781978
key.key_ops = None
19791979

1980+
def test_claims_scope(self):
1981+
key = jwk.JWK().generate(kty='oct')
1982+
1983+
string_header = '{"alg":"HS256"}'
1984+
1985+
# no scopes provided
1986+
claims = '{}'
1987+
t = jwt.JWT(string_header, claims)
1988+
t.make_signed_token(key)
1989+
token = t.serialize()
1990+
self.assertRaises(jwt.JWTMissingClaim, jwt.JWT, jwt=token,
1991+
key=key, check_claims={"scope": "read"})
1992+
1993+
# non-string scopes
1994+
claims = '{"scope": 12345}'
1995+
t = jwt.JWT(string_header, claims)
1996+
t.make_signed_token(key)
1997+
token = t.serialize()
1998+
self.assertRaises(jwt.JWTInvalidClaimValue, jwt.JWT, jwt=token,
1999+
key=key, check_claims={"scope": "read"})
2000+
2001+
# empty scopes
2002+
claims = '{"scope": ""}'
2003+
t = jwt.JWT(string_header, claims)
2004+
t.make_signed_token(key)
2005+
token = t.serialize()
2006+
self.assertRaises(jwt.JWTInvalidClaimValue, jwt.JWT, jwt=token,
2007+
key=key, check_claims={"scope": "read"})
2008+
2009+
# one correct scope
2010+
claims = '{"scope":"read"}'
2011+
t = jwt.JWT(string_header, claims)
2012+
t.make_signed_token(key)
2013+
token = t.serialize()
2014+
jwt.JWT(jwt=token, key=key, check_claims={"scope": "read"})
2015+
self.assertRaises(jwt.JWTInvalidClaimValue, jwt.JWT, jwt=token,
2016+
key=key, check_claims={"scope": "write"})
2017+
2018+
# multiple scopes including the correct one
2019+
claims = '{"scope":"view read write"}'
2020+
t = jwt.JWT(string_header, claims)
2021+
t.make_signed_token(key)
2022+
token = t.serialize()
2023+
jwt.JWT(jwt=token, key=key, check_claims={"scope": "view"})
2024+
jwt.JWT(jwt=token, key=key, check_claims={"scope": "read"})
2025+
jwt.JWT(jwt=token, key=key, check_claims={"scope": "write"})
2026+
self.assertRaises(jwt.JWTInvalidClaimValue, jwt.JWT, jwt=token,
2027+
key=key, check_claims={"scope": "wrong"})
2028+
2029+
# one correct scope, invalid value
2030+
claims = '{"scope":"read"}'
2031+
t = jwt.JWT(string_header, claims)
2032+
t.make_signed_token(key)
2033+
token = t.serialize()
2034+
self.assertRaises(jwt.JWTInvalidClaimFormat, jwt.JWT, jwt=token,
2035+
key=key, check_claims={"scope": 123})
2036+
self.assertRaises(jwt.JWTInvalidClaimFormat, jwt.JWT, jwt=token,
2037+
key=key, check_claims={"scope": ["test", "wrong"]})
2038+
2039+
# finally make sure it doesn't raise if not checked.
2040+
jwt.JWT(jwt=token, key=key)
2041+
19802042

19812043
class ConformanceTests(unittest.TestCase):
19822044

0 commit comments

Comments
 (0)