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

Fix missing PKCE field #321

Merged
merged 2 commits into from
Feb 6, 2024
Merged
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: 2 additions & 0 deletions docs/oauth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Returns the authorization URL where you should redirect the user to ask for thei
* `redirect_uri: str`: Your callback URI where the user will be redirected after the service prompt.
* `state: str = None`: Optional string that will be returned back in the callback parameters to allow you to retrieve state information.
* `scope: Optional[List[str]] = None`: Optional list of scopes to ask for.
* `code_challenge: Optional[str] = None`: Optional code_challenge in a [PKCE context](https://datatracker.ietf.org/doc/html/rfc7636).
* `code_challenge_method: Optional[Literal["plain", "S256"]] = None`: Optional code_challenge_method in a [PKCE context](https://datatracker.ietf.org/doc/html/rfc7636).
* `extras_params: Optional[Dict[str, Any]] = None`: Optional dictionary containing parameters specific to the service.

!!! example
Expand Down
6 changes: 4 additions & 2 deletions httpx_oauth/clients/franceconnect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import secrets
from typing import Any, Dict, List, Optional, Tuple, TypedDict
from typing import Any, Dict, List, Literal, Optional, Tuple, TypedDict

from httpx_oauth.errors import GetIdEmailError
from httpx_oauth.oauth2 import BaseOAuth2
Expand Down Expand Up @@ -58,6 +58,8 @@ async def get_authorization_url(
redirect_uri: str,
state: Optional[str] = None,
scope: Optional[List[str]] = None,
code_challenge: Optional[str] = None,
code_challenge_method: Optional[Literal["plain", "S256"]] = None,
extras_params: Optional[FranceConnectOAuth2AuthorizeParams] = None,
) -> str:
_extras_params = extras_params or {}
Expand All @@ -67,7 +69,7 @@ async def get_authorization_url(
_extras_params["nonce"] = secrets.token_urlsafe()

return await super().get_authorization_url(
redirect_uri, state, scope, _extras_params
redirect_uri, state, scope, extras_params=_extras_params
)

async def get_id_email(self, token: str) -> Tuple[str, Optional[str]]:
Expand Down
9 changes: 9 additions & 0 deletions httpx_oauth/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Dict,
Generic,
List,
Literal,
Optional,
Tuple,
TypeVar,
Expand Down Expand Up @@ -100,6 +101,8 @@ async def get_authorization_url(
redirect_uri: str,
state: Optional[str] = None,
scope: Optional[List[str]] = None,
code_challenge: Optional[str] = None,
code_challenge_method: Optional[Literal["plain", "S256"]] = None,
extras_params: Optional[T] = None,
) -> str:
params = {
Expand All @@ -116,6 +119,12 @@ async def get_authorization_url(
if _scope is not None:
params["scope"] = " ".join(_scope)

if code_challenge is not None:
params["code_challenge"] = code_challenge

if code_challenge_method is not None:
params["code_challenge_method"] = code_challenge_method

if extras_params is not None:
params = {**params, **extras_params} # type: ignore

Expand Down
10 changes: 10 additions & 0 deletions tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ async def test_get_authorization_url_with_scopes(self):
)
assert "scope=SCOPE1+SCOPE2+SCOPE3" in authorization_url

@pytest.mark.asyncio
async def test_get_authorization_url_with_plain_code_challenge(self):
authorization_url = await client.get_authorization_url(
REDIRECT_URI,
code_challenge="CODE_CHALLENGE",
code_challenge_method="plain",
)
assert "code_challenge=CODE_CHALLENGE" in authorization_url
assert "code_challenge_method=plain" in authorization_url

@pytest.mark.asyncio
async def test_get_authorization_url_with_extras_params(self):
authorization_url = await client.get_authorization_url(
Expand Down
Loading