-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
291 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,3 +69,6 @@ examples/*.crt | |
|
||
# Vscode | ||
.vscode/ | ||
|
||
# Pycharm venv | ||
.venv |
35 changes: 35 additions & 0 deletions
35
invenio_oauthclient/alembic/7def990b852e_add_expires_at_and_refresh_token_to_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2016-2018 CERN. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Add expires_at and refresh_token to remote token.""" | ||
|
||
import sqlalchemy as sa | ||
import sqlalchemy_utils | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "7def990b852e" | ||
down_revision = "aaa265b0afa6" | ||
branch_labels = () | ||
depends_on = ("aaa265b0afa6",) | ||
|
||
|
||
def upgrade(): | ||
"""Upgrade database.""" | ||
op.add_column( | ||
"oauthclient_remotetoken", | ||
sa.Column("refresh_token", sqlalchemy_utils.EncryptedType(), nullable=True), | ||
) | ||
op.add_column( | ||
"oauthclient_remotetoken", sa.Column("expires_at", sa.DateTime(), nullable=True) | ||
) | ||
|
||
|
||
def downgrade(): | ||
"""Downgrade database.""" | ||
op.drop_column("oauthclient_remotetoken", "expires_at") | ||
op.drop_column("oauthclient_remotetoken", "refresh_token") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2016-2018 CERN. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Alembic migrations for Invenio-OAuthClient.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2024 CESNET z.s.p.o. | ||
# | ||
# Invenio is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Handler for refreshing access token.""" | ||
|
||
from flask_oauthlib.client import OAuthResponse | ||
from flask_oauthlib.utils import to_bytes | ||
|
||
from invenio_oauthclient.handlers.token import make_expiration_time | ||
|
||
from ..models import RemoteToken | ||
from ..proxies import current_oauthclient | ||
|
||
|
||
def refresh_access_token(token: RemoteToken): | ||
""" | ||
Internal method to refresh the access token. | ||
:param token: the remote token to be refreshed | ||
:returns tuple of (access_token, secret, refresh_token, expires_at) | ||
Note: the current access/refresh token are invalidated during this call | ||
""" | ||
remote_account = token.remote_account | ||
client_id = remote_account.client_id | ||
remote = next( | ||
x | ||
for x in current_oauthclient.oauth.remote_apps.values() | ||
if x.consumer_key == client_id | ||
) | ||
client = remote.make_client() | ||
refresh_token_request = client.prepare_refresh_token_request( | ||
remote.access_token_url, | ||
refresh_token=token.refresh_token, | ||
client_id=remote.consumer_key, | ||
client_secret=remote.consumer_secret, | ||
) | ||
resp, content = remote.http_request( | ||
refresh_token_request[0], | ||
refresh_token_request[1], | ||
data=to_bytes(refresh_token_request[2], remote.encoding), | ||
method="POST", | ||
) | ||
resp = OAuthResponse(resp, content, remote.content_type) | ||
return ( | ||
resp.data.get("access_token"), | ||
"", | ||
resp.data.get("refresh_token"), | ||
make_expiration_time(resp.data.get("expires_in")), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.