Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
shared-libs
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pi-Lab
shared-libs
Commits
3e6e67dd
Commit
3e6e67dd
authored
2 years ago
by
istmxrein
Browse files
Options
Downloads
Patches
Plain Diff
add common authentication functions
parent
c751dad7
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
pilab/auth.py
+55
-0
55 additions, 0 deletions
pilab/auth.py
setup.py
+2
-2
2 additions, 2 deletions
setup.py
with
59 additions
and
2 deletions
.gitignore
0 → 100644
+
2
−
0
View file @
3e6e67dd
**/.idea
**/pilab.egg-info
\ No newline at end of file
This diff is collapsed.
Click to expand it.
pilab/auth.py
0 → 100644
+
55
−
0
View file @
3e6e67dd
import
logging
from
typing
import
Optional
,
List
,
Dict
from
fastapi
import
Header
,
HTTPException
logger
=
logging
.
getLogger
(
__name__
)
ADMIN_GROUPS
=
[
"
/admin
"
]
CUBE_ACCESS_LIST
:
Dict
[
int
,
List
[
str
]]
=
{}
def
is_admin
(
x_forwarded_groups
:
str
):
admin
=
False
if
x_forwarded_groups
and
x_forwarded_groups
is
not
None
:
for
group
in
ADMIN_GROUPS
:
if
group
in
x_forwarded_groups
:
admin
=
True
return
admin
def
get_username
(
usernames
:
List
[
str
]):
for
name
in
usernames
:
if
name
and
name
is
not
None
:
return
name
return
None
async
def
get_user
(
x_forwarded_user
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_preferred_username
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_groups
:
Optional
[
str
]
=
Header
(
None
)):
"""
Extract the username and admin status from the http headers oauth2-proxy provides
"""
logger
.
debug
([
"
X-Forwarded-Preferred-Username:
"
+
x_forwarded_preferred_username
if
x_forwarded_preferred_username
else
""
]
+
[
"
X-Forwarded-User:
"
+
x_forwarded_user
if
x_forwarded_user
else
""
]
+
[
"
X-Forwarded-Groups:
"
+
x_forwarded_groups
if
x_forwarded_groups
else
""
])
admin
=
is_admin
(
x_forwarded_groups
)
username
=
get_username
([
x_forwarded_preferred_username
,
x_forwarded_user
])
return
username
,
admin
async
def
verify_user
(
cube_id
:
int
,
x_forwarded_preferred_username
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_user
:
Optional
[
str
]
=
Header
(
None
),
x_forwarded_groups
:
Optional
[
str
]
=
Header
(
None
)):
admin
=
is_admin
(
x_forwarded_groups
)
username
=
get_username
([
x_forwarded_preferred_username
,
x_forwarded_user
])
if
admin
:
return
True
if
username
is
not
None
and
CUBE_ACCESS_LIST
.
get
(
cube_id
)
is
not
None
:
if
username
in
CUBE_ACCESS_LIST
.
get
(
cube_id
):
return
True
raise
HTTPException
(
status_code
=
401
,
detail
=
"
Unauthorized
"
)
This diff is collapsed.
Click to expand it.
setup.py
+
2
−
2
View file @
3e6e67dd
...
...
@@ -2,14 +2,14 @@ from setuptools import setup, find_packages
setup
(
name
=
'
pilab
'
,
version
=
'
0.
1
.0
'
,
version
=
'
0.
2
.0
'
,
description
=
'
Shared-Libs for the pi-lab microservices
'
,
url
=
'
https://code.fbi.h-da.de/api/v4/projects/27896/packages/pypi/pilab
'
,
author
=
'
Max Reinheimer
'
,
author_email
=
'
maximilian.reinheimer@stud.h-da.de
'
,
license
=
'
MIT
'
,
packages
=
find_packages
(),
install_requires
=
[
'
pika
'
],
install_requires
=
[
'
pika
'
,
'
fastapi
'
],
classifiers
=
[
'
Development Status :: 3 - Alpha
'
,
'
Operating System :: POSIX :: Linux
'
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment