Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SMART
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
3
Issues
3
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UNI-KLU
SMART
Commits
f0d7d0b4
Commit
f0d7d0b4
authored
4 years ago
by
manuel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added users, extracted jwt secret, protected endpoints
parent
1988488c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
24 additions
and
8 deletions
+24
-8
.swagger.yml.swp
...stage-discovery-microservice/app/configs/.swagger.yml.swp
+0
-0
security_util.py
src/modules/security/security_util.py
+4
-2
swagger.yml
src/rest-gateway/app/configs/swagger.yml
+3
-1
user.py
src/rest-gateway/app/routes/user.py
+1
-1
token_service.py
src/rest-gateway/app/services/token_service.py
+16
-4
No files found.
src/data-hub/role-stage-discovery-microservice/app/configs/.swagger.yml.swp
0 → 100644
View file @
f0d7d0b4
File added
This diff is collapsed.
Click to expand it.
src/modules/security/security_util.py
View file @
f0d7d0b4
...
@@ -4,6 +4,7 @@ import network_constants
...
@@ -4,6 +4,7 @@ import network_constants
import
requests
import
requests
import
json
import
json
from
typing
import
Dict
,
List
from
typing
import
Dict
,
List
import
logging
class
TokenStash
:
class
TokenStash
:
'''
'''
...
@@ -82,7 +83,8 @@ def _verify(token:str, roles:List[str]=[]):
...
@@ -82,7 +83,8 @@ def _verify(token:str, roles:List[str]=[]):
token_info
=
decodeToken
(
token
,
roles
=
roles
)
token_info
=
decodeToken
(
token
,
roles
=
roles
)
return
token_info
return
token_info
except
Exception
as
e
:
except
Exception
as
e
:
print
(
"ERROR DURING TOKEN VALIDATION: "
+
str
(
e
))
LOGGER
=
logging
.
getLogger
(
__name__
)
LOGGER
.
error
(
"Token invalid: "
+
str
(
e
))
return
None
return
None
...
@@ -90,4 +92,4 @@ def verifyTokenRegular(token, required_scopes):
...
@@ -90,4 +92,4 @@ def verifyTokenRegular(token, required_scopes):
return
_verify
(
token
)
return
_verify
(
token
)
def
verifyTokenAdmin
(
token
,
required_scopes
):
def
verifyTokenAdmin
(
token
,
required_scopes
):
return
_verify
(
token
,
roles
=
[
"a"
])
return
_verify
(
token
,
roles
=
[
"a"
])
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/rest-gateway/app/configs/swagger.yml
View file @
f0d7d0b4
...
@@ -109,6 +109,8 @@ paths:
...
@@ -109,6 +109,8 @@ paths:
'
400'
:
'
400'
:
description
:
wrong username or password
description
:
wrong username or password
post
:
post
:
security
:
-
JwtAdmin
:
[]
operationId
:
"
routes.user.add"
operationId
:
"
routes.user.add"
tags
:
tags
:
-
"
User"
-
"
User"
...
@@ -246,4 +248,4 @@ definitions:
...
@@ -246,4 +248,4 @@ definitions:
type
:
"
string"
type
:
"
string"
Metadata
:
Metadata
:
type
:
"
string"
type
:
"
string"
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/rest-gateway/app/routes/user.py
View file @
f0d7d0b4
...
@@ -85,4 +85,4 @@ def all():
...
@@ -85,4 +85,4 @@ def all():
users
=
UserService
.
_repository
.
all
()
users
=
UserService
.
_repository
.
all
()
return
str
(
users
)
return
str
(
users
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/rest-gateway/app/services/token_service.py
View file @
f0d7d0b4
...
@@ -6,7 +6,6 @@ import jwt
...
@@ -6,7 +6,6 @@ import jwt
from
datetime
import
datetime
,
timedelta
from
datetime
import
datetime
,
timedelta
from
typing
import
Dict
from
typing
import
Dict
SIGNING_KEY
=
"yteNrMy6142WKwp8fKfrHkS5nlFpxtHgOXJh1ZPsOrV_gTcsO9eMY7aB7HUzRbTRO9dmZhCl3FdPtuvMe3K8aBA_wc2MmHRo8IkUIGmvUJGsAxKFClN_6oNW5fEvoeVKiL1krA-qjWbR_em-WksePgPoTsySW7QbKdi4f7cwuyK2_JZ2fQj9hDKlfJ2GzMXkKiWcfyCTr30yC6BviAFeRDD_Bpvg6znsrXr53Tq66hnwDwQ6QU7aHVu-bERblKZTYuvkSxsov6yRMEVWQoiuBITsQtIOcgSWK4Dy3BjSbqoIcKw3WG-s3wx1lTen19QbEu8vJC64e0iGeGDWT6vbtg"
TOKEN_VALIDITY_IN_DAYS
=
1
TOKEN_VALIDITY_IN_DAYS
=
1
def
verifyTokenRegular
(
token
,
required_scopes
)
->
Dict
:
def
verifyTokenRegular
(
token
,
required_scopes
)
->
Dict
:
...
@@ -42,6 +41,12 @@ class TokenService:
...
@@ -42,6 +41,12 @@ class TokenService:
user
.
last_login
=
str
(
datetime
.
now
())
user
.
last_login
=
str
(
datetime
.
now
())
UserService
.
update
(
user
)
UserService
.
update
(
user
)
@
staticmethod
def
read_secret
()
->
str
:
with
open
(
'/srv/articonf/jwt_secret.txt'
,
'r'
)
as
file
:
secret
=
file
.
read
()
.
replace
(
'
\n
'
,
''
)
return
secret
@
staticmethod
@
staticmethod
def
generate_token
(
user
:
User
)
->
str
:
def
generate_token
(
user
:
User
)
->
str
:
'''
'''
...
@@ -50,6 +55,8 @@ class TokenService:
...
@@ -50,6 +55,8 @@ class TokenService:
- created_at
- created_at
- valid_until
- valid_until
'''
'''
created_at
=
datetime
.
now
()
created_at
=
datetime
.
now
()
valid_until
=
created_at
+
timedelta
(
days
=
1
)
valid_until
=
created_at
+
timedelta
(
days
=
1
)
return
jwt
.
encode
(
return
jwt
.
encode
(
...
@@ -58,7 +65,8 @@ class TokenService:
...
@@ -58,7 +65,8 @@ class TokenService:
'created_at'
:
str
(
created_at
),
'created_at'
:
str
(
created_at
),
'valid_until'
:
str
(
valid_until
),
'valid_until'
:
str
(
valid_until
),
},
},
SIGNING_KEY
,
algorithm
=
'HS256'
TokenService
.
read_secret
(),
algorithm
=
'HS256'
)
.
decode
(
"utf-8"
)
)
.
decode
(
"utf-8"
)
@
staticmethod
@
staticmethod
...
@@ -80,7 +88,11 @@ class TokenService:
...
@@ -80,7 +88,11 @@ class TokenService:
token
=
token
[
7
:]
token
=
token
[
7
:]
try
:
try
:
payload
=
jwt
.
decode
(
token
,
SIGNING_KEY
,
algorithms
=
[
'HS256'
])
payload
=
jwt
.
decode
(
token
,
TokenService
.
read_secret
(),
algorithms
=
[
'HS256'
]
)
except
:
except
:
raise
ValueError
(
'Invalid JWT token (decoding failed)'
)
raise
ValueError
(
'Invalid JWT token (decoding failed)'
)
...
@@ -98,4 +110,4 @@ class TokenService:
...
@@ -98,4 +110,4 @@ class TokenService:
if
now
<=
token_created_at
or
now
>=
valid_until
:
if
now
<=
token_created_at
or
now
>=
valid_until
:
raise
ValueError
(
'Invalid JWT token (token expired)'
)
raise
ValueError
(
'Invalid JWT token (token expired)'
)
return
user
return
user
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment