Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oauth docs #847

Merged
merged 36 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9308ff6
Add oauth docs
bcbogdan Sep 17, 2024
c9127b5
Update the scopes section
bcbogdan Sep 19, 2024
625df2f
Code review changes
bcbogdan Sep 24, 2024
febfb13
Add improvements and new pages
bcbogdan Sep 25, 2024
6aa257b
Add more corrections
bcbogdan Sep 27, 2024
f9dbf43
Change the old microservices auth to private access token authentication
bcbogdan Oct 1, 2024
491a902
Merge branch 'master' into feat/oauth
bcbogdan Oct 1, 2024
b5d7356
Add shouldTryLinkingWithSession user info
bcbogdan Oct 4, 2024
eea5f79
Add minor fixes
bcbogdan Oct 7, 2024
e9fa208
Add more details about token verification
bcbogdan Oct 7, 2024
9ef6949
Add code checking fixes
bcbogdan Oct 8, 2024
d7883e0
Add code validation for java,c# and php
bcbogdan Oct 8, 2024
d91bf86
Add additional information about specific customizations
bcbogdan Oct 9, 2024
234335b
fix broken link
bcbogdan Oct 10, 2024
0acbef1
Type check attempt
bcbogdan Oct 10, 2024
da808a3
Check new languages in circleci
bcbogdan Oct 10, 2024
c8aa371
Add extra ci steps
bcbogdan Oct 10, 2024
30aa2b1
Review fixes
bcbogdan Oct 17, 2024
c0a45a2
Add additional configuration details
bcbogdan Oct 18, 2024
114c880
Change naming to unified login
bcbogdan Oct 18, 2024
e0a5d05
Add additional explanations
bcbogdan Oct 18, 2024
d4ea8a6
Move the m2m guide into the microservices authentication section
bcbogdan Oct 21, 2024
2b1578e
Add info about how write a custom session verification function
bcbogdan Oct 21, 2024
df76452
Add additional information about token validation
bcbogdan Oct 22, 2024
83f001b
Update the custom ui guide
bcbogdan Oct 22, 2024
c39a02f
Add additional disclaimers
bcbogdan Oct 23, 2024
f70e8a8
Add more disclaimers
bcbogdan Oct 23, 2024
ec6f38d
Fix snippets type checking errors
bcbogdan Oct 24, 2024
38e9404
Add additional feedback fixes
bcbogdan Oct 24, 2024
33a5c4f
Add the new icon and additional improvements
bcbogdan Oct 24, 2024
925c71a
Add the additional query param info for android, ios and flutter
bcbogdan Oct 25, 2024
e2068e9
Merge branch 'master' into feat/oauth
bcbogdan Oct 25, 2024
811cfaf
Skip oauth2 routes from interception
bcbogdan Oct 25, 2024
cf06538
Merge branch 'master' into feat/oauth
bcbogdan Oct 25, 2024
5aba97b
Add the multi tenancy page
bcbogdan Oct 25, 2024
349af24
Fix the rest of the code type checking
bcbogdan Oct 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions v2/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,20 @@ module.exports = {
beforeDefaultRemarkPlugins,
},
],
[
"@docusaurus/plugin-content-docs",
{
id: "oauth",
path: "oauth",
routeBasePath: "docs/oauth",
sidebarPath: require.resolve("./oauth/sidebars.js"),
showLastUpdateTime: true,
editUrl: "https://github.com/supertokens/docs/tree/master/v2/",
remarkPlugins: remarkPlugins,
rehypePlugins: rehypePlugins,
beforeDefaultRemarkPlugins,
},
],
[
"@docusaurus/plugin-content-docs",
{
Expand Down
166 changes: 166 additions & 0 deletions v2/oauth/create-an-authentication-provider.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
---
title: Create an authentication provider
hide_title: true
---

bcbogdan marked this conversation as resolved.
Show resolved Hide resolved
import BackendSDKTabs from "/src/components/tabs/BackendSDKTabs"
import TabItem from '@theme/TabItem';
import CoreInjector from "/src/components/coreInjector"
import FrontendPreBuiltUITabs from "/src/components/tabs/FrontendPreBuiltUITabs"

# Multiple Frontend Domains Using a Single Backend

You can use the following guide if you have multiple **`frontend applications`** on different domains, that use the same **`backend service`**.

Using the actual **OAuth 2.0** terminology, we can say that each **`frontend`** can be considered a [**Client**](/docs/oauth/introduction#client) and the **`backend`** will act as both an [**Authorization Server**](/docs/oauth/introduction#authorization) and a [**Resource Server**](/docs/oauth/introduction#resource-server).

:::info
This guide assumes that you already have setup and configured **SuperTokens** in your [**Authentication Service**](/docs/oauth/introduction#authentication-service).

For more information on how to do that please check our [quickstart guide](/docs/thirdparty/introduction).
:::

## 1. Create the OAuth2 Clients

For each of your **`frontend`** applications you will have to create a separate [**OAuth2 client**](/docs/oauth/introduction#client).
This can be done by directly calling the **SuperTokens Core** API.

<CoreInjector defaultValue="https://example.com">

```bash
curl -X POST ^{coreInjector_uri_without_quotes}/recipe/oauth2/admin/clients \
-H "Content-Type: application/json" \
-H "api-key: ^{coreInjector_api_key_without_quotes}" \
-d '{
"client_name": "<YOUR_CLIENT_NAME>",
"response_types": ["code", "id_token"],
"grant_types": ["authorization_code", "refresh_token"],
"redirect_uri": ["https://<YOUR_DOMAIN>.com/oauth/callback"]
}'
```

</CoreInjector>


- `client_name` - A human-readable name of the client that will be presented to the [**end-user**](/docs/oauth/introduction#authorization) during authorization.
- `response_types` - Specifies the types of responses your client expects from the Authorization Server.
- `code`: Indicates that the [**Client**](/#authorization) will receive an [**Authorization Code**](/#authorization) that will be exchanged for an [**Access Token**](/#authorization).
- `id_token`: Indicates that the [**Client**](/#authorization) expects an [**ID Token**](/#authorization)
- `grant_types` - The grant types that the [**Client**](/#authorization) will use.
- `authorization_code`: Allows exchanging the [**Authorization Code**](/#authorization) for an [**Access Token**](/#authorization).
- `refresh_token`: Allows exchanging the [**Refresh Token**](/#authorization) for a new [**Access Token**](/#authorization).
- `redirect_uri` - A list of URIs to which the [**Authorization Server**](/#authorization) will send the user-agent (browser) after completing the authorization step

If the creation was successful, the response will return a `201` status code.
The response body will contain a **`client_id`** field which you will need to use in the next steps.

## 2. Initialize the OAuth2 recipes on the backend


In your **`backend`** application you will need to initialize both the **OAuth2Provider** and the **OAuth2Client** recipes.

<BackendSDKTabs>
<TabItem value="nodejs">

```typescript
import supertokens from "supertokens-node";
import OAuth2Provider from"supertokens-node/recipe/oauth2provider";
import OAuth2Client from"supertokens-node/recipe/oauth2client";

supertokens.init({
framework: "express",
supertokens: {
connectionURI: "<YOUR_CONNECTION_URI>",
apiKey: "<YOUR_API_KEY>",
},
appInfo: {
appName: "",
apiDomain: "<YOUR_API_DOMAIN>",
websiteDomain: "<YOUR_WEBSITE_DOMAIN>",
apiBasePath: "/auth",
websiteBasePath: "/auth"
},
recipeList: [
OAuth2Provider.init(),
// You will have to initialize the OAuth2Client recipe
// for each of your previously created clients
OAuth2Client.init({
clientId: '<CLIENT_ID>',
oidcDiscoveryEndpoint: 'https://<AUTHENTICATION_SERVICE_DOMAIN>/auth/.well-known/openid-configuration'
}),
]
});
```

</TabItem>

<TabItem value="go">

:::caution

At the moment we do not have support creating OAuth2 providers in the Go SDK.

:::

</TabItem>

<TabItem value="python">

:::caution

At the moment we do not have support creating OAuth2 providers in the Python SDK.

:::

</TabItem>

</BackendSDKTabs>

## 3. Initialize the recipe on the Frontend Applications

:::info

The `OAuth2Provider` recipe is available, at the moment, only when using the `supertokens-auth-react` package.

:::

Initialize the `OAuth2Provider` recipe on the frontend of each of your applications, ensuring you set the `authMode` to `header`.

```tsx
import OAuth2Provider from "supertokens-auth-react/recipe/oauth2provider";
import Session from "supertokens-auth-react/recipe/session";


export const SuperTokensConfig = {
appInfo: {
appName: "App Name",
apiDomain: "<AUTHENTICATION_SERVICE_DOMAIN>",
websiteDomain: "<FRONTEND_APPLICATION_DOMAIN>",
},
recipeList: [
OAuth2Provider.init({ authMode: "header" }),
Session.init(),
],
};
```

## 4. Update the login flow in your frontend applications

In your `frontend applications` you will have to add a login action that will direct the user to the authentication page.
You can just use a link for that. The link should be structured like this:

```
<AUTHENTICATION_SERIVICE_DOMAIN>/auth/oauth/auth?client_id=<CLIENT_ID>&redirect_uri=<REDIRECT_URI>
```
- `<AUTHENTICATION_SERIVICE_DOMAIN>` - The domain of your [**Authentication Service**](/docs/oauth/introduction#authentication-service).
The example assumes that you didn't overwrite the default `apiBasePath`.
If that's the case you will have to replace `/auth` with you custom base path.
- `<CLIENT_ID>` - The corresponding client ID based on what you have created in the first step.
- `<REDIRECT_URI>` - The corresponding redirect URI based on what you have created in the first step.

## 5. Test the new authentication flow

With everything set up, you can now test your login flow.
Just use the link that you have created in the previous step and try to access a protected resource.


9 changes: 9 additions & 0 deletions v2/oauth/customizations/add-custom-claims-in-tokens.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Add Custom Claims in Tokens
hide_title: true
---

# Add custom claims in the ID / access token

If you want to properties in the token payloads you can do this by overriding either the `buildIdTokenPayload` or the `buildAccessTokenPayload` functions.

6 changes: 6 additions & 0 deletions v2/oauth/customizations/custom-ui.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Implement a Custom UI
hide_title: true
---

# Implement a Custom UI
9 changes: 9 additions & 0 deletions v2/oauth/customizations/verify-tokens.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Verify Tokens
hide_title: true
---

# Verify Tokens



29 changes: 29 additions & 0 deletions v2/oauth/customizations/working-with-scopes.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Working with Scopes
hide_title: true
---

# Working with Scopes

The allowed scopes are set during the creation process of an **OAuth2 Client**.

By default, our **OAuth2** implementation adds the following built-in scopes:
- `email`: adds the `email` and `email_verified` claims into the Id Token and the User Info.
- `phoneNumber`: adds the `phoneNumber` and `phoneNumber_verified` claims into the Id Token and the User Info
- `roles`: adds the roles claim into the Id Token and Access Token.
- This will contain the roles returned by `getRolesForUser`
- This only works if the `UserRoles` recipe is initialized
- `permissions`: adds the `permissions` claim into the Id Token and Access Token.
- This will contain the list of permissions obtained by concatenating the result of `getPermissionsForRole` for all roles returned by `getRolesForUser`
- This only works if the `UserRoles` recipe is initialized

## Requesting Specific Scopes

The client can request specific scopes by adding `scope` query param to the Authorization URL.
The requested scopes have to be a subset of what is allowed for the client, otherwise the authentication requst will fail.
By default all scopes are granted to the client.

## Overriding granted scopes

If you want to manually modify the list of scopes that are granted to the client during the authentication flow you can do this by using overrides.
You will have to add a custom implementation for `getConsentRequest` and change the value it returns for `requestedScope`.
Loading