Skip to main content

Login with SSO

SSO Provider URL API

When a user clicks a social login button, the application must first obtain the OAuth URL from the backend before redirecting the user. This ensures the backend can track and manage the authentication flow.

Overview

The SSO Provider URL endpoint returns the URL to redirect users to for OAuth authentication with their chosen provider. This is a prerequisite step before initiating any social/OAuth login flow.


Endpoint

POST /idp/v1/Authentication/GetSocialLogInEndPoint

Request

Headers

HeaderRequiredDescription
Content-TypeYesMust be application/json
X-Blocks-KeyYesProject API key

Body

{
"provider": "google",
"audience": "https://<deployed-domain>/login",
"sendAsResponse": true
}

Request Fields

FieldTypeRequiredDescription
providerstringYesOAuth provider identifier (e.g., google, github, microsoft)
audiencestringYesThe redirect URL configured for this project
sendAsResponsebooleanYesMust be true to receive the URL in the response

Response

Success Response

{
"providerUrl": "https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&state=STATTE_CODE&redirect_uri=REDIRECT_URI&client_id=CLIENT_ID_OF_PROVIDER&response_type=code&approval_prompt=auto&access_type=online",
"isAResponse": true,
"error": ""
}

Response Fields

FieldTypeDescription
providerUrlstringFull OAuth authorization URL to redirect the user to
isAResponsebooleanIndicates URL was returned in response (always true)
errorstringError message if request failed (empty string on success)

OAuth URL Structure

The returned providerUrl is a standard OAuth authorization URL with the following components:

https://accounts.google.com/o/oauth2/v2/auth?
scope=https://www.googleapis.com/auth/userinfo.email+
https://www.googleapis.com/auth/userinfo.profile&
state=STATE_CODE&
redirect_uri=REDIRECT_URI&
client_id=CLIENT_ID_OF_PROVDER&
response_type=code&
approval_prompt=auto&
access_type=online

URL Components

ComponentDescription
scopePermissions requested from the OAuth provider
stateRandom string for CSRF protection
redirect_uriWhere the OAuth provider redirects after authentication
client_idOAuth application client ID
response_typeAlways code (authorization code flow)
approval_promptauto means no forced consent prompt
access_typeonline means no offline access

Complete SSO Flow

1. Application → POST /GetSocialLogInEndPoint
← { providerUrl: "https://accounts.google.com/..." }

2. User → Browser redirects to providerUrl (Google)
← User authenticates with Google

3. Google → Browser redirects to redirect_uri with ?code=XXX&state=XXX

4. Application → POST /Token (grant_type=social, code=XXX)
← { access_token, refresh_token }

Error Handling

Error Response

{
"providerUrl": "",
"isAResponse": false,
"error": "Provider not configured"
}

Common Errors

ErrorCause
Provider not configuredProvider not in project's SSO configuration
Invalid audienceRedirect URL doesn't match configuration
Empty errorRequest failed (check network)

State Parameter

The state parameter in the OAuth URL is a security measure:

  1. Generated by backend - Ensures the state is cryptographically random
  2. Included in callback - OAuth provider returns the same state
  3. Validated by application - Ensures the callback is from the initiated request

Important: Always validate the state parameter in the callback matches what was originally sent.


Provider Values

Supported OAuth providers:

ProviderValue in RequestProvider Domain
Googlegoogleaccounts.google.com
GitHubgithubgithub.com
Microsoftmicrosoftlogin.microsoftonline.com
LinkedInlinkedinlinkedin.com
X (Twitter)xapi.twitter.com

Redirect URI Parameter

The redirect_uri value must match the redirect URI configured in the backend. This is typically:

  • Your application's login callback URL
  • Format: https://<deployed-domain>/login

Using the correct red ensures that the OAuth application knows where to redirect after authentication


Security Considerations

URL Validation

Before redirecting, the application should:

  1. Verify providerUrl is not empty
  2. Verify error is empty
  3. Verify URL starts with expected provider domain

State Validation

When receiving the OAuth callback:

  1. Extract state from query parameters
  2. Validate it matches the original state (if stored)
  3. Reject if state is missing or doesn't match

Redirect URI Validation

The backend validates that:

  • The redirect URI matches configured values
  • The request comes from an allowed origin
  • The client_id belongs to the project

Timing Considerations

URL Expiration

The OAuth authorization URL may have a limited validity period:

  • Some providers expire authorization URLs after a few minutes
  • Best practice: Redirect immediately after receiving the URL

Token Exchange

After user authentication:

  • Authorization codes are typically short-lived
  • Exchange them for tokens immediately
  • Do not store or cache authorization codes

Example Implementation

Step 1: Get Provider URL

const response = await fetch("/idp/v1/Authentication/GetSocialLogInEndPoint", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Blocks-Key": "YOUR_PROJECT_KEY",
},
body: JSON.stringify({
provider: "google",
audience: "https://<deployed-domain>/login",
sendAsResponse: true,
}),
});

const data = await response.json();

if (data.error) {
console.error("SSO error:", data.error);
return;
}

// Redirect user to the OAuth provider
window.location.href = data.providerUrl;

Step 2: Handle Callback

// Extract parameters from callback URL
const params = new URLSearchParams(window.location.search);
const code = params.get("code");
const state = params.get("state");

// Exchange code for tokens
const tokenResponse = await fetch("/idp/v1/Authentication/Token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
grant_type: "social",
code: code,
state: state,
}),
});

const tokens = await tokenResponse.json();
// tokens.access_token
// tokens.refresh_token

Summary

AspectDetails
EndpointPOST /idp/v1/Authentication/GetSocialLogInEndPoint
PurposeGet OAuth authorization URL
AuthenticationNone (public endpoint)
Provider URL validityShort-lived, redirect immediately
State validationRequired for security
Error handlingCheck error field in response