-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.py
98 lines (76 loc) · 2.88 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import flask
from flask import render_template, request, Flask, g, send_from_directory, abort, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Table, Column, Float, Integer, String, MetaData, ForeignKey
import json
import random
import string
import os
import time
from web3.auto import w3
from eth_account.messages import defunct_hash_message
from flask_jwt_extended import JWTManager, jwt_required, create_access_token, get_jwt_identity, set_access_cookies
from ethhelper import *
app = Flask(__name__,static_url_path='/static')
app.jinja_env.add_extension('jinja2.ext.do')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
db = SQLAlchemy(app)
# Setup the Flask-JWT-Extended extension
# log2(26^22) ~= 100 (pull at least 100 bits of entropy)
app.config['JWT_SECRET_KEY'] = ''.join(random.choice(string.ascii_lowercase) for i in range(22))
app.config['JWT_TOKEN_LOCATION'] = ['cookies']
app.config['JWT_COOKIE_SECURE'] = True
#app.config['JWT_ACCESS_COOKIE_PATH'] = '/api/'
app.config['JWT_COOKIE_CSRF_PROTECT'] = True
jwt = JWTManager(app)
@app.before_first_request
def setup():
print("[+] running setup")
try:
db.create_all()
print("[+] created users db")
except:
print("[+] users db already exists")
def generate_nonce(self, length=8):
return ''.join([str(randint(0, 9)) for i in range(length)])
class User(db.Model):
public_address = db.Column(db.String(80), primary_key=True, nullable=False, unique=True)
nonce = db.Column(db.Integer(),nullable=False,default=generate_nonce,)
@app.route('/')
def landing():
return render_template("index.html")
@app.route('/secret')
@jwt_required
def secret():
current_user = get_jwt_identity()
numtokens = tokencount(current_user)
if numtokens > 100:
msg="The Galaxy is on Orion's Belt"
else:
msg="You need more than 100 GST to view this message."
return ("HELLO "+str(current_user)+" "+msg)
@app.route('/login', methods=['POST'])
def login():
print("[+] creating session")
print("info: "+(str(request.json)))
public_address = request.json[0]
signature = request.json[1]
domain = "simple-flask-metamask.herokuapp.com"
rightnow = int(time.time())
sortanow = rightnow-rightnow%600
original_message = 'Signing in to {} at {}'.format(domain,sortanow)
print("[+] checking: "+original_message)
message_hash = defunct_hash_message(text=original_message)
signer = w3.eth.account.recoverHash(message_hash, signature=signature)
print("[+] fascinating")
if signer == public_address:
print("[+] this is fine "+str(signer))
# account.nonce = account.generate_nonce()
# db.session.commit()
else:
abort(401, 'could not authenticate signature')
print("[+] OMG looks good")
access_token = create_access_token(identity=public_address)
resp = jsonify({'login': True})
set_access_cookies(resp, access_token)
return resp, 200