Skip to content

Commit

Permalink
Import more JWT addon changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisl8 committed Dec 10, 2023
1 parent 640ffef commit 8064ba5
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 296 deletions.
4 changes: 2 additions & 2 deletions addons/jwt/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ extends EditorPlugin


func _enter_tree():
pass
pass


func _exit_tree():
pass
pass
15 changes: 10 additions & 5 deletions addons/jwt/src/JWT.gd
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
extends RefCounted
class_name JWT
extends RefCounted


static func create(
algorithm: JWTAlgorithm = null, header_claims: Dictionary = {}, payload_claims: Dictionary = {}
) -> JWTBuilder:
return JWTBuilder.new(algorithm, header_claims, payload_claims)

static func create(algorithm: JWTAlgorithm = null, header_claims: Dictionary = {}, payload_claims: Dictionary = {}) -> JWTBuilder:
return JWTBuilder.new(algorithm, header_claims, payload_claims)

static func decode(jwt: String) -> JWTDecoder:
return JWTDecoder.new(jwt)
return JWTDecoder.new(jwt)


static func require(algorithm: JWTAlgorithm) -> JWTVerifierBuilder:
return JWTVerifierBuilder.new(algorithm)
return JWTVerifierBuilder.new(algorithm)
100 changes: 60 additions & 40 deletions addons/jwt/src/JWTAlgorithm.gd
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
extends RefCounted
class_name JWTAlgorithm
extends RefCounted

enum Type {
HMAC1,
HMAC256,
RSA256
}
enum Type { HMAC1, HMAC256, RSA256 }

var _alg: int = -1
var _secret: String = ""
Expand All @@ -14,48 +10,72 @@ var crypto: Crypto = Crypto.new()
var _public_crypto: CryptoKey = CryptoKey.new()
var _private_crypto: CryptoKey = CryptoKey.new()


func get_name() -> String:
match _alg:
# Note: HS1 is not secure and should be removed.
Type.HMAC1: return "HSA1"
Type.HMAC256: return "HS256"
Type.RSA256: return "RS256"
_: return ""
match _alg:
# Note: HS1 is not secure and should be removed.
Type.HMAC1:
return "HSA1"
Type.HMAC256:
return "HS256"
Type.RSA256:
return "RS256"
_:
return ""


func _digest(ctx_type: HashingContext.HashType, data: PackedByteArray) -> PackedByteArray:
var ctx = HashingContext.new()
# Start a SHA-256 context.
ctx.start(ctx_type)
# Check that file exists.
ctx.update(data)
# Get the computed hash.
return ctx.finish()
var ctx = HashingContext.new()
# Start a SHA-256 context.
ctx.start(ctx_type)
# Check that file exists.
ctx.update(data)
# Get the computed hash.
return ctx.finish()


func sign(text: String) -> PackedByteArray:
var signature_bytes: PackedByteArray = []
match self._alg:
Type.HMAC1:
signature_bytes = self.crypto.hmac_digest(HashingContext.HASH_SHA1, self._secret.to_utf8_buffer(), text.to_utf8_buffer())
Type.HMAC256:
signature_bytes = self.crypto.hmac_digest(HashingContext.HASH_SHA256, self._secret.to_utf8_buffer(), text.to_utf8_buffer())
Type.RSA256:
signature_bytes = self.crypto.sign(HashingContext.HASH_SHA256, text.sha256_buffer(), self._private_crypto)
return signature_bytes
var signature_bytes: PackedByteArray = []
match self._alg:
Type.HMAC1:
signature_bytes = self.crypto.hmac_digest(
HashingContext.HASH_SHA1, self._secret.to_utf8_buffer(), text.to_utf8_buffer()
)
Type.HMAC256:
signature_bytes = self.crypto.hmac_digest(
HashingContext.HASH_SHA256, self._secret.to_utf8_buffer(), text.to_utf8_buffer()
)
Type.RSA256:
signature_bytes = self.crypto.sign(
HashingContext.HASH_SHA256, text.sha256_buffer(), self._private_crypto
)
return signature_bytes


# TODO: Debug this.
func verify(jwt: JWTDecoder) -> bool:
var signature_bytes: PackedByteArray = []
match self._alg:
Type.HMAC1:
signature_bytes = self.crypto.hmac_digest(HashingContext.HASH_SHA1, self._secret.to_utf8_buffer(), (jwt.parts[0]+"."+jwt.parts[1]).to_utf8_buffer())
Type.HMAC256:
signature_bytes = self.crypto.hmac_digest(HashingContext.HASH_SHA256, self._secret.to_utf8_buffer(), (jwt.parts[0]+"."+jwt.parts[1]).to_utf8_buffer())
Type.RSA256:
# type, hash, sig, key
print()
return self.crypto.verify(HashingContext.HASH_SHA256, (jwt.parts[0]+"."+jwt.parts[1]).sha256_buffer(), JWTUtils.base64URL_decode(jwt.parts[2]), self._public_crypto)
#signature_bytes = self.crypto.verify(self._public_crypto, .to_utf8_buffer())
return jwt.parts[2] == JWTUtils.base64URL_encode(signature_bytes)
var signature_bytes: PackedByteArray = []
match self._alg:
Type.HMAC1:
signature_bytes = self.crypto.hmac_digest(
HashingContext.HASH_SHA1,
self._secret.to_utf8_buffer(),
(jwt.parts[0] + "." + jwt.parts[1]).to_utf8_buffer()
)
Type.HMAC256:
signature_bytes = self.crypto.hmac_digest(
HashingContext.HASH_SHA256,
self._secret.to_utf8_buffer(),
(jwt.parts[0] + "." + jwt.parts[1]).to_utf8_buffer()
)
Type.RSA256:
# type, hash, sig, key
print()
return self.crypto.verify(
HashingContext.HASH_SHA256,
(jwt.parts[0] + "." + jwt.parts[1]).sha256_buffer(),
JWTUtils.base64URL_decode(jwt.parts[2]),
self._public_crypto
)
#signature_bytes = self.crypto.verify(self._public_crypto, .to_utf8_buffer())
return jwt.parts[2] == JWTUtils.base64URL_encode(signature_bytes)
40 changes: 20 additions & 20 deletions addons/jwt/src/JWTAlgorithmBuilder.gd
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
extends RefCounted
class_name JWTAlgorithmBuilder
extends RefCounted


static func random_secret(length: int = 10) -> String:
return Crypto.new().generate_random_bytes(length).get_string_from_utf8()
return Crypto.new().generate_random_bytes(length).get_string_from_utf8()


static func HSA1(secret: String) -> JWTAlgorithm:
var algorithm: JWTAlgorithm = JWTAlgorithm.new()
algorithm._secret = secret
algorithm._alg = JWTAlgorithm.Type.HMAC1
return algorithm
var algorithm: JWTAlgorithm = JWTAlgorithm.new()
algorithm._secret = secret
algorithm._alg = JWTAlgorithm.Type.HMAC1
return algorithm


static func HS1(secret: String) -> JWTAlgorithm:
return HSA1(secret)
return HSA1(secret)


static func HSA256(secret: String) -> JWTAlgorithm:
var algorithm: JWTAlgorithm = JWTAlgorithm.new()
algorithm._secret = secret
algorithm._alg = JWTAlgorithm.Type.HMAC256
return algorithm
var algorithm: JWTAlgorithm = JWTAlgorithm.new()
algorithm._secret = secret
algorithm._alg = JWTAlgorithm.Type.HMAC256
return algorithm


static func HS256(secret: String) -> JWTAlgorithm:
return HSA256(secret)
return HSA256(secret)


static func RSA256(public_key: CryptoKey, private_key: CryptoKey = CryptoKey.new()) -> JWTAlgorithm:
var algorithm: JWTAlgorithm = JWTAlgorithm.new()
algorithm._public_crypto = public_key
algorithm._private_crypto = private_key
algorithm._alg = JWTAlgorithm.Type.RSA256
return algorithm
var algorithm: JWTAlgorithm = JWTAlgorithm.new()
algorithm._public_crypto = public_key
algorithm._private_crypto = private_key
algorithm._alg = JWTAlgorithm.Type.RSA256
return algorithm


static func RS256(public_key: CryptoKey, private_key: CryptoKey) -> JWTAlgorithm:
return RSA256(public_key, private_key)
return RSA256(public_key, private_key)


static func sign(text: String, algorithm: JWTAlgorithm) -> PackedByteArray:
return algorithm.sign(text)
return algorithm.sign(text)


static func verify(jwt: JWTDecoder, algorithm: JWTAlgorithm) -> bool:
return algorithm.verify(jwt)
return algorithm.verify(jwt)
72 changes: 43 additions & 29 deletions addons/jwt/src/JWTBaseBuilder.gd
Original file line number Diff line number Diff line change
@@ -1,61 +1,75 @@
extends RefCounted
class_name JWTBaseBuilder
extends RefCounted


func with_header(header_claims: Dictionary) -> JWTBaseBuilder:
self.header_claims = header_claims
return self
self.header_claims = header_claims
return self


func with_algorithm(algorithm: String) -> JWTBaseBuilder:
self.header_claims[JWTClaims.Public.ALGORITHM] = algorithm
return self
self.header_claims[JWTClaims.Public.ALGORITHM] = algorithm
return self


func with_type(type: String) -> JWTBaseBuilder:
self.header_claims[JWTClaims.Public.TYPE] = type
return self
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
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
add_claim(JWTClaims.Public.ISSUER, issuer)
return self


func with_subject(subject: String) -> JWTBaseBuilder:
add_claim(JWTClaims.Public.SUBJECT, subject)
return self
add_claim(JWTClaims.Public.SUBJECT, subject)
return self


func with_audience(audience: PackedStringArray) -> JWTBaseBuilder:
add_claim(JWTClaims.Public.AUDIENCE, audience)
return self
add_claim(JWTClaims.Public.AUDIENCE, audience)
return self


# Expires At in UNIX time (Time.get_unix_time_from_system())
func with_expires_at(expires_at: int) -> JWTBaseBuilder:
add_claim(JWTClaims.Public.EXPIRES_AT, expires_at)
return self
add_claim(JWTClaims.Public.EXPIRES_AT, expires_at)
return self


# Not Before in UNIX time (Time.get_unix_time_from_system())
func with_not_before(not_before: int) -> JWTBaseBuilder:
add_claim(JWTClaims.Public.NOT_BEFORE, not_before)
return self
add_claim(JWTClaims.Public.NOT_BEFORE, not_before)
return self


# Issued At in UNIX time (Time.get_unix_time_from_system())
func with_issued_at(issued_at: int) -> JWTBaseBuilder:
add_claim(JWTClaims.Public.ISSUED_AT, issued_at)
return self
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
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
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
for claim in claims.keys():
add_claim(claim, claims[claim])
return self


func add_claim(_claim_name: String, _claim_value) -> void:
return
return
Loading

0 comments on commit 8064ba5

Please sign in to comment.