|
| 1 | +--- |
| 2 | +title: SAML |
| 3 | +hide_title: true |
| 4 | +sidebar_position: 10 |
| 5 | +toc_max_heading_level: 4 |
| 6 | +description: Add SAML authentication to your application |
| 7 | +page_type: tutorial |
| 8 | +recipe: thirdparty |
| 9 | +category: enterprise-login |
| 10 | +--- |
| 11 | + |
| 12 | +# SAML Setup |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +The following guide shows you how to configure SAML with your SuperTokens integration. |
| 17 | +`SAML`, or Security Assertion Markup Language, is an open protocol that exchanges information between the authentication server and the client application. |
| 18 | + |
| 19 | +### How it works? |
| 20 | + |
| 21 | +Your `SAML` identity provider has a metadata file (`.xml`) that you or your end users need to upload to the `SAML` client. |
| 22 | + |
| 23 | +The `.xml` metadata file contains (amongst other things): |
| 24 | +- A unique entity ID that you must keep private. The `SAML` provider uses this to identify your application. |
| 25 | +- A public certificate that verifies the signature attached to the incoming `SAML` response. |
| 26 | +This ensures the response is coming from the expected Identity Provider. |
| 27 | +- Information about where to redirect the end user to when they click on the login button in your application. |
| 28 | +This URL is to a website controlled by the `SAML` provider and asks the end user for their credentials. |
| 29 | + |
| 30 | + |
| 31 | +## Before you start |
| 32 | + |
| 33 | +<PaidFeatureCallout /> |
| 34 | + |
| 35 | +These instructions assume that you already have gone through the main [quickstart guide](/docs/quickstart/introduction). |
| 36 | +If you have skipped that page please follow the tutorial and return here once you're done. |
| 37 | + |
| 38 | + |
| 39 | +:::warning no-title |
| 40 | +You need to use a SuperTokens core that is at least on version `11.3`. |
| 41 | +The feature is only available with the `Node.js` SDK. |
| 42 | +Support for `Python` and `Golang` is in active development. |
| 43 | +::: |
| 44 | + |
| 45 | + |
| 46 | +## Steps |
| 47 | + |
| 48 | +### 1. Get the SAML metadata from your Identity Provider |
| 49 | + |
| 50 | +Before configuring SuperTokens, you need to obtain the SAML metadata XML from your Identity Provider (IDP). |
| 51 | +This is typically available in your IDP's admin console as a downloadable XML file or a metadata URL. |
| 52 | + |
| 53 | +Common locations for metadata: |
| 54 | +- **Azure AD**: Enterprise Applications > Your App > Single sign-on > Federation Metadata XML |
| 55 | +- **Okta**: Applications > Your App > Sign On > SAML Metadata |
| 56 | +- **Google Workspace**: Apps > Web and mobile apps > Your App > Download metadata |
| 57 | + |
| 58 | +### 2. Initialize the SAML recipe in the backend SDK |
| 59 | + |
| 60 | +```typescript |
| 61 | +import { SuperTokens } from "supertokens-node"; |
| 62 | +import Saml from "supertokens-node/recipe/saml"; |
| 63 | + |
| 64 | +SuperTokens.init({ |
| 65 | + supertokens: { |
| 66 | + connectionURI: "<SUPERTOKENS_CONNECTION_URI>", |
| 67 | + apiKey: "<SUPERTOKENS_API_KEY>", |
| 68 | + }, |
| 69 | + appInfo: { |
| 70 | + appName: "App name", |
| 71 | + apiDomain: "<API_DOMAIN>", |
| 72 | + websiteDomain: "<WEBSITE_DOMAIN>", |
| 73 | + }, |
| 74 | + recipeList: [ |
| 75 | + // other recipes |
| 76 | + Saml.init(), |
| 77 | + ] |
| 78 | +}); |
| 79 | + |
| 80 | +``` |
| 81 | + |
| 82 | +### 3. Create a new SAML client |
| 83 | + |
| 84 | +Use the metadata XML obtained in step 1 to create a SAML client. |
| 85 | +The `redirectURIs` should point to your application's callback URL where users will be redirected after authentication. |
| 86 | + |
| 87 | +:::info no-title |
| 88 | +This step assumes that you previously have created a SuperTokens tenant. |
| 89 | +If you have not, please follow the [initial setup guide](/docs/authentication/enterprise/initial-setup). |
| 90 | +::: |
| 91 | + |
| 92 | +```typescript |
| 93 | +import Saml from "supertokens-node/recipe/saml"; |
| 94 | + |
| 95 | +async function createSamlClient() { |
| 96 | + const result = await Saml.createOrUpdateClient({ |
| 97 | + tenantId: "<TENANT_ID>", |
| 98 | + clientId: "<CLIENT_ID>", |
| 99 | + clientSecret: "<CLIENT_SECRET>", |
| 100 | + redirectURIs: ["https://your-app.com/auth/callback"], |
| 101 | + defaultRedirectURI: "https://your-app.com/auth/callback", |
| 102 | + metadataXML: "<METADATA_XML_FROM_IDP>", |
| 103 | + allowIDPInitiatedLogin: true, |
| 104 | + enableRequestSigning: true, |
| 105 | + }); |
| 106 | + |
| 107 | + // Save the clientId for use in the ThirdParty provider configuration |
| 108 | + console.log("Client ID:", result.clientId); |
| 109 | +} |
| 110 | + |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | +| Name | Type | Description | Required | |
| 115 | +| ------------------------ | --------------------- | ---------------------------------------------------------------------------------------------------------------------- | -------- | |
| 116 | +| `tenantId` | `string` | The unique identifier of the tenant for which the SAML client is being created or updated. | Yes | |
| 117 | +| `clientId` | `string` | The unique identifier for the SAML client. If provided, updates the existing client; if omitted, creates a new client. | No | |
| 118 | +| `clientSecret` | `string` | The secret key associated with the SAML client for authentication purposes. | No | |
| 119 | +| `redirectURIs` | `string[]` | An array of URIs where the user agent should be redirected after successful authentication. | Yes | |
| 120 | +| `defaultRedirectURI` | `string` | The default URI to redirect to if no specific redirect URI is specified. | Yes | |
| 121 | +| `metadataXML` | `string` | The SAML metadata XML string containing configuration details for the Identity Provider. | Yes | |
| 122 | +| `allowIDPInitiatedLogin` | `boolean` | A flag indicating whether login requests initiated by the Identity Provider are allowed. | No | |
| 123 | +| `enableRequestSigning` | `boolean` | A flag indicating whether SAML requests should be digitally signed for security. | No | |
| 124 | +| `userContext` | `Record<string, any>` | An optional object containing additional context or metadata for the operation. | No | |
| 125 | + |
| 126 | + |
| 127 | +### 4. Configure your Identity Provider |
| 128 | + |
| 129 | +After creating the SAML client, you need to configure your Identity Provider with your application's Service Provider (SP) details. |
| 130 | + |
| 131 | +On the IDP side, configure the following properties: |
| 132 | +- **Entity ID**: Should match the `saml_sp_entity_id` value used in your [tenant configuration](/docs/authentication/enterprise/manage-tenants#update-a-tenant). The default value is `https://saml.supertokens.com`. |
| 133 | +- **ACS URL** (Assertion Consumer Service URL): `<API_DOMAIN>/auth/<TENANT_ID>saml/callback` |
| 134 | + |
| 135 | +### 5. Add the ThirdParty provider |
| 136 | + |
| 137 | +Update your SuperTokens initialization to include the ThirdParty recipe with your SAML provider. |
| 138 | +The `thirdPartyId` must start with `saml-` followed by your custom identifier. |
| 139 | + |
| 140 | +```typescript |
| 141 | +import { SuperTokens } from "supertokens-node"; |
| 142 | +import Saml from "supertokens-node/recipe/saml"; |
| 143 | +import ThirdParty from "supertokens-node/recipe/thirdparty"; |
| 144 | + |
| 145 | +SuperTokens.init({ |
| 146 | + supertokens: { |
| 147 | + connectionURI: "<SUPERTOKENS_CONNECTION_URI>", |
| 148 | + apiKey: "<SUPERTOKENS_API_KEY>", |
| 149 | + }, |
| 150 | + appInfo: { |
| 151 | + appName: "App name", |
| 152 | + apiDomain: "<API_DOMAIN>", |
| 153 | + websiteDomain: "<WEBSITE_DOMAIN>", |
| 154 | + }, |
| 155 | + recipeList: [ |
| 156 | + Saml.init(), |
| 157 | + ThirdParty.init({ |
| 158 | + signInAndUpFeature: { |
| 159 | + providers: [ |
| 160 | + { |
| 161 | + config: { |
| 162 | + // Name that will be shown on the login page |
| 163 | + name: "Azure SAML", |
| 164 | + // Must start with "saml-" |
| 165 | + thirdPartyId: "saml-azure", |
| 166 | + clients: [ |
| 167 | + { |
| 168 | + // The clientId from step 3 |
| 169 | + clientId: "<CLIENT_ID>", |
| 170 | + }, |
| 171 | + ], |
| 172 | + }, |
| 173 | + } |
| 174 | + ], |
| 175 | + }, |
| 176 | + }), |
| 177 | + ] |
| 178 | +}); |
| 179 | + |
| 180 | +``` |
| 181 | + |
| 182 | + |
| 183 | +## See also |
| 184 | + |
| 185 | +<ReferenceCard.Grid> |
| 186 | + <ReferenceCard href="/docs/authentication/enterprise/common-domain-login" label="Implement common domain login" /> |
| 187 | + <ReferenceCard href="/docs/authentication/enterprise/subdomain-login" label="Implement subdomain login" /> |
| 188 | + <ReferenceCard href="/docs/authentication/enterprise/manage-tenants" label="Manage tenants" /> |
| 189 | + <ReferenceCard href="/docs/authentication/enterprise/manage-apps" label="Manage apps" /> |
| 190 | +</ReferenceCard.Grid> |
0 commit comments