Skip to content

Instantly share code, notes, and snippets.

@jainamoswal
Last active June 24, 2022 23:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jainamoswal/279e5259a5c24f37cd44ea446c373ac4 to your computer and use it in GitHub Desktop.
Save jainamoswal/279e5259a5c24f37cd44ea446c373ac4 to your computer and use it in GitHub Desktop.
from flask import Flask, request, redirect
from utils import verify
app = Flask(__name__)
@app.route('/')
def main():
if verify(request.args.to_dict()) == request.args.get('hash'):
return f"Hey {request.args.get('first_name')}!"
else:
return "Sorry, You didn't clicked the Auth Button and just redirected here."
if __name__ == '__main__':
app.run()
import hashlib
import hmac
BOT_TOKEN = '1797603327:AAFFCpZIU1pvq4-hL5pmgX4IJR7LtLL9xfU'
def verify(request_data):
request_data = request_data.copy()
request_data.pop('hash', None)
request_data_alphabetical_order = sorted(request_data.items(), key=lambda x: x[0])
data_check_string = []
for data_pair in request_data_alphabetical_order:
key, value = data_pair[0], data_pair[1]
data_check_string.append(f"{key}={value}")
data_check_string = '\n'.join(data_check_string)
secret_key = hashlib.sha256(BOT_TOKEN.encode()).digest()
return hmac.new(secret_key, msg=data_check_string.encode(), digestmod=hashlib.sha256).hexdigest()
@jainamoswal
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment