<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=Using_Okta_with_FastAPI</id>
	<title>Using Okta with FastAPI - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=Using_Okta_with_FastAPI"/>
	<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Using_Okta_with_FastAPI&amp;action=history"/>
	<updated>2026-05-18T17:22:33Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://performiq.com/kb/index.php?title=Using_Okta_with_FastAPI&amp;diff=5265&amp;oldid=prev</id>
		<title>PeterHarding at 02:40, 7 May 2022</title>
		<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Using_Okta_with_FastAPI&amp;diff=5265&amp;oldid=prev"/>
		<updated>2022-05-07T02:40:41Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 12:40, 7 May 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l139&quot;&gt;Line 139:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 139:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Okta]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Okta]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;:&lt;/del&gt;:FastApi]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:FastApi]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>PeterHarding</name></author>
	</entry>
	<entry>
		<id>https://performiq.com/kb/index.php?title=Using_Okta_with_FastAPI&amp;diff=5264&amp;oldid=prev</id>
		<title>PeterHarding: Created page with &quot;  &lt;pre&gt; # ----------------------------------------------------------------------------- # Okta Login # -----------------------------------------------------------------------------  okta_config = {   &quot;auth_uri&quot;: &quot;https://dev-xxxx.okta.com/oauth2/default/v1/authorize&quot;,   &quot;client_id&quot;: &quot;xxxx&quot;,   &quot;client_secret&quot;: &quot;xxxx-&quot;,   &quot;redirect_uri&quot;: &quot;http://127.0.0.1:8000/authorization-code/callback&quot;,   &quot;issuer&quot;: &quot;https://dev-xxxx.okta.com/oauth2/default&quot;,   &quot;token_uri&quot;: &quot;https://dev-...&quot;</title>
		<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Using_Okta_with_FastAPI&amp;diff=5264&amp;oldid=prev"/>
		<updated>2022-05-07T02:39:43Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;  &amp;lt;pre&amp;gt; # ----------------------------------------------------------------------------- # Okta Login # -----------------------------------------------------------------------------  okta_config = {   &amp;quot;auth_uri&amp;quot;: &amp;quot;https://dev-xxxx.okta.com/oauth2/default/v1/authorize&amp;quot;,   &amp;quot;client_id&amp;quot;: &amp;quot;xxxx&amp;quot;,   &amp;quot;client_secret&amp;quot;: &amp;quot;xxxx-&amp;quot;,   &amp;quot;redirect_uri&amp;quot;: &amp;quot;http://127.0.0.1:8000/authorization-code/callback&amp;quot;,   &amp;quot;issuer&amp;quot;: &amp;quot;https://dev-xxxx.okta.com/oauth2/default&amp;quot;,   &amp;quot;token_uri&amp;quot;: &amp;quot;https://dev-...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# -----------------------------------------------------------------------------&lt;br /&gt;
