-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Nicolò Santilio | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
extends Reference | ||
class_name JWTBaseBuilder | ||
|
||
|
||
func with_header(header_claims: Dictionary) -> JWTBaseBuilder: | ||
self.header_claims = header_claims | ||
return self | ||
|
||
func with_algorithm(algorithm: String) -> JWTBaseBuilder: | ||
self.header_claims[JWTClaims.Public.ALGORITHM] = algorithm | ||
return self | ||
|
||
func with_type(type: String) -> JWTBaseBuilder: | ||
self.header_claims[JWTClaims.Public.TYPE] = type | ||
return self | ||
|
||
func with_key_id(key_id: String) -> JWTBaseBuilder: | ||
self.header_claims[JWTClaims.Public.KEY_ID] = key_id | ||
return self | ||
|
||
func with_issuer(issuer: String) -> JWTBaseBuilder: | ||
add_claim(JWTClaims.Public.ISSUER, issuer) | ||
return self | ||
|
||
func with_subject(subject: String) -> JWTBaseBuilder: | ||
add_claim(JWTClaims.Public.SUBJECT, subject) | ||
return self | ||
|
||
func with_audience(audience: PoolStringArray) -> JWTBaseBuilder: | ||
add_claim(JWTClaims.Public.AUDIENCE, audience) | ||
return self | ||
|
||
# Expires At in UNIX time (OS.get_unix_time()) | ||
func with_expires_at(expires_at: int) -> JWTBaseBuilder: | ||
add_claim(JWTClaims.Public.EXPRIES_AT, expires_at) | ||
return self | ||
|
||
# Not Before in UNIX time (OS.get_unix_time()) | ||
func with_not_before(not_before: int) -> JWTBaseBuilder: | ||
add_claim(JWTClaims.Public.NOT_BEFORE, not_before) | ||
return self | ||
|
||
# Issued At in UNIX time (OS.get_unix_time()) | ||
func with_issued_at(issued_at: int) -> JWTBaseBuilder: | ||
add_claim(JWTClaims.Public.ISSUED_AT, issued_at) | ||
return self | ||
|
||
func with_jwt_id(jwt_id: String) -> JWTBaseBuilder: | ||
self.header_claims[JWTClaims.Public.JWT_ID] = jwt_id | ||
return self | ||
|
||
func with_claim(name: String, value) -> JWTBaseBuilder: | ||
add_claim(name, str(value)) | ||
return self | ||
|
||
func with_payload(claims: Dictionary) -> JWTBaseBuilder: | ||
for claim in claims.keys(): | ||
add_claim(claim, claims[claim]) | ||
return self | ||
|
||
func add_claim(claim_name: String, claim_value) -> void: | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
extends JWTBaseBuilder | ||
class_name JWTVerifierBuilder | ||
|
||
var algorithm: JWTAlgorithm | ||
var claims: Dictionary = {} | ||
var leeway: int = 0 | ||
var ignore_issued_at: bool = false | ||
|
||
func _init(algorithm: JWTAlgorithm): | ||
self.algorithm = algorithm | ||
|
||
func add_claim(name: String, value) -> void: | ||
match typeof(value): | ||
TYPE_ARRAY, TYPE_STRING_ARRAY: | ||
if value.size() == 0 : | ||
self.claims.erase(name) | ||
return | ||
TYPE_STRING: | ||
if value.length() == 0: | ||
self.claims.erase(name) | ||
return | ||
_: | ||
if value == null: | ||
self.claims.erase(name) | ||
return | ||
self.claims[name] = value | ||
|
||
func with_any_of_issuers(issuers: PoolStringArray) -> JWTVerifierBuilder: | ||
add_claim(JWTClaims.Public.ISSUER, issuers) | ||
return self | ||
|
||
func with_any_of_audience(audience: PoolStringArray) -> JWTVerifierBuilder: | ||
add_claim(JWTClaims.Public.AUDIENCE, audience) | ||
return self | ||
|
||
func accept_leeway(leeway: int) -> JWTVerifierBuilder: | ||
self.leeway = leeway | ||
return self | ||
|
||
func accept_expire_at(leeway: int) -> JWTVerifierBuilder: | ||
with_expires_at(leeway) | ||
return self | ||
|
||
func accept_not_before(leeway: int) -> JWTVerifierBuilder: | ||
with_not_before(leeway) | ||
return self | ||
|
||
func accept_issued_at(leeway: int) -> JWTVerifierBuilder: | ||
with_issued_at(leeway) | ||
return self | ||
|
||
func ignore_issued_at() -> JWTVerifierBuilder: | ||
self.ignore_issued_at = true | ||
return self | ||
|
||
func with_claim_presence(claim_name: String) -> JWTVerifierBuilder: | ||
add_claim(claim_name, null) | ||
return self | ||
|
||
func _add_leeway() -> void: | ||
if (not claims.has(JWTClaims.Public.EXPRIES_AT)): | ||
claims[JWTClaims.Public.EXPRIES_AT] = self.leeway | ||
if (not claims.has(JWTClaims.Public.NOT_BEFORE)): | ||
claims[JWTClaims.Public.NOT_BEFORE] = self.leeway | ||
if (not claims.has(JWTClaims.Public.ISSUED_AT)): | ||
claims[JWTClaims.Public.ISSUED_AT] = self.leeway | ||
if (ignore_issued_at): | ||
claims.erase(JWTClaims.Public.ISSUED_AT) | ||
|
||
func build(clock: int = OS.get_unix_time()) -> JWTVerifier: | ||
_add_leeway() | ||
return JWTVerifier.new(self.algorithm, self.claims, clock) |