import jwt
from datetime import datetime, timedelta
from flask import request, abort, render_template, Flask
import os
from dotenv import load_dotenv


load_dotenv()
JWT_ALG = 'HS256' if not os.getenv('JWT_ALG') else os.getenv('JWT_ALG')
JWT_PUBLIC = os.getenv('JWT_PUBLIC')
JWT_SECRET = os.getenv('JWT_SECRET')


def encode(payload, algorithm=JWT_ALG):
  headers = {
    "alg": algorithm,
    "typ": "JWT"
  }
  payload.update({    # payload
    "exp": datetime.utcnow() + timedelta(days=1)
  })
  try:
    token = jwt.encode(payload, JWT_SECRET, algorithm, headers=headers)
    return token
  except Exception as e:
    return e
  
def decode(token, algorithm=JWT_ALG):
  try:
    if algorithm == 'HS256':
      decoded = jwt.decode(token, JWT_SECRET, algorithm)
    elif algorithm == 'RS256':
      decoded = jwt.decode(token, JWT_PUBLIC, algorithms='RS256')
    return decoded
  except:
    return 'err'
  
  
def auth(func):
  def validate_auth(*args, **kwargs):
    print('[v] JWT_ALG', JWT_ALG, 'JWT_SECRET', JWT_SECRET, 'JWT_PUBLIC', JWT_PUBLIC, 'request.headers:', 
          request.headers, ' request.data:', request.data, ' request.json:', request.json)
    try:
      token = request.headers.get('Authorization').split(' ')[1]
      if JWT_ALG == 'HS256':
        secret = JWT_SECRET
      elif JWT_ALG == 'RS256':
        secret = JWT_PUBLIC
      result = decode(token, secret)
      if result == 'err':
        return render_template('login.html')
      return func(*args, **kwargs)
    except Exception as e:
      print('[x] exception:', e)
      return render_template('login.html')
  return validate_auth
  
  
# secret = 'a2c4e6f8'
# encoded = encode({"some": "payload"}, secret, algorithm="HS256")
# print(decode(encoded, secret, algorithm="HS256"))

  
# private_key = b"-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAwhvqCC+37A+UXgcvDl+7nbVjDI3QErdZBkI1VypVBMkKKWHM\nNLMdHk0bIKL+1aDYTRRsCKBy9ZmSSX1pwQlO/3+gRs/MWG27gdRNtf57uLk1+lQI\n6hBDozuyBR0YayQDIx6VsmpBn3Y8LS13p4pTBv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O5O\n7MBoPLkXrcLTCARiZ9nTkQKBgQCsaBGnnkzOObQWnIny1L7s9j+UxHseCEJguR0v\nXMVh1+5uYc5CvGp1yj5nDGldJ1KrN+rIwMh0FYt+9dq99fwDTi8qAqoridi9Wl4t\nIXc8uH5HfBT3FivBtLucBjJgOIuK90ttj8JNp30tbynkXCcfk4NmS23L21oRCQyy\nlmqNDQKBgQDRvzEB26isJBr7/fwS0QbuIlgzEZ9T3ZkrGTFQNfUJZWcUllYI0ptv\ny7ShHOqyvjsC3LPrKGyEjeufaM5J8EFrqwtx6UB/tkGJ2bmd1YwOWFHvfHgHCZLP\n34ZNURCvxRV9ZojS1zmDRBJrSo7+/K0t28hXbiaTOjJA18XAyyWmGg==\n-----END RSA PRIVATE KEY-----\n"
# public_key = b"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwhvqCC+37A+UXgcvDl+7\nnbVjDI3QErdZBkI1VypVBMkKKWHMNLMdHk0bIKL+1aDYTRRsCKBy9ZmSSX1pwQlO\n/3+gRs/MWG27gdRNtf57uLk1+lQI6hBDozuyBR0YayQDIx6VsmpBn3Y8LS13p4pT\nBvirlsdX+jXrbOEaQphn0OdQo0WDoOwwsPCNCKoIMbUOtUCowvjesFXlWkwG1zeM\nzlD1aDDS478PDZdckPjT96ICzqe4O1Ok6fRGnor2UTmuPy0f1tI0F7Ol5DHAD6pZ\nbkhB70aTBuWDGLDR0iLenzyQecmD4aU19r1XC9AHsVbQzxHrP8FveZGlV/nJOBJw\nFwIDAQAB\n-----END PUBLIC KEY-----\n"
# encoded = encode({"some": "payload"}, private_key, algorithm="RS256")
# print(decode(encoded, public_key, algorithm="RS256"))

