-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.py
More file actions
67 lines (51 loc) · 2.05 KB
/
authentication.py
File metadata and controls
67 lines (51 loc) · 2.05 KB
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
from google.oauth2 import id_token
from google.auth.transport import requests
import routes, userfuncs, gets
from routes import app
import json
from db import db, Group, Event, User
from flask import Flask, request
@app.route('/api/users/', methods=['POST'])
def auth_user():
# (Receive token by HTTPS POST)
# ...
try:
# Specify the CLIENT_ID of the app that accesses the backend:
id_info = id_token.verify_oauth2_token(token, requests.Request(), CLIENT_ID)
# Or, if multiple clients access the backend server:
# idinfo = id_token.verify_oauth2_token(token, requests.Request())
# if idinfo['aud'] not in [CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]:
# raise ValueError('Could not verify audience.')
if id_info['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
raise ValueError('Wrong issuer.')
# If auth request is from a G Suite domain:
# if idinfo['hd'] != GSUITE_DOMAIN_NAME:
# raise ValueError('Wrong hosted domain.')
# ID token is valid. Get the user's Google Account ID from the decoded token.
user_id = id_info['sub']
except ValueError:
# Invalid token
pass
g_user = User.query.filter_by(id=user_id).first()
if user is not None:
#login
pass
else:
@app.route('/api/register', methods=['POST'])
def register():
post_body = json.loads(request.data)
#if 'title' not in post_body or 'username' not in post_body:
#return json.dumps({'success': False, 'error': 'Needs title or username'}), 404
user = User(
id=user_id,
username=post_body['username'],
g_user = id_info
)
try:
db.session.add(user)
db.session.commit()
status = 'success'
except:
status = 'this user is already registered'
db.session.close()
return jsonify({'result': status})