Skip to content

Commit 995debc

Browse files
committed
feat(code_grant): Add exchange_code_for_response
The new exchange_code_for_response allows the user to access the entire response and e.g. use refresh tokens.
1 parent ebca482 commit 995debc

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

oauth2_cli_auth/code_grant.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import base64
2+
import urllib.parse
3+
import urllib.request
14
import webbrowser
5+
from collections.abc import Callable
26
from dataclasses import dataclass
3-
import urllib.request
4-
import urllib.parse
5-
import base64
7+
68
from oauth2_cli_auth._urllib_util import _load_json
79

810

@@ -63,17 +65,20 @@ def get_auth_url(client_info: OAuth2ClientInfo, redirect_uri: str) -> str:
6365
f"&response_type=code")
6466

6567

66-
def exchange_code_for_access_token(client_info: OAuth2ClientInfo, redirect_uri: str, code: str,
67-
access_token_field: str = "access_token") -> str:
68+
def exchange_code_for_response(
69+
client_info: OAuth2ClientInfo,
70+
redirect_uri: str,
71+
code: str,
72+
) -> dict:
6873
"""
69-
Exchange a code for an access token using the endpoints from client info
74+
Exchange a code for an access token using the endpoints from client info and return the entire response
7075
7176
:param client_info: Info about oauth2 client
7277
:param redirect_uri: Callback URL
7378
:param code: Code to redeem
7479
:param access_token_field: Name of the field containing the access token to use. This might differ depending on
75-
the provider you are using. For example for Auth0 you have to set this to id_token
76-
:return: Extracted access token from response
80+
the provider you are using. For example for Auth0 you have to set this to id_token
81+
:return: Response from OAuth2 endpoint
7782
"""
7883
headers = {
7984
"Content-Type": "application/x-www-form-urlencoded",
@@ -90,4 +95,25 @@ def exchange_code_for_access_token(client_info: OAuth2ClientInfo, redirect_uri:
9095
request = urllib.request.Request(client_info.token_url, data=encoded_data, headers=headers)
9196
json_response = _load_json(request)
9297

98+
return json_response
99+
100+
101+
def exchange_code_for_access_token(
102+
client_info: OAuth2ClientInfo,
103+
redirect_uri: str,
104+
code: str,
105+
access_token_field: str = "access_token"
106+
) -> str:
107+
"""
108+
Exchange a code for an access token using the endpoints from client info
109+
110+
:param client_info: Info about oauth2 client
111+
:param redirect_uri: Callback URL
112+
:param code: Code to redeem
113+
:param access_token_field: Name of the field containing the access token to use. This might differ depending on
114+
the provider you are using. For example for Auth0 you have to set this to id_token
115+
:return: Extracted access token from response
116+
"""
117+
json_response = exchange_code_for_response(client_info, redirect_uri, code)
118+
93119
return json_response.get(access_token_field)

0 commit comments

Comments
 (0)