Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable token refresh for Gitlab #422

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Changelog

`unreleased`_
-------------
nothing yet
* Added auto refresh support for Gitlab

`7.0.0`_ (2023-05-10)
---------------------
Expand Down
10 changes: 10 additions & 0 deletions flask_dance/contrib/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def make_gitlab_blueprint(
client_secret=None,
*,
scope=None,
refresh=False,
redirect_url=None,
redirect_to=None,
login_url=None,
Expand All @@ -39,6 +40,9 @@ def make_gitlab_blueprint(
client_id (str): The client ID for your application on GitLab.
client_secret (str): The client secret for your application on GitLab
scope (str, optional): comma-separated list of scopes for the OAuth token
refresh (bool): Whether this instance of `Gitlab` supports token refreshes.
`Refresh opt out was removed in Gitlab 15.0
<https://docs.gitlab.com/ee/integration/oauth_provider.html#access-token-expiration>`
redirect_url (str): the URL to redirect to after the authentication
dance is complete
redirect_to (str): if ``redirect_url`` is not defined, the name of the
Expand Down Expand Up @@ -66,6 +70,8 @@ def make_gitlab_blueprint(
:rtype: :class:`~flask_dance.consumer.OAuth2ConsumerBlueprint`
:returns: A :doc:`blueprint <flask:blueprints>` to attach to your Flask app.
"""
auto_refresh_url = None

if not verify_tls_certificates:
if session_class:
raise ValueError(
Expand All @@ -74,6 +80,9 @@ def make_gitlab_blueprint(
else:
session_class = NoVerifyOAuth2Session

if refresh:
auto_refresh_url = f"https://{hostname}/oauth/token"

gitlab_bp = OAuth2ConsumerBlueprint(
"gitlab",
__name__,
Expand All @@ -83,6 +92,7 @@ def make_gitlab_blueprint(
base_url=f"https://{hostname}/api/v4/",
authorization_url=f"https://{hostname}/oauth/authorize",
token_url=f"https://{hostname}/oauth/token",
auto_refresh_url=auto_refresh_url,
redirect_url=redirect_url,
redirect_to=redirect_to,
login_url=login_url,
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ docs = [
"sqlalchemy>=1.3.11",
"pytest",
"betamax",
"pillow<=9.5"
]
sqla = ["sqlalchemy>=1.3.11"]
signals = ["blinker"]
Expand Down
13 changes: 13 additions & 0 deletions tests/contrib/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,16 @@ def test_no_verify_api_call(make_app, mocker):
mock_on_request.assert_called()
call_verify = mock_on_request.call_args[1].get("verify", True)
assert call_verify is False


def test_blueprint_factory_refresh():
glbp = make_gitlab_blueprint(
client_id="foo",
client_secret="bar",
scope="read_user",
hostname="my-instance.com",
redirect_to="index",
refresh=True,
)

assert glbp.auto_refresh_url == "https://my-instance.com/oauth/token"
Loading