SaaSKit Complete: Complete SaaSKit documentation including FSA, SSO, SCIM, user management, and authorization
---
# DOCUMENT BOUNDARY
---
# Role based access control (RBAC)
> Control what authenticated users can access in your application based on their roles and permissions
When users access features in your application, your app needs to control what actions they can perform. These permissions might be set by your app as defaults or by organization administrators. For example, in a project management application, you can allow some users to create projects while restricting others to only view existing projects. Role-based access control (RBAC) provides the framework to implement these permissions systematically. After users authenticate through Scalekit, your application receives an access token containing their roles and permissions. Use this token to make authorization decisions and control access to features and resources. Access tokens contain two key components for authorization: **Roles** group related permissions together and define what users can do in your system. Common examples include Admin, Manager, Editor, and Viewer. Roles can inherit permissions from other roles, creating hierarchical access levels. **Permissions** represent specific actions users can perform, formatted as `resource:action` patterns like `projects:create` or `tasks:read`. Use permissions for granular access control when you need precise control over individual capabilities. Access token contents ```json { "aud": ["skc_987654321098765432"], "client_id": "skc_987654321098765432", "exp": 1750850145, "iat": 1750849845, "iss": "http://example.localhost:8889", "jti": "tkn_987654321098765432", "nbf": 1750849845, "roles": ["project_manager", "member"], "oid": "org_69615647365005430", "permissions": ["projects:create", "projects:read", "tasks:assign"], "sid": "ses_987654321098765432", "sub": "usr_987654321098765432" } ``` Scalekit automatically assigns the `admin` role to the first user in each organization and the `member` role to subsequent users. Your application uses the role and permission information from Scalekit to make final authorization decisions at runtime. Start by defining the roles and permissions your application needs.
---
# DOCUMENT BOUNDARY
---
# Multi-App Authentication
> Share authentication across web, mobile, and desktop applications with a unified session
Register multiple applications as OAuth clients that share a single Scalekit user session. Users authenticate once and gain access everywhere across your web app, mobile app, desktop client, and documentation site. Each application gets its own OAuth client with appropriate credentials based on its type, while all apps share the same underlying session. [Check out the example apps ](https://github.com/scalekit-inc/multiapp-demo) Use multi-app authentication when you ship multiple apps (web, mobile, desktop, or SPA), users expect to stay signed in across surfaces, or you need centralized session control and auditability. Each app gets its own OAuth client for clearer audit logs, safer scope boundaries, and easier maintenance. This eliminates friction from repeated logins and closes security gaps from inconsistent session handling. ## How multi-app authentication works [Section titled “How multi-app authentication works”](#how-multi-app-authentication-works) 1. [Register](/authenticate/fsa/multiapp/manage-apps/) each application as an OAuth client in Scalekit. 2. User logs into any app. 3. Scalekit creates a session for that user. 4. Other apps detect the session and skip the login prompt. 5. Logging out of any app terminates the shared session. ## Application types and authentication flows [Section titled “Application types and authentication flows”](#application-types-and-authentication-flows) Each application is registered separately in Scalekit and receives its own OAuth client. Choose the application type based on whether it has a backend server that can securely store credentials: | App Type | Description | Has Backend? | Uses Secret? | Auth Flow | | --------------------------------------------------------------------------- | ----------------------------------------------------------- | :----------: | :----------: | ------------------ | | [**Web app** (Express, Django, Rails)](/authenticate/fsa/multiapp/web-app) | Server-rendered or backend-driven apps with secure secrets. | ✓ | ✓ | Authorization Code | | [**SPA** (React, Vue, Angular)](/authenticate/fsa/multiapp/single-page-app) | Frontend-only apps running fully in the browser. | ✗ | ✗ | Auth Code + PKCE | | [**Mobile** (iOS, Android)](/authenticate/fsa/multiapp/native-app) | iOS or Android apps using system browser flows. | ✗ | ✗ | Auth Code + PKCE | | [**Desktop** (Electron, Tauri)](/authenticate/fsa/multiapp/native-app) | Electron or native desktop apps with deep links. | ✗ | ✗ | Auth Code + PKCE | Even though each app has a different `client_id`, they all rely on the same Scalekit user session. Separate clients per app give you clearer audit logs, safer scope boundaries, and easier long-term maintenance. ## Implementation steps [Section titled “Implementation steps”](#implementation-steps) 1. **Create applications in Scalekit** — [Create applications](/authenticate/fsa/multiapp/manage-apps) in Scalekit for each of your apps. During setup, select the app type based on whether it has a backend and needs client secrets. 2. **Configure redirect URLs for each app** — Redirects are registered endpoints in Scalekit that control where users are sent during authentication flows. [Configure redirect URLs](/authenticate/fsa/multiapp/manage-apps/#configure-redirect-urls) for each application. 3. **Implement login flow for each app** — Once your applications are registered, each app follows an OAuth-based authentication flow. Use the [login implementation guide](/authenticate/fsa/implement-login/) for implementing login/signup flow in your apps. 4. **Manage sessions and token refresh** — After users successfully authenticate in any of your apps, you receive session tokens that manage their access. Use the [session management guide](/authenticate/fsa/manage-session/) to manage sessions in your apps. 5. **Implement logout** — Initiate logout by calling the `/oidc/logout` endpoint with the relevant parameters. Clear your local application session when refresh token exchange fails, or configure back-channel logout to proactively sign users out across all applications sharing the same session. Follow the [logout implementation guide](/authenticate/fsa/logout/) to implement logout in your apps. ## Troubleshooting [Section titled “Troubleshooting”](#troubleshooting)
---
# DOCUMENT BOUNDARY
---
# SaaSKit: Add auth to my app
> SaaSKit — Hosted auth pages, managed sessions, secure logout. Purpose built. Simple where it counts
You’ll implement sign-up, login, and logout flows with secure session management and user management included. The foundation you build here extends to features like workspaces, enterprise SSO, MCP authentication, and SCIM provisioning. ### Build with a coding agent * Claude Code ```bash /plugin marketplace add scalekit-inc/claude-code-authstack ``` ```bash /plugin install full-stack-auth@scalekit-auth-stack ``` * Codex ```bash curl -fsSL https://raw.githubusercontent.com/scalekit-inc/codex-authstack/main/install.sh | bash ``` ```bash # Restart Codex # Plugin Directory -> Scalekit Auth Stack -> install full-stack-auth ``` * GitHub Copilot CLI ```bash copilot plugin marketplace add scalekit-inc/github-copilot-authstack ``` ```bash copilot plugin install full-stack-auth@scalekit-auth-stack ``` * 40+ agents ```bash npx skills add scalekit-inc/skills --skill implementing-scalekit-fsa ``` [Continue building with AI →](/dev-kit/build-with-ai/full-stack-auth/) *** 1. ## Set up Scalekit [Section titled “Set up Scalekit”](#set-up-scalekit) Use the following instructions to install the SDK for your technology stack. * Node.js ```bash npm install @scalekit-sdk/node ``` * Python ```sh pip install scalekit-sdk-python ``` * Go ```sh go get -u github.com/scalekit-inc/scalekit-sdk-go ``` * Java ```groovy /* Gradle users - add the following to your dependencies in build file */ implementation "com.scalekit:scalekit-sdk-java:2.0.11" ``` ```xml com.scalekit scalekit-sdk-java 2.0.11 ``` If you haven’t already, add your Scalekit credentials to your environment variables file: .env ```sh SCALEKIT_ENVIRONMENT_URL= SCALEKIT_CLIENT_ID= SCALEKIT_CLIENT_SECRET= ``` ### Register redirect URLs for your app [Section titled “Register redirect URLs for your app”](#register-redirect-urls-for-your-app) You need to register redirect URLs for your application. Go to **Scalekit dashboard** → **Authentication** → **Redirect URLs** and configure: * **Allowed callback URLs**: The endpoint where users are sent after successful authentication to exchange authorization codes and retrieve profile information. [Learn more](/guides/dashboard/redirects/#allowed-callback-urls) * **Initiate login URL**: The endpoint in your app that redirects users to Scalekit’s `/authorize` endpoint. Required when authentication is not initiated from your app, for example, when a user accepts an organization invitation or starts sign-in directly from their identity provider (IdP-initiated SSO). [Learn more](/guides/dashboard/redirects/#initiate-login-url) 2. ## Redirect users to sign up (or) login [Section titled “Redirect users to sign up (or) login”](#redirect-users-to-sign-up-or-login) An authorization URL is an endpoint that redirects users to Scalekit’s sign-in page. Use the Scalekit SDK to construct this URL with your redirect URI and required scopes. * Node.js routes/auth.ts ```javascript 1 // Must match the allowed callback URL you registered in the dashboard 2 const redirectUri = 'http://localhost:3000/auth/callback'; 3 4 // Request user profile data (openid, profile, email) and session tracking (offline_access) 5 // offline_access enables refresh tokens so users can stay logged in across sessions 6 const options = { 7 scopes: ['openid', 'profile', 'email', 'offline_access'] 8 }; 9 10 const authorizationUrl = scalekit.getAuthorizationUrl(redirectUri, options); 11 // Generated URL will look like: 12 // https:///oauth/authorize?response_type=code&client_id=skc_1234&scope=openid%20profile%20email%20offline_access&redirect_uri=https%3A%2F%2Fyourapp.com%2Fauth%2Fcallback 13 14 res.redirect(authorizationUrl); ``` * Python app/auth/routes.py ```python 1 from scalekit import AuthorizationUrlOptions 2 3 # Must match the allowed callback URL you registered in the dashboard 4 redirect_uri = 'http://localhost:3000/auth/callback' 5 6 # Request user profile data (openid, profile, email) and session tracking (offline_access) 7 # offline_access enables refresh tokens so users can stay logged in across sessions 8 options = AuthorizationUrlOptions() 9 options.scopes = ['openid', 'profile', 'email', 'offline_access'] 10 11 12 authorization_url = scalekit.get_authorization_url(redirect_uri, options) 13 # Generated URL will look like: 14 # https:///oauth/authorize?response_type=code&client_id=skc_1234&scope=openid%20profile%20email%20offline_access&redirect_uri=https%3A%2F%2Fyourapp.com%2Fcallback 15 16 return redirect(authorization_url) ``` * Go internal/http/auth.go ```go 1 // Must match the allowed callback URL you registered in the dashboard 2 redirectUri := "http://localhost:3000/auth/callback" 3 4 // Request user profile data (openid, profile, email) and session tracking (offline_access) 5 // offline_access enables refresh tokens so users can stay logged in across sessions 6 options := scalekit.AuthorizationUrlOptions{ 7 Scopes: []string{"openid", "profile", "email", "offline_access"} 8 } 9 10 authorizationUrl, err := scalekitClient.GetAuthorizationUrl(redirectUri, options) 11 // Generated URL will look like: 12 // https:///oauth/authorize?response_type=code&client_id=skc_1234&scope=openid%20profile%20email%20offline_access&redirect_uri=https%3A%2F%2Fyourapp.com%2Fcallback 13 if err != nil { 14 // Handle error based on your application's error handling strategy 15 panic(err) 16 } 17 18 c.Redirect(http.StatusFound, authorizationUrl.String()) ``` * Java AuthController.java ```java 1 import com.scalekit.internal.http.AuthorizationUrlOptions; 2 import java.net.URL; 3 import java.util.Arrays; 4 5 // Must match the allowed callback URL you registered in the dashboard 6 String redirectUri = "http://localhost:3000/auth/callback"; 7 8 // Request user profile data (openid, profile, email) and session tracking (offline_access) 9 // offline_access enables refresh tokens so users can stay logged in across sessions 10 AuthorizationUrlOptions options = new AuthorizationUrlOptions(); 11 options.setScopes(Arrays.asList("openid", "profile", "email", "offline_access")); 12 13 URL authorizationUrl = scalekit.authentication().getAuthorizationUrl(redirectUri, options); 14 // Generated URL will look like: 15 // https:///oauth/authorize?response_type=code&client_id=skc_1234&scope=openid%20profile%20email%20offline_access&redirect_uri=https%3A%2F%2Fyourapp.com%2Fcallback ``` This redirects users to Scalekit’s managed sign-in page where they can authenticate. The page includes default authentication methods for users to toggle between sign in and sign up. 3. ## Get user details from the callback [Section titled “Get user details from the callback”](#get-user-details-from-the-callback) After successful authentication, Scalekit creates a user record and sends the user information to your callback endpoint. In authentication flow, Scalekit redirects to your callback URL with an authorization code. Your application exchanges this code for the user’s profile information and session tokens. * Node.js routes/auth-callback.ts ```javascript 1 import scalekit from '@/utils/auth.js' 2 const redirectUri = ''; 3 4 // Get the authorization code from the scalekit initiated callback 5 app.get('/auth/callback', async (req, res) => { 6 const { code, error, error_description } = req.query; 7 8 if (error) { 9 return res.status(401).json({ error, error_description }); 10 } 11 12 try { 13 // Exchange the authorization code for user profile and session tokens 14 // Returns: user (profile info), idToken (JWT with user claims), accessToken (JWT with roles/permissions), refreshToken 15 const authResult = await scalekit.authenticateWithCode( 16 code, redirectUri 17 ); 18 19 const { user, idToken, accessToken, refreshToken } = authResult; 20 // idToken: Decode to access full user profile (sub, oid, email, name) 21 // accessToken: Contains roles and permissions for authorization decisions 22 // refreshToken: Use to obtain new access tokens when they expire 23 24 // "user" object contains the user's profile information 25 // Next step: Create a session and log in the user 26 res.redirect('/dashboard/profile'); 27 } catch (err) { 28 console.error('Error exchanging code:', err); 29 res.status(500).json({ error: 'Failed to authenticate user' }); 30 } 31 }); ``` * Python app/auth/callback.py ```python 1 from flask import Flask, request, redirect, jsonify 2 from scalekit import ScalekitClient, CodeAuthenticationOptions 3 4 app = Flask(__name__) 5 # scalekit imported from your auth utils 6 7 redirect_uri = 'http://localhost:3000/auth/callback' 8 9 @app.route('/auth/callback') 10 def callback(): 11 code = request.args.get('code') 12 error = request.args.get('error') 13 error_description = request.args.get('error_description') 14 15 if error: 16 return jsonify({'error': error, 'error_description': error_description}), 401 17 18 try: 19 # Exchange the authorization code for user profile and session tokens 20 # Returns: user (profile info), id_token (JWT with user claims), access_token (JWT with roles/permissions), refresh_token 21 options = CodeAuthenticationOptions() 22 auth_result = scalekit.authenticate_with_code( 23 code, redirect_uri, options 24 ) 25 26 user = auth_result["user"] 27 # id_token: Decode to access full user profile (sub, oid, email, name) 28 # access_token: Contains roles and permissions for authorization decisions 29 # refresh_token: Use to obtain new access tokens when they expire 30 31 # "user" object contains the user's profile information 32 # Next step: Create a session and log in the user 33 return redirect('/dashboard/profile') 34 except Exception as err: 35 print(f'Error exchanging code: {err}') 36 return jsonify({'error': 'Failed to authenticate user'}), 500 ``` * Go internal/http/auth\_callback.go ```go 1 package main 2 3 import ( 4 "log" 5 "net/http" 6 "os" 7 "github.com/gin-gonic/gin" 8 "github.com/scalekit-inc/scalekit-sdk-go" 9 ) 10 11 // Create Scalekit client instance 12 var scalekitClient = scalekit.NewScalekitClient( 13 os.Getenv("SCALEKIT_ENVIRONMENT_URL"), 14 os.Getenv("SCALEKIT_CLIENT_ID"), 15 os.Getenv("SCALEKIT_CLIENT_SECRET"), 16 ) 17 18 const redirectUri = "http://localhost:3000/auth/callback" 19 20 func callbackHandler(c *gin.Context) { 21 code := c.Query("code") 22 errorParam := c.Query("error") 23 errorDescription := c.Query("error_description") 24 25 if errorParam != "" { 26 c.JSON(http.StatusUnauthorized, gin.H{ 27 "error": errorParam, 28 "error_description": errorDescription, 29 }) 30 return 31 } 32 33 // Exchange the authorization code for user profile and session tokens 34 // Returns: User (profile info), IdToken (JWT with user claims), AccessToken (JWT with roles/permissions), RefreshToken 35 options := scalekit.AuthenticationOptions{} 36 authResult, err := scalekitClient.AuthenticateWithCode( 37 c.Request.Context(), code, redirectUri, options, 38 ) 39 40 if err != nil { 41 log.Printf("Error exchanging code: %v", err) 42 c.JSON(http.StatusInternalServerError, gin.H{ 43 "error": "Failed to authenticate user", 44 }) 45 return 46 } 47 48 user := authResult.User 49 // IdToken: Decode to access full user profile (sub, oid, email, name) 50 // AccessToken: Contains roles and permissions for authorization decisions 51 // RefreshToken: Use to obtain new access tokens when they expire 52 53 // "user" object contains the user's profile information 54 // Next step: Create a session and log in the user 55 c.Redirect(http.StatusFound, "/dashboard/profile") 56 } ``` * Java CallbackController.java ```java 1 import com.scalekit.ScalekitClient; 2 import com.scalekit.internal.http.AuthenticationOptions; 3 import com.scalekit.internal.http.AuthenticationResponse; 4 import org.springframework.web.bind.annotation.*; 5 import org.springframework.web.servlet.view.RedirectView; 6 import org.springframework.http.ResponseEntity; 7 import org.springframework.http.HttpStatus; 8 import java.util.HashMap; 9 import java.util.Map; 10 11 @RestController 12 public class CallbackController { 13 14 private final String redirectUri = "http://localhost:3000/auth/callback"; 15 16 @GetMapping("/auth/callback") 17 public Object callback( 18 @RequestParam(required = false) String code, 19 @RequestParam(required = false) String error, 20 @RequestParam(name = "error_description", required = false) String errorDescription 21 ) { 22 if (error != null) { 23 // handle error 24 } 25 26 try { 27 // Exchange the authorization code for user profile and session tokens 28 // Returns: user (profile info), idToken (JWT with user claims), accessToken (JWT with roles/permissions), refreshToken 29 AuthenticationOptions options = new AuthenticationOptions(); 30 AuthenticationResponse authResult = scalekit 31 .authentication() 32 .authenticateWithCode(code,redirectUri,options); 33 34 var user = authResult.getIdTokenClaims(); 35 // idToken: Decode to access full user profile (sub, oid, email, name) 36 // accessToken: Contains roles and permissions for authorization decisions 37 // refreshToken: Use to obtain new access tokens when they expire 38 39 // "user" object contains the user's profile information 40 // Next step: Create a session and log in the user 41 return new RedirectView("/dashboard/profile"); 42 43 } catch (Exception err) { 44 // Handle exception (e.g., log error, return error response) 45 } 46 } 47 } ``` The `authResult` object contains: * `user` - Common user details with email, name, and verification status * `idToken` - JWT containing verified full user identity claims (includes: `sub` user ID, `oid` organization ID, `email`, `name`, `exp` expiration) * `accessToken` - Short-lived token that determines current access context (includes: `sub` user ID, `oid` organization ID, `roles`, `permissions`, `exp` expiration) * `refreshToken` - Long-lived token to obtain new access tokens - Auth result ```js 1 { 2 user: { 3 email: "john.doe@example.com", 4 emailVerified: true, 5 givenName: "John", 6 name: "John Doe", 7 id: "usr_74599896446906854" 8 }, 9 idToken: "eyJhbGciO..", // Decode for full user details 10 11 accessToken: "eyJhbGciOi..", 12 refreshToken: "rt_8f7d6e5c4b3a2d1e0f9g8h7i6j..", 13 expiresIn: 299 // in seconds 14 } ``` - Decoded ID token ID token decoded ```json 1 { 2 "at_hash": "ec_jU2ZKpFelCKLTRWiRsg", 3 "aud": [ 4 "skc_58327482062864390" 5 ], 6 "azp": "skc_58327482062864390", 7 "c_hash": "6wMreK9kWQQY6O5R0CiiYg", 8 "client_id": "skc_58327482062864390", 9 "email": "john.doe@example.com", 10 "email_verified": true, 11 "exp": 1742975822, 12 "family_name": "Doe", 13 "given_name": "John", 14 "iat": 1742974022, 15 "iss": "https://scalekit-z44iroqaaada-dev.scalekit.cloud", 16 "name": "John Doe", 17 "oid": "org_59615193906282635", 18 "sid": "ses_65274187031249433", 19 "sub": "usr_63261014140912135" 20 } ``` - Decoded access token Decoded access token ```json 1 { 2 "aud": [ 3 "prd_skc_7848964512134X699" 4 ], 5 "client_id": "prd_skc_7848964512134X699", 6 "exp": 1758265247, 7 "iat": 1758264947, 8 "iss": "https://login.devramp.ai", 9 "jti": "tkn_90928731115292X63", 10 "nbf": 1758264947, 11 "oid": "org_89678001X21929734", 12 "permissions": [ 13 "workspace_data:write", 14 "workspace_data:read" 15 ], 16 "roles": [ 17 "admin" 18 ], 19 "sid": "ses_90928729571723X24", 20 "sub": "usr_8967800122X995270", 21 // External identifiers if updated on Scalekit 22 "xoid": "ext_org_123", // Organization ID 23 "xuid": "ext_usr_456", // User ID 24 } ``` The user details are packaged in the form of JWT tokens. Decode the `idToken` to access full user profile information (email, name, organization ID) and the `accessToken` to check user roles and permissions for authorization decisions. See [Complete login with code exchange](/authenticate/fsa/complete-login/) for detailed token claim references and verification instructions. 4. ## Create and manage user sessions [Section titled “Create and manage user sessions”](#create-and-manage-user-sessions) The access token is a JWT that contains the user’s permissions and roles. It expires in 5 minutes (default) but [can be configured](/authenticate/fsa/manage-session/#configure-session-security-and-duration). When it expires, use the refresh token to obtain a new access token. The refresh token is long-lived and designed for this purpose. The Scalekit SDK provides methods to refresh access tokens automatically. However, you must log the user out when the refresh token itself expires or becomes invalid. * Node.js ```javascript 1 import cookieParser from 'cookie-parser'; 2 // Set cookie parser middleware 3 app.use(cookieParser()); 4 5 // Store access token in HttpOnly cookie with Path scoping to API routes 6 res.cookie('accessToken', authResult.accessToken, { 7 maxAge: (authResult.expiresIn - 60) * 1000, 8 httpOnly: true, 9 secure: true, 10 path: '/api', 11 sameSite: 'strict' 12 }); 13 14 // Store refresh token in separate HttpOnly cookie with Path scoped to refresh endpoint 15 res.cookie('refreshToken', authResult.refreshToken, { 16 httpOnly: true, 17 secure: true, 18 path: '/auth/refresh', 19 sameSite: 'strict' 20 }); ``` * Python ```python 1 from flask import Flask, make_response 2 import os 3 4 # Cookie parsing is built-in with Flask's request object 5 app = Flask(__name__) 6 7 response = make_response() 8 9 # Store access token in HttpOnly cookie with Path scoping to API routes 10 response.set_cookie( 11 'accessToken', 12 auth_result.access_token, 13 max_age=auth_result.expires_in - 60, # seconds in Flask 14 httponly=True, 15 secure=True, 16 path='/api', 17 samesite='Strict' 18 ) 19 20 # Store refresh token in separate HttpOnly cookie with Path scoped to refresh endpoint 21 response.set_cookie( 22 'refreshToken', 23 auth_result.refresh_token, 24 httponly=True, 25 secure=True, 26 path='/auth/refresh', 27 samesite='Strict' 28 ) ``` * Go ```go 1 import ( 2 "net/http" 3 "os" 4 ) 5 6 // Set SameSite mode for CSRF protection 7 c.SetSameSite(http.SameSiteStrictMode) 8 9 // Store access token in HttpOnly cookie with Path scoping to API routes 10 c.SetCookie( 11 "accessToken", 12 authResult.AccessToken, 13 authResult.ExpiresIn-60, // seconds in Gin 14 "/api", 15 "", 16 os.Getenv("GIN_MODE") == "release", 17 true, 18 ) 19 20 // Store refresh token in separate HttpOnly cookie with Path scoped to refresh endpoint 21 c.SetCookie( 22 "refreshToken", 23 authResult.RefreshToken, 24 0, // No expiry for refresh token cookie 25 "/auth/refresh", 26 "", 27 os.Getenv("GIN_MODE") == "release", 28 true, 29 ) ``` * Java ```java 1 import javax.servlet.http.Cookie; 2 import javax.servlet.http.HttpServletResponse; 3 4 // Store access token in HttpOnly cookie with Path scoping to API routes 5 Cookie accessTokenCookie = new Cookie("accessToken", authResult.getAccessToken()); 6 accessTokenCookie.setMaxAge(authResult.getExpiresIn() - 60); // seconds in Spring 7 accessTokenCookie.setHttpOnly(true); 8 accessTokenCookie.setSecure(true); 9 accessTokenCookie.setPath("/api"); 10 response.addCookie(accessTokenCookie); 11 12 // Store refresh token in separate HttpOnly cookie with Path scoped to refresh endpoint 13 Cookie refreshTokenCookie = new Cookie("refreshToken", authResult.getRefreshToken()); 14 refreshTokenCookie.setHttpOnly(true); 15 refreshTokenCookie.setSecure(true); 16 refreshTokenCookie.setPath("/auth/refresh"); 17 response.addCookie(refreshTokenCookie); 18 response.setHeader("Set-Cookie", 19 response.getHeader("Set-Cookie") + "; SameSite=Strict"); ``` This sets browser cookies with the session tokens. Every request to your backend needs to verify the `accessToken` to ensure the user is authenticated. If expired, use the `refreshToken` to get a new access token. * Node.js ```javascript 1 // Middleware to verify and refresh tokens if needed 2 const verifyToken = async (req, res, next) => { 3 try { 4 // Get access token from cookie and decrypt it 5 const accessToken = req.cookies.accessToken; 6 const decryptedAccessToken = decrypt(accessToken); 7 8 if (!accessToken) { 9 return res.status(401).json({ message: 'No access token provided' }); 10 } 11 12 // Use Scalekit SDK to validate the token 13 const isValid = await scalekit.validateAccessToken(decryptedAccessToken); 14 15 if (!isValid) { 16 // Use stored refreshToken to get a new access token 17 const { 18 user, 19 idToken, 20 accessToken, 21 refreshToken: newRefreshToken, 22 } = await scalekit.refreshAccessToken(refreshToken); 23 24 // Store the new refresh token 25 // Update the cookie with the new access token 26 } 27 next(); 28 }; 29 30 // Example of using the middleware to protect routes 31 app.get('/dashboard', verifyToken, (req, res) => { 32 // The user object is now available in req.user 33 res.json({ 34 message: 'This is a protected route', 35 user: req.user 36 }); 37 }); ``` * Python ```python 1 from functools import wraps 2 from flask import request, jsonify, make_response 3 4 def verify_token(f): 5 """Decorator to verify and refresh tokens if needed""" 6 @wraps(f) 7 def decorated_function(*args, **kwargs): 8 try: 9 # Get access token from cookie 10 access_token = request.cookies.get('accessToken') 11 12 if not access_token: 13 return jsonify({'message': 'No access token provided'}), 401 14 15 # Decrypt the accessToken using the same encryption algorithm 16 decrypted_access_token = decrypt(access_token) 17 18 # Use Scalekit SDK to validate the token 19 is_valid = scalekit.validate_access_token(decrypted_access_token) 20 21 if not is_valid: 22 # Get stored refresh token 23 refresh_token = get_stored_refresh_token() 24 25 if not refresh_token: 26 return jsonify({'message': 'No refresh token available'}), 401 27 28 # Use stored refreshToken to get a new access token 29 token_response = scalekit.refresh_access_token(refresh_token) 30 31 # Python SDK returns dict with access_token and refresh_token 32 new_access_token = token_response.get('access_token') 33 new_refresh_token = token_response.get('refresh_token') 34 35 # Store the new refresh token 36 store_refresh_token(new_refresh_token) 37 38 # Update the cookie with the new access token 39 encrypted_new_access_token = encrypt(new_access_token) 40 response = make_response(f(*args, **kwargs)) 41 response.set_cookie( 42 'accessToken', 43 encrypted_new_access_token, 44 httponly=True, 45 secure=True, 46 path='/', 47 samesite='Strict' 48 ) 49 50 return response 51 52 # If the token was valid we just invoke the view as-is 53 return f(*args, **kwargs) 54 55 except Exception as e: 56 return jsonify({'message': f'Token verification failed: {str(e)}'}), 401 57 58 return decorated_function 59 60 # Example of using the decorator to protect routes 61 @app.route('/dashboard') 62 @verify_token 63 def dashboard(): 64 return jsonify({ 65 'message': 'This is a protected route', 66 'user': getattr(request, 'user', None) 67 }) ``` * Go ```go 1 import ( 2 "context" 3 "net/http" 4 ) 5 6 // verifyToken is a middleware that ensures a valid access token or refreshes it if expired. 7 func verifyToken(next http.HandlerFunc) http.HandlerFunc { 8 return func(w http.ResponseWriter, r *http.Request) { 9 // Retrieve the access token from the user's cookie 10 cookie, err := r.Cookie("accessToken") 11 if err != nil { 12 // No access token cookie found; reject the request 13 http.Error(w, `{"message": "No access token provided"}`, http.StatusUnauthorized) 14 return 15 } 16 17 accessToken := cookie.Value 18 19 // Decrypt the access token before validation 20 decryptedAccessToken, err := decrypt(accessToken) 21 if err != nil { 22 // Could not decrypt access token; treat as invalid 23 http.Error(w, `{"message": "Token decryption failed"}`, http.StatusUnauthorized) 24 return 25 } 26 27 // Validate the access token using the Scalekit SDK 28 isValid, err := scalekitClient.ValidateAccessToken(r.Context(), decryptedAccessToken) 29 if err != nil || !isValid { 30 // Access token is invalid or expired 31 32 // Attempt to retrieve the stored refresh token 33 refreshToken, err := getStoredRefreshToken(r) 34 if err != nil { 35 // No refresh token is available; cannot continue 36 http.Error(w, `{"message": "No refresh token available"}`, http.StatusUnauthorized) 37 return 38 } 39 40 // Use the refresh token to obtain a new access token from Scalekit 41 tokenResponse, err := scalekitClient.RefreshAccessToken(r.Context(), refreshToken) 42 if err != nil { 43 // Refresh attempt failed; likely an expired or invalid refresh token 44 http.Error(w, `{"message": "Token refresh failed"}`, http.StatusUnauthorized) 45 return 46 } 47 48 // Save the new refresh token so it can be reused for future requests 49 err = storeRefreshToken(tokenResponse.RefreshToken) 50 if err != nil { 51 // Could not store the new refresh token 52 http.Error(w, `{"message": "Failed to store refresh token"}`, http.StatusInternalServerError) 53 return 54 } 55 56 // Encrypt the new access token before setting it in the cookie 57 encryptedNewAccessToken, err := encrypt(tokenResponse.AccessToken) 58 if err != nil { 59 // Could not encrypt new access token 60 http.Error(w, `{"message": "Token encryption failed"}`, http.StatusInternalServerError) 61 return 62 } 63 64 // Issue a new accessToken cookie with updated credentials 65 newCookie := &http.Cookie{ 66 Name: "accessToken", 67 Value: encryptedNewAccessToken, 68 HttpOnly: true, 69 Secure: true, 70 Path: "/", 71 SameSite: http.SameSiteStrictMode, 72 } 73 http.SetCookie(w, newCookie) 74 75 // Mark the token as valid in the request context and proceed 76 r = r.WithContext(context.WithValue(r.Context(), "tokenValid", true)) 77 } else { 78 // The access token is valid; continue with marked context 79 r = r.WithContext(context.WithValue(r.Context(), "tokenValid", true)) 80 } 81 82 // Pass the request along to the next handler in the chain 83 next(w, r) 84 } 85 } 86 87 // dashboardHandler demonstrates a protected route that requires authentication. 88 func dashboardHandler(w http.ResponseWriter, r *http.Request) { 89 w.Header().Set("Content-Type", "application/json") 90 w.Write([]byte(`{ 91 "message": "This is a protected route", 92 "tokenValid": true 93 }`)) 94 } 95 96 // Usage example: 97 // Attach middleware to the /dashboard route: 98 // http.HandleFunc("/dashboard", verifyToken(dashboardHandler)) ``` * Java ```java 1 import javax.servlet.http.HttpServletRequest; 2 import javax.servlet.http.HttpServletResponse; 3 import javax.servlet.http.Cookie; 4 import org.springframework.web.servlet.HandlerInterceptor; 5 6 @Component 7 public class TokenVerificationInterceptor implements HandlerInterceptor { 8 @Override 9 public boolean preHandle( 10 HttpServletRequest request, 11 HttpServletResponse response, 12 Object handler 13 ) throws Exception { 14 try { 15 // Get access token from cookie 16 String accessToken = getCookieValue(request, "accessToken"); 17 String refreshToken = getCookieValue(request, "refreshToken"); 18 19 // Decrypt the tokens 20 String decryptedAccessToken = decrypt(accessToken); 21 String decryptedRefreshToken = decrypt(refreshToken); 22 23 // Use Scalekit SDK to validate the token 24 boolean isValid = scalekit.authentication().validateAccessToken(decryptedAccessToken); 25 26 27 // Use refreshToken to get a new access token 28 AuthenticationResponse tokenResponse = scalekit 29 .authentication() 30 .refreshToken(decryptedRefreshToken); 31 32 // Update the cookie with the new access token and refresh token 33 String encryptedNewAccessToken = encrypt(tokenResponse.getAccessToken()); 34 String encryptedNewRefreshToken = encrypt(tokenResponse.getRefreshToken()); 35 36 Cookie accessTokenCookie = new Cookie("accessToken", encryptedNewAccessToken); 37 accessTokenCookie.setHttpOnly(true); 38 accessTokenCookie.setSecure(true); 39 accessTokenCookie.setPath("/"); 40 response.addCookie(accessTokenCookie); 41 42 Cookie refreshTokenCookie = new Cookie("refreshToken", encryptedNewRefreshToken); 43 refreshTokenCookie.setHttpOnly(true); 44 refreshTokenCookie.setSecure(true); 45 refreshTokenCookie.setPath("/"); 46 response.addCookie(refreshTokenCookie); 47 48 return true; 49 } catch (Exception e) { 50 // handle exception 51 } 52 } 53 54 private String getCookieValue(HttpServletRequest request, String cookieName) { 55 Cookie[] cookies = request.getCookies(); 56 if (cookies != null) { 57 for (Cookie cookie : cookies) { 58 if (cookieName.equals(cookie.getName())) { 59 return cookie.getValue(); 60 } 61 } 62 } 63 return null; 64 } 65 } ``` Authenticated users can access your dashboard. The app enforces session policies using session tokens. To change session policies, go to Dashboard > Authentication > Session Policy in the Scalekit dashboard. 5. ## Log out the user [Section titled “Log out the user”](#log-out-the-user) Session persistence depends on the session policy configured in the Scalekit dashboard. To log out a user, clear local session data and invalidate the user’s session in Scalekit. * Node.js ```javascript 1 app.get('/logout', (req, res) => { 2 // Clear all session data including cookies and local storage 3 clearSessionData(); 4 5 const logoutUrl = scalekit.getLogoutUrl( 6 idTokenHint, // ID token to invalidate 7 postLogoutRedirectUri // URL that scalekit redirects after session invalidation 8 ); 9 10 // Redirect the user to the Scalekit logout endpoint to begin invalidating the session. 11 res.redirect(logoutUrl); // This URL can only be used once and expires after logout. 12 }); ``` * Python ```python 1 from flask import Flask, redirect 2 from scalekit.common.scalekit import LogoutUrlOptions 3 4 app = Flask(__name__) 5 6 @app.route('/logout') 7 def logout(): 8 # Clear all session data including cookies and local storage 9 clear_session_data() 10 11 # Generate Scalekit logout URL 12 options = LogoutUrlOptions( 13 id_token_hint=id_token, 14 post_logout_redirect_uri=post_logout_redirect_uri 15 ) 16 logout_url = scalekit.get_logout_url(options) 17 18 # Redirect to Scalekit's logout endpoint 19 # Note: This is a one-time use URL that becomes invalid after use 20 return redirect(logout_url) ``` * Go ```go 1 package main 2 3 import ( 4 "net/http" 5 "github.com/gin-gonic/gin" 6 "github.com/scalekit-inc/scalekit-sdk-go" 7 ) 8 9 func logoutHandler(c *gin.Context) { 10 // Clear all session data including cookies and local storage 11 clearSessionData() 12 13 // Generate Scalekit logout URL 14 options := scalekit.LogoutUrlOptions{ 15 IdTokenHint: idToken, 16 PostLogoutRedirectUri: postLogoutRedirectUri, 17 } 18 logoutUrl, err := scalekitClient.GetLogoutUrl(options) 19 if err != nil { 20 c.JSON(http.StatusInternalServerError, gin.H{ 21 "error": "Failed to generate logout URL", 22 }) 23 return 24 } 25 26 // Redirect to Scalekit's logout endpoint 27 // Note: This is a one-time use URL that becomes invalid after use 28 c.Redirect(http.StatusFound, logoutUrl.String()) 29 } ``` * Java ```java 1 import com.scalekit.internal.http.LogoutUrlOptions; 2 import org.springframework.web.bind.annotation.*; 3 import org.springframework.web.servlet.view.RedirectView; 4 import java.net.URL; 5 6 @RestController 7 public class LogoutController { 8 9 @GetMapping("/logout") 10 public RedirectView logout() { 11 12 clearSessionData(); 13 14 15 LogoutUrlOptions options = new LogoutUrlOptions(); 16 options.setIdTokenHint(idToken); 17 options.setPostLogoutRedirectUri(postLogoutRedirectUri); 18 19 URL logoutUrl = scalekit.authentication() 20 .getLogoutUrl(options); 21 22 23 // Note: This is a one-time use URL that becomes invalid after use 24 return new RedirectView(logoutUrl.toString()); 25 } 26 } ``` The logout process completes when Scalekit invalidates the user’s session and redirects them to your [registered post-logout URL](/guides/dashboard/redirects/#post-logout-url). This single integration unlocks multiple authentication methods, including Magic Link & OTP, social sign-ins, enterprise single sign-on (SSO), and robust user management features. As you continue working with Scalekit, you’ll discover even more features that enhance your authentication workflows.
---
# DOCUMENT BOUNDARY
---
# Add Modular SCIM provisioning
> Automate user provisioning with SCIM. Directory API and webhooks for real-time user data sync
This guide shows you how to automate user provisioning with SCIM using Scalekit’s Directory API and webhooks. You’ll learn to sync user data in real-time, create webhook endpoints for instant updates, and build automated provisioning workflows that keep your application’s user data synchronized with your customers’ directory providers. With [SCIM Provisioning](/directory/guides/user-provisioning-basics) from Scalekit, you can: * Use **webhooks** to listen for events from your customers’ directory providers (e.g., user updates, group changes) * Use **REST APIs** to list users, groups, and directories on demand Scalekit abstracts the complexities of various directory providers, giving you a single interface to automate user lifecycle management. This enables you to create accounts for new hires during onboarding, deactivate accounts when employees depart, and adjust access levels as employees change roles.  ### Build with a coding agent * Claude Code ```bash /plugin marketplace add scalekit-inc/claude-code-authstack ``` ```bash /plugin install modular-scim@scalekit-auth-stack ``` * Codex ```bash curl -fsSL https://raw.githubusercontent.com/scalekit-inc/codex-authstack/main/install.sh | bash ``` ```bash # Restart Codex # Plugin Directory -> Scalekit Auth Stack -> install modular-scim ``` * GitHub Copilot CLI ```bash copilot plugin marketplace add scalekit-inc/github-copilot-authstack ``` ```bash copilot plugin install modular-scim@scalekit-auth-stack ``` * 40+ agents ```bash npx skills add scalekit-inc/skills --skill implementing-scim-provisioning ``` [Continue building with AI →](/dev-kit/build-with-ai/scim/) ## User provisioning with Scalekit’s directory API [Section titled “User provisioning with Scalekit’s directory API”](#user-provisioning-with-scalekits-directory-api) Scalekit’s directory API allows you to fetch information about users, groups, and directories associated with an organization on-demand. This approach is ideal for scheduled synchronization tasks, bulk data imports, or when you need to ensure your application’s user data matches the latest directory provider state. Let’s explore how to use the Directory API to retrieve user and group data programmatically. 1. ### Setting up the SDK [Section titled “Setting up the SDK”](#setting-up-the-sdk) Before you begin, ensure that your organization [has a directory set up in Scalekit](/guides/user-management/scim-provisioning/). Scalekit offers language-specific SDKs for fast SSO integration. Use the installation instructions below for your technology stack: * Node.js ```bash npm install @scalekit-sdk/node ``` * Python ```sh pip install scalekit-sdk-python ``` * Go ```sh go get -u github.com/scalekit-inc/scalekit-sdk-go ``` * Java ```groovy /* Gradle users - add the following to your dependencies in build file */ implementation "com.scalekit:scalekit-sdk-java:2.0.11" ``` ```xml com.scalekit scalekit-sdk-java 2.0.11 ``` Navigate to **Dashboard > Developers > Settings > API Credentials** to obtain your credentials. Store your credentials securely in environment variables: .env ```shell 1 # Get these values from Dashboard > Developers > Settings > API Credentials 2 SCALEKIT_ENVIRONMENT_URL='https://b2b-app-dev.scalekit.com' 3 SCALEKIT_CLIENT_ID='' 4 SCALEKIT_CLIENT_SECRET='' ``` 2. ### Initialize the SDK and make your first API call [Section titled “Initialize the SDK and make your first API call”](#initialize-the-sdk-and-make-your-first-api-call) Initialize the Scalekit client with your environment variables and make your first API call to list organizations. * cURL Terminal ```bash 1 # Security: Replace with a valid access token from Scalekit 2 # This token authorizes your API requests to access organization data 3 4 # Use case: Verify API connectivity and test authentication 5 # Examples: Initial setup testing, debugging integration issues 6 7 curl -L "https://$SCALEKIT_ENVIRONMENT_URL/api/v1/organizations?page_size=5" \ 8 -H "Authorization: Bearer " ``` * Node.js Node.js ```javascript 1 import { ScalekitClient } from '@scalekit-sdk/node'; 2 3 // Initialize Scalekit client with environment variables 4 // Security: Always use environment variables for sensitive credentials 5 const scalekit = new ScalekitClient( 6 process.env.SCALEKIT_ENVIRONMENT_URL, 7 process.env.SCALEKIT_CLIENT_ID, 8 process.env.SCALEKIT_CLIENT_SECRET, 9 ); 10 11 try { 12 // Use case: Retrieve organizations for bulk user provisioning workflows 13 // Examples: Multi-tenant applications, enterprise customer onboarding 14 const { organizations } = await scalekit.organization.listOrganization({ 15 pageSize: 5, 16 }); 17 18 console.log(`Organization name: ${organizations[0].display_name}`); 19 console.log(`Organization ID: ${organizations[0].id}`); 20 } catch (error) { 21 console.error('Failed to list organizations:', error); 22 // Handle error appropriately for your application 23 } ``` * Python Python ```python 1 from scalekit import ScalekitClient 2 import os 3 4 # Initialize the SDK client with environment variables 5 # Security: Use os.getenv() to securely access credentials 6 scalekit_client = ScalekitClient( 7 env_url=os.getenv("SCALEKIT_ENVIRONMENT_URL"), 8 client_id=os.getenv("SCALEKIT_CLIENT_ID"), 9 client_secret=os.getenv("SCALEKIT_CLIENT_SECRET") 10 ) 11 12 try: 13 # Use case: Sync user data across multiple organizations 14 # Examples: Scheduled provisioning tasks, HR system integration 15 org_list = scalekit_client.organization.list_organizations(page_size=100) 16 17 if org_list: 18 print(f'Organization details: {org_list[0]}') 19 print(f'Organization ID: {org_list[0].id}') 20 except Exception as error: 21 print(f'Error listing organizations: {error}') 22 # Implement appropriate error handling for your use case ``` * Go Go ```go 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 8 "github.com/scalekit/scalekit-go" 9 ) 10 11 // Initialize Scalekit client with environment variables 12 // Security: Always load credentials from environment, not hardcoded 13 scalekitClient := scalekit.NewScalekitClient( 14 os.Getenv("SCALEKIT_ENVIRONMENT_URL"), 15 os.Getenv("SCALEKIT_CLIENT_ID"), 16 os.Getenv("SCALEKIT_CLIENT_SECRET"), 17 ) 18 19 // Use case: Get specific organization for directory sync operations 20 // Examples: Targeted user provisioning, organization-specific workflows 21 organization, err := scalekitClient.Organization.GetOrganization( 22 ctx, 23 organizationId, 24 ) 25 if err != nil { 26 // Handle error appropriately for your application 27 return fmt.Errorf("failed to get organization: %w", err) 28 } ``` * Java Java ```java 1 import com.scalekit.ScalekitClient; 2 3 // Initialize Scalekit client with environment variables 4 // Security: Use System.getenv() to securely access credentials 5 ScalekitClient scalekitClient = new ScalekitClient( 6 System.getenv("SCALEKIT_ENVIRONMENT_URL"), 7 System.getenv("SCALEKIT_CLIENT_ID"), 8 System.getenv("SCALEKIT_CLIENT_SECRET") 9 ); 10 11 try { 12 // Use case: List organizations for automated provisioning workflows 13 // Examples: Enterprise customer setup, multi-tenant management 14 ListOrganizationsResponse organizations = scalekitClient.organizations() 15 .listOrganizations(5, ""); 16 17 if (!organizations.getOrganizations().isEmpty()) { 18 Organization firstOrg = organizations.getOrganizations().get(0); 19 System.out.println("Organization name: " + firstOrg.getDisplayName()); 20 System.out.println("Organization ID: " + firstOrg.getId()); 21 } 22 } catch (ScalekitException error) { 23 System.err.println("Failed to list organizations: " + error.getMessage()); 24 // Implement appropriate error handling 25 } ``` 3. ### Retrieve a directory [Section titled “Retrieve a directory”](#retrieve-a-directory) After successfully listing organizations, you’ll need to retrieve the specific directory to begin syncing user and group data. You can retrieve directories using either the organization and directory IDs, or fetch the primary directory for an organization. * Node.js Node.js ```javascript 1 try { 2 // Use case: Get specific directory when organization has multiple directories 3 // Examples: Department-specific provisioning, multi-division companies 4 const { directory } = await scalekit.directory.getDirectory('', ''); 5 console.log(`Directory name: ${directory.name}`); 6 7 // Use case: Get primary directory for simple provisioning workflows 8 // Examples: Small organizations, single-directory setups 9 const { directory } = await scalekit.directory.getPrimaryDirectoryByOrganizationId(''); 10 console.log(`Primary directory ID: ${directory.id}`); 11 } catch (error) { 12 console.error('Failed to retrieve directory:', error); 13 // Handle error appropriately for your application 14 } ``` * Python Python ```python 1 try: 2 # Use case: Access specific directory for targeted user sync operations 3 # Examples: Regional offices, business unit-specific provisioning 4 directory = scalekit_client.directory.get_directory( 5 organization_id='', directory_id='' 6 ) 7 print(f'Directory name: {directory.name}') 8 9 # Use case: Get primary directory for streamlined user management 10 # Examples: Standard employee provisioning, main company directory 11 primary_directory = scalekit_client.directory.get_primary_directory_by_organization_id( 12 organization_id='' 13 ) 14 print(f'Primary directory ID: {primary_directory.id}') 15 except Exception as error: 16 print(f'Error retrieving directory: {error}') 17 # Implement appropriate error handling ``` * Go Go ```go 1 // Use case: Retrieve specific directory for granular access control 2 // Examples: Multi-tenant environments, department-level provisioning 3 directory, err := scalekitClient.Directory().GetDirectory(ctx, organizationId, directoryId) 4 if err != nil { 5 return fmt.Errorf("failed to get directory: %w", err) 6 } 7 fmt.Printf("Directory name: %s\n", directory.Name) 8 9 // Use case: Get primary directory for simplified user management 10 // Examples: Automated provisioning workflows, bulk user imports 11 directory, err := scalekitClient.Directory().GetPrimaryDirectoryByOrganizationId(ctx, organizationId) 12 if err != nil { 13 return fmt.Errorf("failed to get primary directory: %w", err) 14 } 15 fmt.Printf("Primary directory ID: %s\n", directory.ID) ``` * Java Java ```java 1 try { 2 // Use case: Access specific directory for detailed user management 3 // Examples: Custom provisioning logic, directory-specific rules 4 Directory directory = scalekitClient.directories() 5 .getDirectory("", ""); 6 System.out.println("Directory name: " + directory.getName()); 7 8 // Use case: Get primary directory for standard provisioning workflows 9 // Examples: Employee onboarding, automated user sync 10 Directory primaryDirectory = scalekitClient.directories() 11 .getPrimaryDirectoryByOrganizationId(""); 12 System.out.println("Primary directory ID: " + primaryDirectory.getId()); 13 } catch (ScalekitException error) { 14 System.err.println("Failed to retrieve directory: " + error.getMessage()); 15 // Implement appropriate error handling 16 } ``` 4. ### List users in a directory [Section titled “List users in a directory”](#list-users-in-a-directory) Once you have the directory information, you can fetch users within that directory. This is commonly used for bulk user synchronization and maintaining an up-to-date user database. * Node.js Node.js ```javascript 1 try { 2 // Use case: Bulk user synchronization and provisioning 3 // Examples: New customer onboarding, scheduled user data sync 4 const { users } = await scalekit.directory.listDirectoryUsers('', ''); 5 6 // Process each user for provisioning or updates 7 users.forEach(user => { 8 console.log(`User email: ${user.email}, Name: ${user.name}`); 9 // TODO: Implement your user provisioning logic here 10 }); 11 } catch (error) { 12 console.error('Failed to list directory users:', error); 13 // Handle error appropriately for your application 14 } ``` * Python Python ```python 1 try: 2 # Use case: Automated user provisioning workflows 3 # Examples: HR system integration, bulk user imports 4 directory_users = scalekit_client.directory.list_directory_users( 5 organization_id='', directory_id='' 6 ) 7 8 # Process each user for local database updates 9 for user in directory_users: 10 print(f'User email: {user.email}, Name: {user.name}') 11 # TODO: Implement your user synchronization logic here 12 except Exception as error: 13 print(f'Error listing directory users: {error}') 14 # Implement appropriate error handling ``` * Go Go ```go 1 // Configure pagination options for large user directories 2 options := &ListDirectoryUsersOptions{ 3 PageSize: 50, // Adjust based on your needs 4 PageToken: "", 5 } 6 7 // Use case: Paginated user retrieval for large directories 8 // Examples: Enterprise customer provisioning, regular sync jobs 9 directoryUsers, err := scalekitClient.Directory().ListDirectoryUsers(ctx, organizationId, directoryId, options) 10 if err != nil { 11 return fmt.Errorf("failed to list directory users: %w", err) 12 } 13 14 // Process each user 15 for _, user := range directoryUsers.Users { 16 fmt.Printf("User email: %s, Name: %s\n", user.Email, user.Name) 17 // TODO: Implement your user provisioning logic 18 } ``` * Java Java ```java 1 // Configure options for user listing with pagination 2 var options = ListDirectoryResourceOptions.builder() 3 .pageSize(50) // Adjust based on your requirements 4 .pageToken("") 5 .includeDetail(true) // Include detailed user information 6 .build(); 7 8 try { 9 // Use case: Enterprise user management and synchronization 10 // Examples: Scheduled sync tasks, user provisioning automation 11 ListDirectoryUsersResponse usersResponse = scalekitClient.directories() 12 .listDirectoryUsers(directory.getId(), organizationId, options); 13 14 // Process each user for provisioning 15 for (User user : usersResponse.getUsers()) { 16 System.out.println("User email: " + user.getEmail() + ", Name: " + user.getName()); 17 // TODO: Implement your user provisioning logic here 18 } 19 } catch (ScalekitException error) { 20 System.err.println("Failed to list directory users: " + error.getMessage()); 21 // Implement appropriate error handling 22 } ``` 5. ### List groups in a directory [Section titled “List groups in a directory”](#list-groups-in-a-directory) Groups are essential for implementing role-based access control (RBAC) in your application. After retrieving users, you can fetch groups to manage permissions and access levels based on organizational structure. * Node.js Node.js ```javascript 1 try { 2 // Use case: Role-based access control implementation 3 // Examples: Department-level permissions, project-based access 4 const { groups } = await scalekit.directory.listDirectoryGroups( 5 '', 6 '', 7 ); 8 9 // Process each group for RBAC setup 10 groups.forEach(group => { 11 console.log(`Group name: ${group.name}, ID: ${group.id}`); 12 // TODO: Implement your group-based permission logic here 13 }); 14 } catch (error) { 15 console.error('Failed to list directory groups:', error); 16 // Handle error appropriately for your application 17 } ``` * Python Python ```python 1 try: 2 # Use case: Department-based access control 3 # Examples: Engineering vs Sales permissions, project team access 4 directory_groups = scalekit_client.directory.list_directory_groups( 5 directory_id='', organization_id='' 6 ) 7 8 # Process each group for permission mapping 9 for group in directory_groups: 10 print(f'Group name: {group.name}, ID: {group.id}') 11 # TODO: Implement your group-based permission logic here 12 except Exception as error: 13 print(f'Error listing directory groups: {error}') 14 # Implement appropriate error handling ``` * Go Go ```go 1 // Configure pagination for group listing 2 options := &ListDirectoryGroupsOptions{ 3 PageSize: 25, // Adjust based on expected group count 4 PageToken: "", 5 } 6 7 // Use case: Organizational role management 8 // Examples: Enterprise role hierarchy, department-based access 9 directoryGroups, err := scalekitClient.Directory().ListDirectoryGroups(ctx, organizationId, directoryId, options) 10 if err != nil { 11 return fmt.Errorf("failed to list directory groups: %w", err) 12 } 13 14 // Process each group for RBAC implementation 15 for _, group := range directoryGroups.Groups { 16 fmt.Printf("Group name: %s, ID: %s\n", group.Name, group.ID) 17 // TODO: Implement your group-based permission logic 18 } ``` * Java Java ```java 1 // Configure options for detailed group information 2 var options = ListDirectoryResourceOptions.builder() 3 .pageSize(25) // Adjust based on your requirements 4 .pageToken("") 5 .includeDetail(true) // Include group membership details 6 .build(); 7 8 try { 9 // Use case: Enterprise permission management 10 // Examples: Role assignments, access level configurations 11 ListDirectoryGroupsResponse groupsResponse = scalekitClient.directories() 12 .listDirectoryGroups(directory.getId(), organizationId, options); 13 14 // Process each group for permission mapping 15 for (Group group : groupsResponse.getGroups()) { 16 System.out.println("Group name: " + group.getName() + ", ID: " + group.getId()); 17 // TODO: Implement your group-based permission logic here 18 } 19 } catch (ScalekitException error) { 20 System.err.println("Failed to list directory groups: " + error.getMessage()); 21 // Implement appropriate error handling 22 } ``` Scalekit’s Directory API provides a simple way to fetch user and group information on-demand. Refer to our [API reference](https://docs.scalekit.com/apis/) to explore more capabilities. ## Realtime user provisioning with webhooks [Section titled “Realtime user provisioning with webhooks”](#realtime-user-provisioning-with-webhooks) While the Directory API is perfect for scheduled synchronization, webhooks enable immediate, real-time user provisioning. When directory providers send events to Scalekit, we forward them instantly to your application, allowing you to respond to user changes as they happen. This approach is ideal for scenarios requiring immediate action, such as new employee onboarding or emergency access revocation. 1. ### Create a secure webhook endpoint [Section titled “Create a secure webhook endpoint”](#create-a-secure-webhook-endpoint) Create a webhook endpoint to receive real-time events from directory providers. After implementing your endpoint, register it in **Dashboard > Webhooks** where you’ll receive a secret for payload verification. Critical security requirement Always verify webhook signatures before processing events. This prevents unauthorized parties from triggering your provisioning logic and protects against replay attacks. * Node.js Express.js ```javascript 1 app.post('/webhook', async (req, res) => { 2 // Security: ALWAYS verify requests are from Scalekit before processing 3 // This prevents unauthorized parties from triggering your provisioning logic 4 5 const event = req.body; 6 const headers = req.headers; 7 const secret = process.env.SCALEKIT_WEBHOOK_SECRET; 8 9 try { 10 // Verify webhook signature to prevent replay attacks and forged requests 11 await scalekit.verifyWebhookPayload(secret, headers, event); 12 } catch (error) { 13 console.error('Webhook signature verification failed:', error); 14 // Return 400 for invalid signatures - this prevents processing malicious requests 15 return res.status(400).json({ error: 'Invalid signature' }); 16 } 17 18 try { 19 // Use case: Real-time user provisioning based on directory events 20 // Examples: New hire onboarding, emergency access revocation, role changes 21 const { email, name } = event.data; 22 23 // Process the webhook event based on its type 24 switch (event.type) { 25 case 'organization.directory.user_created': 26 await createUserAccount(email, name); 27 break; 28 case 'organization.directory.user_updated': 29 await updateUserAccount(email, name); 30 break; 31 case 'organization.directory.user_deleted': 32 await deactivateUserAccount(email); 33 break; 34 default: 35 console.log(`Unhandled event type: ${event.type}`); 36 } 37 38 res.status(201).json({ message: 'Webhook processed successfully' }); 39 } catch (processingError) { 40 console.error('Failed to process webhook event:', processingError); 41 res.status(500).json({ error: 'Processing failed' }); 42 } 43 }); ``` * Python FastAPI ```python 1 from fastapi import FastAPI, Request, HTTPException 2 import os 3 import json 4 5 app = FastAPI() 6 7 @app.post("/webhook") 8 async def api_webhook(request: Request): 9 # Security: ALWAYS verify webhook signatures before processing events 10 # This prevents unauthorized webhook calls and replay attacks 11 12 headers = request.headers 13 body = await request.json() 14 15 try: 16 # Verify webhook payload using the secret from Scalekit dashboard 17 # Get this from Dashboard > Webhooks after registering your endpoint 18 is_valid = scalekit_client.verify_webhook_payload( 19 secret=os.getenv("SCALEKIT_WEBHOOK_SECRET"), 20 headers=headers, 21 payload=json.dumps(body).encode('utf-8') 22 ) 23 24 if not is_valid: 25 raise HTTPException(status_code=400, detail="Invalid webhook signature") 26 27 except Exception as verification_error: 28 print(f"Webhook verification failed: {verification_error}") 29 raise HTTPException(status_code=400, detail="Webhook verification failed") 30 31 # Use case: Instant user provisioning based on directory events 32 # Examples: Automated onboarding, immediate access revocation, role updates 33 try: 34 event_type = body.get("type") 35 event_data = body.get("data", {}) 36 email = event_data.get("email") 37 name = event_data.get("name") 38 39 if event_type == "organization.directory.user_created": 40 await create_user_account(email, name) 41 elif event_type == "organization.directory.user_updated": 42 await update_user_account(email, name) 43 elif event_type == "organization.directory.user_deleted": 44 await deactivate_user_account(email) 45 46 return JSONResponse(status_code=201, content={"status": "processed"}) 47 48 except Exception as processing_error: 49 print(f"Failed to process webhook: {processing_error}") 50 raise HTTPException(status_code=500, detail="Event processing failed") ``` * Java Spring Boot ```java 1 @PostMapping("/webhook") 2 public ResponseEntity webhook( 3 @RequestBody String body, 4 @RequestHeader Map headers) { 5 6 // Security: ALWAYS verify webhook signatures before processing 7 // This prevents malicious webhook calls and protects against replay attacks 8 9 String secret = System.getenv("SCALEKIT_WEBHOOK_SECRET"); 10 11 try { 12 // Verify webhook signature using Scalekit SDK 13 boolean isValid = scalekitClient.webhook() 14 .verifyWebhookPayload(secret, headers, body.getBytes()); 15 16 if (!isValid) { 17 return ResponseEntity.badRequest().body("Invalid webhook signature"); 18 } 19 20 } catch (Exception verificationError) { 21 System.err.println("Webhook verification failed: " + verificationError.getMessage()); 22 return ResponseEntity.badRequest().body("Webhook verification failed"); 23 } 24 25 try { 26 // Use case: Real-time user lifecycle management 27 // Examples: Employee onboarding, access termination, role modifications 28 ObjectMapper mapper = new ObjectMapper(); 29 JsonNode rootNode = mapper.readTree(body); 30 31 String eventType = rootNode.get("type").asText(); 32 JsonNode data = rootNode.get("data"); 33 34 switch (eventType) { 35 case "organization.directory.user_created": 36 String email = data.get("email").asText(); 37 String name = data.get("name").asText(); 38 createUserAccount(email, name); 39 break; 40 case "organization.directory.user_updated": 41 updateUserAccount(data); 42 break; 43 case "organization.directory.user_deleted": 44 deactivateUserAccount(data.get("email").asText()); 45 break; 46 default: 47 System.out.println("Unhandled event type: " + eventType); 48 } 49 50 return ResponseEntity.status(HttpStatus.CREATED).body("Webhook processed"); 51 52 } catch (Exception processingError) { 53 System.err.println("Failed to process webhook event: " + processingError.getMessage()); 54 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) 55 .body("Event processing failed"); 56 } 57 } ``` * Go Go ```go 1 // Security: Store webhook secret securely in environment variables 2 // Get this from Dashboard > Webhooks after registering your endpoint 3 webhookSecret := os.Getenv("SCALEKIT_WEBHOOK_SECRET") 4 5 http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) { 6 // Security: ALWAYS verify webhook signatures before processing events 7 // This prevents unauthorized webhook calls and replay attacks 8 9 if r.Method != http.MethodPost { 10 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) 11 return 12 } 13 14 body, err := io.ReadAll(r.Body) 15 if err != nil { 16 http.Error(w, err.Error(), http.StatusBadRequest) 17 return 18 } 19 defer r.Body.Close() 20 21 // Extract webhook headers for verification 22 headers := map[string]string{ 23 "webhook-id": r.Header.Get("webhook-id"), 24 "webhook-signature": r.Header.Get("webhook-signature"), 25 "webhook-timestamp": r.Header.Get("webhook-timestamp"), 26 } 27 28 // Verify webhook signature to prevent malicious requests 29 _, err = scalekitClient.VerifyWebhookPayload(webhookSecret, headers, body) 30 if err != nil { 31 http.Error(w, "Invalid webhook signature", http.StatusBadRequest) 32 return 33 } 34 35 // Use case: Instant user provisioning and lifecycle management 36 // Examples: Real-time onboarding, emergency access revocation, role synchronization 37 var webhookEvent WebhookEvent 38 if err := json.Unmarshal(body, &webhookEvent); err != nil { 39 http.Error(w, "Invalid webhook payload", http.StatusBadRequest) 40 return 41 } 42 43 switch webhookEvent.Type { 44 case "organization.directory.user_created": 45 err = createUserAccount(webhookEvent.Data.Email, webhookEvent.Data.Name) 46 case "organization.directory.user_updated": 47 err = updateUserAccount(webhookEvent.Data) 48 case "organization.directory.user_deleted": 49 err = deactivateUserAccount(webhookEvent.Data.Email) 50 default: 51 fmt.Printf("Unhandled event type: %s\n", webhookEvent.Type) 52 } 53 54 if err != nil { 55 http.Error(w, "Failed to process webhook", http.StatusInternalServerError) 56 return 57 } 58 59 w.WriteHeader(http.StatusCreated) 60 w.Write([]byte(`{"status": "processed"}`)) 61 }) ``` 2. ### Register your webhook endpoint [Section titled “Register your webhook endpoint”](#register-your-webhook-endpoint) After implementing your secure webhook endpoint, register it in the Scalekit dashboard to start receiving events: 1. Navigate to **Dashboard > Webhooks** 2. Click **+Add Endpoint** 3. Enter your webhook endpoint URL (e.g., `https://your-app.com/api/webhooks/scalekit`) 4. Add a meaningful description for your reference 5. Select the event types you want to receive. Common choices include: * `organization.directory.user_created` - New user provisioning * `organization.directory.user_updated` - User profile changes * `organization.directory.user_deleted` - User deactivation * `organization.directory.group_created` - New group creation * `organization.directory.group_updated` - Group modifications Once registered, your webhook endpoint will start receiving event payloads from directory providers in real-time. 3. ### Process webhook events [Section titled “Process webhook events”](#process-webhook-events) Scalekit standardizes event payloads across different directory providers, ensuring consistent data structure regardless of whether your customers use Azure AD, Okta, Google Workspace, or other providers. When directory changes occur, Scalekit sends events with the following structure: Webhook event payload ```json 1 { 2 "id": "evt_1234567890", 3 "type": "organization.directory.user_created", 4 "data": { 5 "email": "john.doe@company.com", 6 "name": "John Doe", 7 "organization_id": "org_12345", 8 "directory_id": "dir_67890" 9 }, 10 "timestamp": "2024-01-15T10:30:00Z" 11 } ``` You have now successfully implemented and registered a webhook endpoint, enabling your application to receive real-time events for automated user provisioning. Your system can now respond instantly to directory changes, providing seamless user lifecycle management. Refer to our [webhook implementation guide](/authenticate/implement-workflows/implement-webhooks/) for the complete list of available event types and payload structures.
---
# DOCUMENT BOUNDARY
---
# Overview of modelling users and organizations
> Put together a data model for your app's users and organizations
Authenticated users now have access to your app. Now is the time to consider how you’ll structure your data model for users and organizations. This foundational model will serve you well as you implement features such as workspaces, user invitations, role-based access control, and more—ultimately enabling your application to fully support B2B use cases. Organizations and Users are the two first-class entities in Scalekit * An **Organization** serves as a dedicated tenant within the application, representing a distinct entity like a company or project. A **User** is an individual account granted access to interact with the application. Typically belong to organization(s). This is a simplified view of the relationship between these two entities  This model makes it easy to implement essential B2B capabilities in your application. ## Flexible user sign-in options for organizations [Section titled “Flexible user sign-in options for organizations”](#flexible-user-sign-in-options-for-organizations) Configure your application to support multiple authentication methods, allowing users to choose their preferred sign-in options. Also, this is crucial for enabling organization administrators to set and enforce specific authentication policies for their users. A primary use case is implementing enterprise Single Sign-On (SSO). This allows your customers to authenticate their users through their organization’s existing Identity Provider (IdP), such as Okta, Google, or Microsoft Entra ID where IdP verifies the user’s identity, granting them secure access to your application. With Scalekit as your authentication platform, administrators can easily enforce authentication policies for their organization’s users. Scalekit handles this enforcement automatically, either applying organization-specific policies or defaulting to your application’s preferred authentication methods on the login page. Configuring these settings is straightforward—simply toggle the desired options in your Scalekit environment through the dashboard or API. #### User records deduplication [Section titled “User records deduplication”](#user-records-deduplication) Regardless of which authentication methods your users choose, Scalekit automatically recognizes users with identical email addresses as the same individual. This eliminates the need for your application to manage multiple user records for the same person and ensures consistent identity recognition across different authentication flows. * Two different Users cannot have the same email address within the same Scalekit environment. * Scalekit automatically consolidates accounts. If a user logs in with an email and password and later uses Google OAuth with the same email, both authentication methods will be linked to the same User record. ## On how users join and leave organizations [Section titled “On how users join and leave organizations”](#on-how-users-join-and-leave-organizations) Control how users join and are provisioned into organizations. Scalekit provides a flexible user provisioning engine to manage the entire user lifecycle. This includes: * Sending and managing user invitations. * Allowing users to discover and join organizations based on their email domain. * Enabling membership in multiple organizations. * Securely de-provisioning users when they leave an organization. These capabilities are built-in, allowing you to deliver a secure and seamless user management experience from day one. ## Enforce user roles and permissions [Section titled “Enforce user roles and permissions”](#enforce-user-roles-and-permissions) While your product may offer a wide range of features, not all users should have identical access or capabilities. For example, in a project management tool, you might allow some users to create projects, while others may have permission only to view them. Managing user permissions can be complex. Scalekit simplifies this by providing the necessary roles and permissions your application needs to make authorization decisions at runtime. When a user [completes the login flow](/authenticate/fsa/complete-login/#decoding-token-claims), the access token issued by Scalekit contains their assigned roles. Your application can inspect this token to control access to different features. By default, Scalekit assigns an `admin` role to the organization creator and a `member` role to all other users, providing a solid foundation for your authorization logic. ## Modify user memberships [Section titled “Modify user memberships”](#modify-user-memberships) Scalekit tracks how users belong to organizations through a `memberships` property on each User object. This property contains an array of membership objects that define the user’s relationship to each organization they belong to. Each membership object includes these key properties: * `organization_id`: Identifies which organization the user belongs to * `roles`: Specifies the user’s roles (assigned by your application) within that organization * `status`: Indicates whether the membership is active, pending invite or invite expired The memberships property enables users to belong to multiple organizations while maintaining clear role and status information for each relationship. ```json 1 { 2 "memberships": [ 3 { 4 "join_time": "2025-06-27T10:57:43.720Z", 5 "membership_status": "ACTIVE", 6 "metadata": { 7 "department": "engineering", 8 "location": "nyc-office" 9 }, 10 "name": "string", 11 "organization_id": "org_1234abcd5678efgh", 12 "primary_identity_provider": "OKTA", 13 "roles": [ 14 { 15 "id": "role_admin", 16 "name": "Admin" 17 } 18 ] 19 }, 20 { 21 "join_time": "2025-07-15T14:30:22.451Z", 22 "membership_status": "ACTIVE", 23 "metadata": { 24 "department": "product", 25 "location": "sf-office" 26 }, 27 "name": "Jane Smith", 28 "organization_id": "org_9876zyxw5432vuts", 29 "primary_identity_provider": "GOOGLE", 30 "roles": [ 31 { 32 "id": "role_prod_manager", 33 "name": "Product Manager" 34 } 35 ] 36 } 37 ], 38 } ``` #### Migrating from a 1-to-1 model [Section titled “Migrating from a 1-to-1 model”](#migrating-from-a-1-to-1-model) In a 1-to-1 data model, each user is associated with a single organization. The user’s identity is tied to that specific organization, and they cannot belong to multiple organizations with the same identity. This model is common in applications that were not originally built with multi-tenancy in mind, or where each customer’s data and user base are kept entirely separate. For example, many traditional enterprise software applications like **Slack**, **QuickBooks**, or **Adobe Creative Suite** use this model - each customer purchases their own license and has their own separate user accounts that cannot be shared across different customer organizations. #### Migrating from a 1-to-many model [Section titled “Migrating from a 1-to-many model”](#migrating-from-a-1-to-many-model) If your application allows a single user to be part of multiple organizations, their profile in Scalekit will also be shared across those organizations. While the user’s core profile is consistent, each organization membership stores distinct information like roles, status, and metadata. If you already have a membership table that links users and organizations, you can add the Scalekit `user_id` to that table. When you update a user’s profile, the changes will apply across all their organization memberships. | Aspect | 1-to-1 | 1-to-many | | ------------------- | ------------------------------- | ------------------------------- | | **User belongs to** | One organization | Multiple organizations | | **Email address** | Tied to one org | Unique across environment | | **Authentication** | Per-organization | Across all orgs | | **Example apps** | Adobe Creative, QuickBooks | Slack, GitHub, Figma | | **Scalekit use** | Simpler setup, less flexibility | Full multi-tenancy capabilities |
---
# DOCUMENT BOUNDARY
---
# Complete login with code exchange
> Process authentication callbacks and handle redirect flows after users authenticate with Scalekit
Once users have successfully verified their identity using their chosen login method, Scalekit will have gathered the necessary user information for your app to complete the login process. However, your app must provide a callback endpoint where Scalekit can exchange an authorization code to return your app the user details. 1. ## Validate the `state` parameter recommended [Section titled “Validate the state parameter ”](#validate-the-state-parameter-) Before exchanging the authorization code, your application must validate the `state` parameter returned by Scalekit. Compare it with the value you stored in the user’s session before redirecting them. This critical step prevents Cross-Site Request Forgery (CSRF) attacks, ensuring the authentication response corresponds to a request initiated by the same user. * Node.js Validate state in Express.js ```javascript 1 const { state } = req.query; 2 3 // Assumes you are using a session middleware like express-session 4 const storedState = req.session.oauthState; 5 delete req.session.oauthState; // State should be used only once 6 7 if (!state || state !== storedState) { 8 console.error('Invalid state parameter'); 9 return res.redirect('/login?error=invalid_state'); 10 } ``` * Python Validate state in Flask ```python 1 from flask import session, request, redirect 2 3 state = request.args.get('state') 4 5 # Retrieve and remove stored state from session 6 stored_state = session.pop('oauth_state', None) 7 8 if not state or state != stored_state: 9 print('Invalid state parameter') 10 return redirect('/login?error=invalid_state') ``` * Go Validate state in Gin ```go 1 stateParam := c.Query("state") 2 3 // Assumes you are using a session library like gin-contrib/sessions 4 session := sessions.Default(c) 5 storedState := session.Get("oauth_state") 6 session.Delete("oauth_state") // State should be used only once 7 session.Save() 8 9 if stateParam == "" || stateParam != storedState { 10 log.Println("Invalid state parameter") 11 c.Redirect(http.StatusFound, "/login?error=invalid_state") 12 return 13 } ``` * Java Validate state in Spring ```java 1 // Assumes HttpSession is injected into your controller method 2 String storedState = (String) session.getAttribute("oauth_state"); 3 session.removeAttribute("oauth_state"); // State should be used only once 4 5 if (state == null || !state.equals(storedState)) { 6 System.err.println("Invalid state parameter"); 7 return new RedirectView("/login?error=invalid_state"); 8 } ``` 2. ## Exchange authorization code for tokens [Section titled “Exchange authorization code for tokens”](#exchange-authorization-code-for-tokens) Once the `state` is validated, your app can safely exchange the authorization code for tokens. The Scalekit SDK simplifies this process with the `authenticateWithCode` method, which handles the secure server-to-server request. * Node.js Express.js callback handler ```javascript 1 app.get('/auth/callback', async (req, res) => { 2 const { code, error, error_description, state } = req.query; 3 4 // Add state validation here (see previous step) 5 6 // Handle errors first 7 if (error) { 8 console.error('Authentication error:', error); 9 return res.redirect('/login?error=auth_failed'); 10 } 11 12 if (!code) { 13 return res.redirect('/login?error=missing_code'); 14 } 15 16 try { 17 // Exchange code for user data 18 const authResult = await scalekit.authenticateWithCode( 19 code, 20 'https://yourapp.com/auth/callback' 21 ); 22 23 const { user, accessToken, refreshToken } = authResult; 24 25 // TODO: Store user session (next guide covers this) 26 // req.session.user = user; 27 28 res.redirect('/dashboard'); 29 30 } catch (error) { 31 console.error('Token exchange failed:', error); 32 res.redirect('/login?error=exchange_failed'); 33 } 34 }); ``` * Python Flask callback handler ```python 1 @app.route('/auth/callback') 2 def auth_callback(): 3 code = request.args.get('code') 4 error = request.args.get('error') 5 state = request.args.get('state') 6 7 # TODO: Add state validation here (see previous step) 8 9 # Handle errors first 10 if error: 11 print(f'Authentication error: {error}') 12 return redirect('/login?error=auth_failed') 13 14 if not code: 15 return redirect('/login?error=missing_code') 16 17 try: 18 # Exchange code for user data 19 options = CodeAuthenticationOptions() 20 auth_result = scalekit.authenticate_with_code( 21 code, 22 'https://yourapp.com/auth/callback', 23 options 24 ) 25 26 user = auth_result.user 27 # access_token = auth_result.access_token 28 # refresh_token = auth_result.refresh_token 29 30 # TODO: Store user session (next guide covers this) 31 # session['user'] = user 32 33 return redirect('/dashboard') 34 35 except Exception as e: 36 print(f'Token exchange failed: {e}') 37 return redirect('/login?error=exchange_failed') ``` * Go Gin callback handler ```go 1 func authCallbackHandler(c *gin.Context) { 2 code := c.Query("code") 3 errorParam := c.Query("error") 4 stateParam := c.Query("state") 5 6 // TODO: Add state validation here (see previous step) 7 8 // Handle errors first 9 if errorParam != "" { 10 log.Printf("Authentication error: %s", errorParam) 11 c.Redirect(http.StatusFound, "/login?error=auth_failed") 12 return 13 } 14 15 if code == "" { 16 c.Redirect(http.StatusFound, "/login?error=missing_code") 17 return 18 } 19 20 // Exchange code for user data 21 options := scalekit.AuthenticationOptions{} 22 authResult, err := scalekitClient.AuthenticateWithCode( 23 c.Request.Context(), code, 24 "https://yourapp.com/auth/callback", 25 options, 26 ) 27 28 if err != nil { 29 log.Printf("Token exchange failed: %v", err) 30 c.Redirect(http.StatusFound, "/login?error=exchange_failed") 31 return 32 } 33 34 user := authResult.User 35 // accessToken := authResult.AccessToken 36 // refreshToken := authResult.RefreshToken 37 38 // TODO: Store user session (next guide covers this) 39 // session.Set("user", user) 40 41 c.Redirect(http.StatusFound, "/dashboard") 42 } ``` * Java Spring callback handler ```java 1 @GetMapping("/auth/callback") 2 public Object authCallback( 3 @RequestParam(required = false) String code, 4 @RequestParam(required = false) String error, 5 @RequestParam(required = false) String state, 6 HttpSession session 7 ) { 8 // TODO: Add state validation here (see previous step) 9 10 // Handle errors first 11 if (error != null) { 12 System.err.println("Authentication error: " + error); 13 return new RedirectView("/login?error=auth_failed"); 14 } 15 16 if (code == null) { 17 return new RedirectView("/login?error=missing_code"); 18 } 19 20 try { 21 // Exchange code for user data 22 AuthenticationOptions options = new AuthenticationOptions(); 23 AuthenticationResponse authResult = scalekit 24 .authentication() 25 .authenticateWithCode(code, "https://yourapp.com/auth/callback", options); 26 27 var user = authResult.getIdTokenClaims(); 28 // String accessToken = authResult.getAccessToken(); 29 // String refreshToken = authResult.getRefreshToken(); 30 31 // TODO: Store user session (next guide covers this) 32 // session.setAttribute("user", user); 33 34 return new RedirectView("/dashboard"); 35 36 } catch (Exception e) { 37 System.err.println("Token exchange failed: " + e.getMessage()); 38 return new RedirectView("/login?error=exchange_failed"); 39 } 40 } ``` The authorization `code` can be redeemed only once and expires in approx \~10 minutes. Reuse or replay attempts typically return errors like `invalid_grant`. If this occurs, start a new login flow to obtain a fresh `code` and `state`. The `authResult` object returned contains: ```js { user: { email: "john.doe@example.com", emailVerified: true, givenName: "John", name: "John Doe", id: "usr_74599896446906854" }, idToken: "eyJhbGciO..", // Decode for full user details accessToken: "eyJhbGciOi..", refreshToken: "rt_8f7d6e5c4b3a2d1e0f9g8h7i6j..", expiresIn: 299 // in seconds } ``` | Key | Description | | -------------- | ------------------------------------------------------------- | | `user` | Common user details with email, name, and verification status | | `idToken` | JWT containing verified full user identity claims | | `accessToken` | Short-lived token that determines current access | | `refreshToken` | Long-lived token to obtain new access tokens | 3. ## Decoding token claims [Section titled “Decoding token claims”](#decoding-token-claims) The `idToken` and `accessToken` are JSON Web Tokens (JWT) that contain user claims. These tokens can be decoded to retrieve comprehensive user and access information. * Node.js Decode ID token ```javascript 1 // Use a library like 'jsonwebtoken' 2 const jwt = require('jsonwebtoken'); 3 4 // The idToken from the authResult object 5 const { idToken } = authResult; 6 7 // Decode the token without verifying its signature 8 const decoded = jwt.decode(idToken); 9 10 console.log('Decoded claims:', decoded); ``` * Python Decode ID token ```python 1 # Use a library like 'PyJWT' 2 import jwt 3 4 # The id_token from the auth_result object 5 id_token = auth_result.id_token 6 7 # Decode the token without verifying its signature 8 decoded = jwt.decode(id_token, options={"verify_signature": False}) 9 print(f'Decoded claims: {decoded}') ``` * Go Decode ID token ```go 1 // Use a library like 'github.com/golang-jwt/jwt/v5' 2 import ( 3 "fmt" 4 "github.com/golang-jwt/jwt/v5" 5 ) 6 7 // The IdToken from the authResult object 8 idToken := authResult.IdToken 9 token, _, err := new(jwt.Parser).ParseUnverified(idToken, jwt.MapClaims{}) 10 if err != nil { 11 fmt.Printf("Error parsing token: %v\n", err) 12 return 13 } 14 15 if claims, ok := token.Claims.(jwt.MapClaims); ok { 16 fmt.Printf("Decoded claims: %+v\n", claims) 17 } ``` * Java Decode ID token ```java 1 // Use a library like 'com.auth0:java-jwt' 2 import com.auth0.jwt.JWT; 3 import com.auth0.jwt.interfaces.DecodedJWT; 4 import com.auth0.jwt.interfaces.Claim; 5 import com.auth0.jwt.exceptions.JWTDecodeException; 6 import java.util.Map; 7 8 try { 9 // The idToken from the authResult object 10 String idToken = authResult.getIdToken(); 11 12 // Decode the token without verifying its signature 13 DecodedJWT decodedJwt = JWT.decode(idToken); 14 Map claims = decodedJwt.getClaims(); 15 16 System.out.println("Decoded claims: " + claims); 17 } catch (JWTDecodeException exception){ 18 // Invalid token 19 System.err.println("Failed to decode ID token: " + exception.getMessage()); 20 } ``` The decoded token claims contain: * Decoded ID token ID token decoded ```json 1 { 2 "iss": "https://scalekit-z44iroqaaada-dev.scalekit.cloud", // Issuer: Scalekit environment URL (must match your environment) 3 "aud": ["skc_58327482062864390"], // Audience: Your client ID (must match for validation) 4 "azp": "skc_58327482062864390", // Authorized party: Usually same as aud 5 "sub": "usr_63261014140912135", // Subject: User's unique identifier 6 "oid": "org_59615193906282635", // Organization ID: User's organization 7 "exp": 1742975822, // Expiration: Unix timestamp (validate token hasn't expired) 8 "iat": 1742974022, // Issued at: Unix timestamp when token was issued 9 "at_hash": "ec_jU2ZKpFelCKLTRWiRsg", // Access token hash: For token binding validation 10 "c_hash": "6wMreK9kWQQY6O5R0CiiYg", // Authorization code hash: For code binding validation 11 "amr": ["conn_123"], // Authentication method reference: Connection ID used for auth 12 "email": "john.doe@example.com", // User's email address 13 "email_verified": true, // Email verification status 14 "name": "John Doe", // User's full name (optional) 15 "given_name": "John", // User's first name (optional) 16 "family_name": "Doe", // User's last name (optional) 17 "picture": "https://...", // Profile picture URL (optional) 18 "locale": "en", // User's locale preference (optional) 19 "sid": "ses_65274187031249433", // Session ID: Links token to user session 20 "client_id": "skc_58327482062864390", // Client ID: Your application identifier 21 "xoid": "ext_org_123", // External organization ID (if mapped) 22 } ``` * Decoded access token Decoded access token ```json 1 { 2 "iss": "https://login.devramp.ai", // Issuer: Scalekit environment URL (must match your environment) 3 "aud": ["prd_skc_7848964512134X699"], // Audience: Your client ID (must match for validation) 4 "sub": "usr_8967800122X995270", // Subject: User's unique identifier 5 "oid": "org_89678001X21929734", // Organization ID: User's organization 6 "exp": 1758265247, // Expiration: Unix timestamp (validate token hasn't expired) 7 "iat": 1758264947, // Issued at: Unix timestamp when token was issued 8 "nbf": 1758264947, // Not before: Unix timestamp (token valid from this time) 9 "jti": "tkn_90928731115292X63", // JWT ID: Unique token identifier 10 "sid": "ses_90928729571723X24", // Session ID: Links token to user session 11 "client_id": "prd_skc_7848964512134X699", // Client ID: Your application identifier 12 "roles": ["admin"], // Roles: User roles within organization (optional, for authorization) 13 "permissions": ["workspace_data:write", "workspace_data:read"], // Permissions: resource:action format (optional, for granular access control) 14 "scope": "openid profile email", // OAuth scopes granted (optional) 15 "xoid": "ext_org_123", // External organization ID (if mapped) 16 "xuid": "ext_usr_456" // External user ID (if mapped) 17 } ``` 4. ## Verifying access tokens optional [Section titled “Verifying access tokens ”](#verifying-access-tokens-) The Scalekit SDK provides methods to validate tokens automatically. When you use the SDK’s `validateAccessToken` method, it: 1. Verifies the token signature using Scalekit’s public keys 2. Checks the token hasn’t expired (`exp` claim) 3. Validates the issuer (`iss` claim) matches your environment 4. Ensures the audience (`aud` claim) matches your client ID If you need to manually verify tokens, fetch the public signing keys from the JSON Web Key Set (JWKS) endpoint: JWKS endpoint ```sh 1 https:///keys ``` For example, if your Scalekit Environment URL is `https://your-environment.scalekit.com`, the keys can be found at `https://your-environment.scalekit.com/keys`. An `IdToken` contains comprehensive profile information about the user. You can save this in your database for app use cases, using [your own identifier](/fsa/guides/organization-identifiers/). Now, let’s utilize *access and refresh tokens* to manage user access and maintain active sessions. ## Common login scenarios [Section titled “Common login scenarios”](#common-login-scenarios) Customize the login flow by passing different parameters when creating the authorization URL. These scenarios help you route users to specific organizations, force re-authentication, or direct users to signup.
---
# DOCUMENT BOUNDARY
---
# Initiate user signup or login
> Create authorization URLs and redirect users to Scalekit's hosted login page
Login initiation begins your authentication flow. You redirect users to Scalekit’s hosted login page by creating an authorization URL with appropriate parameters.When users visit this URL, Scalekit’s authorization server validates the request, displays the login interface, and handles authentication through your configured connection methods (SSO, social providers, Magic Link or Email OTP Authorization URL format ```sh /oauth/authorize? response_type=code& # always `code` for authorization code flow client_id=& # Dashboard > Developers > Settings > API Credentials redirect_uri=& # Dashboard > Authentication > Redirect URLs > Allowed Callback URLs scope=openid+profile+email+offline_access& # Permissions requested. Include `offline_access` for refresh tokens state= # prevent CSRF attacks ``` The authorization request includes several parameters that control authentication behavior: * **Required parameters** ensure Scalekit can identify your application and return the user securely * **Optional parameters** enable organization routing and pre-populate fields * **Security parameters** prevent unauthorized access attempts Understand each parameter and how it controls the authorization flow: | Query parameter | Description | | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `response_type` | Set to `code` for authorization code flow Required Indicates the expected response type | | `client_id` | Your application’s public identifier from the dashboard Required Scalekit uses this to identify and validate your application | | `redirect_uri` | Your application’s callback URL where Scalekit returns the authorization code Required Must be registered in your dashboard settings | | `scope` | Space-separated list of permissions Required Always include `openid profile email`. Add `offline_access` to request refresh tokens for extended sessions | | `state` | Random string generated by your application Recommended Scalekit returns this unchanged. Use it to prevent CSRF attacks and maintain request state | | `prompt` | Value to control the authentication flow Recommended Use `login` to force re-authentication Use `create` to trigger sign up page Use `select_account` to select an account if they have multiple accounts | | `organization_id` | Skip routing the user to the hosted login page and route them to the social connection configured for the organization Optional | | `connection_id` | Skip routing the user to the hosted login page and route them to a specific social connection Optional | | `login_hint` | Used for [Home Realm Discovery](/authenticate/auth-methods/enterprise-sso/#identify-and-enforce-sso-for-organization-users). Scalekit extracts the email domain from `login_hint` and routes the user to the matching organization’s SSO connection based on configured domain rules Optional | | `provider` | Skip routing user to hosted login page and direct user to a specific social connection. Supported values: `google`, `microsoft`, `github`, `gitlab`, `linkedin`, and `salesforce` Optional | ## Set up login flow [Section titled “Set up login flow”](#set-up-login-flow) 1. #### Add `state` parameter recommended [Section titled “Add state parameter ”](#add-state-parameter-) Always generate a cryptographically secure random string for the `state` parameter and store it temporarily (session, local storage, cache, etc). This can be used to validate that the state value returned in the callback matches the original value you sent. This prevents **CSRF (Cross-Site Request Forgery)** attacks where an attacker tricks users into approving unauthorized authentication requests. * Node.js Generate and store state ```javascript 1 // Generate secure random state 2 const state = require('crypto').randomBytes(32).toString('hex'); 3 // Store it temporarily (session, local storage, cache, etc) 4 sessionStorage.oauthState = state; ``` * Python Generate and store state ```python 1 import os 2 import secrets 3 4 # Generate secure random state 5 state = secrets.token_hex(32) 6 # Store it temporarily (session, local storage, cache, etc) 7 session['oauth_state'] = state ``` * Go Generate and store state ```go 1 import ( 2 "crypto/rand" 3 "encoding/hex" 4 ) 5 6 // Generate secure random state 7 b := make([]byte, 32) 8 rand.Read(b) 9 state := hex.EncodeToString(b) 10 // Store it temporarily (session, local storage, cache, etc) 11 // Example for Go: use a storage library 12 // session.Set("oauth_state", state) ``` * Java Generate and store state ```java 1 import java.security.SecureRandom; 2 import java.util.Base64; 3 4 // Generate secure random state 5 SecureRandom sr = new SecureRandom(); 6 byte[] randomBytes = new byte[32]; 7 sr.nextBytes(randomBytes); 8 String state = Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes); 9 // Store it temporarily (session, local storage, cache, etc) 10 // Example for Java: use any storage library 11 // session.setAttribute("oauth_state", state); ``` 2. #### Redirect to the authorization URL [Section titled “Redirect to the authorization URL”](#redirect-to-the-authorization-url) Use the Scalekit SDK to generate the authorization URL. This method constructs the URL locally without making network requests. Redirect users to this URL to start authentication. * Node.js Express.js ```diff 1 import { Scalekit } from '@scalekit-sdk/node'; 2 3 const scalekit = new Scalekit(/* your credentials */); 4 5 // Basic authorization URL for general login 6 const redirectUri = 'https://yourapp.com/auth/callback'; 7 const options = { 8 scopes: ['openid', 'profile', 'email', 'offline_access'], 9 state: sessionStorage.oauthState, 10 }; 11 12 const authorizationUrl = scalekit.getAuthorizationUrl(redirectUri, options); 13 14 // Redirect user to Scalekit's hosted login page 15 res.redirect(authorizationUrl); ``` * Python Flask ```python 1 from scalekit import ScalekitClient, AuthorizationUrlOptions 2 3 scalekit = ScalekitClient(/* your credentials */) 4 5 # Basic authorization URL for general login 6 redirect_uri = 'https://yourapp.com/auth/callback' 7 options = AuthorizationUrlOptions( 8 scopes=['openid', 'profile', 'email', 'offline_access'], 9 state=session['oauth_state'] # Add this line 10 ) 11 12 authorization_url = scalekit.get_authorization_url(redirect_uri, options) 13 14 # Redirect user to Scalekit's hosted login page 15 return redirect(authorization_url) ``` * Go Gin ```go 1 import "github.com/scalekit-inc/scalekit-sdk-go" 2 3 scalekit := scalekit.NewScalekitClient(/* your credentials */) 4 5 // Basic authorization URL for general login 6 redirectUri := "https://yourapp.com/auth/callback" 7 options := scalekit.AuthorizationUrlOptions{ 8 Scopes: []string{"openid", "profile", "email", "offline_access"}, 9 State: "your_generated_state", // Add this line 10 } 11 12 authorizationUrl, err := scalekitClient.GetAuthorizationUrl(redirectUri, options) 13 14 // Redirect user to Scalekit's hosted login page 15 c.Redirect(http.StatusFound, authorizationUrl.String()) ``` * Java Spring ```java 1 import com.scalekit.ScalekitClient; 2 import com.scalekit.internal.http.AuthorizationUrlOptions; 3 4 ScalekitClient scalekit = new ScalekitClient(/* your credentials */); 5 6 // Basic authorization URL for general login 7 String redirectUri = "https://yourapp.com/auth/callback"; 8 AuthorizationUrlOptions options = new AuthorizationUrlOptions(); 9 options.setScopes(Arrays.asList("openid", "profile", "email", "offline_access")); 10 options.setState("your_generated_state"); // Add this line 11 12 URL authorizationUrl = scalekit.authentication().getAuthorizationUrl(redirectUri, options); 13 14 // Redirect user to Scalekit's hosted login page 15 return new RedirectView(authorizationUrl.toString()); ``` Scalekit will try to verify the user’s identity and redirect them to your application’s callback URL. If the user is a new user, Scalekit will automatically create a new user account. ## Dedicated sign up flow [Section titled “Dedicated sign up flow”](#dedicated-sign-up-flow) Cases where your app wants to keep the sign up flow seperate and dedicated to creating the user account, you can use the `prompt: 'create'` parameter to redirect the user to the sign up page. * Node.js Express.js ```diff 1 const redirectUri = 'http://localhost:3000/api/callback'; 2 const options = { 3 scopes: ['openid', 'profile', 'email', 'offline_access'], 4 prompt: 'create', // explicitly takes you to sign up flow 5 }; 6 7 const authorizationUrl = scalekit.getAuthorizationUrl(redirectUri, options); 8 9 res.redirect(authorizationUrl); ``` * Python Flask ```diff 1 from scalekit import AuthorizationUrlOptions 2 3 redirect_uri = 'http://localhost:3000/api/callback' 4 options = AuthorizationUrlOptions() 5 options.scopes=['openid', 'profile', 'email', 'offline_access'] 6 options.prompt='create' # optional: explicitly takes you to sign up flow 7 8 authorization_url = scalekit.get_authorization_url(redirect_uri, options) 9 10 # For web frameworks like Flask/Django: 11 # return redirect(authorization_url) ``` * Go Gin ```diff 1 redirectUri := "http://localhost:3000/api/callback" 2 options := scalekit.AuthorizationUrlOptions{ 3 Scopes: []string{"openid", "profile", "email", "offline_access"}, 4 +Prompt: "create", // explicitly takes you to sign up flow 5 } 6 7 authorizationUrl, err := scalekitClient.GetAuthorizationUrl(redirectUri, options) 8 if err != nil { 9 // handle error appropriately 10 panic(err) 11 } 12 13 // For web frameworks like Gin: 14 // c.Redirect(http.StatusFound, authorizationUrl.String()) ``` * Java Spring ```diff 1 import com.scalekit.internal.http.AuthorizationUrlOptions; 2 import java.net.URL; 3 import java.util.Arrays; 4 5 String redirectUri = "http://localhost:3000/api/callback"; 6 AuthorizationUrlOptions options = new AuthorizationUrlOptions(); 7 options.setScopes(Arrays.asList("openid", "profile", "email", "offline_access")); 8 +options.setPrompt("create"); 9 10 URL authorizationUrl = scalekit.authentication().getAuthorizationUrl(redirectUri, options); ``` After the user authenticates either in signup or login flows: 1. Scalekit generates an authorization code 2. Makes a callback to your registered allowed callback URL 3. Your backend exchanges the code for tokens by making a server-to-server request This approach keeps sensitive operations server-side and protects your application’s credentials. Let’s take a look at how to complete the login in the next step.
---
# DOCUMENT BOUNDARY
---
# Assign roles to users
> Learn how to assign roles to users in your application using to dashboard, SDK, or automated provisioning
After registering roles and permissions for your application, Scalekit provides multiple ways to assign roles to users. These roles allow your app to make the access control decisions as scalekit sends them to your app in the access token. ## Auto assign roles as users join organizations [Section titled “Auto assign roles as users join organizations”](#auto-assign-roles-as-users-join-organizations) By default, the organization creator automatically receives the `admin` role, while users who join later receive the `member` role. You can customize these defaults to match your application’s security requirements. For instance, in a CRM system, you may want to set the default role for new members to a read-only role like `viewer` to prevent accidental data modifications. 1. Go to **Dashboard** > **Roles & Permissions** > **Roles** tab 2. Select the roles available and choose defaults for organization creator and member  This automatically assigns these roles to every users who joins any organization in your Scalekit environment. ## Set a default role for new organization members [Section titled “Set a default role for new organization members”](#set-a-default-role-for-new-organization-members) You can also configure a default role that is automatically assigned to users who join a specific organization. This organization-level setting **overrides** the application-level default role described above, allowing finer-grained control per organization.  ## Let users assign roles to others API [Section titled “Let users assign roles to others ”](#let-users-assign-roles-to-others-) Enable organization administrators to manage user roles directly within your application. By building features like “Change role” or “Assign permissions” into your app, you can provide a management experience without requiring administrators to leave your app. To implement role assignment functionality, follow these essential prerequisites: 1. **Verify administrator permissions**: Ensure the user performing the role assignment has the `admin` role or an equivalent role with the necessary permissions. Check the `permissions` property in their access token to confirm they have role management capabilities. * Node.js Verify permissions ```javascript 1 // Decode JWT and check admin permissions 2 const decodedToken = decodeJWT(adminAccessToken); 3 4 // Check if user has admin role or required permissions 5 const isAdmin = decodedToken.roles.includes('admin'); 6 const hasPermission = decodedToken.permissions?.includes('users.write') || 7 decodedToken.permissions?.includes('roles.assign'); 8 9 if (!isAdmin && !hasPermission) { 10 throw new Error('Insufficient permissions to assign roles'); 11 } ``` * Python Verify permissions ```python 1 # Decode JWT and check admin permissions 2 decoded_token = decode_jwt(access_token) 3 4 # Check if user has admin role or required permissions 5 is_admin = 'admin' in decoded_token.get('roles', []) 6 has_permission = any(perm in decoded_token.get('permissions', []) 7 for perm in ['users.write', 'roles.assign']) 8 9 if not is_admin and not has_permission: 10 raise PermissionError("Insufficient permissions to assign roles") ``` * Go Verify permissions ```go 1 // Decode JWT and check admin permissions 2 decodedToken, err := decodeJWT(accessToken) 3 if err != nil { 4 return ValidationResult{Success: false, Error: "Invalid token"} 5 } 6 7 // Check if user has admin role or required permissions 8 roles := decodedToken["roles"].([]interface{}) 9 permissions := decodedToken["permissions"].([]interface{}) 10 11 isAdmin := false 12 hasPermission := false 13 14 for _, role := range roles { 15 if role == "admin" { 16 isAdmin = true 17 break 18 } 19 } 20 21 for _, perm := range permissions { 22 if perm == "users.write" || perm == "roles.assign" { 23 hasPermission = true 24 break 25 } 26 } 27 28 if !isAdmin && !hasPermission { 29 return ValidationResult{Success: false, Error: "Insufficient permissions"} 30 } ``` * Java Verify permissions ```java 1 // Decode JWT and check admin permissions 2 Claims decodedToken = decodeJWT(accessToken); 3 4 @SuppressWarnings("unchecked") 5 List userRoles = (List) decodedToken.get("roles"); 6 @SuppressWarnings("unchecked") 7 List permissions = (List) decodedToken.get("permissions"); 8 9 // Check if user has admin role or required permissions 10 boolean isAdmin = userRoles != null && userRoles.contains("admin"); 11 boolean hasPermission = permissions != null && 12 (permissions.contains("users.write") || permissions.contains("roles.assign")); 13 14 if (!isAdmin && !hasPermission) { 15 throw new SecurityException("Insufficient permissions to assign roles"); 16 } ``` 2. **Collect required identifiers**: Gather the necessary parameters for the API call: * `user_id`: The unique identifier of the user whose role you’re changing * `organization_id`: The organization where the role assignment applies * `roles`: An array of role names to assign to the user - Node.js Collect and validate identifiers ```javascript 1 // Structure and validate role assignment data 2 const roleAssignmentData = { 3 user_id: targetUserId, 4 organization_id: targetOrgId, 5 roles: newRoles, 6 // Additional metadata for auditing 7 performed_by: decodedToken.sub, 8 timestamp: new Date().toISOString() 9 }; 10 11 // Validate required fields 12 if (!roleAssignmentData.user_id || !roleAssignmentData.organization_id || !roleAssignmentData.roles) { 13 throw new Error('Missing required identifiers for role assignment'); 14 } ``` - Python Collect and validate identifiers ```python 1 # Structure and validate role assignment data 2 role_assignment_data = { 3 'user_id': target_user_id, 4 'organization_id': target_org_id, 5 'roles': new_roles, 6 # Additional metadata for auditing 7 'performed_by': decoded_token.get('sub'), 8 'timestamp': datetime.utcnow().isoformat() 9 } 10 11 # Validate required fields 12 if not all([role_assignment_data['user_id'], 13 role_assignment_data['organization_id'], 14 role_assignment_data['roles']]): 15 raise ValueError("Missing required identifiers for role assignment") ``` - Go Collect and validate identifiers ```go 1 // Structure and validate role assignment data 2 roleAssignmentData := map[string]interface{}{ 3 "user_id": req.UserID, 4 "organization_id": req.OrganizationID, 5 "roles": req.Roles, 6 // Additional metadata for auditing 7 "performed_by": decodedToken["sub"], 8 "timestamp": time.Now().UTC().Format(time.RFC3339), 9 } 10 11 // Validate required fields 12 if req.UserID == "" || req.OrganizationID == "" || len(req.Roles) == 0 { 13 return ValidationResult{Success: false, Error: "Missing required identifiers"} 14 } ``` - Java Collect and validate identifiers ```java 1 // Structure and validate role assignment data 2 Map roleAssignmentData = new HashMap<>(); 3 roleAssignmentData.put("user_id", request.userId); 4 roleAssignmentData.put("organization_id", request.organizationId); 5 roleAssignmentData.put("roles", request.roles); 6 7 // Additional metadata for auditing 8 roleAssignmentData.put("performed_by", decodedToken.getSubject()); 9 roleAssignmentData.put("timestamp", Instant.now().toString()); 10 11 // Validate required fields 12 if (request.userId == null || request.organizationId == null || request.roles == null) { 13 throw new IllegalArgumentException("Missing required identifiers for role assignment"); 14 } ``` 3. **Call Scalekit SDK to update user role**: Use the validated data to make the API call that assigns the new roles to the user through the Scalekit membership update endpoint. * Node.js Update user role with Scalekit SDK ```javascript 1 // Use case: Update user membership after validation 2 const validationResult = await prepareRoleAssignment( 3 adminAccessToken, 4 targetUserId, 5 targetOrgId, 6 newRoles 7 ); 8 9 if (!validationResult.success) { 10 return res.status(403).json({ error: validationResult.error }); 11 } 12 13 // Initialize Scalekit client (reference installation guide for setup) 14 const scalekit = new ScalekitClient( 15 process.env.SCALEKIT_ENVIRONMENT_URL, 16 process.env.SCALEKIT_CLIENT_ID, 17 process.env.SCALEKIT_CLIENT_SECRET 18 ); 19 20 // Make the API call to update user roles 21 try { 22 const result = await scalekit.user.updateMembership({ 23 user_id: validationResult.data.user_id, 24 organization_id: validationResult.data.organization_id, 25 roles: validationResult.data.roles 26 }); 27 28 console.log(`Role assigned successfully:`, result); 29 return res.json({ 30 success: true, 31 message: "Role updated successfully", 32 data: result 33 }); 34 } catch (error) { 35 console.error(`Failed to assign role: ${error.message}`); 36 return res.status(500).json({ 37 error: "Failed to update role", 38 details: error.message 39 }); 40 } ``` * Python Update user role with Scalekit SDK ```python 1 # Use case: Update user membership after validation 2 validation_result = prepare_role_assignment( 3 access_token, 4 target_user_id, 5 target_org_id, 6 new_roles 7 ) 8 9 if not validation_result['success']: 10 return jsonify({'error': validation_result['error']}), 403 11 12 # Initialize Scalekit client (reference installation guide for setup) 13 scalekit_client = ScalekitClient( 14 env_url=os.getenv("SCALEKIT_ENVIRONMENT_URL"), 15 client_id=os.getenv("SCALEKIT_CLIENT_ID"), 16 client_secret=os.getenv("SCALEKIT_CLIENT_SECRET") 17 ) 18 19 # Make the API call to update user roles 20 try: 21 from scalekit.v1.users.users_pb2 import UpdateMembershipRequest 22 23 request = UpdateMembershipRequest( 24 user_id=validation_result['data']['user_id'], 25 organization_id=validation_result['data']['organization_id'], 26 roles=validation_result['data']['roles'] 27 ) 28 29 result = scalekit_client.users.update_membership(request=request) 30 print(f"Role assigned successfully: {result}") 31 32 return jsonify({ 33 'success': True, 34 'message': 'Role updated successfully', 35 'data': str(result) 36 }) 37 38 except Exception as error: 39 print(f"Failed to assign role: {error}") 40 return jsonify({ 41 'error': 'Failed to update role', 42 'details': str(error) 43 }), 500 ``` * Go Update user role with Scalekit SDK ```go 1 // Use case: Update user membership after validation 2 validationResult := prepareRoleAssignment(ctx, accessToken, req) 3 4 if !validationResult.Success { 5 http.Error(w, validationResult.Error, http.StatusForbidden) 6 return 7 } 8 9 // Initialize Scalekit client (reference installation guide for setup) 10 scalekitClient := scalekit.NewScalekitClient( 11 os.Getenv("SCALEKIT_ENVIRONMENT_URL"), 12 os.Getenv("SCALEKIT_CLIENT_ID"), 13 os.Getenv("SCALEKIT_CLIENT_SECRET"), 14 ) 15 16 // Make the API call to update user roles 17 data := validationResult.Data.(map[string]interface{}) 18 updateRequest := &scalekit.UpdateMembershipRequest{ 19 UserId: data["user_id"].(string), 20 OrganizationId: data["organization_id"].(string), 21 Roles: data["roles"].([]string), 22 } 23 24 result, err := scalekitClient.Membership().UpdateMembership(ctx, updateRequest) 25 if err != nil { 26 log.Printf("Failed to assign role: %v", err) 27 http.Error(w, "Failed to update role", http.StatusInternalServerError) 28 return 29 } 30 31 log.Printf("Role assigned successfully: %+v", result) 32 json.NewEncoder(w).Encode(map[string]interface{}{ 33 "success": true, 34 "message": "Role updated successfully", 35 "data": result, 36 }) ``` * Java Update user role with Scalekit SDK ```java 1 // Use case: Update user membership after validation 2 ValidationResult validationResult = prepareRoleAssignment(accessToken, request); 3 4 if (!validationResult.success) { 5 return ResponseEntity.status(403).body(Map.of("error", validationResult.error)); 6 } 7 8 // Initialize Scalekit client (reference installation guide for setup) 9 ScalekitClient scalekitClient = new ScalekitClient( 10 System.getenv("SCALEKIT_ENVIRONMENT_URL"), 11 System.getenv("SCALEKIT_CLIENT_ID"), 12 System.getenv("SCALEKIT_CLIENT_SECRET") 13 ); 14 15 // Make the API call to update user roles 16 try { 17 @SuppressWarnings("unchecked") 18 Map data = (Map) validationResult.data; 19 20 UpdateMembershipRequest updateRequest = UpdateMembershipRequest.newBuilder() 21 .setUserId((String) data.get("user_id")) 22 .setOrganizationId((String) data.get("organization_id")) 23 .addAllRoles((List) data.get("roles")) 24 .build(); 25 26 UpdateMembershipResponse response = scalekitClient.users().updateMembership(updateRequest); 27 System.out.println("Role assigned successfully: " + response); 28 29 return ResponseEntity.ok(Map.of( 30 "success", true, 31 "message", "Role updated successfully", 32 "data", response.toString() 33 )); 34 35 } catch (Exception e) { 36 System.err.println("Failed to assign role: " + e.getMessage()); 37 return ResponseEntity.status(500).body(Map.of( 38 "error", "Failed to update role", 39 "details", e.getMessage() 40 )); 41 } ``` 4. **Handle response and provide feedback**: Return appropriate success/error responses to the administrator and update your application’s UI accordingly. * Node.js Handle API response ```javascript 1 // Success response handling 2 if (result.success) { 3 // Update UI to reflect role change 4 await updateUserInterface(targetUserId, newRoles); 5 6 // Send notification to user (optional) 7 await notifyUserOfRoleChange(targetUserId, newRoles); 8 9 // Log the action for audit purposes 10 await logRoleChange({ 11 performed_by: decodedToken.sub, 12 target_user: targetUserId, 13 organization: targetOrgId, 14 old_roles: previousRoles, 15 new_roles: newRoles, 16 timestamp: new Date().toISOString() 17 }); 18 } ``` * Python Handle API response ```python 1 # Success response handling 2 if result.get('success'): 3 # Update UI to reflect role change 4 await update_user_interface(target_user_id, new_roles) 5 6 # Send notification to user (optional) 7 await notify_user_of_role_change(target_user_id, new_roles) 8 9 # Log the action for audit purposes 10 await log_role_change({ 11 'performed_by': decoded_token.get('sub'), 12 'target_user': target_user_id, 13 'organization': target_org_id, 14 'old_roles': previous_roles, 15 'new_roles': new_roles, 16 'timestamp': datetime.utcnow().isoformat() 17 }) ``` * Go Handle API response ```go 1 // Success response handling 2 if success { 3 // Update UI to reflect role change 4 updateUserInterface(targetUserID, newRoles) 5 6 // Send notification to user (optional) 7 notifyUserOfRoleChange(targetUserID, newRoles) 8 9 // Log the action for audit purposes 10 logRoleChange(map[string]interface{}{ 11 "performed_by": decodedToken["sub"], 12 "target_user": targetUserID, 13 "organization": targetOrgID, 14 "old_roles": previousRoles, 15 "new_roles": newRoles, 16 "timestamp": time.Now().UTC().Format(time.RFC3339), 17 }) 18 } ``` * Java Handle API response ```java 1 // Success response handling 2 if (response.getBody().containsKey("success") && 3 Boolean.TRUE.equals(response.getBody().get("success"))) { 4 5 // Update UI to reflect role change 6 updateUserInterface(targetUserId, newRoles); 7 8 // Send notification to user (optional) 9 notifyUserOfRoleChange(targetUserId, newRoles); 10 11 // Log the action for audit purposes 12 logRoleChange(Map.of( 13 "performed_by", decodedToken.getSubject(), 14 "target_user", targetUserId, 15 "organization", targetOrgId, 16 "old_roles", previousRoles, 17 "new_roles", newRoles, 18 "timestamp", Instant.now().toString() 19 )); 20 } ```
---
# DOCUMENT BOUNDARY
---
# Create and manage roles and permissions
> Set up roles and permissions to control access in your application
Before writing any code, take a moment to plan your application’s authorization model. A well-designed structure for roles and permissions is crucial for security and maintainability. Start by considering the following questions: * What are the actions your users can perform? * How many distinct roles does your application need? Your application’s use cases will determine the answers. Here are a few common patterns: * **Simple roles**: Some applications, like an online whiteboarding tool, may only need a few roles with implicit permissions. For example, `Admin`, `Editor`, and `Viewer`. In this case, you might not even need to define granular permissions. * **Pre-defined roles and permissions**: Many applications have a fixed set of roles built from specific permissions. For a project management tool, you could define permissions like `projects:create` and `tasks:assign`, then group them into roles like `Project Manager` and `Team Member`. * **Customer-defined Roles**: For complex applications, you might allow organization owners to create custom roles with a specific set of permissions. These roles are specific to an organization rather than global to your application. Scalekit provides the flexibility to build authorization for any of these use cases. Once you have a clear plan, you can start creating your permissions and roles. Define the permissions your application needs by registering them with Scalekit. Use the `resource:action` format for clear, self-documenting permission names. You can skip this step, in case permissions may not fit your app’s authorization model. 1. ## Define the actions your users can perform as permissions [Section titled “Define the actions your users can perform as permissions”](#define-the-actions-your-users-can-perform-as-permissions) * Node.js Create permissions ```javascript 1 // Initialize Scalekit client 2 // Use case: Register all available actions in your project management app 3 import { ScalekitClient } from "@scalekit-sdk/node"; 4 5 const scalekit = new ScalekitClient( 6 process.env.SCALEKIT_ENVIRONMENT_URL, 7 process.env.SCALEKIT_CLIENT_ID, 8 process.env.SCALEKIT_CLIENT_SECRET 9 ); 10 11 // Define your application's permissions 12 const permissions = [ 13 { 14 name: "projects:create", 15 description: "Allows users to create new projects" 16 }, 17 { 18 name: "projects:read", 19 description: "Allows users to view project details" 20 }, 21 { 22 name: "projects:update", 23 description: "Allows users to modify existing projects" 24 }, 25 { 26 name: "projects:delete", 27 description: "Allows users to remove projects" 28 }, 29 { 30 name: "tasks:assign", 31 description: "Allows users to assign tasks to team members" 32 } 33 ]; 34 35 // Register each permission with Scalekit 36 for (const permission of permissions) { 37 await scalekit.permission.createPermission(permission); 38 console.log(`Created permission: ${permission.name}`); 39 } 40 41 // Your application's permissions are now registered with Scalekit ``` * Python Create permissions ```python 1 # Initialize Scalekit client 2 # Use case: Register all available actions in your project management app 3 from scalekit import ScalekitClient 4 5 scalekit_client = ScalekitClient( 6 env_url=os.getenv("SCALEKIT_ENVIRONMENT_URL"), 7 client_id=os.getenv("SCALEKIT_CLIENT_ID"), 8 client_secret=os.getenv("SCALEKIT_CLIENT_SECRET") 9 ) 10 11 # Define your application's permissions 12 from scalekit.v1.roles.roles_pb2 import CreatePermission 13 14 permissions = [ 15 CreatePermission( 16 name="projects:create", 17 description="Allows users to create new projects" 18 ), 19 CreatePermission( 20 name="projects:read", 21 description="Allows users to view project details" 22 ), 23 CreatePermission( 24 name="projects:update", 25 description="Allows users to modify existing projects" 26 ), 27 CreatePermission( 28 name="projects:delete", 29 description="Allows users to remove projects" 30 ), 31 CreatePermission( 32 name="tasks:assign", 33 description="Allows users to assign tasks to team members" 34 ) 35 ] 36 37 # Register each permission with Scalekit 38 for permission in permissions: 39 scalekit_client.permissions.create_permission(permission=permission) 40 print(f"Created permission: {permission.name}") 41 42 # Your application's permissions are now registered with Scalekit ``` * Go Create permissions ```go 1 // Initialize Scalekit client 2 // Use case: Register all available actions in your project management app 3 package main 4 5 import ( 6 "context" 7 "log" 8 "github.com/scalekit-inc/scalekit-sdk-go" 9 ) 10 11 func main() { 12 sc := scalekit.NewScalekitClient( 13 os.Getenv("SCALEKIT_ENVIRONMENT_URL"), 14 os.Getenv("SCALEKIT_CLIENT_ID"), 15 os.Getenv("SCALEKIT_CLIENT_SECRET"), 16 ) 17 18 // Define your application's permissions 19 permissions := []*scalekit.CreatePermission{ 20 { 21 Name: "projects:create", 22 Description: "Allows users to create new projects", 23 }, 24 { 25 Name: "projects:read", 26 Description: "Allows users to view project details", 27 }, 28 { 29 Name: "projects:update", 30 Description: "Allows users to modify existing projects", 31 }, 32 { 33 Name: "projects:delete", 34 Description: "Allows users to remove projects", 35 }, 36 { 37 Name: "tasks:assign", 38 Description: "Allows users to assign tasks to team members", 39 }, 40 } 41 42 // Register each permission with Scalekit 43 for _, permission := range permissions { 44 _, err := sc.Permission().CreatePermission(ctx, permission) 45 if err != nil { 46 log.Printf("Failed to create permission: %s", permission.Name) 47 continue 48 } 49 fmt.Printf("Created permission: %s\n", permission.Name) 50 } 51 52 // Your application's permissions are now registered with Scalekit 53 } ``` * Java Create permissions ```java 1 // Initialize Scalekit client 2 // Use case: Register all available actions in your project management app 3 import com.scalekit.ScalekitClient; 4 import com.scalekit.grpc.scalekit.v1.roles.*; 5 6 ScalekitClient scalekitClient = new ScalekitClient( 7 System.getenv("SCALEKIT_ENVIRONMENT_URL"), 8 System.getenv("SCALEKIT_CLIENT_ID"), 9 System.getenv("SCALEKIT_CLIENT_SECRET") 10 ); 11 12 // Define your application's permissions 13 List permissions = Arrays.asList( 14 CreatePermission.newBuilder() 15 .setName("projects:create") 16 .setDescription("Allows users to create new projects") 17 .build(), 18 CreatePermission.newBuilder() 19 .setName("projects:read") 20 .setDescription("Allows users to view project details") 21 .build(), 22 CreatePermission.newBuilder() 23 .setName("projects:update") 24 .setDescription("Allows users to modify existing projects") 25 .build(), 26 CreatePermission.newBuilder() 27 .setName("projects:delete") 28 .setDescription("Allows users to remove projects") 29 .build(), 30 CreatePermission.newBuilder() 31 .setName("tasks:assign") 32 .setDescription("Allows users to assign tasks to team members") 33 .build() 34 ); 35 36 // Register each permission with Scalekit 37 for (CreatePermission permission : permissions) { 38 try { 39 CreatePermissionRequest request = CreatePermissionRequest.newBuilder() 40 .setPermission(permission) 41 .build(); 42 43 scalekitClient.permissions().createPermission(request); 44 System.out.println("Created permission: " + permission.getName()); 45 } catch (Exception e) { 46 System.err.println("Error creating permission: " + e.getMessage()); 47 } 48 } 49 50 // Your application's permissions are now registered with Scalekit ``` 2. ## Register roles your applications will use [Section titled “Register roles your applications will use”](#register-roles-your-applications-will-use) Once you have defined permissions, group them into roles that match your application’s access patterns. * Node.js Create roles with permissions ```javascript 1 // Define roles with their associated permissions 2 // Use case: Create standard roles for your project management application 3 const roles = [ 4 { 5 name: 'project_admin', 6 display_name: 'Project Administrator', 7 description: 'Full access to manage projects and team members', 8 permissions: [ 9 'projects:create', 'projects:read', 'projects:update', 'projects:delete', 10 'tasks:assign' 11 ] 12 }, 13 { 14 name: 'project_manager', 15 display_name: 'Project Manager', 16 description: 'Can manage projects and assign tasks', 17 permissions: [ 18 'projects:create', 'projects:read', 'projects:update', 19 'tasks:assign' 20 ] 21 }, 22 { 23 name: 'team_member', 24 display_name: 'Team Member', 25 description: 'Can view projects and participate in tasks', 26 permissions: [ 27 'projects:read' 28 ] 29 } 30 ]; 31 32 // Register each role with Scalekit 33 for (const role of roles) { 34 await scalekit.role.createRole(role); 35 console.log(`Created role: ${role.name}`); 36 } 37 38 // Your application's roles are now registered with Scalekit ``` * Python Create roles with permissions ```python 1 # Define roles with their associated permissions 2 # Use case: Create standard roles for your project management application 3 from scalekit.v1.roles.roles_pb2 import CreateRole 4 5 roles = [ 6 CreateRole( 7 name="project_admin", 8 display_name="Project Administrator", 9 description="Full access to manage projects and team members", 10 permissions=["projects:create", "projects:read", "projects:update", "projects:delete", "tasks:assign"] 11 ), 12 CreateRole( 13 name="project_manager", 14 display_name="Project Manager", 15 description="Can manage projects and assign tasks", 16 permissions=["projects:create", "projects:read", "projects:update", "tasks:assign"] 17 ), 18 CreateRole( 19 name="team_member", 20 display_name="Team Member", 21 description="Can view projects and participate in tasks", 22 permissions=["projects:read"] 23 ) 24 ] 25 26 # Register each role with Scalekit 27 for role in roles: 28 scalekit_client.roles.create_role(role=role) 29 print(f"Created role: {role.name}") 30 31 # Your application's roles are now registered with Scalekit ``` * Go Create roles with permissions ```go 1 // Define roles with their associated permissions 2 // Use case: Create standard roles for your project management application 3 roles := []*scalekit.CreateRole{ 4 { 5 Name: "project_admin", 6 DisplayName: "Project Administrator", 7 Description: "Full access to manage projects and team members", 8 Permissions: []string{"projects:create", "projects:read", "projects:update", "projects:delete", "tasks:assign"}, 9 }, 10 { 11 Name: "project_manager", 12 DisplayName: "Project Manager", 13 Description: "Can manage projects and assign tasks", 14 Permissions: []string{"projects:create", "projects:read", "projects:update", "tasks:assign"}, 15 }, 16 { 17 Name: "team_member", 18 DisplayName: "Team Member", 19 Description: "Can view projects and participate in tasks", 20 Permissions: []string{"projects:read"}, 21 }, 22 } 23 24 // Register each role with Scalekit 25 for _, role := range roles { 26 _, err := sc.Role().CreateRole(ctx, role) 27 if err != nil { 28 log.Printf("Failed to create role: %s", role.Name) 29 continue 30 } 31 fmt.Printf("Created role: %s\n", role.Name) 32 } 33 34 // Your application's roles are now registered with Scalekit ``` * Java Create roles with permissions ```java 1 // Define roles with their associated permissions 2 // Use case: Create standard roles for your project management application 3 List roles = Arrays.asList( 4 CreateRole.newBuilder() 5 .setName("project_admin") 6 .setDisplayName("Project Administrator") 7 .setDescription("Full access to manage projects and team members") 8 .addAllPermissions(Arrays.asList("projects:create", "projects:read", "projects:update", "projects:delete", "tasks:assign")) 9 .build(), 10 CreateRole.newBuilder() 11 .setName("project_manager") 12 .setDisplayName("Project Manager") 13 .setDescription("Can manage projects and assign tasks") 14 .addAllPermissions(Arrays.asList("projects:create", "projects:read", "projects:update", "tasks:assign")) 15 .build(), 16 CreateRole.newBuilder() 17 .setName("team_member") 18 .setDisplayName("Team Member") 19 .setDescription("Can view projects and participate in tasks") 20 .addPermissions("projects:read") 21 .build() 22 ); 23 24 // Register each role with Scalekit 25 for (CreateRole role : roles) { 26 try { 27 CreateRoleRequest request = CreateRoleRequest.newBuilder() 28 .setRole(role) 29 .build(); 30 31 scalekitClient.roles().createRole(request); 32 System.out.println("Created role: " + role.getName()); 33 } catch (Exception e) { 34 System.err.println("Error creating role: " + e.getMessage()); 35 } 36 } 37 38 // Your application's roles are now registered with Scalekit ``` ## Inherit permissions through roles [Section titled “Inherit permissions through roles”](#inherit-permissions-through-roles) Large applications with extensive feature sets require sophisticated role and permission management. Scalekit enables role inheritance, allowing you to create a hierarchical access control system. Permissions can be grouped into roles, and new roles can be derived from existing base roles, providing a flexible and scalable approach to defining user access. Role assignment in Scalekit automatically grants a user all permissions defined within that role. This is how you can implement use it: 1. Your app defines the permissions and assigns to a role. Let’s say `viewer` role. 2. When creating new role called `editor`, you specify that it inherits the permissions from the `viewer` role. 3. When creating new role called `project_owner`, you specify that it inherits the permissions from the `editor` role. Take a look at our [Roles and Permissions APIs](https://docs.scalekit.com/apis/#tag/roles/get/api/v1/roles). ## Manage roles and permissions in the dashboard [Section titled “Manage roles and permissions in the dashboard”](#manage-roles-and-permissions-in-the-dashboard) For most applications, the simplest way to create and manage roles and permissions is through the Scalekit dashboard. This approach works well when you have a fixed set of roles and permissions that don’t need to be modified by users in your application. You can set up your authorization model once during application configuration and manage it through the dashboard going forward.  1. Navigate to **Dashboard** > **Roles & Permissions** > **Permissions** to create permissions: * Click **Create Permission** and provide: * **Name** - Machine-friendly identifier (e.g., `projects:create`) * **Display Name** - Human-readable label (e.g., “Create Projects”) * **Description** - Clear explanation of what this permission allows 2. Go to **Dashboard** > **Roles & Permissions** > **Roles** to create roles: * Click **Create Role** and provide: * **Name** - Machine-friendly identifier (e.g., `project_manager`) * **Display Name** - Human-readable label (e.g., “Project Manager”) * **Description** - Clear explanation of the role’s purpose * **Permissions** - Select the permissions to include in this role 3. Configure default roles for new users who join organizations 4. Organization administrators can create organization-specific roles by going to **Dashboard** > **Organizations** > **Select organization** > **Roles** Now that you have created roles and permissions in Scalekit, the next step is to assign these roles to users in your application. ### Configure organization specific roles [Section titled “Configure organization specific roles”](#configure-organization-specific-roles) Organization-level roles let organization administrators create custom roles that apply only within their specific organization. These roles are separate from any application-level roles you define.  You can create organization-level roles from the Scalekit Dashboard: * Go to **Organizations → Select an organization → Roles** * In **Organization roles** section, Click **+ Add role** and provide: * **Display name**: Human-readable name (e.g., “Manager”) * **Name (key)**: Machine-friendly identifier (e.g., `manager`) * **Description**: Clear explanation of what users with this role can do
---
# DOCUMENT BOUNDARY
---
# Implement access control
> Verify permissions and roles in your application code to control user access
After configuring permissions and roles, the next critical step is implementing access control directly within your application code. This is achieved by carefully examining the roles and permissions embedded in the user’s access token to make authorization decisions. Scalekit conveniently packages these authorization details during the authentication process, providing you with a comprehensive set of data to make precise access control decisions without requiring additional API calls. This section focuses on implementing access control, which naturally follows user authentication. We recommend completing the authentication [quickstart](/authenticate/fsa/quickstart) before diving into these access control implementation details. ## Start by inspecting the access token [Section titled “Start by inspecting the access token”](#start-by-inspecting-the-access-token) When you [exchange the code for a user profile](/authenticate/fsa/complete-login/), Scalekit also adds additional information that help your app determine the access control decisions. * Auth result ```js 1 { 2 user: { 3 email: "john.doe@example.com", 4 emailVerified: true, 5 givenName: "John", 6 name: "John Doe", 7 id: "usr_74599896446906854" 8 }, 9 idToken: "eyJhbGciO..", // Decode for full user details 10 11 accessToken: "eyJhbGciOi..", 12 refreshToken: "rt_8f7d6e5c4b3a2d1e0f9g8h7i6j..", 13 expiresIn: 299 // in seconds 14 } ``` * Decoded ID token ID token decoded ```json 1 { 2 "at_hash": "ec_jU2ZKpFelCKLTRWiRsg", 3 "aud": [ 4 "skc_58327482062864390" 5 ], 6 "azp": "skc_58327482062864390", 7 "c_hash": "6wMreK9kWQQY6O5R0CiiYg", 8 "client_id": "skc_58327482062864390", 9 "email": "john.doe@example.com", 10 "email_verified": true, 11 "exp": 1742975822, 12 "family_name": "Doe", 13 "given_name": "John", 14 "iat": 1742974022, 15 "iss": "https://scalekit-z44iroqaaada-dev.scalekit.cloud", 16 "name": "John Doe", 17 "oid": "org_59615193906282635", 18 "sid": "ses_65274187031249433", 19 "sub": "usr_63261014140912135" 20 } ``` * Decoded access token Decoded access token ```json 1 { 2 "aud": [ 3 "prd_skc_7848964512134X699" 4 ], 5 "client_id": "prd_skc_7848964512134X699", 6 "exp": 1758265247, 7 "iat": 1758264947, 8 "iss": "https://login.devramp.ai", 9 "jti": "tkn_90928731115292X63", 10 "nbf": 1758264947, 11 "oid": "org_89678001X21929734", 12 "permissions": [ 13 "workspace_data:write", 14 "workspace_data:read" 15 ], 16 "roles": [ 17 "admin" 18 ], 19 "sid": "ses_90928729571723X24", 20 "sub": "usr_8967800122X995270", 21 // External identifiers if updated on Scalekit 22 "xoid": "ext_org_123", // Organization ID 23 "xuid": "ext_usr_456", // User ID 24 } ``` Let’s closely look at the access token: Decoded access token ```json { "aud": ["skc_987654321098765432"], "client_id": "skc_987654321098765432", "exp": 1750850145, "iat": 1750849845, "iss": "http://example.localhost:8889", "jti": "tkn_987654321098765432", "nbf": 1750849845, "roles": ["project_manager", "member"], "oid": "org_69615647365005430", "permissions": ["projects:create", "projects:read", "projects:update", "tasks:assign"], "sid": "ses_987654321098765432", "sub": "usr_987654321098765432" } ``` The `roles` and `permissions` values provide runtime insights into the user’s access constraints directly within the access token, eliminating the need for additional API requests. Crucially, always validate the token’s integrity before relying on the embedded authorization details. * Node.js Validate and decode access token in middleware ```javascript 1 // Middleware to validate tokens and extract authorization data 2 const validateAndExtractAuth = async (req, res, next) => { 3 try { 4 // Extract access token from cookie (decrypt if needed) 5 const accessToken = decrypt(req.cookies.accessToken); 6 7 // Validate the token using Scalekit SDK 8 const isValid = await scalekit.validateAccessToken(accessToken); 9 10 if (!isValid) { 11 return res.status(401).json({ error: 'Invalid or expired token' }); 12 } 13 14 // Decode token to get roles and permissions using any JWT decode library 15 const tokenData = await decodeAccessToken(accessToken); 16 17 // Make authorization data available to route handlers 18 req.user = { 19 id: tokenData.sub, 20 organizationId: tokenData.oid, 21 roles: tokenData.roles || [], 22 permissions: tokenData.permissions || [] 23 }; 24 25 next(); 26 } catch (error) { 27 return res.status(401).json({ error: 'Authentication failed' }); 28 } 29 }; ``` * Python Validate and decode access token ```python 1 from scalekit import ScalekitClient 2 from functools import wraps 3 import jwt 4 5 scalekit_client = ScalekitClient(/* your credentials */) 6 7 def validate_and_extract_auth(f): 8 @wraps(f) 9 def decorated_function(*args, **kwargs): 10 try: 11 # Extract access token from cookie (decrypt if needed) 12 access_token = decrypt(request.cookies.get('accessToken')) 13 14 # Validate the token using Scalekit SDK 15 is_valid = scalekit_client.validate_access_token(access_token) 16 17 if not is_valid: 18 return jsonify({'error': 'Invalid or expired token'}), 401 19 20 # Decode token to get roles and permissions 21 token_data = scalekit_client.decode_access_token(access_token) 22 23 # Make authorization data available to route handlers 24 request.user = { 25 'id': token_data.get('sub'), 26 'organization_id': token_data.get('oid'), 27 'roles': token_data.get('roles', []), 28 'permissions': token_data.get('permissions', []) 29 } 30 31 return f(*args, **kwargs) 32 except Exception as e: 33 return jsonify({'error': 'Authentication failed'}), 401 34 35 return decorated_function ``` * Go Validate and decode access token ```go 1 import ( 2 "context" 3 "encoding/json" 4 "net/http" 5 "github.com/scalekit-inc/scalekit-sdk-go" 6 ) 7 8 scalekitClient := scalekit.NewScalekitClient(/* your credentials */) 9 10 func validateAndExtractAuth(next http.HandlerFunc) http.HandlerFunc { 11 return func(w http.ResponseWriter, r *http.Request) { 12 // Extract access token from cookie (decrypt if needed) 13 cookie, err := r.Cookie("accessToken") 14 if err != nil { 15 http.Error(w, `{"error": "No access token provided"}`, http.StatusUnauthorized) 16 return 17 } 18 19 accessToken, err := decrypt(cookie.Value) 20 if err != nil { 21 http.Error(w, `{"error": "Token decryption failed"}`, http.StatusUnauthorized) 22 return 23 } 24 25 // Validate the token using Scalekit SDK 26 isValid, err := scalekitClient.ValidateAccessToken(r.Context(), accessToken) 27 if err != nil || !isValid { 28 http.Error(w, `{"error": "Invalid or expired token"}`, http.StatusUnauthorized) 29 return 30 } 31 32 // Decode token to get roles and permissions using any JWT decode lib 33 tokenData, err := DecodeAccessToken(accessToken) 34 if err != nil { 35 http.Error(w, `{"error": "Token decode failed"}`, http.StatusUnauthorized) 36 return 37 } 38 39 // Add authorization data to request context 40 user := map[string]interface{}{ 41 "id": tokenData["sub"], 42 "organization_id": tokenData["oid"], 43 "roles": tokenData["roles"], 44 "permissions": tokenData["permissions"], 45 } 46 47 ctx := context.WithValue(r.Context(), "user", user) 48 next(w, r.WithContext(ctx)) 49 } 50 } ``` * Java Validate and decode access token ```java 1 import com.scalekit.ScalekitClient; 2 import javax.servlet.http.HttpServletRequest; 3 import javax.servlet.http.HttpServletResponse; 4 import org.springframework.web.servlet.HandlerInterceptor; 5 import java.util.Map; 6 import java.util.HashMap; 7 8 @Component 9 public class AuthorizationInterceptor implements HandlerInterceptor { 10 private final ScalekitClient scalekit; 11 12 @Override 13 public boolean preHandle( 14 HttpServletRequest request, 15 HttpServletResponse response, 16 Object handler 17 ) throws Exception { 18 try { 19 // Extract access token from cookie (decrypt if needed) 20 String accessToken = getCookieValue(request, "accessToken"); 21 String decryptedToken = decrypt(accessToken); 22 23 // Validate the token using Scalekit SDK 24 boolean isValid = scalekit.authentication().validateAccessToken(decryptedToken); 25 26 if (!isValid) { 27 response.setStatus(HttpStatus.UNAUTHORIZED.value()); 28 response.getWriter().write("{\"error\": \"Invalid or expired token\"}"); 29 return false; 30 } 31 32 // Decode token to get roles and permissions using any JWT decode lib 33 Map tokenData = decodeAccessToken(decryptedToken); 34 35 // Make authorization data available to controllers 36 Map user = new HashMap<>(); 37 user.put("id", tokenData.get("sub")); 38 user.put("organizationId", tokenData.get("oid")); 39 user.put("roles", tokenData.get("roles")); 40 user.put("permissions", tokenData.get("permissions")); 41 42 request.setAttribute("user", user); 43 return true; 44 45 } catch (Exception e) { 46 response.setStatus(HttpStatus.UNAUTHORIZED.value()); 47 response.getWriter().write("{\"error\": \"Authentication failed\"}"); 48 return false; 49 } 50 } 51 } ``` This approach makes user roles and permissions available throughout different routes of your application, enabling consistent and secure access control across all endpoints. ## Verify user’s role to allow access to protected resources [Section titled “Verify user’s role to allow access to protected resources”](#verify-users-role-to-allow-access-to-protected-resources) Role-based access control (RBAC) provides a straightforward way to manage permissions by grouping them into logical roles. Instead of checking individual permissions for every action, your application can simply verify if the user has the required role, making access control decisions more efficient and easier to maintain. * Node.js Role-based access control ```javascript 1 // Helper function to check roles 2 function hasRole(user, requiredRole) { 3 return user.roles && user.roles.includes(requiredRole); 4 } 5 6 // Middleware to require specific roles 7 function requireRole(role) { 8 return (req, res, next) => { 9 if (!hasRole(req.user, role)) { 10 return res.status(403).json({ 11 error: `Access denied. Required role: ${role}` 12 }); 13 } 14 next(); 15 }; 16 } 17 18 // Admin-only routes 19 app.get('/api/admin/users', validateAndExtractAuth, requireRole('admin'), (req, res) => { 20 // Only admin users can access this endpoint 21 res.json(getAllUsers(req.user.organizationId)); 22 }); 23 24 // Multiple role check 25 app.post('/api/admin/invite-user', validateAndExtractAuth, (req, res) => { 26 const user = req.user; 27 28 // Allow admins or managers to invite users 29 if (!hasRole(user, 'admin') && !hasRole(user, 'manager')) { 30 return res.status(403).json({ error: 'Only admins and managers can invite users' }); 31 } 32 33 const invitation = createUserInvitation(req.body, user.organizationId); 34 res.json(invitation); 35 }); ``` * Python Role-based access control ```python 1 # Helper function to check roles 2 def has_role(user, required_role): 3 roles = user.get('roles', []) 4 return required_role in roles 5 6 # Decorator to require specific roles 7 def require_role(role): 8 def decorator(f): 9 @wraps(f) 10 def decorated_function(*args, **kwargs): 11 user = getattr(request, 'user', {}) 12 if not has_role(user, role): 13 return jsonify({'error': f'Access denied. Required role: {role}'}), 403 14 return f(*args, **kwargs) 15 return decorated_function 16 return decorator 17 18 # Admin-only routes 19 @app.route('/api/admin/users') 20 @validate_and_extract_auth 21 @require_role('admin') 22 def get_all_users(): 23 # Only admin users can access this endpoint 24 return jsonify(get_all_users_for_org(request.user['organization_id'])) 25 26 # Multiple role check 27 @app.route('/api/admin/invite-user', methods=['POST']) 28 @validate_and_extract_auth 29 def invite_user(): 30 user = request.user 31 32 # Allow admins or managers to invite users 33 if not has_role(user, 'admin') and not has_role(user, 'manager'): 34 return jsonify({'error': 'Only admins and managers can invite users'}), 403 35 36 invitation = create_user_invitation(request.json, user['organization_id']) 37 return jsonify(invitation) ``` * Go Role-based access control ```go 1 // Helper function to check roles 2 func hasRole(user map[string]interface{}, requiredRole string) bool { 3 roles, ok := user["roles"].([]interface{}) 4 if !ok { 5 return false 6 } 7 8 for _, role := range roles { 9 if roleStr, ok := role.(string); ok && roleStr == requiredRole { 10 return true 11 } 12 } 13 return false 14 } 15 16 // Middleware to require specific roles 17 func requireRole(role string) func(http.HandlerFunc) http.HandlerFunc { 18 return func(next http.HandlerFunc) http.HandlerFunc { 19 return func(w http.ResponseWriter, r *http.Request) { 20 user := r.Context().Value("user").(map[string]interface{}) 21 22 if !hasRole(user, role) { 23 http.Error(w, fmt.Sprintf(`{"error": "Access denied. Required role: %s"}`, role), http.StatusForbidden) 24 return 25 } 26 27 next(w, r) 28 } 29 } 30 } 31 32 // Admin-only routes 33 func getAllUsersHandler(w http.ResponseWriter, r *http.Request) { 34 user := r.Context().Value("user").(map[string]interface{}) 35 orgId := user["organization_id"].(string) 36 37 // Only admin users can access this endpoint 38 users := getAllUsersForOrg(orgId) 39 json.NewEncoder(w).Encode(users) 40 } 41 42 // Route setup with role middleware 43 http.HandleFunc("/api/admin/users", validateAndExtractAuth(requireRole("admin")(getAllUsersHandler))) ``` * Java Role-based access control ```java 1 @RestController 2 public class AdminController { 3 4 // Helper method to check roles 5 private boolean hasRole(Map user, String requiredRole) { 6 List roles = (List) user.get("roles"); 7 return roles != null && roles.contains(requiredRole); 8 } 9 10 // Admin-only endpoint 11 @GetMapping("/api/admin/users") 12 public ResponseEntity> getAllUsers(HttpServletRequest request) { 13 Map user = (Map) request.getAttribute("user"); 14 15 // Check for admin role 16 if (!hasRole(user, "admin")) { 17 return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); 18 } 19 20 String orgId = (String) user.get("organizationId"); 21 List users = userService.getAllUsersForOrg(orgId); 22 return ResponseEntity.ok(users); 23 } 24 25 @PostMapping("/api/admin/invite-user") 26 public ResponseEntity inviteUser( 27 @RequestBody InviteUserRequest request, 28 HttpServletRequest httpRequest 29 ) { 30 Map user = (Map) httpRequest.getAttribute("user"); 31 32 // Allow admins or managers to invite users 33 if (!hasRole(user, "admin") && !hasRole(user, "manager")) { 34 return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); 35 } 36 37 String orgId = (String) user.get("organizationId"); 38 Invitation invitation = userService.createInvitation(request, orgId); 39 return ResponseEntity.ok(invitation); 40 } 41 } ``` ## Verify user’s permissions to allow specific actions [Section titled “Verify user’s permissions to allow specific actions”](#verify-users-permissions-to-allow-specific-actions) Permission-based access control provides granular control over specific actions and resources within your application. While roles offer broad access patterns, permissions allow you to define exactly what operations users can perform, enabling precise security controls and the principle of least privilege. * Node.js Permission-based access control ```javascript 1 // Helper function to check permissions 2 function hasPermission(user, requiredPermission) { 3 return user.permissions && user.permissions.includes(requiredPermission); 4 } 5 6 // Middleware to require specific permissions 7 function requirePermission(permission) { 8 return (req, res, next) => { 9 if (!hasPermission(req.user, permission)) { 10 return res.status(403).json({ 11 error: `Access denied. Required permission: ${permission}` 12 }); 13 } 14 next(); 15 }; 16 } 17 18 // Protected routes with permission checks 19 app.get('/api/projects', validateAndExtractAuth, requirePermission('projects:read'), (req, res) => { 20 // User has projects:read permission - allow access 21 res.json(getProjects(req.user.organizationId)); 22 }); 23 24 app.post('/api/projects', validateAndExtractAuth, requirePermission('projects:create'), (req, res) => { 25 // User has projects:create permission - allow creation 26 const newProject = createProject(req.body, req.user.organizationId); 27 res.json(newProject); 28 }); 29 30 // Multiple permission check 31 app.delete('/api/projects/:id', validateAndExtractAuth, (req, res) => { 32 const user = req.user; 33 34 // Check if user has either admin role or specific delete permission 35 if (!hasPermission(user, 'projects:delete') && !user.roles.includes('admin')) { 36 return res.status(403).json({ error: 'Cannot delete projects' }); 37 } 38 39 deleteProject(req.params.id, user.organizationId); 40 res.json({ success: true }); 41 }); ``` * Python Permission-based access control ```python 1 # Helper function to check permissions 2 def has_permission(user, required_permission): 3 permissions = user.get('permissions', []) 4 return required_permission in permissions 5 6 # Decorator to require specific permissions 7 def require_permission(permission): 8 def decorator(f): 9 @wraps(f) 10 def decorated_function(*args, **kwargs): 11 user = getattr(request, 'user', {}) 12 if not has_permission(user, permission): 13 return jsonify({'error': f'Access denied. Required permission: {permission}'}), 403 14 return f(*args, **kwargs) 15 return decorated_function 16 return decorator 17 18 # Protected routes with permission checks 19 @app.route('/api/projects') 20 @validate_and_extract_auth 21 @require_permission('projects:read') 22 def get_projects(): 23 # User has projects:read permission - allow access 24 return jsonify(get_projects_for_org(request.user['organization_id'])) 25 26 @app.route('/api/projects', methods=['POST']) 27 @validate_and_extract_auth 28 @require_permission('projects:create') 29 def create_project(): 30 # User has projects:create permission - allow creation 31 new_project = create_project_for_org(request.json, request.user['organization_id']) 32 return jsonify(new_project) 33 34 # Multiple permission check 35 @app.route('/api/projects/', methods=['DELETE']) 36 @validate_and_extract_auth 37 def delete_project(project_id): 38 user = request.user 39 40 # Check if user has either admin role or specific delete permission 41 if not has_permission(user, 'projects:delete') and 'admin' not in user.get('roles', []): 42 return jsonify({'error': 'Cannot delete projects'}), 403 43 44 delete_project_from_org(project_id, user['organization_id']) 45 return jsonify({'success': True}) ``` * Go Permission-based access control ```go 1 // Helper function to check permissions 2 func hasPermission(user map[string]interface{}, requiredPermission string) bool { 3 permissions, ok := user["permissions"].([]interface{}) 4 if !ok { 5 return false 6 } 7 8 for _, perm := range permissions { 9 if permStr, ok := perm.(string); ok && permStr == requiredPermission { 10 return true 11 } 12 } 13 return false 14 } 15 16 // Middleware to require specific permissions 17 func requirePermission(permission string) func(http.HandlerFunc) http.HandlerFunc { 18 return func(next http.HandlerFunc) http.HandlerFunc { 19 return func(w http.ResponseWriter, r *http.Request) { 20 user := r.Context().Value("user").(map[string]interface{}) 21 22 if !hasPermission(user, permission) { 23 http.Error(w, fmt.Sprintf(`{"error": "Access denied. Required permission: %s"}`, permission), http.StatusForbidden) 24 return 25 } 26 27 next(w, r) 28 } 29 } 30 } 31 32 // Protected routes with permission checks 33 func getProjectsHandler(w http.ResponseWriter, r *http.Request) { 34 user := r.Context().Value("user").(map[string]interface{}) 35 orgId := user["organization_id"].(string) 36 37 // User has projects:read permission - allow access 38 projects := getProjectsForOrg(orgId) 39 json.NewEncoder(w).Encode(projects) 40 } 41 42 func createProjectHandler(w http.ResponseWriter, r *http.Request) { 43 user := r.Context().Value("user").(map[string]interface{}) 44 orgId := user["organization_id"].(string) 45 46 // User has projects:create permission - allow creation 47 var projectData map[string]interface{} 48 json.NewDecoder(r.Body).Decode(&projectData) 49 50 newProject := createProjectForOrg(projectData, orgId) 51 json.NewEncoder(w).Encode(newProject) 52 } 53 54 // Route setup with middleware 55 http.HandleFunc("/api/projects", validateAndExtractAuth(requirePermission("projects:read")(getProjectsHandler))) 56 http.HandleFunc("/api/projects/create", validateAndExtractAuth(requirePermission("projects:create")(createProjectHandler))) ``` * Java Permission-based access control ```java 1 @RestController 2 public class ProjectController { 3 4 // Helper method to check permissions 5 private boolean hasPermission(Map user, String requiredPermission) { 6 List permissions = (List) user.get("permissions"); 7 return permissions != null && permissions.contains(requiredPermission); 8 } 9 10 // Annotation-based permission checking 11 @GetMapping("/api/projects") 12 @PreAuthorize("hasPermission('projects:read')") 13 public ResponseEntity> getProjects(HttpServletRequest request) { 14 Map user = (Map) request.getAttribute("user"); 15 String orgId = (String) user.get("organizationId"); 16 17 // User has projects:read permission - allow access 18 List projects = projectService.getProjectsForOrg(orgId); 19 return ResponseEntity.ok(projects); 20 } 21 22 @PostMapping("/api/projects") 23 public ResponseEntity createProject( 24 @RequestBody CreateProjectRequest request, 25 HttpServletRequest httpRequest 26 ) { 27 Map user = (Map) httpRequest.getAttribute("user"); 28 29 // Check permission manually 30 if (!hasPermission(user, "projects:create")) { 31 return ResponseEntity.status(HttpStatus.FORBIDDEN) 32 .body(null); 33 } 34 35 String orgId = (String) user.get("organizationId"); 36 Project newProject = projectService.createProject(request, orgId); 37 return ResponseEntity.ok(newProject); 38 } 39 40 @DeleteMapping("/api/projects/{projectId}") 41 public ResponseEntity