Skip to content

Instantly share code, notes, and snippets.

@arujit
Created July 14, 2017 07:31
Show Gist options
  • Save arujit/10abce8b7dfe6f7be1ebb8a47d229dd7 to your computer and use it in GitHub Desktop.
Save arujit/10abce8b7dfe6f7be1ebb8a47d229dd7 to your computer and use it in GitHub Desktop.
import jwt
import sys
class InitializeJWT(object ):
_instance = None
def __new__(cls, private_path, public_path):
try:
cls._publickey_file = open(public_path, "r")
cls._privatekey_file = open(private_path, "r")
except:
raise IOError
sys.exit()
else:
cls._public_key = cls._publickey_file.read()
cls._private_key = cls._privatekey_file.read()
if not isinstance(cls._instance, cls):
cls._instance = object.__new__(cls)
return cls._instance
class JwtModule(InitializeJWT):
@classmethod
def encoding(cls, payload):
return jwt.encode(payload, cls._private_key, algorithm="RS512")
@classmethod
def decoding(cls, encoded_token):
return jwt.decode(encoded_token, cls._public_key, verify=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment