From 2cf8a6f889cd114cbd2b8ed220c15a60ec2a0324 Mon Sep 17 00:00:00 2001 From: fenix-hub Date: Sat, 13 Nov 2021 14:06:44 +0100 Subject: [PATCH] Update LICENSE --- LICENSE | 21 ++++++++ addons/jwt/src/JWTBaseBuilder.gd | 62 ++++++++++++++++++++++++ addons/jwt/src/JWTVerifierBuilder.gd | 72 ++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 LICENSE create mode 100644 addons/jwt/src/JWTBaseBuilder.gd create mode 100644 addons/jwt/src/JWTVerifierBuilder.gd diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3a1d240 --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/addons/jwt/src/JWTBaseBuilder.gd b/addons/jwt/src/JWTBaseBuilder.gd new file mode 100644 index 0000000..d656d0d --- /dev/null +++ b/addons/jwt/src/JWTBaseBuilder.gd @@ -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 diff --git a/addons/jwt/src/JWTVerifierBuilder.gd b/addons/jwt/src/JWTVerifierBuilder.gd new file mode 100644 index 0000000..636fbfe --- /dev/null +++ b/addons/jwt/src/JWTVerifierBuilder.gd @@ -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)