-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorization_code.py
More file actions
226 lines (177 loc) · 8.07 KB
/
authorization_code.py
File metadata and controls
226 lines (177 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# The Okta software accompanied by this notice is provided pursuant to the following terms:
# Copyright © 2026-Present, Okta, Inc.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
# License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
# coding: utf-8
"""Authorization Code Flow (PKCE) sample application.
Usage
-----
**Step 1 - Start the flow (prints the authorize URL):**
python -m samples.authorization_code --config okta.json
**Step 2 - Open the URL in a browser, sign in, then pass the redirect URL back:**
python -m samples.authorization_code --config okta.json \\
"https://your-redirect-uri?code=...&state=..."
The sample persists the flow context (PKCE verifier, state, nonce) to a
temporary JSON file between the two invocations so that ``resume()`` can
validate state and exchange the authorization code for tokens.
"""
from __future__ import annotations
import argparse
import asyncio
import json
import sys
import tempfile
from pathlib import Path
from okta_client.authfoundation import (
ConfigurationFileNotFoundError,
InvalidConfigurationError,
OAuth2Error,
)
from okta_client.oauth2auth import (
AuthorizationCodeContext,
AuthorizationCodeFlow,
Prompt,
)
from samples.common.sample_setup import build_oauth_client, load_configuration
from samples.common.token_output import print_token_details
_DEFAULT_CONTEXT_FILE = Path(tempfile.gettempdir()) / "okta_authcode_context.json"
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def _build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Authorization Code Flow (PKCE) sample.",
epilog=(
"Run without a redirect URI to start the flow and print the authorize URL.\n"
"Run with a redirect URI to exchange the authorization code for tokens."
),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("--config", help="Path to okta.json or okta.ini")
parser.add_argument("--issuer", help="Issuer URL")
parser.add_argument("--client_id", help="Client ID")
parser.add_argument("--scope", help="Scopes (space-separated)")
parser.add_argument("--redirect_uri", help="Redirect URI (registered in Okta)")
parser.add_argument("--logout_redirect_uri", help="Logout redirect URI")
parser.add_argument("--client_secret", help="Client secret")
parser.add_argument("--login_hint", help="Pre-populate the username field")
parser.add_argument(
"--prompt",
choices=[p.value for p in Prompt],
help="Prompt behavior (none, consent, login, login consent)",
)
parser.add_argument("--no-par", dest="par", action="store_false", default=True,
help="Disable Pushed Authorization Requests")
parser.add_argument("--context-file", dest="context_file", default=None,
help="Path to save/load flow context between start and resume "
f"(default: {_DEFAULT_CONTEXT_FILE})")
parser.add_argument("--verbose", action="store_true", help="Log raw requests and responses")
parser.add_argument(
"redirect_url",
nargs="?",
default=None,
help="The full redirect URL from the browser (step 2). Omit for step 1.",
)
return parser
# ---------------------------------------------------------------------------
# Context persistence between start and resume
# ---------------------------------------------------------------------------
def _context_file_path(args: argparse.Namespace) -> Path:
"""Resolve the context file path from CLI args or the default."""
if args.context_file:
return Path(args.context_file)
return _DEFAULT_CONTEXT_FILE
def _save_context(ctx: AuthorizationCodeContext, context_file: Path) -> None:
"""Persist the context fields needed for resume."""
context_file.write_text(json.dumps(ctx.to_dict(), indent=2), encoding="utf-8")
def _load_context(context_file: Path) -> AuthorizationCodeContext:
"""Reload a context from the saved context file."""
if not context_file.exists():
print(f"No saved flow context found at {context_file}. Run without a redirect URL first.", file=sys.stderr)
sys.exit(1)
data = json.loads(context_file.read_text(encoding="utf-8"))
return AuthorizationCodeContext.from_dict(data)
def _cleanup_context(context_file: Path) -> None:
"""Remove the saved context file after a successful resume."""
if context_file.exists():
context_file.unlink()
# ---------------------------------------------------------------------------
# Flow steps
# ---------------------------------------------------------------------------
def _run_start(args: argparse.Namespace) -> None:
"""Step 1: build the authorization URL and print it."""
try:
config = load_configuration(args)
except (ConfigurationFileNotFoundError, InvalidConfigurationError) as error:
print(f"Configuration error: {error}", file=sys.stderr)
sys.exit(1)
oauth_client = build_oauth_client(config, verbose=args.verbose)
prompt = Prompt(args.prompt) if args.prompt else None
ctx = AuthorizationCodeContext(
login_hint=args.login_hint,
prompt=prompt,
pushed_authorization_request_enabled=args.par,
)
flow = AuthorizationCodeFlow(client=oauth_client)
try:
authorize_url = asyncio.run(flow.start(context=ctx))
except OAuth2Error as error:
print(f"Failed to build authorize URL: {error}", file=sys.stderr)
sys.exit(1)
except Exception as error:
print(f"Unexpected error: {error}", file=sys.stderr)
sys.exit(1)
assert flow.context is not None
_save_context(flow.context, _context_file_path(args))
print()
print("Open the following URL in your browser to sign in:")
print()
print(f" {authorize_url}")
print()
print("After signing in, copy the full redirect URL from the browser address bar")
print("and run this sample again, passing it as a positional argument:")
print()
print(' python -m samples.authorization_code --config <config> "<redirect_url>"')
print()
def _run_resume(args: argparse.Namespace, redirect_url: str) -> None:
"""Step 2: exchange the authorization code for tokens."""
try:
config = load_configuration(args)
except (ConfigurationFileNotFoundError, InvalidConfigurationError) as error:
print(f"Configuration error: {error}", file=sys.stderr)
sys.exit(1)
oauth_client = build_oauth_client(config, verbose=args.verbose)
saved_ctx = _load_context(_context_file_path(args))
flow = AuthorizationCodeFlow(client=oauth_client)
# Manually begin the flow and set the saved context so resume() can work.
asyncio.run(flow._begin(saved_ctx))
try:
token = asyncio.run(flow.resume(redirect_url))
except OAuth2Error as error:
print(f"Token exchange failed: {error}", file=sys.stderr)
sys.exit(1)
except Exception as error:
print(f"Unexpected error: {error}", file=sys.stderr)
sys.exit(1)
_cleanup_context(_context_file_path(args))
print()
print_token_details(token)
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
parser = _build_parser()
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
return
args = parser.parse_args()
if args.redirect_url:
_run_resume(args, args.redirect_url)
else:
_run_start(args)
if __name__ == "__main__":
main()