Code Monkey home page Code Monkey logo

godot-engine.jwt's Introduction

Godot Engine GDScript JWT

JSON Web Token library for Godot Engine written in GDScript

Godot 3.x - Godot 4.x

Create HS256 JWT

var secret: String = JWTAlgorithmBuilder.random_secret(5)
var jwt_algorithm: JWTAlgorithm = JWTAlgorithmBuilder.HS256(secret)
var jwt_builder: JWTBuilder = JWT.create() \
.with_expires_at(OS.get_unix_time()) \
.with_issuer("Godot") \
.with_claim("id","someid")
var jwt: String = jwt_builder.sign(jwt_algorithm)

Verify HS256 JWT

var jwt: String = "<a jwt>"
var secret: String = "<your secret token>"
var jwt_algorithm: JWTAlgorithm = JWTAlgorithmBuilder.HS256(secret)
var jwt_verifier: JWTVerifier = JWT.require(jwt_algorithm) \
    .with_claim("my-claim","my-value") \
    .build() # Reusable Verifier
if jwt_verifier.verify(jwt) == JWTVerifier.JWTExceptions.OK :
	print("Verified!")
else:
	print(jwt_verifier.exception)

Create RS256 JWT

var private_key : CryptoKey = crypto.generate_rsa(4096)
var public_key : CryptoKey = CryptoKey.new()
public_key.load_from_string(private_key.save_to_string(true))

var jwt_algorithm: JWTAlgorithm = JWTAlgorithmBuilder.RS256(public_key, private_key)
var jwt_builder: JWTBuilder = JWT.create() \
    .with_expires_at(OS.get_unix_time()) \
    .with_issuer("Godot") \
    .with_claim("id","someid")
var jwt: String = jwt_builder.sign(jwt_algorithm)

Verify RS256 JWT

var private_key: CryptoKey = CryptoKey.new()
var public_key: CryptoKey = CryptoKey.new()
private_key.load_from_string("<your private key PEM string>", false)
public_key.load_from_string("<your public key PEM string>", true)

var jwt: String = "<a jwt>"
var jwt_algorithm: JWTAlgorithm = JWTAlgorithmBuilder.RS256(public_key)
var jwt_verifier: JWTVerifier = JWT.require(jwt_algorithm) \
    .with_claim("id","someid") \
    .build() # Reusable Verifier
if jwt_verifier.verify(jwt) == JWTVerifier.JWTExceptions.OK :
	print("Verified!")
else:
	print(jwt_verifier.exception)

Decode JWT

var jwt: String = "<a jwt>"
var jwt_decoder: JWTDecoder = JWT.decode(jwt)
# Get the JWT as an Array
print("%s.%s.%s" % jwt_decoder.parts)
# Decode a specific part
print(JWTUtils.base64URL_decode(jwt_decoder.get_payload()))

JWT Utils

JWTUtils.base64URL_encode(bytes: PoolByteArray) -> String
JWTUtils.base64URL_decode(string: String) -> PoolByteArray

Supported Algorithms

  • HS1 (HMAC with SHA1)
  • HS256 (HMAC with SHA256)
  • RS256 (RSA with SHA256)

godot-engine.jwt's People

Contributors

fenix-hub avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.