-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update JWT plugin code again and adapt to new usage.
- Loading branch information
Showing
9 changed files
with
147 additions
and
192 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 |
---|---|---|
@@ -1,81 +1,83 @@ | ||
class_name JWTAlgorithm | ||
extends RefCounted | ||
|
||
enum Type { HMAC1, HMAC256, RSA256 } | ||
|
||
var _alg: int = -1 | ||
var _secret: String = "" | ||
func get_name() -> String: | ||
assert(false, "JWTAlgorithm subclasses must implement `get_name()`") | ||
return "" | ||
|
||
var crypto: Crypto = Crypto.new() | ||
var _public_crypto: CryptoKey = CryptoKey.new() | ||
var _private_crypto: CryptoKey = CryptoKey.new() | ||
|
||
func sign(_text: String) -> PackedByteArray: | ||
assert(false, "JWTAlgorithm subclasses must implement `sign()`") | ||
return [] | ||
|
||
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 "" | ||
|
||
|
||
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() | ||
|
||
|
||
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 | ||
|
||
|
||
# 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) | ||
|
||
func verify(_jwt: JWTDecoder) -> bool: | ||
assert(false, "JWTAlgorithm subclasses must implement `verify()`") | ||
return false | ||
|
||
|
||
# TODO: HSA1 is not secure and should be removed | ||
class HSA1: | ||
extends JWTAlgorithm | ||
var _secret: PackedByteArray | ||
|
||
func _init(secret: String): | ||
push_warning("HSA1 is not secure, and should not be used") | ||
self._secret = secret.to_utf8_buffer() | ||
|
||
func get_name() -> String: | ||
return "HSA1" | ||
|
||
func sign(text: String) -> PackedByteArray: | ||
var crypto := Crypto.new() | ||
return crypto.hmac_digest(HashingContext.HASH_SHA1, self._secret, text.to_utf8_buffer()) | ||
|
||
func verify(jwt: JWTDecoder) -> bool: | ||
var payload: String = jwt.get_header() + "." + jwt.get_payload() | ||
var signature_bytes := self.sign(payload) | ||
return jwt.get_signature() == JWTUtils.urlsafe_b64encode(signature_bytes) | ||
|
||
|
||
class HS256: | ||
extends JWTAlgorithm | ||
var _secret: PackedByteArray | ||
|
||
func get_name() -> String: | ||
return "HS256" | ||
|
||
func _init(secret: String): | ||
self._secret = secret.to_utf8_buffer() | ||
|
||
func sign(text: String) -> PackedByteArray: | ||
var crypto := Crypto.new() | ||
return crypto.hmac_digest(HashingContext.HASH_SHA256, self._secret, text.to_utf8_buffer()) | ||
|
||
func verify(jwt: JWTDecoder) -> bool: | ||
var payload: String = jwt.get_header() + "." + jwt.get_payload() | ||
var signature_bytes := self.sign(payload) | ||
return jwt.get_signature() == JWTUtils.urlsafe_b64encode(signature_bytes) | ||
|
||
|
||
class RS256: | ||
extends JWTAlgorithm | ||
|
||
var _public_key: CryptoKey | ||
var _private_key: CryptoKey | ||
|
||
func _init(public_key: CryptoKey, private_key := CryptoKey.new()): | ||
self._public_key = public_key | ||
self._private_key = private_key | ||
|
||
func get_name() -> String: | ||
return "RS256" | ||
|
||
func sign(text: String) -> PackedByteArray: | ||
var crypto := Crypto.new() | ||
return crypto.sign(HashingContext.HASH_SHA256, text.sha256_buffer(), self._private_key) | ||
|
||
func verify(jwt: JWTDecoder) -> bool: | ||
var crypto := Crypto.new() | ||
var payload: PackedByteArray = (jwt.get_header() + "." + jwt.get_payload()).sha256_buffer() | ||
var signature: PackedByteArray = JWTUtils.urlsafe_b64decode(jwt.get_signature()) | ||
return crypto.verify(HashingContext.HASH_SHA256, payload, signature, self._public_key) |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.