|
1 |
| -from configparser import ConfigParser |
| 1 | +import os |
2 | 2 |
|
3 | 3 | from flask import Flask
|
4 | 4 | from flask import jsonify
|
|
7 | 7 |
|
8 | 8 | from authenticator.adapters.db import api as db_api
|
9 | 9 | from authenticator.adapters.login import loginmanager
|
| 10 | +from authenticator.utils import utils |
10 | 11 |
|
11 |
| -config = ConfigParser() |
12 |
| -config.read("config.ini") |
13 |
| - |
14 |
| -database = config.get('default', 'db') |
15 |
| -host = config.get('default', 'db_host') |
16 |
| -port = config.get('default', 'db_port') |
| 12 | +database = os.environ.get("db", "authenticator") |
| 13 | +host = os.environ.get("db_host", "mongo") |
| 14 | +port = os.environ.get("db_port", "27017") |
17 | 15 |
|
18 | 16 | app = Flask(__name__)
|
19 |
| -app.config['MONGO_URI'] = f'mongodb://{host}:{port}/{database}' |
20 |
| -app.config['SECRET_KEY'] = "b9dd1b2f" |
21 |
| -app.config['GOOGLE_CLIENT_ID'] = config.get('google-oauth', 'client_id') |
22 |
| -app.config['GOOGLE_CLIENT_SECRET'] = config.get( |
23 |
| - 'google-oauth', 'client_secret') |
| 17 | +app.config["MONGO_URI"] = f"mongodb://{host}:{port}/{database}" |
| 18 | +app.config["SECRET_KEY"] = "b9dd1b2f" |
| 19 | +app.config["GOOGLE_CLIENT_ID"] = os.environ.get("GOOGLE_CLIENT_ID", None) |
| 20 | +app.config["GOOGLE_CLIENT_SECRET"] = os.environ.get("GOOGLE_CLIENT_SECRET", None) |
24 | 21 |
|
25 | 22 |
|
26 | 23 | db_obj = db_api.MongoAdapters(app)
|
27 | 24 | login_obj = loginmanager.loginManager(app)
|
28 | 25 |
|
29 | 26 |
|
30 |
| -@app.route('/users', methods=['GET']) |
| 27 | +@app.route("/users", methods=["GET"]) |
31 | 28 | def get_users():
|
32 | 29 | return db_obj.get_all_users()
|
33 | 30 |
|
34 | 31 |
|
35 |
| -@app.route('/', methods=['GET']) |
| 32 | +@app.route("/", methods=["GET"]) |
36 | 33 | def index():
|
37 | 34 | return "Welcome to nomad Authenticator."
|
38 | 35 |
|
39 | 36 |
|
40 | 37 | def add_users(name, email, provider):
|
41 |
| - return db_obj.add_users(name, email, provider) |
| 38 | + |
| 39 | + print("HELOO") |
| 40 | + return db_obj.add_users( |
| 41 | + name, email, utils.get_current_time(), provider, "admin" |
| 42 | + ) |
| 43 | + return db_obj.add_users(name, email, utils.get_current_time(), provider, "user") |
42 | 44 |
|
43 | 45 |
|
44 |
| -@app.route('/login/google') |
| 46 | +@app.route("/login/google") |
45 | 47 | def google_login():
|
46 | 48 | login_obj.auth.register(
|
47 |
| - name='google', |
| 49 | + name="google", |
48 | 50 | client_id=app.config["GOOGLE_CLIENT_ID"],
|
49 | 51 | client_secret=app.config["GOOGLE_CLIENT_SECRET"],
|
50 |
| - access_token_url='https://accounts.google.com/o/oauth2/token', |
| 52 | + access_token_url="https://accounts.google.com/o/oauth2/token", |
51 | 53 | access_token_params=None,
|
52 |
| - authorize_url='https://accounts.google.com/o/oauth2/auth', |
| 54 | + authorize_url="https://accounts.google.com/o/oauth2/auth", |
53 | 55 | authorize_params=None,
|
54 |
| - api_base_url='https://www.googleapis.com/oauth2/v1/', |
55 |
| - userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo', |
56 |
| - client_kwargs={'scope': 'openid email profile'}, |
| 56 | + api_base_url="https://www.googleapis.com/oauth2/v1/", |
| 57 | + userinfo_endpoint="https://openidconnect.googleapis.com/v1/userinfo", |
| 58 | + client_kwargs={"scope": "openid email profile"}, |
57 | 59 | )
|
58 |
| - google = login_obj.auth.create_client('google') |
59 |
| - redirect_uri = url_for('google_authorize', _external=True) |
| 60 | + google = login_obj.auth.create_client("google") |
| 61 | + redirect_uri = url_for("google_authorize", _external=True) |
60 | 62 | return google.authorize_redirect(redirect_uri)
|
61 | 63 |
|
62 | 64 |
|
63 |
| -@app.route('/login/google/authorize') |
| 65 | +@app.route("/login/google/authorize") |
64 | 66 | def google_authorize():
|
65 |
| - google = login_obj.auth.create_client('google') |
66 |
| - token = google.authorize_access_token() |
67 |
| - resp = google.get('userinfo').json() |
68 |
| - return add_users(resp['name'], resp['email'], "google") |
| 67 | + google = login_obj.auth.create_client("google") |
| 68 | + try: |
| 69 | + token = google.authorize_access_token() |
| 70 | + resp = google.get("userinfo").json() |
| 71 | + return add_users(resp["name"], resp["email"], "google") |
| 72 | + except Exception: |
| 73 | + return redirect("/login/google") |
69 | 74 |
|
70 | 75 |
|
71 |
| -@app.route('/logout') |
| 76 | +@app.route("/logout") |
72 | 77 | def user_logout():
|
73 | 78 | for key in list(session.keys()):
|
74 | 79 | session.pop(key)
|
75 |
| - return redirect('/') |
| 80 | + return redirect("/") |
76 | 81 |
|
77 | 82 |
|
78 |
| -if __name__ == '__main__': |
79 |
| - app.run(debug=True) |
| 83 | +if __name__ == "__main__": |
| 84 | + app.run(debug=True, host="0.0.0.0", port=5000) |
0 commit comments