Compare commits
7 Commits
auth-with-
...
d96dfa8800
Author | SHA1 | Date | |
---|---|---|---|
d96dfa8800
|
|||
2f0dd2ab9f | |||
ea7b6391c1 | |||
cbf3002b93 | |||
59de00527d | |||
6d4f933585 | |||
1390dfa8e6 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/.env
|
/.env
|
||||||
|
*.pyc
|
||||||
|
1
Pipfile
1
Pipfile
@ -14,6 +14,7 @@ pyyaml = "~=5.4.1"
|
|||||||
|
|
||||||
[dev-packages]
|
[dev-packages]
|
||||||
flake8 = "~=3.9.2"
|
flake8 = "~=3.9.2"
|
||||||
|
yapf = "~=0.31.0"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.8"
|
python_version = "3.8"
|
||||||
|
@ -43,6 +43,13 @@ python -m unittest discover ki
|
|||||||
flake8
|
flake8
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Formatierung
|
||||||
|
|
||||||
|
Um ein einheitliches Quellcode-Erlebnis zu haben, kann der Code mit yapf neu formatiert werden:
|
||||||
|
|
||||||
|
```
|
||||||
|
yapf -i --recursive ki/
|
||||||
|
```
|
||||||
|
|
||||||
### Testbenutzer
|
### Testbenutzer
|
||||||
|
|
||||||
|
43
ki/auth.py
43
ki/auth.py
@ -5,41 +5,28 @@ from app import app, db
|
|||||||
from ki.models import User, Token
|
from ki.models import User, Token
|
||||||
|
|
||||||
|
|
||||||
class UserWrongCredentialsException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class UserAllreadyLoggedInException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def auth(username, password):
|
def auth(username, password):
|
||||||
auth_file_path = app.config["KI_DATA_DIR"] + "/auth.yml"
|
auth_file_path = app.config["KI_DATA_DIR"] + "/auth.yml"
|
||||||
|
|
||||||
with open(auth_file_path, "r") as auth_file_stream:
|
with open(auth_file_path, "r") as auth_file_stream:
|
||||||
|
users = yaml.safe_load(auth_file_stream)
|
||||||
|
|
||||||
try:
|
if username not in users:
|
||||||
users = yaml.safe_load(auth_file_stream)
|
|
||||||
|
|
||||||
except yaml.YAMLError:
|
|
||||||
print('Could not parse auth.yml.')
|
|
||||||
|
|
||||||
try:
|
|
||||||
auth_user = users[username]
|
|
||||||
|
|
||||||
if auth_user["password"] != password:
|
|
||||||
raise UserWrongCredentialsException
|
|
||||||
|
|
||||||
except (UserWrongCredentialsException, KeyError):
|
|
||||||
print('Wrong username/password combination')
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
else:
|
auth_user = users[username]
|
||||||
user = User.query.filter(User.auth_id.__eq__(username)).first()
|
|
||||||
|
|
||||||
token = Token(token=str(uuid.uuid4()), user=user)
|
if auth_user["password"] != password:
|
||||||
|
return None
|
||||||
|
|
||||||
db.session.add(token)
|
user = User.query.filter(User.auth_id.__eq__(username)).first()
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
return token
|
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()
|
||||||
|
|
||||||
|
return token
|
||||||
|
@ -1 +1 @@
|
|||||||
from ki import models, commands, routes # noqa
|
from ki import models, commands, routes # noqa
|
||||||
|
@ -99,7 +99,7 @@ def login():
|
|||||||
if token is None:
|
if token is None:
|
||||||
return make_response({}, 403)
|
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")
|
@app.route("/users/<user_id>/profile")
|
||||||
|
@ -22,13 +22,14 @@ class TestSkillsEndpoint(unittest.TestCase):
|
|||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
{
|
{
|
||||||
"skills": [
|
"skills": [{
|
||||||
{"id": 1, "name": "PHP"},
|
"id": 1,
|
||||||
{"id": 3, "name": "Python"}
|
"name": "PHP"
|
||||||
]
|
}, {
|
||||||
},
|
"id": 3,
|
||||||
response.json
|
"name": "Python"
|
||||||
)
|
}]
|
||||||
|
}, response.json)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "main":
|
if __name__ == "main":
|
||||||
|
Reference in New Issue
Block a user