# Okta Login&lt;br /&gt;
# -----------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
okta_config = {&lt;br /&gt;
  &amp;quot;auth_uri&amp;quot;: &amp;quot;https://dev-xxxx.okta.com/oauth2/default/v1/authorize&amp;quot;,&lt;br /&gt;
  &amp;quot;client_id&amp;quot;: &amp;quot;xxxx&amp;quot;,&lt;br /&gt;
  &amp;quot;client_secret&amp;quot;: &amp;quot;xxxx-&amp;quot;,&lt;br /&gt;
  &amp;quot;redirect_uri&amp;quot;: &amp;quot;http://127.0.0.1:8000/authorization-code/callback&amp;quot;,&lt;br /&gt;
  &amp;quot;issuer&amp;quot;: &amp;quot;https://dev-xxxx.okta.com/oauth2/default&amp;quot;,&lt;br /&gt;
  &amp;quot;token_uri&amp;quot;: &amp;quot;https://dev-xxxx.okta.com/oauth2/default/v1/token&amp;quot;,&lt;br /&gt;
  &amp;quot;userinfo_uri&amp;quot;: &amp;quot;https://dev-xxxx.okta.com/oauth2/default/v1/userinfo&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
APP_STATE = &amp;#039;ApplicationState&amp;#039;&lt;br /&gt;
NONCE     = &amp;#039;SampleNonce&amp;#039;&lt;br /&gt;
BASE_URL  = &amp;quot;https://dev-xxxx.okta.com/oauth2/default/v1/authorize&amp;quot;&lt;br /&gt;
&lt;br /&gt;
@app.get(&amp;quot;/login&amp;quot;, response_class=HTMLResponse)&lt;br /&gt;
def login():&lt;br /&gt;
    # get request params&lt;br /&gt;
    query_params = {&amp;#039;client_id&amp;#039;: okta_config[&amp;quot;client_id&amp;quot;],&lt;br /&gt;
                    &amp;#039;redirect_uri&amp;#039;: okta_config[&amp;quot;redirect_uri&amp;quot;],&lt;br /&gt;
                    &amp;#039;scope&amp;#039;: &amp;quot;openid email profile&amp;quot;,&lt;br /&gt;
                    &amp;#039;state&amp;#039;: APP_STATE,&lt;br /&gt;
                    &amp;#039;nonce&amp;#039;: NONCE,&lt;br /&gt;
                    &amp;#039;response_type&amp;#039;: &amp;#039;code&amp;#039;,&lt;br /&gt;
                    &amp;#039;response_mode&amp;#039;: &amp;#039;query&amp;#039;}&lt;br /&gt;
&lt;br /&gt;
    encoded_params = requests.compat.urlencode(query_params)&lt;br /&gt;
&lt;br /&gt;
    print(f&amp;quot;  encoded_params |{encoded_params}|&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    request_uri = f&amp;quot;{okta_config[&amp;#039;auth_uri&amp;#039;]}?{encoded_params}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    print(f&amp;quot;  request_uri |{request_uri}|&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    return RedirectResponse(url=request_uri, status_code=303)&lt;br /&gt;
&lt;br /&gt;
# -----------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
@app.get(&amp;quot;/authorization-code/callback&amp;quot;)&lt;br /&gt;
def callback(code: str, state: str):&lt;br /&gt;
    headers = {&amp;#039;Content-Type&amp;#039;: &amp;#039;application/x-www-form-urlencoded&amp;#039;}&lt;br /&gt;
    # code = request.args.get(&amp;quot;code&amp;quot;)&lt;br /&gt;
    if not code:&lt;br /&gt;
        return &amp;quot;The code was not returned or is not accessible&amp;quot;, 403&lt;br /&gt;
&lt;br /&gt;
    print(f&amp;quot;                   code |{code}|&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;                  state |{state}|&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    query_params = {&amp;#039;grant_type&amp;#039;: &amp;#039;authorization_code&amp;#039;,&lt;br /&gt;
                    &amp;#039;code&amp;#039;: code,&lt;br /&gt;
                    &amp;#039;redirect_uri&amp;#039;: okta_config[&amp;quot;redirect_uri&amp;quot;]&lt;br /&gt;
                    }&lt;br /&gt;
&lt;br /&gt;
    encoded_params = requests.compat.urlencode(query_params)&lt;br /&gt;
&lt;br /&gt;
    print(f&amp;quot;         encoded_params |{encoded_params}|&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    exchange = requests.post(&lt;br /&gt;
        okta_config[&amp;quot;token_uri&amp;quot;],&lt;br /&gt;
        headers=headers,&lt;br /&gt;
        data=encoded_params,&lt;br /&gt;
        auth=(okta_config[&amp;quot;client_id&amp;quot;], okta_config[&amp;quot;client_secret&amp;quot;]),&lt;br /&gt;
    ).json()&lt;br /&gt;
&lt;br /&gt;
    # Get tokens and validate&lt;br /&gt;
&lt;br /&gt;
    token_type = exchange.get(&amp;quot;token_type&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;             token_type |{token_type}|&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    #if not exchange.get(&amp;quot;token_type&amp;quot;):&lt;br /&gt;
    #    return &amp;quot;Unsupported token type. Should be &amp;#039;Bearer&amp;#039;.&amp;quot;, 403&lt;br /&gt;
&lt;br /&gt;
    access_token = exchange[&amp;quot;access_token&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
    print(f&amp;quot;           access_token |{access_token}|&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    id_token = exchange[&amp;quot;id_token&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
    print(f&amp;quot;               id_token |{id_token}|&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    #if not is_access_token_valid(access_token, okta_config[&amp;quot;issuer&amp;quot;]):&lt;br /&gt;
    #    return &amp;quot;Access token is invalid&amp;quot;, 403&lt;br /&gt;
&lt;br /&gt;
    #if not is_id_token_valid(id_token, config[&amp;quot;issuer&amp;quot;], okta_config[&amp;quot;client_id&amp;quot;], NONCE):&lt;br /&gt;
    #    return &amp;quot;ID token is invalid&amp;quot;, 403&lt;br /&gt;
&lt;br /&gt;
    # Authorization flow successful, get userinfo and login user&lt;br /&gt;
    userinfo_response = requests.get(okta_config[&amp;quot;userinfo_uri&amp;quot;],&lt;br /&gt;
                                     headers={&amp;#039;Authorization&amp;#039;: f&amp;#039;Bearer {access_token}&amp;#039;}).json()&lt;br /&gt;
&lt;br /&gt;
    unique_id  = userinfo_response[&amp;quot;sub&amp;quot;]&lt;br /&gt;
    user_email = userinfo_response[&amp;quot;email&amp;quot;]&lt;br /&gt;
    user_name  = userinfo_response[&amp;quot;given_name&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
    print(f&amp;quot;              unique_id |{unique_id}|&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;             user_email |{user_email}|&amp;quot;)&lt;br /&gt;
    print(f&amp;quot;              user_name |{user_name}|&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    #user = User(&lt;br /&gt;
    #    user_id=unique_id, name=user_name, email=user_email&lt;br /&gt;
    #)&lt;br /&gt;
&lt;br /&gt;
    #if not User.get(unique_id):&lt;br /&gt;
    #    User.create(unique_id, user_name, user_email)&lt;br /&gt;
&lt;br /&gt;
    # login_user(user)&lt;br /&gt;
&lt;br /&gt;
    return RedirectResponse(url=&amp;quot;/profile&amp;quot;, status_code=303)&lt;br /&gt;
&lt;br /&gt;
# -----------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
@app.get(&amp;quot;/profile&amp;quot;, response_class=HTMLResponse)&lt;br /&gt;
async def profile(request: Request):&lt;br /&gt;
&lt;br /&gt;
    ctx = Context(&amp;quot;Home&amp;quot;)&lt;br /&gt;
    &lt;br /&gt;
    user_info = {&amp;quot;user_id&amp;quot;: &amp;quot;asasd&amp;quot;, &amp;quot;name&amp;quot;: &amp;quot;aaaa&amp;quot;, &amp;quot;email&amp;quot;: &amp;quot;bbb@bbb.com&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
    #ctx.user = User(**user_info)&lt;br /&gt;
    ctx.user = User.create(&amp;quot;asasd&amp;quot;, &amp;quot;aaaa&amp;quot;, &amp;quot;bbb@bbb.com&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    return templates.TemplateResponse(&amp;quot;profile.html&amp;quot;, {&amp;quot;request&amp;quot;: request, &amp;quot;ctx&amp;quot;: ctx})&lt;br /&gt;
&lt;br /&gt;
# -----------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
@app.post(&amp;quot;/logout&amp;quot;, response_class=HTMLResponse)&lt;br /&gt;
async def logout(request: Request):&lt;br /&gt;
    return RedirectResponse(url=&amp;quot;/login&amp;quot;, status_code=status.HTTP_302_FOUND)&lt;br /&gt;
&lt;br /&gt;
# -----------------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Okta]]&lt;br /&gt;
[[Category::FastApi]]&lt;/div&gt;</summary>
		<author><name>PeterHarding</name></author>
	</entry>
</feed>