Skip to content

Commit 00ad2f2

Browse files
author
Georgio Tunson
committed
initial commit
1 parent 8e922da commit 00ad2f2

File tree

18,607 files changed

+2436929
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

18,607 files changed

+2436929
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

src/.DS_Store

6 KB
Binary file not shown.

src/auth/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM python:3.10-slim-bullseye
2+
3+
RUN apt-get update \
4+
&& apt-get install -y --no-install-recommends --no-install-suggests \
5+
build-essential default-libmysqlclient-dev \
6+
&& pip install --no-cache-dir --upgrade pip
7+
8+
WORKDIR /app
9+
COPY ./requirements.txt /app
10+
RUN pip install --no-cache-dir --requirement /app/requirements.txt
11+
COPY . /app
12+
13+
EXPOSE 5000
14+
15+
CMD ["python3", "server.py"]

src/auth/init.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CREATE USER 'auth_user'@'localhost' IDENTIFIED BY 'Aauth123';
2+
3+
CREATE DATABASE auth;
4+
5+
GRANT ALL PRIVILEGES ON auth.* TO 'auth_user'@'localhost';
6+
7+
USE auth;
8+
9+
CREATE TABLE user (
10+
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
11+
email VARCHAR(255) NOT NULL UNIQUE,
12+
password VARCHAR(255) NOT NULL
13+
);
14+
15+
INSERT INTO user (email, password) VALUES ('georgio@email.com', 'Admin123');
16+
17+
18+
19+
20+
21+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: auth
5+
labels:
6+
app: auth
7+
spec:
8+
replicas: 2
9+
selector:
10+
matchLabels:
11+
app: auth
12+
strategy:
13+
type: RollingUpdate
14+
rollingUpdate:
15+
maxSurge: 3
16+
template:
17+
metadata:
18+
labels:
19+
app: auth
20+
spec:
21+
containers:
22+
- name: auth
23+
image: sweasytech/auth
24+
ports:
25+
- containerPort: 5000
26+
envFrom:
27+
- configMapRef:
28+
name: auth-configmap
29+
- secretRef:
30+
name: auth-secret

src/auth/manifests/configmap.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: auth-configmap
5+
data:
6+
MYSQL_HOST: host.minikube.internal
7+
MYSQL_USER: auth_user
8+
MYSQL_DB: auth
9+
MYSQL_PORT: "3306"

src/auth/manifests/secret.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: auth-secret
5+
stringData:
6+
MYSQL_PASSWORD: Auth123
7+
JWT_SECRET: sarcasm
8+
type: Opaque

src/auth/manifests/service.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: auth
5+
spec:
6+
selector:
7+
app: auth
8+
type: ClusterIP
9+
ports:
10+
- port: 5000
11+
targetPort: 5000
12+
protocol: TCP

src/auth/requirements.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
astroid==2.9.3
2+
click==8.0.3
3+
Flask==2.0.2
4+
Flask-MySQLdb==0.2.0
5+
isort==5.10.1
6+
itsdangerous==2.0.1
7+
jedi==0.18.1
8+
Jinja2==3.0.3
9+
lazy-object-proxy==1.7.1
10+
MarkupSafe==2.0.1
11+
mccabe==0.6.1
12+
mysqlclient==2.1.0
13+
parso==0.8.3
14+
platformdirs==2.4.1
15+
PyJWT==2.3.0
16+
pylint==2.12.2
17+
toml==0.10.2
18+
Werkzeug==2.0.2
19+
wrapt==1.13.3

src/auth/server.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import jwt, datetime, os
2+
from flask import Flask, request
3+
from flask_mysqldb import MySQL
4+
5+
server = Flask(__name__)
6+
mysql = MySQL(server)
7+
8+
# config
9+
server.config["MYSQL_HOST"] = os.environ.get("MYSQL_HOST")
10+
server.config["MYSQL_USER"] = os.environ.get("MYSQL_USER")
11+
server.config["MYSQL_PASSWORD"] = os.environ.get("MYSQL_PASSWORD")
12+
server.config["MYSQL_DB"] = os.environ.get("MYSQL_DB")
13+
server.config["MYSQL_PORT"] = os.environ.get("MYSQL_PORT")
14+
15+
16+
@server.route("/login", methods=["POST"])
17+
def login():
18+
auth = request.authorization
19+
if not auth:
20+
return "missing credentials", 401
21+
22+
# check db for username and password
23+
cur = mysql.connection.cursor()
24+
res = cur.execute(
25+
"SELECT email, password FROM user WHERE email=%s", (auth.username,)
26+
)
27+
28+
if res > 0:
29+
user_row = cur.fetchone()
30+
email = user_row[0]
31+
password = user_row[1]
32+
33+
if auth.username != email or auth.password != password:
34+
return "invalid credentials", 401
35+
else:
36+
return createJWT(auth.username, os.environ.get("JWT_SECRET"), True)
37+
else:
38+
return "invalide credentials", 401
39+
40+
41+
@server.route("/validate", method=["POST"])
42+
def validate():
43+
encoded_jwt = request.headers["Authorization"]
44+
45+
if not encoded_jwt:
46+
return "missing credentials", 401
47+
48+
encoded_jwt = encoded_jwt.split(" ")[1]
49+
50+
try:
51+
decoded = jwt.decode(
52+
encoded_jwt, os.environ.get("JWT_SECRET"), algorithm=["HS256"]
53+
)
54+
except:
55+
return "not authorized", 403
56+
57+
return decoded, 200
58+
59+
60+
def createJWT(username, secret, authz):
61+
return jwt.encode(
62+
{
63+
"username": username,
64+
"exp": datetime.datetime.now(tz=datetime.timezone.utc)
65+
+ datetime.timedelta(days=1),
66+
"iat": datetime.datetime.utcnow(),
67+
"admin": authz,
68+
},
69+
secret,
70+
algorithm="HS256",
71+
)
72+
73+
74+
if __name__ == "__main__":
75+
server.run(host="0.0.0.0", port=5000)

0 commit comments

Comments
 (0)