Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Memri
pymemri
Commits
fb49b50e
Commit
fb49b50e
authored
3 years ago
by
Alp Deniz Ogut
Browse files
Options
Download
Email Patches
Plain Diff
Refresh token is optional
parent
017e20ac
Pipeline
#3362
passed with stage
in 3 minutes and 36 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
nbs/plugin.authenticators.oauth.ipynb
+1
-1
nbs/plugin.authenticators.oauth.ipynb
pymemri/plugin/authenticators/oauth.py
+1
-1
pymemri/plugin/authenticators/oauth.py
with
2 additions
and
2 deletions
+2
-2
nbs/plugin.authenticators.oauth.ipynb
+
1
-
1
View file @
fb49b50e
...
...
@@ -90,7 +90,7 @@
" def store_tokens(self, tokens):\n",
" account = self.pluginRun.account[0]\n",
" account.accessToken = tokens['access_token']\n",
" account.refreshToken = tokens['refresh_token']\n",
" account.refreshToken = tokens['refresh_token']
if 'refresh_token' in tokens else None
\n",
" account.update(self.client)\n",
"\n",
" @abc.abstractmethod\n",
...
...
%% Cell type:code id: tags:
```
python
%
load_ext
autoreload
%
autoreload
2
# default_exp plugin.authenticators.oauth
```
%% Cell type:code id: tags:
```
python
# export
# hide
import
abc
from
time
import
sleep
from
pymemri.plugin.states
import
RUN_USER_ACTION_NEEDED
,
RUN_USER_ACTION_COMPLETED
```
%% Cell type:markdown id: tags:
## OAuth Authenticator
OAuth Authenticator provides an easy interface to setup standard Open Authorization (OAuth) with 3rd party services.
Simply calling
`authenticate()`
should load the related account with required credentials.
Inheriting class should implement:
-
get_oauth_url() to setup OAuth endpoint
-
get_tokens_from_code() to retrieve tokens from the service with returned OAuth code
-
refresh_tokens() to get new token pairs
%% Cell type:code id: tags:
```
python
# export
# hide
class
OAuthAuthenticator
(
metaclass
=
abc
.
ABCMeta
):
SLEEP_INTERVAL
=
1.0
def
__init__
(
self
,
client
,
pluginRun
):
self
.
client
=
client
self
.
pluginRun
=
pluginRun
self
.
isTest
=
False
def
authenticate
(
self
):
tokens
=
None
try
:
if
not
self
.
pluginRun
.
account
[
0
].
refreshToken
:
raise
Exception
(
"Refresh token is empty"
)
tokens
=
self
.
refresh_tokens
(
self
.
pluginRun
.
account
[
0
].
refreshToken
)
except
:
# no account exists or expired refresh token
url
=
self
.
get_oauth_url
()
code
=
self
.
present_url_to_user
(
url
)
tokens
=
self
.
get_tokens_from_code
(
code
)
# if not tokens: raise an exception or set pluginrun.state=FAILED and pluginRun.message=ERROR_MESSAGE
self
.
store_tokens
(
tokens
)
def
present_url_to_user
(
self
,
url
):
if
self
.
isTest
:
return
'dummy_code'
# request user to visit url
self
.
pluginRun
.
oAuthUrl
=
url
self
.
pluginRun
.
state
=
RUN_USER_ACTION_NEEDED
self
.
pluginRun
.
update
(
self
.
client
)
# WAIT HERE = BLOCK
while
True
:
sleep
(
self
.
SLEEP_INTERVAL
)
self
.
pluginRun
=
self
.
client
.
get
(
self
.
pluginRun
.
id
)
if
self
.
pluginRun
.
state
==
RUN_USER_ACTION_COMPLETED
:
return
self
.
pluginRun
.
account
[
0
].
code
def
store_tokens
(
self
,
tokens
):
account
=
self
.
pluginRun
.
account
[
0
]
account
.
accessToken
=
tokens
[
'access_token'
]
account
.
refreshToken
=
tokens
[
'refresh_token'
]
account
.
refreshToken
=
tokens
[
'refresh_token'
]
if
'refresh_token'
in
tokens
else
None
account
.
update
(
self
.
client
)
@
abc
.
abstractmethod
def
get_oauth_url
(
self
):
raise
NotImplemented
()
@
abc
.
abstractmethod
def
get_tokens_from_code
(
self
,
code
):
""" Gets access and refresh tokens from 3rd party service
and returns them in form:
{
'access_token': '...',
'refresh_token': '...'
}
"""
raise
NotImplemented
()
@
abc
.
abstractmethod
def
refresh_tokens
(
self
):
""" Gets new tokens by using an existing refresh token
and returns them in form:
{
'access_token': '...',
'refresh_token': '...'
}
"""
# use self.pluginRun.account[0].refreshToken
raise
NotImplemented
()
```
%% Cell type:code id: tags:
```
python
# export
# hide
class
ExampleOAuthAuthenticator
(
OAuthAuthenticator
):
def
get_oauth_url
(
self
):
return
"https://example.com/oauth"
def
get_tokens_from_code
(
self
,
code
):
return
{
'access_token'
:
'dummy_access_token'
,
'refresh_token'
:
'dummy_refresh_token'
}
def
refresh_tokens
(
self
,
refreshToken
):
return
{
'access_token'
:
'refreshed_dummy_access_token'
,
'refresh_token'
:
'refreshed_dummy_refresh_token'
}
```
...
...
This diff is collapsed.
Click to expand it.
pymemri/plugin/authenticators/oauth.py
+
1
-
1
View file @
fb49b50e
...
...
@@ -52,7 +52,7 @@ class OAuthAuthenticator(metaclass=abc.ABCMeta):
def
store_tokens
(
self
,
tokens
):
account
=
self
.
pluginRun
.
account
[
0
]
account
.
accessToken
=
tokens
[
'access_token'
]
account
.
refreshToken
=
tokens
[
'refresh_token'
]
account
.
refreshToken
=
tokens
[
'refresh_token'
]
if
'refresh_token'
in
tokens
else
None
account
.
update
(
self
.
client
)
@
abc
.
abstractmethod
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Snippets