<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-M74D8PB" height="0" width="0" style="display:none;visibility:hidden">
Loading
Skip to NavigationSkip to Main Content
0D54z00009ZzXkdCAFOkta Classic EngineSingle Sign-OnAnswered2023-08-21T17:01:12.000Z2023-08-20T09:17:22.000Z2023-08-21T17:01:12.000Z

SSO implementation using SAML 2.0 Okta Python FastAPI

I'm trying to implement SSO using Okta as an identity provider using SAML. I want to be able to login via the Okta login page, parse the SAML response and store it as a session. I didn't find a good use case for PySAML2 and I think my configuration is wrong.

I have two routes for it, `/sso/login` do redirect to Okta login page, and after that my Okta app do post request to `/sso/acs` route.

This code example is using `pysaml2` package, I also tried to use `python3-saml` but faced the same problem.

Could you help me fix my SAML config and explain how to parse it correctly and what should I store in the session to protect other routes that require authentication?

 

```

from fastapi import FastAPI, Request

from fastapi.responses import RedirectResponse

from starlette.middleware.sessions import SessionMiddleware

from saml2 import BINDING_HTTP_POST, BINDING_HTTP_REDIRECT

from saml2.client import Saml2Client

from saml2.config import Config as Saml2Config

 

app = FastAPI()

 

 

* SAML configuration

 

* Audience Restriction

OKTA_ENTITY_ID = 'http://localhost:8000'

 

* Identity Provider Single Sign-On URL

OKTA_SSO_URL = 'https://dev-domain.okta.com/app/dev-62477416_samltestapp_1/exkatg6gjmlgcxm6O5d7/sso/saml'

 

* Sign out URL

OKTA_SIGN_OUT_URL = 'https://dev-domain.okta.com'

 

* Identity Provider Issuer

OKTA_ISSUER = 'http://www.okta.com/exkatg6gjmlgcxm6O5d7'

 

* X.509 Certificate:

OKTA_CERTIFICATE = '''

-----BEGIN CERTIFICATE-----

certificate_string

-----END CERTIFICATE------

'''

 

CONFIG = {

"debug": True,

"entityid": OKTA_ENTITY_ID,

"name": "SAML Test App",

"service": {

"idp": {

"endpoints": {

"single_sign_on_service": [

(

OKTA_SSO_URL,

BINDING_HTTP_REDIRECT,

),

],

"single_logout_service": [

(

OKTA_SIGN_OUT_URL,

BINDING_HTTP_REDIRECT,

),

],

},

"x509cert": OKTA_CERTIFICATE,

}

}

}

 

 

@app.middleware("http")

async def validate_user(request: Request, call_next):

* end_point = request.url.path

* request.session["name"] = "some random value"

print(request.session)

response = await call_next(request)

return response

 

app.add_middleware(SessionMiddleware, secret_key='my-secret-key')

 

 

@app.get("/sso/login")

async def sso_login():

return RedirectResponse(OKTA_SSO_URL)

 

 

@app.post("/sso/acs")

async def sso_acs(request: Request):

config = Saml2Config()

config.load(CONFIG)

client = Saml2Client(config=config)

authn_response = await request.form()

client.parse_authn_request_response(authn_response.get('SAMLResponse'), BINDING_HTTP_POST)

return {"message": "Successfully logged in"}

 

```


This question is closed.
Loading
SSO implementation using SAML 2.0 Okta Python FastAPI
Okta Support Agent (Beta)

Terms of Service

Hello! I'm the Okta Support Agent (Beta), your go-to resource for questions about Okta products. Please remember for your security and privacy, do not share any Personally Identifiable Information (PII) such as passwords, IP addresses, or other details with me.

Okta Privacy Policy and Terms of Service apply