Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-python
Manage
Activity
Members
Labels
Plan
Issues
3
Issue boards
Milestones
Wiki
Custom issue tracker
Code
Merge requests
0
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-python
Commits
7893a4f6
Commit
7893a4f6
authored
4 years ago
by
Georgiy Eterevskiy
Browse files
Options
Downloads
Patches
Plain Diff
Add OAuth2Gateway for gRPC
parent
d9dde442
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!3
Добавлен плагин аутентификации
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
perxis/auth.py
+43
-0
43 additions, 0 deletions
perxis/auth.py
requirements.txt
+8
-0
8 additions, 0 deletions
requirements.txt
with
51 additions
and
0 deletions
perxis/auth.py
0 → 100644
+
43
−
0
View file @
7893a4f6
import
time
import
grpc
from
oauthlib.oauth2
import
BackendApplicationClient
,
OAuth2Token
from
requests_oauthlib
import
OAuth2Session
class
OAuth2Gateway
(
grpc
.
AuthMetadataPlugin
):
_token
=
None
def
__init__
(
self
,
client_id
:
str
,
client_secret
:
str
,
token_url
:
str
,
audience
:
str
,
signature_header_key
:
str
=
'
Authorization
'
,
token_type
:
str
=
'
Bearer
'
)
->
None
:
self
.
_client_id
=
client_id
self
.
_client_secret
=
client_secret
self
.
_token_url
=
token_url
self
.
_audience
=
audience
self
.
_signature_header_key
=
signature_header_key
self
.
_token_type
=
token_type
self
.
oauth2
=
OAuth2Session
(
client
=
BackendApplicationClient
(
client_id
=
client_id
))
def
__call__
(
self
,
context
:
grpc
.
AuthMetadataContext
,
callback
:
grpc
.
AuthMetadataPluginCallback
)
->
None
:
callback
(((
self
.
_signature_header_key
,
f
'
{
self
.
_token_type
}
{
self
.
token
[
"
access_token
"
]
}
'
),),
None
)
@property
def
token
(
self
)
->
OAuth2Token
:
if
self
.
_token
is
None
:
self
.
_token
=
self
.
oauth2
.
fetch_token
(
token_url
=
self
.
_token_url
,
client_secret
=
self
.
_client_secret
,
audience
=
self
.
_audience
)
else
:
if
self
.
_token
.
expires_at
and
self
.
_token
.
expires_at
<
time
.
time
():
self
.
_token
=
self
.
oauth2
.
refresh_token
(
token_url
=
self
.
_token_url
,
client_secret
=
self
.
_client_secret
,
audience
=
self
.
_audience
)
return
self
.
_token
This diff is collapsed.
Click to expand it.
requirements.txt
+
8
−
0
View file @
7893a4f6
certifi
==2020.12.5
chardet
==4.0.0
grpcio
==1.36.1
grpcio
==1.36.1
idna
==2.10
oauthlib
==3.1.0
protobuf
==3.15.4
protobuf
==3.15.4
PyJWT
==2.0.1
requests
==2.25.1
requests-oauthlib
==1.3.0
six
==1.15.0
six
==1.15.0
urllib3
==1.26.4
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