Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
ViaResto-website
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aymeric Chaumont
ViaResto-website
Commits
c364ef80
Commit
c364ef80
authored
Jul 12, 2022
by
Aymeric Chaumont
Browse files
Options
Downloads
Patches
Plain Diff
delete accidently gitted file
parent
bd249ceb
No related branches found
No related tags found
2 merge requests
!30
Restaurants route
,
!28
improve front
Pipeline
#43977
passed with warnings
Jul 12, 2022
Stage: build
Stage: test
Changes
1
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/routers/auth.py
+0
-146
0 additions, 146 deletions
backend/routers/auth.py
with
0 additions
and
146 deletions
backend/routers/auth.py
deleted
100644 → 0
+
0
−
146
View file @
bd249ceb
from
pydantic
import
BaseModel
from
fastapi
import
HTTPException
,
APIRouter
,
Response
,
Depends
from
uuid
import
UUID
,
uuid4
from
fastapi_sessions.backends.implementations
import
InMemoryBackend
from
fastapi_sessions.session_verifier
import
SessionVerifier
from
fastapi_sessions.frontends.implementations
import
SessionCookie
,
CookieParameters
from
fastapi.responses
import
RedirectResponse
from
urllib.parse
import
urlencode
from
dotenv
import
load_dotenv
from
sqlalchemy.orm
import
Session
from
requests
import
get
,
post
import
os
load_dotenv
()
class
SessionData
(
BaseModel
):
username
:
str
cookie_params
=
CookieParameters
()
# Uses UUID
cookie
=
SessionCookie
(
cookie_name
=
"
cookie
"
,
identifier
=
"
general_verifier
"
,
auto_error
=
True
,
secret_key
=
"
DONOTUSE
"
,
cookie_params
=
cookie_params
,
)
backend
=
InMemoryBackend
[
UUID
,
SessionData
]()
class
BasicVerifier
(
SessionVerifier
[
UUID
,
SessionData
]):
def
__init__
(
self
,
*
,
identifier
:
str
,
auto_error
:
bool
,
backend
:
InMemoryBackend
[
UUID
,
SessionData
],
auth_http_exception
:
HTTPException
,
):
self
.
_identifier
=
identifier
self
.
_auto_error
=
auto_error
self
.
_backend
=
backend
self
.
_auth_http_exception
=
auth_http_exception
@property
def
identifier
(
self
):
return
self
.
_identifier
@property
def
backend
(
self
):
return
self
.
_backend
@property
def
auto_error
(
self
):
return
self
.
_auto_error
@property
def
auth_http_exception
(
self
):
return
self
.
_auth_http_exception
def
verify_session
(
self
,
model
:
SessionData
)
->
bool
:
"""
If the session exists, it is valid
"""
return
True
verifier
=
BasicVerifier
(
identifier
=
"
general_verifier
"
,
auto_error
=
True
,
backend
=
backend
,
auth_http_exception
=
HTTPException
(
status_code
=
403
,
detail
=
"
invalid session
"
),
)
router
=
APIRouter
(
prefix
=
"
/api
"
,
tags
=
[
"
auth
"
])
@router.post
(
"
/create_session/{name}
"
)
async
def
create_session
(
name
:
str
,
response
:
Response
):
session
=
uuid4
()
data
=
SessionData
(
username
=
name
)
await
backend
.
create
(
session
,
data
)
cookie
.
attach_to_response
(
response
,
session
)
return
f
"
created session for
{
name
}
"
@router.get
(
"
/whoami
"
,
dependencies
=
[
Depends
(
cookie
)])
async
def
whoami
(
session_data
:
SessionData
=
Depends
(
verifier
)):
return
session_data
@router.post
(
"
/delete_session
"
)
async
def
del_session
(
response
:
Response
,
session_id
:
UUID
=
Depends
(
cookie
)):
await
backend
.
delete
(
session_id
)
cookie
.
delete_from_response
(
response
)
return
"
deleted session
"
@router.get
(
"
/login
"
)
async
def
login
(
code
):
if
not
code
:
new_state
=
crud
.
create_state
(
db
)
params
=
urlencode
({
"
client_id
"
:
os
.
getenv
(
"
CLIENT_ID
"
),
"
redirect_uri
"
:
f
"
{
os
.
getenv
(
'
API_ROOT
'
)
}
/login
"
,
"
response_type
"
:
"
code
"
,
"
state
"
:
new_state
.
state
,
"
scope
"
:
"
default linkcs-event:read
"
})
return
RedirectResponse
(
f
"
{
os
.
getenv
(
'
AUTH_ROOT
'
)
}
/oauth/authorize?
{
params
}
"
)
stored_states
=
crud
.
get_states
(
db
)
if
state
not
in
map
(
lambda
state
:
state
.
state
,
stored_states
):
raise
HTTPException
(
status_code
=
403
,
detail
=
"
State Invalid
"
)
crud
.
delete_state
(
state
,
db
)
headers
=
{
"
content-type
"
:
"
application/x-www-form-urlencoded
"
}
data
=
{
"
grant_type
"
:
"
authorization_code
"
,
"
code
"
:
code
,
"
redirect_uri
"
:
f
"
{
os
.
getenv
(
'
API_ROOT
'
)
}
/login
"
,
"
client_id
"
:
os
.
getenv
(
"
CLIENT_ID
"
),
"
client_secret
"
:
os
.
getenv
(
"
CLIENT_SECRET
"
),
}
token_response
=
post
(
f
"
{
os
.
getenv
(
'
AUTH_ROOT
'
)
}
/oauth/token
"
,
data
=
data
,
headers
=
headers
)
access_token
=
token_response
.
json
()[
"
access_token
"
]
user_info
=
get
(
f
"
{
os
.
getenv
(
'
AUTH_ROOT
'
)
}
/api/user/show/me
"
,
headers
=
{
"
Authorization
"
:
f
"
Bearer
{
access_token
}
"
}
)
return
crud
.
create_user
(
user_info
.
json
(),
db
)
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