Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
78439a7ed8
|
|||
0377278ea0
|
|||
49e8aa43b4
|
|||
35a997774f
|
|||
08734cb82c
|
|||
875301b437
|
|||
da06065959
|
|||
fe007cbfe7
|
@ -39,7 +39,7 @@ try:
|
|||||||
|
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
INSERT INTO users (login, password)
|
INSERT INTO users (login, pwhash)
|
||||||
VALUES(?, ?)
|
VALUES(?, ?)
|
||||||
""", [user, pwhash])
|
""", [user, pwhash])
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
|
20
auth.py
20
auth.py
@ -13,7 +13,9 @@ DB_HOST = os.environ["DB_HOST"]
|
|||||||
DB_NAME = os.environ["DB_NAME"]
|
DB_NAME = os.environ["DB_NAME"]
|
||||||
|
|
||||||
JWT_ISSUER = os.environ["JWT_ISSUER"]
|
JWT_ISSUER = os.environ["JWT_ISSUER"]
|
||||||
JWT_SECRET = os.environ["JWT_SECRET"]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class NoUserException(Exception):
|
class NoUserException(Exception):
|
||||||
@ -28,6 +30,13 @@ class PasswordMismatchException(Exception):
|
|||||||
|
|
||||||
UserEntry = namedtuple('UserEntry', ['id', 'login', 'expiry', 'claims'])
|
UserEntry = namedtuple('UserEntry', ['id', 'login', 'expiry', 'claims'])
|
||||||
|
|
||||||
|
JWT_PRIV_KEY = ""
|
||||||
|
with open('/opt/app/config/authservice.key', 'r') as f:
|
||||||
|
JWT_PRIV_KEY = f.read().replace('\n','')
|
||||||
|
|
||||||
|
JWT_PUB_KEY = ""
|
||||||
|
with open('/opt/app/config/authservice.pub', 'r') as f:
|
||||||
|
JWT_PUB_KEY = f.read().replace('\n','')
|
||||||
|
|
||||||
|
|
||||||
def getUserEntryFromDB(application: str, login: str):
|
def getUserEntryFromDB(application: str, login: str):
|
||||||
@ -57,7 +66,7 @@ def getUserEntryFromDB(application: str, login: str):
|
|||||||
for claimObj in cur:
|
for claimObj in cur:
|
||||||
print("DEBUG: getUserEntryFromDB: add claim {} -> {}".format(claimObj["key"], claimObj["value"]))
|
print("DEBUG: getUserEntryFromDB: add claim {} -> {}".format(claimObj["key"], claimObj["value"]))
|
||||||
if claimObj["key"] in claims:
|
if claimObj["key"] in claims:
|
||||||
if isinstance(claimObj["key"], list):
|
if isinstance(claims[claimObj["key"]], list):
|
||||||
claims[claimObj["key"]].append(claimObj["value"])
|
claims[claimObj["key"]].append(claimObj["value"])
|
||||||
else:
|
else:
|
||||||
claims[claimObj["key"]] = [ claims[claimObj["key"]] ]
|
claims[claimObj["key"]] = [ claims[claimObj["key"]] ]
|
||||||
@ -101,9 +110,9 @@ def generateToken(**args):
|
|||||||
}
|
}
|
||||||
for claim in userEntry.claims.items():
|
for claim in userEntry.claims.items():
|
||||||
# print("DEBUG: generateToken: add claim {} -> {}".format(claim[0], claim[1]))
|
# print("DEBUG: generateToken: add claim {} -> {}".format(claim[0], claim[1]))
|
||||||
payload["x-{}".format(claim[0])] = claim[1]
|
payload[claim[0]] = claim[1]
|
||||||
|
|
||||||
return jwt.encode(payload, JWT_ISSUER)
|
return jwt.encode(payload, JWT_PRIV_KEY, algorithm='RS256')
|
||||||
except NoUserException:
|
except NoUserException:
|
||||||
print("ERROR: generateToken: no user found, login or application wrong")
|
print("ERROR: generateToken: no user found, login or application wrong")
|
||||||
raise werkzeug.exceptions.Unauthorized()
|
raise werkzeug.exceptions.Unauthorized()
|
||||||
@ -119,3 +128,6 @@ def generateToken(**args):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("ERROR: generateToken: unspecific exception: {}".format(str(e)))
|
print("ERROR: generateToken: unspecific exception: {}".format(str(e)))
|
||||||
raise werkzeug.exceptions.Unauthorized()
|
raise werkzeug.exceptions.Unauthorized()
|
||||||
|
|
||||||
|
def getPubKey():
|
||||||
|
return JWT_PUB_KEY
|
||||||
|
13
openapi.yaml
13
openapi.yaml
@ -35,6 +35,19 @@ paths:
|
|||||||
type: string
|
type: string
|
||||||
security:
|
security:
|
||||||
- jwt: ['secret']
|
- jwt: ['secret']
|
||||||
|
/pubkey:
|
||||||
|
get:
|
||||||
|
tags: [ "JWT" ]
|
||||||
|
summary: Get the public key of this issuer
|
||||||
|
operationId: auth.getPubKey
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: public key
|
||||||
|
content:
|
||||||
|
'text/plain':
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
|
||||||
|
|
||||||
components:
|
components:
|
||||||
securitySchemes:
|
securitySchemes:
|
||||||
|
13
readme.md
Normal file
13
readme.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
Generate the RSA key pair using:
|
||||||
|
|
||||||
|
|
||||||
|
Private key (keep it secret!):
|
||||||
|
|
||||||
|
openssl genrsa -out authservice.key 2048
|
||||||
|
|
||||||
|
|
||||||
|
Extract the public key (publish it):
|
||||||
|
|
||||||
|
openssl rsa -in authservice.pem -outform PEM -pubout -out authservice.pub
|
||||||
|
|
||||||
|
|
@ -3,7 +3,7 @@ from flask_cors import CORS
|
|||||||
|
|
||||||
# instantiate the webservice
|
# instantiate the webservice
|
||||||
app = connexion.App(__name__)
|
app = connexion.App(__name__)
|
||||||
app.add_api('openapi.yaml', options = {"swagger_ui": False})
|
app.add_api('openapi.yaml', options = {"swagger_ui": True})
|
||||||
|
|
||||||
# CORSify it - otherwise Angular won't accept it
|
# CORSify it - otherwise Angular won't accept it
|
||||||
CORS(app.app)
|
CORS(app.app)
|
||||||
|
Reference in New Issue
Block a user