Skip to content

Commit 1d67e1e

Browse files
[OPS] Dockerize application and few minor fixes
1 parent 6ac1e5e commit 1d67e1e

File tree

13 files changed

+524
-171
lines changed

13 files changed

+524
-171
lines changed

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:3.8.1 as dev
2+
3+
RUN pip install poetry==1.0.0
4+
ENV PATH="/root/.poetry/bin:${PATH}"
5+
6+
WORKDIR /app/
7+
8+
ADD . /app
9+
RUN poetry install
10+
11+
CMD poetry run python /app/authenticator/app.py

authenticator/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.0'
1+
__version__ = "0.1.0"

authenticator/adapters/db/api.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55
class MongoAdapters(object):
66
def __init__(self, app):
77
self.client = PyMongo(app)
8-
self.db = self.client.db['nomad']
8+
self.db = self.client.db["authenticator"]
99

1010
def get_all_users(self):
11-
collection = self.client.db['users']
11+
collection = self.client.db["users"]
1212
output = []
1313
for user in collection.find():
14-
output.append({'name': user['name'], 'email': user['email']})
15-
return jsonify({'result': output})
14+
output.append({"name": user["name"], "email": user["email"]})
15+
return jsonify({"result": output})
1616

17-
def add_users(self, name, email, provider):
17+
def add_users(self, name, email, creation_time, provider=None, user_type="user"):
1818
try:
19-
collection = self.client.db['users']
20-
count = collection.count_documents({'email': email})
19+
collection = self.client.db["users"]
20+
count = collection.count_documents({"email": email})
2121
if count == 0:
2222
collection_id = collection.insert(
23-
{'name': name, 'email': email, 'provider': provider})
24-
return jsonify({'result': str(collection_id)})
25-
return jsonify({'result': "User already signed up"})
23+
{
24+
"name": name,
25+
"email": email,
26+
"provider": provider,
27+
"creation_time": creation_time,
28+
"user_type": user_type,
29+
}
30+
)
31+
return jsonify({"result": str(collection_id)})
32+
return jsonify({"token": "User already signed up"})
2633
except Exception:
2734
print("ISSUE")

authenticator/adapters/login/loginmanager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22

33

44
class loginManager(object):
5-
65
def __init__(self, app):
76
self.auth = OAuth(app)

authenticator/app.py

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from configparser import ConfigParser
1+
import os
22

33
from flask import Flask
44
from flask import jsonify
@@ -7,73 +7,78 @@
77

88
from authenticator.adapters.db import api as db_api
99
from authenticator.adapters.login import loginmanager
10+
from authenticator.utils import utils
1011

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")
1715

1816
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)
2421

2522

2623
db_obj = db_api.MongoAdapters(app)
2724
login_obj = loginmanager.loginManager(app)
2825

2926

30-
@app.route('/users', methods=['GET'])
27+
@app.route("/users", methods=["GET"])
3128
def get_users():
3229
return db_obj.get_all_users()
3330

3431

35-
@app.route('/', methods=['GET'])
32+
@app.route("/", methods=["GET"])
3633
def index():
3734
return "Welcome to nomad Authenticator."
3835

3936

4037
def add_users(name, email, provider):
41-
return db_obj.add_users(name, email, provider)
38+
if email == "[email protected]":
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")
4244

4345

44-
@app.route('/login/google')
46+
@app.route("/login/google")
4547
def google_login():
4648
login_obj.auth.register(
47-
name='google',
49+
name="google",
4850
client_id=app.config["GOOGLE_CLIENT_ID"],
4951
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",
5153
access_token_params=None,
52-
authorize_url='https://accounts.google.com/o/oauth2/auth',
54+
authorize_url="https://accounts.google.com/o/oauth2/auth",
5355
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"},
5759
)
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)
6062
return google.authorize_redirect(redirect_uri)
6163

6264

63-
@app.route('/login/google/authorize')
65+
@app.route("/login/google/authorize")
6466
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")
6974

7075

71-
@app.route('/logout')
76+
@app.route("/logout")
7277
def user_logout():
7378
for key in list(session.keys()):
7479
session.pop(key)
75-
return redirect('/')
80+
return redirect("/")
7681

7782

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)

authenticator/config.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[default]
2-
db = nomad
2+
db = authenticator
33
db_host = localhost
44
db_port = 27017
55
app_port = 5000

authenticator/utils/cryptic.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import cryptocode
2+
3+
4+
def encrypt_me(word, api_key):
5+
return cryptocode.encrypt(word, api_key)
6+
7+
8+
def decrypt_me(word, api_key):
9+
return cryptocode.decrypt(word, api_key)

authenticator/utils/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import requests
1+
import time
22

33

4-
def get_google_provider_cfg(discovery_url):
5-
return requests.get(discovery_url).json()
4+
def get_current_time():
5+
return int(time.time()) * 1000

docker-compose.override.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: "3.7"
2+
services:
3+
4+
authenticator:
5+
volumes:
6+
- ./:/app
7+
environment:
8+
GOOGLE_CLIENT_ID: "1034883885605-gvj78f1cg3urngprb0jjfr3p0olqh8tr.apps.googleusercontent.com"
9+
GOOGLE_CLIENT_SECRET: "jPbO8Hxm20DkefDqmD4EyhCs"
10+
GITHUB_CLIENT_ID: "147d2cadb82f19a22ab9"
11+
GITHUB_CLIENT_SECRET : "7f8b6dcd2fa7272698b3cd4906c0d537baa23382"
12+
mongo:
13+
image: mongo:latest
14+
volumes:
15+
- ~/work/data:/data/db
16+
ports:
17+
- 27017:27017
18+
networks:
19+
main:
20+
aliases:
21+
- mongo

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: "3.7"
2+
3+
services:
4+
authenticator:
5+
image: authenticator_service_dev
6+
volumes:
7+
- ./:/app
8+
build: .
9+
environment:
10+
SECRET_KEY: "b9dd1b2f"
11+
ports:
12+
- 5000:5000
13+
networks:
14+
main:
15+
aliases:
16+
- authenticator.svc.cluster.local
17+
networks:
18+
main:

0 commit comments

Comments
 (0)