Compare commits
1 Commits
ea7b6391c1
...
auth-with-
Author | SHA1 | Date | |
---|---|---|---|
35cafe1780 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1 @@
|
|||||||
/.env
|
/.env
|
||||||
*.pyc
|
|
||||||
|
1
Pipfile
1
Pipfile
@ -14,7 +14,6 @@ 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,13 +43,6 @@ 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
|
||||||
|
|
||||||
|
25
ki/auth.py
25
ki/auth.py
@ -5,27 +5,40 @@ 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:
|
||||||
|
|
||||||
|
try:
|
||||||
users = yaml.safe_load(auth_file_stream)
|
users = yaml.safe_load(auth_file_stream)
|
||||||
|
|
||||||
if username not in users:
|
except yaml.YAMLError:
|
||||||
return None
|
print('Could not parse auth.yml.')
|
||||||
|
|
||||||
|
try:
|
||||||
auth_user = users[username]
|
auth_user = users[username]
|
||||||
|
|
||||||
if auth_user["password"] != password:
|
if auth_user["password"] != password:
|
||||||
|
raise UserWrongCredentialsException
|
||||||
|
|
||||||
|
except (UserWrongCredentialsException, KeyError):
|
||||||
|
print('Wrong username/password combination')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
else:
|
||||||
user = User.query.filter(User.auth_id.__eq__(username)).first()
|
user = User.query.filter(User.auth_id.__eq__(username)).first()
|
||||||
|
|
||||||
if user is None:
|
|
||||||
user = User(auth_id=username)
|
|
||||||
db.session.add(user)
|
|
||||||
|
|
||||||
token = Token(token=str(uuid.uuid4()), user=user)
|
token = Token(token=str(uuid.uuid4()), user=user)
|
||||||
|
|
||||||
db.session.add(token)
|
db.session.add(token)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
@ -22,14 +22,13 @@ class TestSkillsEndpoint(unittest.TestCase):
|
|||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
{
|
{
|
||||||
"skills": [{
|
"skills": [
|
||||||
"id": 1,
|
{"id": 1, "name": "PHP"},
|
||||||
"name": "PHP"
|
{"id": 3, "name": "Python"}
|
||||||
}, {
|
]
|
||||||
"id": 3,
|
},
|
||||||
"name": "Python"
|
response.json
|
||||||
}]
|
)
|
||||||
}, response.json)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "main":
|
if __name__ == "main":
|
||||||
|
Reference in New Issue
Block a user