Compare commits
10 Commits
auth-with-
...
a349eff9b0
Author | SHA1 | Date | |
---|---|---|---|
a349eff9b0
|
|||
9e48953fc3 | |||
d96dfa8800
|
|||
2f0dd2ab9f | |||
ea7b6391c1 | |||
3dcba71a6d
|
|||
cbf3002b93 | |||
59de00527d | |||
6d4f933585 | |||
1390dfa8e6 |
17
.drone.yml
17
.drone.yml
@ -4,7 +4,7 @@ type: docker
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: qa
|
||||
- name: qa_main
|
||||
image: python:3.8-alpine
|
||||
commands:
|
||||
- apk add --no-cache gcc g++ musl-dev python3-dev
|
||||
@ -12,3 +12,18 @@ steps:
|
||||
- pipenv install --dev
|
||||
- pipenv run flake8
|
||||
- pipenv run python -m unittest discover ki
|
||||
when:
|
||||
branch:
|
||||
- main
|
||||
|
||||
- name: qa_pr
|
||||
image: python:3.8-alpine
|
||||
commands:
|
||||
- apk add --no-cache gcc g++ musl-dev python3-dev
|
||||
- pip3 install pipenv
|
||||
- pipenv install --dev
|
||||
- pipenv run flake8
|
||||
- pipenv run python -m unittest discover ki
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/.env
|
||||
*.pyc
|
||||
|
1
Pipfile
1
Pipfile
@ -14,6 +14,7 @@ pyyaml = "~=5.4.1"
|
||||
|
||||
[dev-packages]
|
||||
flake8 = "~=3.9.2"
|
||||
yapf = "~=0.31.0"
|
||||
|
||||
[requires]
|
||||
python_version = "3.8"
|
||||
|
@ -21,7 +21,6 @@ rm data/ki.sqlite
|
||||
cp env.dev .env
|
||||
pipenv install --dev
|
||||
pipenv shell
|
||||
export FLASK_APP=app.py
|
||||
flask db upgrade
|
||||
flask seed
|
||||
flask run
|
||||
@ -43,6 +42,13 @@ python -m unittest discover ki
|
||||
flake8
|
||||
```
|
||||
|
||||
### Formatierung
|
||||
|
||||
Um ein einheitliches Quellcode-Erlebnis zu haben, kann der Code mit yapf neu formatiert werden:
|
||||
|
||||
```
|
||||
yapf -i --recursive ki/
|
||||
```
|
||||
|
||||
### Testbenutzer
|
||||
|
||||
|
10
app.py
10
app.py
@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv, find_dotenv
|
||||
@ -7,9 +8,14 @@ from flask_migrate import Migrate
|
||||
|
||||
load_dotenv(find_dotenv())
|
||||
|
||||
loglevel = os.getenv("KI_LOGLEVEL", logging.WARNING)
|
||||
loglevel = int(loglevel)
|
||||
logging.basicConfig(level=loglevel)
|
||||
logging.debug("Hello from KI")
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("SQLALCHEMY_DATABASE_URI")
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("SQLALCHEMY_DATABASE_URI")
|
||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||
app.config["KI_DATA_DIR"] = os.path.dirname(__file__) + "/data"
|
||||
app.config["KI_AUTH"] = os.getenv("KI_AUTH")
|
||||
db = SQLAlchemy(app)
|
||||
|
14
drone.yml
14
drone.yml
@ -1,14 +0,0 @@
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: qa
|
||||
image: python3.8-alpine
|
||||
commands:
|
||||
- apk add --no-cache gcc g++ musl-dev python3-dev
|
||||
- pip3 install pipenv
|
||||
- pipenv install --system
|
||||
- flake8
|
||||
- python -m unittest discover ki
|
6
env.dev
6
env.dev
@ -1,3 +1,9 @@
|
||||
SQLALCHEMY_DATABASE_URI = 'sqlite:///data/ki.sqlite'
|
||||
|
||||
FLASK_APP=app.py
|
||||
FLASK_ENV=development
|
||||
|
||||
KI_AUTH=file
|
||||
|
||||
# 10 = debug
|
||||
KI_LOGLEVEL=10
|
||||
|
25
ki/auth.py
25
ki/auth.py
@ -5,40 +5,27 @@ from app import app, db
|
||||
from ki.models import User, Token
|
||||
|
||||
|
||||
class UserWrongCredentialsException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UserAllreadyLoggedInException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def auth(username, password):
|
||||
auth_file_path = app.config["KI_DATA_DIR"] + "/auth.yml"
|
||||
|
||||
with open(auth_file_path, "r") as auth_file_stream:
|
||||
|
||||
try:
|
||||
users = yaml.safe_load(auth_file_stream)
|
||||
|
||||
except yaml.YAMLError:
|
||||
print('Could not parse auth.yml.')
|
||||
if username not in users:
|
||||
return None
|
||||
|
||||
try:
|
||||
auth_user = users[username]
|
||||
|
||||
if auth_user["password"] != password:
|
||||
raise UserWrongCredentialsException
|
||||
|
||||
except (UserWrongCredentialsException, KeyError):
|
||||
print('Wrong username/password combination')
|
||||
return None
|
||||
|
||||
else:
|
||||
user = User.query.filter(User.auth_id.__eq__(username)).first()
|
||||
|
||||
token = Token(token=str(uuid.uuid4()), user=user)
|
||||
if user is None:
|
||||
user = User(auth_id=username)
|
||||
db.session.add(user)
|
||||
|
||||
token = Token(token=str(uuid.uuid4()), user=user)
|
||||
db.session.add(token)
|
||||
db.session.commit()
|
||||
|
||||
|
@ -99,7 +99,7 @@ def login():
|
||||
if token is None:
|
||||
return make_response({}, 403)
|
||||
|
||||
return make_response({"token": token.token})
|
||||
return make_response({"token": token.token, "user_id": token.user_id})
|
||||
|
||||
|
||||
@app.route("/users/<user_id>/profile")
|
||||
|
@ -22,13 +22,14 @@ class TestSkillsEndpoint(unittest.TestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(
|
||||
{
|
||||
"skills": [
|
||||
{"id": 1, "name": "PHP"},
|
||||
{"id": 3, "name": "Python"}
|
||||
]
|
||||
},
|
||||
response.json
|
||||
)
|
||||
"skills": [{
|
||||
"id": 1,
|
||||
"name": "PHP"
|
||||
}, {
|
||||
"id": 3,
|
||||
"name": "Python"
|
||||
}]
|
||||
}, response.json)
|
||||
|
||||
|
||||
if __name__ == "main":
|
||||
|
Reference in New Issue
Block a user