1 Commits

Author SHA1 Message Date
73b94e8aa2 jwt, first try not working 2021-01-24 20:41:08 +01:00
3 changed files with 274 additions and 228 deletions

View File

@ -20,7 +20,9 @@ RUN \
pip3 install connexion && \ pip3 install connexion && \
pip3 install connexion[swagger-ui] && \ pip3 install connexion[swagger-ui] && \
pip3 install uwsgi && \ pip3 install uwsgi && \
pip3 install flask-cors pip3 install flask-cors && \
pip3 install python-jose[cryptography] && \
pip3 install six
RUN \ RUN \
mkdir -p ${APP_DIR} && \ mkdir -p ${APP_DIR} && \

44
auth.py Normal file
View File

@ -0,0 +1,44 @@
import time
import connexion
import six
from werkzeug.exceptions import Unauthorized
from jose import JWTError, jwt
JWT_ISSUER = 'de.hottis.hausverwaltung'
JWT_SECRET = 'streng_geheim'
JWT_LIFETIME_SECONDS = 600
JWT_ALGORITHM = 'HS256'
def generate_token(user_id):
timestamp = _current_timestamp()
payload = {
"iss": JWT_ISSUER,
"iat": int(timestamp),
"exp": int(timestamp + JWT_LIFETIME_SECONDS),
"sub": str(user_id),
}
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
def decode_token(token):
try:
return jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
except JWTError as e:
six.raise_from(Unauthorized, e)
def get_secret(user, token_info) -> str:
return '''
You are user_id {user} and the secret is 'wbevuec'.
Decoded token claims: {token_info}.
'''.format(user=user, token_info=token_info)
def _current_timestamp() -> int:
return int(time.time())

View File

