Skip to content

Commit e8be841

Browse files
committed
End-to-end test case
1 parent 0cb8a8f commit e8be841

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

oauth2cli/authcode.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,17 @@ def __exit__(self, exc_type, exc_val, exc_tb):
210210
args = parser.parse_args()
211211
client = Client({"authorization_endpoint": args.endpoint}, args.client_id)
212212
with AuthCodeReceiver(port=args.port) as receiver:
213-
auth_uri = client.build_auth_request_uri(
214-
"code",
213+
flow = client.initiate_auth_code_flow(
215214
scope=args.scope.split() if args.scope else None,
216-
redirect_uri="http://{h}:{p}".format(h=args.host, p=receiver.get_port()))
215+
redirect_uri="http://{h}:{p}".format(h=args.host, p=receiver.get_port()),
216+
)
217217
print(json.dumps(receiver.get_auth_response(
218-
auth_uri=auth_uri,
218+
auth_uri=flow["auth_uri"],
219219
welcome_template=
220220
"<a href='$auth_uri'>Sign In</a>, or <a href='$abort_uri'>Abort</a",
221221
error_template="Oh no. $error",
222222
success_template="Oh yeah. Got $code",
223223
timeout=60,
224+
state=flow["state"], # Optional
224225
), indent=4))
225226

tests/test_client.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import requests
1111

1212
from oauth2cli.oidc import Client
13-
from oauth2cli.authcode import obtain_auth_code
13+
from oauth2cli.authcode import obtain_auth_code, AuthCodeReceiver
1414
from oauth2cli.assertion import JwtSigner
1515
from tests import unittest, Oauth2TestCase
1616
from tests.http_client import MinimalHttpClient, MinimalResponse
@@ -153,6 +153,33 @@ def test_auth_code(self):
153153
redirect_uri=redirect_uri)
154154
self.assertLoosely(result, lambda: self.assertIn('access_token', result))
155155

156+
@unittest.skipUnless(
157+
"authorization_endpoint" in CONFIG.get("openid_configuration", {}),
158+
"authorization_endpoint missing")
159+
def test_auth_code_flow(self):
160+
with AuthCodeReceiver(port=CONFIG.get("listen_port")) as receiver:
161+
flow = self.client.initiate_auth_code_flow(
162+
redirect_uri="http://localhost:%d" % receiver.get_port(),
163+
scope=CONFIG.get("scope"),
164+
)
165+
auth_response = receiver.get_auth_response(
166+
auth_uri=flow["auth_uri"],
167+
state=flow["state"], # Optional but recommended
168+
timeout=120,
169+
welcome_template="""<html><body>
170+
authorization_endpoint = {a}, client_id = {i}
171+
<a href="$auth_uri">Sign In</a> or <a href="$abort_uri">Abort</a>
172+
</body></html>""".format(
173+
a=CONFIG["openid_configuration"]["authorization_endpoint"],
174+
i=CONFIG.get("client_id")),
175+
)
176+
self.assertIsNotNone(
177+
auth_response.get("code"), "Error: {}, Detail: {}".format(
178+
auth_response.get("error"), auth_response))
179+
result = self.client.obtain_token_by_auth_code_flow(flow, auth_response)
180+
#TBD: data={"resource": CONFIG.get("resource")}, # MSFT AAD v1 only
181+
self.assertLoosely(result, lambda: self.assertIn('access_token', result))
182+
156183
@unittest.skipUnless(
157184
CONFIG.get("openid_configuration", {}).get("device_authorization_endpoint"),
158185
"device_authorization_endpoint is missing")

0 commit comments

Comments
 (0)