Skip to content

Commit

Permalink
Merge pull request #16 from anuj0456/deployment-setup
Browse files Browse the repository at this point in the history
login api added to generate jwt on login
  • Loading branch information
anuj0456 authored Jan 13, 2025
2 parents e708a1c + 3dde0f2 commit f54e678
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions db_handler/sample_vault/secrets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ brand_name = "AiLert"
# api_key = add sendgrid api key and uncomment

[JWT]
# user_id = test
# token = generate a random token that your apis will accept
15 changes: 14 additions & 1 deletion router/routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import csv
from app.main import *
from utils.auth_utility import token_required
from db_handler import TaskType, SchedulerState
from utils.auth_utility import create_token, token_required
from utils.utility import is_valid_email, is_email_subscribed, save_to_csv

from flask_cors import CORS
Expand All @@ -26,6 +26,19 @@
}
})

config = configparser.ConfigParser()
config.read('db_handler/vault/secrets.ini')
user_id = config["JWT"]["user_id"]


@bp.route('/login', methods=['POST'])
def login():
token = create_token(user_id)
return jsonify({
"status": "success",
"token": token
})


@bp.route('/start-scheduler/<task_type>', methods=['POST'])
@limiter.limit("5 per hour")
Expand Down
17 changes: 17 additions & 0 deletions utils/auth_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@
import configparser
from functools import wraps
from flask import request, jsonify
from datetime import datetime, timedelta

config = configparser.ConfigParser()
config.read('db_handler/vault/secrets.ini')
JWT_SECRET_KEY = config["JWT"]["token"]


def create_token(user_id):
payload = {
'exp': datetime.now() + timedelta(days=1),
'iat': datetime.now(),
'sub': user_id
}

token = jwt.encode(
payload,
JWT_SECRET_KEY,
algorithm='HS256'
)
return token


def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
Expand Down

0 comments on commit f54e678

Please sign in to comment.