Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
c7dbaeabbb
|
|||
0911a73085
|
|||
1de73e99e3
|
|||
b44af0658a
|
|||
309b4c6ba8
|
|||
a921fb6a0f
|
|||
f56db65012
|
|||
ef0793be4e
|
|||
3f2442e259
|
|||
78439a7ed8
|
|||
0377278ea0
|
31
auth.py
31
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):
|
||||||
@ -29,6 +31,20 @@ class PasswordMismatchException(Exception):
|
|||||||
UserEntry = namedtuple('UserEntry', ['id', 'login', 'expiry', 'claims'])
|
UserEntry = namedtuple('UserEntry', ['id', 'login', 'expiry', 'claims'])
|
||||||
|
|
||||||
|
|
||||||
|
JWT_PRIV_KEY = ""
|
||||||
|
try:
|
||||||
|
JWT_PRIV_KEY = os.environ["JWT_PRIV_KEY"]
|
||||||
|
except KeyError:
|
||||||
|
with open('/opt/app/config/authservice.key', 'r') as f:
|
||||||
|
JWT_PRIV_KEY = f.read()
|
||||||
|
|
||||||
|
JWT_PUB_KEY = ""
|
||||||
|
try:
|
||||||
|
JWT_PUB_KEY = os.environ["JWT_PUB_KEY"]
|
||||||
|
except KeyError:
|
||||||
|
with open('/opt/app/config/authservice.pub', 'r') as f:
|
||||||
|
JWT_PUB_KEY = f.read()
|
||||||
|
|
||||||
|
|
||||||
def getUserEntryFromDB(application: str, login: str):
|
def getUserEntryFromDB(application: str, login: str):
|
||||||
conn = None
|
conn = None
|
||||||
@ -97,13 +113,14 @@ def generateToken(**args):
|
|||||||
"iss": JWT_ISSUER,
|
"iss": JWT_ISSUER,
|
||||||
"iat": int(timestamp),
|
"iat": int(timestamp),
|
||||||
"exp": int(timestamp + userEntry.expiry),
|
"exp": int(timestamp + userEntry.expiry),
|
||||||
"sub": str(userEntry.id)
|
"sub": str(userEntry.id),
|
||||||
|
"aud": application
|
||||||
}
|
}
|
||||||
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[claim[0]] = claim[1]
|
payload[claim[0]] = claim[1]
|
||||||
|
|
||||||
return jwt.encode(payload, JWT_SECRET, algorithm='RS256')
|
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 +136,11 @@ 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 generateTokenFromEnc(**args):
|
||||||
|
cryptContent = args["body"]
|
||||||
|
raise werkzeug.exceptions.NotImplemented("Stay tuned, will be added soon")
|
||||||
|
return str(cryptContent)
|
||||||
|
|
||||||
|
def getPubKey():
|
||||||
|
return JWT_PUB_KEY
|
||||||
|
32
openapi.yaml
32
openapi.yaml
@ -7,7 +7,7 @@ paths:
|
|||||||
/auth:
|
/auth:
|
||||||
post:
|
post:
|
||||||
tags: [ "JWT" ]
|
tags: [ "JWT" ]
|
||||||
summary: Return JWT token
|
summary: Accept login and password, return JWT token
|
||||||
operationId: auth.generateToken
|
operationId: auth.generateToken
|
||||||
requestBody:
|
requestBody:
|
||||||
content:
|
content:
|
||||||
@ -21,6 +21,23 @@ paths:
|
|||||||
'text/plain':
|
'text/plain':
|
||||||
schema:
|
schema:
|
||||||
type: string
|
type: string
|
||||||
|
/authe:
|
||||||
|
post:
|
||||||
|
tags: [ "JWT" ]
|
||||||
|
summary: Accept encrypted set of credentials, return JWT token
|
||||||
|
operationId: auth.generateTokenFromEnc
|
||||||
|
requestBody:
|
||||||
|
content:
|
||||||
|
'text/plain':
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: JWT token
|
||||||
|
content:
|
||||||
|
'text/plain':
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
/secret:
|
/secret:
|
||||||
get:
|
get:
|
||||||
tags: [ "JWT" ]
|
tags: [ "JWT" ]
|
||||||
@ -35,6 +52,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
|
||||||
|
|
||||||
|
|
9
testjwe.py
Normal file
9
testjwe.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from jose import jwe
|
||||||
|
|
||||||
|
|
||||||
|
JWT_PUB_KEY = os.environ["JWT_PUB_KEY"]
|
||||||
|
|
||||||
|
plainText = "BlaBlaBla123"
|
||||||
|
cryptText = jwe.encrypt(plainText, JWT_PUB_KEY, "A256GCM", "RSA-OAEP")
|
||||||
|
|
||||||
|
print(cryptText)
|
Reference in New Issue
Block a user