@ -1,7 +1,7 @@
openapi: 3.0.0 openapi: 3.0.0
info: info:
title: Hausverwaltung title: Hausverwaltung-JWT
version: "0.1" version: "0.2"
paths: paths:
/hv/objekte: /hv/objekte:
@ -12,12 +12,10 @@ paths:
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': type: array
schema: items:
type: array $ref: '#/components/Objekt'
items:
$ref: '#/components/schemas/Objekt'
404: 404:
description: No Objekte available description: No Objekte available
500: 500:
@ -30,16 +28,13 @@ paths:
parameters: parameters:
- name: id - name: id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': $ref: '#/components/Objekt'
schema:
$ref: '#/components/schemas/Objekt'
404: 404:
description: Objekt not found description: Objekt not found
500: 500:
@ -52,12 +47,10 @@ paths:
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': type: array
schema: items:
type: array $ref: '#/components/Wohnung'
items:
$ref: '#/components/schemas/Wohnung'
404: 404:
description: No Wohnung available description: No Wohnung available
500: 500:
@ -70,18 +63,15 @@ paths:
parameters: parameters:
- name: id - name: id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': type: array
schema: items:
type: array $ref: '#/components/Wohnung'
items:
$ref: '#/components/schemas/Wohnung'
404: 404:
description: No Wohnung available description: No Wohnung available
500: 500:
@ -94,16 +84,13 @@ paths:
parameters: parameters:
- name: id - name: id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': $ref: '#/components/Wohnung'
schema:
$ref: '#/components/schemas/Wohnung'
404: 404:
description: Wohnung not found description: Wohnung not found
500: 500:
@ -116,12 +103,10 @@ paths:
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': type: array
schema: items:
type: array $ref: '#/components/Mieter'
items:
$ref: '#/components/schemas/Mieter'
404: 404:
description: No Mieter available description: No Mieter available
500: 500:
@ -134,16 +119,13 @@ paths:
parameters: parameters:
- name: id - name: id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': $ref: '#/components/Mieter'
schema:
$ref: '#/components/schemas/Mieter'
404: 404:
description: Mieter not found description: Mieter not found
500: 500:
@ -156,16 +138,13 @@ paths:
parameters: parameters:
- name: id - name: id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': $ref: '#/components/Forderung'
schema:
$ref: '#/components/schemas/Forderung'
404: 404:
description: Forderung not found description: Forderung not found
500: 500:
@ -178,18 +157,15 @@ paths:
parameters: parameters:
- name: mieter_id - name: mieter_id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': type: array
schema: items:
type: array $ref: '#/components/Forderung'
items:
$ref: '#/components/schemas/Forderung'
404: 404:
description: No Forderung available description: No Forderung available
500: 500:
@ -202,16 +178,13 @@ paths:
parameters: parameters:
- name: id - name: id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': $ref: '#/components/Zahlung'
schema:
$ref: '#/components/schemas/Zahlung'
404: 404:
description: Zahlung not found description: Zahlung not found
500: 500:
@ -224,18 +197,15 @@ paths:
parameters: parameters:
- name: mieter_id - name: mieter_id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response. description: Successful response.
content: schema:
'application/json': type: array
schema: items:
type: array $ref: '#/components/Zahlung'
items:
$ref: '#/components/schemas/Zahlung'
404: 404:
description: No Zahlung available description: No Zahlung available
500: 500:
@ -248,23 +218,19 @@ paths:
parameters: parameters:
- name: mieter_id - name: mieter_id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
- name: year - name: year
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response description: Successful response
content: schema:
'application/json': type: array
schema: items:
type: array $ref: '#/components/ZahlungForderung'
items:
$ref: '#/components/schemas/ZahlungForderung'
404: 404:
description: No ZahlungForderung available description: No ZahlungForderung available
500: 500:
@ -277,159 +243,193 @@ paths:
parameters: parameters:
- name: mieter_id - name: mieter_id
in: path in: path
type: integer
required: true required: true
schema:
type: integer
- name: year - name: year
in: path in: path
type: integer
required: true required: true
schema:
type: integer
responses: responses:
200: 200:
description: Successful response description: Successful response
content: schema:
'application/json': $ref: '#/components/Saldo'
schema:
$ref: '#/components/schemas/Saldo'
404: 404:
description: Neither Forderungen nor Zahlungen available description: Neither Forderungen nor Zahlungen available
500: 500:
description: Some server error description: Some server error
# /hv/zahlung: /hv/zahlung:
# post: post:
# tags: [ "Zahlung" ] tags: [ "Zahlung" ]
# operationId: ZahlungenForderungen.put_zahlung operationId: ZahlungenForderungen.put_zahlung
# summary: Inserts a new Zahlung summary: Inserts a new Zahlung
# parameters: parameters:
# - name: zahlung - name: zahlung
# in: body in: body
# schema: schema:
# $ref: '#/components/schemas/Zahlung' $ref: '#/components/Zahlung'
# responses: responses:
# 202: 202:
# description: Zahlung successfully inserted description: Zahlung successfully inserted
# 500: 500:
# description: Some server or database error description: Some server or database error
/auth/{user_id}:
get:
tags: [ "jwt" ]
summary: Return JWT token
operationId: auth.generate_token
parameters:
- name: user_id
description: User unique identifier
in: path
required: true
example: 12
schema:
type: integer
responses:
'200':
description: JWT token
content:
'text/plain':
schema:
type: string
/secret:
get:
tags: [ "jwt" ]
summary: Return secret string
operationId: auth.get_secret
responses:
'200':
description: secret response
content:
'text/plain':
schema:
type: string
security:
- jwt: ['secret']
components: components:
schemas: Objekt:
Objekt: description: Objekt type
description: Objekt type type: object
type: object properties:
properties: id:
id: type: integer
type: integer shortname:
shortname: type: string
type: string flaeche:
flaeche: type: number
type: number Wohnung:
Wohnung: description: Wohnung type
description: Wohnung type type: object
type: object properties:
properties: id:
id: type: integer
type: integer objekt:
objekt: type: integer
type: integer shortname:
shortname: type: string
type: string flaeche:
flaeche: type: number
type: number objekt_shortname:
objekt_shortname: type: string
type: string Mieter:
Mieter: description: Mieter type
description: Mieter type type: object
type: object properties:
properties: id:
id: type: integer
type: integer objekt:
objekt: type: integer
type: integer wohnung:
wohnung: type: integer
type: integer wohnung_shortname:
wohnung_shortname: type: string
type: string objekt_shortname:
objekt_shortname: type: string
type: string anrede:
anrede: type: string
type: string vorname:
vorname: type: string
type: string nachname:
nachname: type: string
type: string strasse:
strasse: type: string
type: string plz:
plz: type: string
type: string ort:
ort: type: string
type: string telefon:
telefon: type: string
type: string einzug:
einzug: type: string
type: string auszug:
auszug: type: string
type: string Forderung:
Forderung: description: Forderung type
description: Forderung type type: object
type: object properties:
properties: id:
id: type: integer
type: integer mieter:
mieter: type: integer
type: integer datum:
datum: type: string
type: string betrag:
betrag: type: number
type: number kommentar:
kommentar: type: string
type: string ref_wohnung:
ref_wohnung: type: number
type: number Zahlung:
Zahlung: description: Zahlung type
description: Zahlung type type: object
type: object properties:
properties: id:
id: type: integer
type: integer mieter:
mieter: type: integer
type: integer datum_ist:
datum_ist: type: string
type: string datum_soll:
datum_soll: type: string
type: string betrag:
betrag: type: number
type: number kommentar:
kommentar: type: string
type: string ZahlungForderung:
ZahlungForderung: description: ZahlungForderung type
description: ZahlungForderung type type: object
type: object properties:
properties: zf_type:
zf_type: type: string
type: string id:
id: type: integer
type: integer datum_soll:
datum_soll: type: string
type: string datum_ist:
datum_ist: type: string
type: string betrag_zahlung:
betrag_zahlung: type: number
type: number betrag_forderung:
betrag_forderung: type: number
type: number kommentar:
kommentar: type: string
type: string mieter:
mieter: type: number
type: number Saldo:
Saldo: description: Saldo type
description: Saldo type type: object
type: object properties:
properties: forderungen:
forderungen: type: number
type: number zahlungen:
zahlungen: type: number
type: number saldo:
saldo: type: number
type: number securitySchemes:
jwt:
type: http
scheme: bearer
bearerFormat: JWT
x-bearerInfoFunc: auth.decode_token