|
1 | | -# from api import app |
2 | | -# from os import environ |
3 | | -# from flask import Flask, request, redirect, session, url_for |
4 | | -# from flask_dance.contrib.github import make_github_blueprint, github |
5 | | - |
6 | | -# print("hello") |
7 | | -# app.secret_key = environ.get("APP_SECRET") |
8 | | -# github_blueprint = make_github_blueprint( |
9 | | -# client_id = environ.get("GITHUB_ID"), |
10 | | -# client_secret = environ.get("GITHUB_SECRET") |
11 | | -# ) |
12 | | - |
13 | | -# app.register_blueprint(github_blueprint, url_prefix="/login") |
14 | | - |
15 | | -# @app.route("/login/") |
16 | | -# def index(): |
17 | | -# if not github.authorized: |
18 | | -# return redirect(url_for("github.login")) |
19 | | -# resp = github.get("/user") |
20 | | -# uid = resp.json()["id"] |
21 | | -# print(uid) |
22 | | -# return "You are @{login} on GitHub".format(login=resp.json()["login"]) |
| 1 | +from flask_jwt_extended import JWTManager, jwt_required, get_jwt_identity, get_jwt_claims, create_access_token |
| 2 | +from api.models import User |
| 3 | +from flask import request, jsonify, session, Flask, redirect, session, url_for |
| 4 | +from api import app |
| 5 | +from os import environ |
| 6 | +from api.controllers import userController |
| 7 | +from flask_dance.contrib.github import make_github_blueprint, github |
| 8 | +from flask_dance.consumer import oauth_authorized |
| 9 | + |
| 10 | +app.secret_key = environ.get("APP_SECRET") |
| 11 | +github_blueprint = make_github_blueprint( |
| 12 | + client_id = environ.get("GITHUB_ID"), |
| 13 | + client_secret = environ.get("GITHUB_SECRET") |
| 14 | +) |
| 15 | + |
| 16 | +app.register_blueprint(github_blueprint, url_prefix="/login") |
| 17 | + |
| 18 | +app.config['JWT_SECRET_KEY'] = environ.get("JWT_SECRET_KEY") |
| 19 | +jwt = JWTManager(app) |
| 20 | + |
| 21 | +@jwt.user_identity_loader |
| 22 | +def user_identity_lookup(user): |
| 23 | + return user.id |
| 24 | + |
| 25 | +@app.route("/login", methods=["GET"]) |
| 26 | +def login_route(): |
| 27 | + account = request.args.get('account') |
| 28 | + session['action'] = "login" |
| 29 | + session['redirect'] = request.args.get('redirect') |
| 30 | + session['state'] = request.args.get('state','{}') |
| 31 | + |
| 32 | + if account == 'github': |
| 33 | + return redirect(url_for("github.login")) |
| 34 | + else: |
| 35 | + return "", 400 |
| 36 | + |
| 37 | +@app.route("/register", methods=["GET"]) |
| 38 | +def register_route(): |
| 39 | + account = request.args.get('account') |
| 40 | + session['action'] = "register" |
| 41 | + # TODO remove or not? |
| 42 | + session['username'] = request.args.get('username') |
| 43 | + session['redirect'] = request.args.get('redirect') |
| 44 | + session['state'] = request.args.get('state','{}') |
| 45 | + |
| 46 | + if account == 'github': |
| 47 | + return redirect(url_for("github.login")) |
| 48 | + else: |
| 49 | + return "", 400 |
| 50 | + |
| 51 | +@oauth_authorized.connect |
| 52 | +def oathed(blueprint, token): |
| 53 | + if session['action'] == 'login': |
| 54 | + return login_callback(blueprint) |
| 55 | + elif session['action'] == 'register': |
| 56 | + return register_callback(blueprint) |
| 57 | + else: |
| 58 | + return "", 501 |
| 59 | + |
| 60 | + |
| 61 | +def login_callback(blueprint): |
| 62 | + if blueprint.name == "github": |
| 63 | + resp = github.get("/user").json() |
| 64 | + id = resp["id"] |
| 65 | + user = userController.get_user(github_id=id) |
| 66 | + redirect_token = f"?state={session.pop('state', '{}')}" |
| 67 | + if user: |
| 68 | + access_token = create_access_token(identity=user) |
| 69 | + redirect_token += f"&token={access_token}" |
| 70 | + |
| 71 | + return redirect(session.pop("redirect") + redirect_token) |
| 72 | + |
| 73 | +def register_callback(blueprint): |
| 74 | + if blueprint.name == "github": |
| 75 | + resp = github.get("/user").json() |
| 76 | + id = resp["id"] |
| 77 | + user = userController.get_user(github_id=id) |
| 78 | + if not user: |
| 79 | + user = userController.create_user(github_id=id, name=session.pop('username', "Anton")) |
| 80 | + access_token = create_access_token(identity=user) |
| 81 | + redirect_token = f"?state={session.pop('state')}&token={access_token}" |
| 82 | + return redirect(session.pop('redirect') + redirect_token) |
| 83 | + |
| 84 | +# Actually deprecated |
| 85 | +# should be /user in userview but I'll leave it until Routes branch adds and merges it |
| 86 | +@app.route("/getcurrentuser", methods=["GET"]) |
| 87 | +@jwt_required |
| 88 | +def getCurrentUser(): |
| 89 | + current_user = userController.get_user(id=get_jwt_identity()) |
| 90 | + return jsonify(current_user.as_dict()), 200 |
0 commit comments