Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uffd
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Luca (strifel)
uffd
Commits
8473d4dc
Commit
8473d4dc
authored
3 years ago
by
Julian
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring of OAuth2 models
parent
306c8502
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
uffd/database.py
+23
-0
23 additions, 0 deletions
uffd/database.py
uffd/oauth2/models.py
+4
-26
4 additions, 26 deletions
uffd/oauth2/models.py
uffd/oauth2/views.py
+2
-3
2 additions, 3 deletions
uffd/oauth2/views.py
with
29 additions
and
29 deletions
uffd/database.py
+
23
−
0
View file @
8473d4dc
from
collections
import
OrderedDict
from
sqlalchemy
import
MetaData
,
event
from
sqlalchemy.types
import
TypeDecorator
,
Text
from
sqlalchemy.ext.mutable
import
MutableList
from
flask_sqlalchemy
import
SQLAlchemy
from
flask.json
import
JSONEncoder
...
...
@@ -40,3 +42,24 @@ class SQLAlchemyJSON(JSONEncoder):
result
[
key
]
=
getattr
(
o
,
key
)
return
result
return
JSONEncoder
.
default
(
self
,
o
)
class
CommaSeparatedList
(
TypeDecorator
):
# For some reason TypeDecorator.process_literal_param and
# TypeEngine.python_type are abstract but not actually required
# pylint: disable=abstract-method
impl
=
Text
cache_ok
=
True
def
process_bind_param
(
self
,
value
,
dialect
):
if
value
is
None
:
return
None
for
item
in
value
:
if
'
,
'
in
item
:
raise
ValueError
(
'
Items of comma-separated list must not contain commas
'
)
return
'
,
'
.
join
(
value
)
def
process_result_value
(
self
,
value
,
dialect
):
if
value
is
None
:
return
None
return
MutableList
(
value
.
split
(
'
,
'
))
This diff is collapsed.
Click to expand it.
uffd/oauth2/models.py
+
4
−
26
View file @
8473d4dc
...
...
@@ -2,11 +2,11 @@ import datetime
from
flask
import
current_app
from
flask_babel
import
get_locale
,
gettext
as
_
from
sqlalchemy
import
Column
,
Integer
,
String
,
DateTime
,
Text
,
ForeignKey
from
sqlalchemy
import
Column
,
Integer
,
String
,
DateTime
,
ForeignKey
from
sqlalchemy.orm
import
relationship
from
sqlalchemy.ext.hybrid
import
hybrid_property
from
uffd.database
import
db
from
uffd.database
import
db
,
CommaSeparatedList
from
uffd.tasks
import
cleanup_task
from
uffd.session.models
import
DeviceLoginInitiation
,
DeviceLoginType
...
...
@@ -64,18 +64,7 @@ class OAuth2Grant(db.Model):
code
=
Column
(
String
(
255
),
index
=
True
,
nullable
=
False
)
redirect_uri
=
Column
(
String
(
255
),
nullable
=
False
)
expires
=
Column
(
DateTime
,
nullable
=
False
,
default
=
lambda
:
datetime
.
datetime
.
utcnow
()
+
datetime
.
timedelta
(
seconds
=
100
))
_scopes
=
Column
(
Text
,
nullable
=
False
,
default
=
''
)
@property
def
scopes
(
self
):
if
self
.
_scopes
:
return
self
.
_scopes
.
split
()
return
[]
def
delete
(
self
):
db
.
session
.
delete
(
self
)
db
.
session
.
commit
()
return
self
scopes
=
Column
(
'
_scopes
'
,
CommaSeparatedList
(),
nullable
=
False
,
default
=
tuple
())
@hybrid_property
def
expired
(
self
):
...
...
@@ -106,18 +95,7 @@ class OAuth2Token(db.Model):
access_token
=
Column
(
String
(
255
),
unique
=
True
,
nullable
=
False
)
refresh_token
=
Column
(
String
(
255
),
unique
=
True
,
nullable
=
False
)
expires
=
Column
(
DateTime
,
nullable
=
False
)
_scopes
=
Column
(
Text
,
nullable
=
False
,
default
=
''
)
@property
def
scopes
(
self
):
if
self
.
_scopes
:
return
self
.
_scopes
.
split
()
return
[]
def
delete
(
self
):
db
.
session
.
delete
(
self
)
db
.
session
.
commit
()
return
self
scopes
=
Column
(
'
_scopes
'
,
CommaSeparatedList
(),
nullable
=
False
,
default
=
tuple
())
@hybrid_property
def
expired
(
self
):
...
...
This diff is collapsed.
Click to expand it.
uffd/oauth2/views.py
+
2
−
3
View file @
8473d4dc
...
...
@@ -66,7 +66,7 @@ class UffdRequestValidator(oauthlib.oauth2.RequestValidator):
def
save_authorization_code
(
self
,
client_id
,
code
,
oauthreq
,
*
args
,
**
kwargs
):
grant
=
OAuth2Grant
(
user
=
oauthreq
.
user
,
client_id
=
client_id
,
code
=
code
[
'
code
'
],
redirect_uri
=
oauthreq
.
redirect_uri
,
_
scopes
=
'
'
.
join
(
oauthreq
.
scopes
)
)
redirect_uri
=
oauthreq
.
redirect_uri
,
scopes
=
oauthreq
.
scopes
)
db
.
session
.
add
(
grant
)
db
.
session
.
commit
()
# Oauthlib does not really provide a way to customize grant code generation.
...
...
@@ -95,7 +95,6 @@ class UffdRequestValidator(oauthlib.oauth2.RequestValidator):
db
.
session
.
commit
()
def
save_bearer_token
(
self
,
token_data
,
oauthreq
,
*
args
,
**
kwargs
):
OAuth2Token
.
query
.
filter_by
(
client_id
=
oauthreq
.
client
.
client_id
,
user
=
oauthreq
.
user
).
delete
()
tok
=
OAuth2Token
(
user
=
oauthreq
.
user
,
client_id
=
oauthreq
.
client
.
client_id
,
...
...
@@ -103,7 +102,7 @@ class UffdRequestValidator(oauthlib.oauth2.RequestValidator):
access_token
=
token_data
[
'
access_token
'
],
refresh_token
=
token_data
[
'
refresh_token
'
],
expires_in_seconds
=
token_data
[
'
expires_in
'
],
_
scopes
=
'
'
.
join
(
oauthreq
.
scopes
)
scopes
=
oauthreq
.
scopes
)
db
.
session
.
add
(
tok
)
db
.
session
.
commit
()
...
...
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