Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: Add Single-Sign-On to API documentation #2670

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 131 additions & 102 deletions apps/www/content/glossary/single-sign-on.mdx
Original file line number Diff line number Diff line change
@@ -1,162 +1,191 @@
---
title: "Single Sign-On API: Comprehensive Guide"
description: Unlock SSO API power. Learn REST API SSO authentication, AWS SSO API implementation. Real-world examples. Start today.
title: "SSO API: Comprehensive Guide to Single Sign-On"
description: Unlock SSO API power. Learn REST API SSO authentication, AWS SSO API implementation. Explore real-world examples.
h1: "Single Sign-On API: Understanding and Implementation"
term: single-sign-on
categories: []
takeaways:
tldr: Single-Sign-On (SSO) is an authentication scheme that allows a user to log in with a single ID and password to any of several related, yet independent, software systems.
tldr: Single-Sign-On (SSO) is an authentication process that allows a user to access multiple applications or services with one set of login credentials.
definitionAndStructure:
- key: Authentication Flow
value: Token-Based
- key: OAuth 2.0
value: Access Tokens
- key: Token Management
value: Refresh Tokens
- key: Unattended Authentication
value: IdP Managed
- key: SCIM
value: Account Synchronization
- key: Authentication
value: Credential Verification
- key: Single Login
value: Multiple Access
- key: Identity Provider
value: Credential Issuer
historicalContext:
- key: Introduced
value: Late 1990s
- key: Origin
value: Web Services (Single-Sign-On)
- key: Evolution
value: Enterprise Single-Sign-On
value: Federated Single-Sign-On
usageInAPIs:
tags:
- rest
- oauth
- single-sign-on
- saml-2.0
description: Single-Sign-On is used in APIs to provide seamless user authentication across multiple systems. It is often implemented using OAuth 2.0 for token-based authentication and SCIM for account synchronization. SSO is particularly useful in SaaS applications and REST APIs.
- Authentication
- Authorization
- Security
description: In APIs, Single-Sign-On is used to authenticate users across multiple services, reducing the need for multiple logins and enhancing user experience. It is commonly implemented using protocols like SAML, OAuth, and OpenID Connect. APIs like the FedCM API, WebAuthn, and the Storage Access API play crucial roles in enabling SSO.
bestPractices:
- Use OAuth 2.0 for token-based authentication in SSO.
- Manage authentication through the Identity Provider (IdP) to maintain security.
- Use SCIM for account synchronization between the service provider and the IdP.
- Implement strong encryption methods to secure user credentials during the SSO process.
- Ensure the SSO solution complies with privacy regulations and standards.
- Use token-based authentication methods, like OAuth, for secure and efficient SSO implementation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Expand security best practices

The current best practices section misses critical security considerations.

Add these important security practices:

 - Implement strong encryption methods to secure user credentials during the SSO process.
 - Ensure the SSO solution complies with privacy regulations and standards.
 - Use token-based authentication methods, like OAuth, for secure and efficient SSO implementation.
+- Implement proper session management with secure session timeouts
+- Use HTTPS for all SSO-related communications
+- Implement rate limiting to prevent brute force attacks
+- Maintain detailed audit logs for security monitoring
+- Implement Multi-Factor Authentication (MFA) options
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- Implement strong encryption methods to secure user credentials during the SSO process.
- Ensure the SSO solution complies with privacy regulations and standards.
- Use token-based authentication methods, like OAuth, for secure and efficient SSO implementation.
- Implement strong encryption methods to secure user credentials during the SSO process.
- Ensure the SSO solution complies with privacy regulations and standards.
- Use token-based authentication methods, like OAuth, for secure and efficient SSO implementation.
- Implement proper session management with secure session timeouts
- Use HTTPS for all SSO-related communications
- Implement rate limiting to prevent brute force attacks
- Maintain detailed audit logs for security monitoring
- Implement Multi-Factor Authentication (MFA) options

recommendedReading:
- url: https://auth0.com/learn/single-sign-on/
title: Understanding Single Sign-On (SSO)
- url: https://www.youtube.com/watch?v=996OiexHze0
title: OAuth 2.0 and OpenID Connect (in plain English)
- url: https://tools.ietf.org/html/rfc7642
title: "SCIM: System for Cross-domain Identity Management"
didYouKnow: Single-Sign-On not only improves user experience by reducing password fatigue, but also enhances security by minimizing the risk of password phishing.
title: Understanding Single Sign-On
- url: https://auth0.com/docs/sso/current/regular-web-apps
title: Single Sign-On for Regular Web Apps
- url: https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API
title: The Basics of WebAuthn API
didYouKnow: Single-Sign-On not only improves user experience by reducing password fatigue, but it also enhances security by limiting the potential for password compromise.
faq:
- answer: Single Sign-On (SSO) in API context refers to a user authentication process that allows a user to use one set of login credentials (like username and password) to access multiple applications. The service authenticates the end-user for all the applications they have been given rights to and eliminates further prompts when the user switches applications during the same session. In the context of APIs, SSO can be used to provide a unified login mechanism across different systems or services, enhancing user experience and security.
- answer: Single Sign-On (SSO) in API context refers to a user authentication process that allows a user to use one set of login credentials (like username and password) to access multiple applications. The service authenticates the end user for all the applications they have been given rights to and eliminates further prompts when the user switches applications during the same session. In the context of APIs, SSO can be used to provide a unified login mechanism across different API services.
question: What is SSO in API?
- answer: Creating your own Single Sign-On (SSO) involves several steps. First, identify the applications you want to connect to SSO. Second, integrate with an Identity Provider (IdP), which will handle the authentication process. Third, verify the data in your identity directory to ensure it's accurate and up-to-date. Fourth, evaluate user privileges to determine what each user should have access to. Finally, ensure the SSO system is highly available and secure. This involves implementing security measures such as encryption, regular audits, and monitoring to detect and respond to any security incidents.
- answer: Creating your own Single Sign-On (SSO) involves several steps. First, identify the applications you want to connect to SSO. Second, integrate with an Identity Provider (IdP), which will handle the authentication process. Third, verify the data in your identity directory to ensure it's accurate and up-to-date. Fourth, evaluate user privileges to determine what each user should have access to. Finally, ensure the SSO system is highly available and secure, implementing necessary security measures such as encryption and regular audits.
question: How do I create my own SSO?
- answer: The Single Sign-On (SSO) method is an authentication scheme that allows users to log in with a single ID and password to any of several related, yet independent, software systems. It is a common procedure in enterprises, where a client accesses multiple resources connected to a local area network (LAN). With SSO, a user logs in once and gains access to all systems without being prompted to log in again for each of them.
- answer: The Single Sign-On (SSO) method is an authentication scheme that allows users to log in with a single ID and password to any of several related, yet independent, software systems. It is a common procedure in enterprises, where a client accesses multiple resources connected to a local area network (LAN). SSO reduces the number of times a user has to log on and off different software systems, thereby streamlining the user experience.
question: What is the single sign-on SSO method?
- answer: OAuth 2.0 works in a REST API by providing a process for end-users to authorize third-party access to their server resources without sharing their credentials. It uses token-based authentication and authorization. When a user authenticates, the authorization server issues an access token that the application can use for authentication when making requests to the resource server on behalf of the user. The access token defines the scope and duration of the access. The application doesn't need to know the user's identity, which makes OAuth a secure and powerful method for handling access control.
- answer: OAuth 2.0 works in the REST API by providing a process for end-users to authorize third-party access to their server resources without sharing their credentials. It uses access tokens, which are issued by the authorization server and used by the client to access the resources of the resource owner. The client requests an access token from the authorization server, authenticates its own identity, and presents the authorization grant (permission from the resource owner). If valid, the authorization server issues an access token to the client.
question: How does OAuth work in the rest API?
updatedAt: 2024-11-15T12:58:49.000Z
updatedAt: 2024-11-20T15:36:31.000Z
slug: single-sign-on
---

**Single Sign-On (SSO)** is a user authentication process that allows users to access multiple applications with a single set of login credentials, such as a username and password. This approach is particularly beneficial in environments where users need to access various applications or systems, as it enhances security and user experience by reducing password fatigue and minimizing the time spent re-entering credentials across different platforms.
**Single Sign-On (SSO)** is a user authentication process that allows users to access multiple applications with a single set of login credentials, such as a username and password. This method is particularly beneficial in environments where users need to access various applications or systems, enhancing security and user experience by reducing password fatigue and minimizing the time spent re-entering credentials across different platforms.

## Understanding Single Sign-On (SSO) Concepts

Single Sign-On (SSO) enables users to authenticate with multiple systems and applications using one set of credentials managed by a central service. This central service authenticates the user and provides a token or similar credential that is trusted by all participating applications. SSO is widely adopted in enterprise environments to streamline user access to applications across different platforms and enhance security by centralizing user authentication.
Single Sign-On (SSO) enables users to authenticate with multiple systems and applications using one set of credentials managed by a central service. This central service authenticates the user and provides a token that other applications can use for access confirmation. SSO is widely adopted in enterprise environments to streamline user access to a multitude of applications and services, making it a crucial concept for API developers.

## API vs SSO: Key Differences Explained

Understanding the differences between **APIs (Application Programming Interfaces)** and **SSO (Single Sign-On)** is crucial for developers. APIs allow software applications to interact with each other by sending requests and receiving responses. In contrast, SSO is a security process that facilitates a single user authentication process across multiple applications. While APIs can be utilized to implement SSO, the primary function of SSO is to authenticate users, not to facilitate direct application interaction.
While **APIs (Application Programming Interfaces)** are defined protocols for building and integrating application software, **SSO** is a user authentication service that allows users to utilize one set of login credentials across multiple applications. The primary distinction lies in their functions: APIs serve as a bridge for interaction between different software programs, whereas SSO focuses on simplifying user authentication across various systems. Understanding the differences between API and SSO is essential for developers looking to implement secure and efficient authentication methods.

## Implementing AWS SSO API for Authentication

To implement SSO using the **AWS SSO API**, developers can leverage the AWS SDK. Below is a simple example in Python using Boto3, AWS's SDK for Python:
The **AWS SSO API** provides a way to manage access to multiple AWS accounts and applications. By using the AWS SDK, developers can integrate SSO into their applications seamlessly. Here’s a basic example in Python using Boto3:

```python
import boto3

# Initialize a session using your AWS credentials
session = boto3.Session(
aws_access_key_id='YOUR_KEY',
aws_secret_access_key='YOUR_SECRET',
region_name='YOUR_REGION'
)

# Create an SSO client
sso_client = session.client('sso')
# Initialize the SSO Client
sso_client = boto3.client('sso', region_name='us-west-2')

# List the available SSO instances
instances = sso_client.list_instances()
print(instances)
# List the available accounts
response = sso_client.list_accounts()
print(response)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Enhance AWS SSO example with error handling and security best practices

The current AWS SSO example lacks essential error handling and security configurations.

Consider updating the example to include:

 # Initialize the SSO Client
-sso_client = boto3.client('sso', region_name='us-west-2')
+session = boto3.Session(
+    region_name='us-west-2',
+    profile_name='sso_user'  # Use named profile for better security
+)
+sso_client = session.client(
+    'sso',
+    config=boto3.Config(
+        retries=dict(max_attempts=3),  # Add retry configuration
+        connect_timeout=5,
+        read_timeout=5
+    )
+)

-# List the available accounts
-response = sso_client.list_accounts()
-print(response)
+try:
+    response = sso_client.list_accounts(
+        MaxResults=100,  # Implement pagination
+        NextToken=None
+    )
+    accounts = response['accountList']
+    while 'NextToken' in response:
+        response = sso_client.list_accounts(
+            MaxResults=100,
+            NextToken=response['NextToken']
+        )
+        accounts.extend(response['accountList'])
+except sso_client.exceptions.UnauthorizedException:
+    logger.error("Authentication failed. Please check your credentials.")
+    raise
+except sso_client.exceptions.TooManyRequestsException:
+    logger.error("Rate limit exceeded. Implement exponential backoff.")
+    raise
+except Exception as e:
+    logger.error(f"Unexpected error: {str(e)}")
+    raise

Committable suggestion skipped: line range outside the PR's diff.

```
This code snippet initializes a session with AWS and creates an SSO client to list all SSO instances, demonstrating a practical **AWS SSO API** implementation.

This code snippet initializes an SSO client for AWS and lists all accounts associated with the SSO instance, demonstrating a practical **single sign-on example** for AWS environments.

## REST API SSO Authentication: A Practical Guide

When implementing SSO in a **REST API**, developers typically use OAuth2 or OpenID Connect protocols. Here’s a straightforward example using OAuth2 with Python’s requests library:
To implement SSO in a **REST API**, developers typically use OAuth tokens. Here’s an example of how to handle this in Node.js:

```python
import requests

# Define the endpoint and your access credentials
token_url = "https://your-auth-server.com/oauth/token"
client_id = 'your-client-id'
client_secret = 'your-client-secret'

# Request token
response = requests.post(token_url, data={
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret
})

# Extract the token from the response
token = response.json().get('access_token')
```javascript
const express = require('express');
const request = require('request');

const app = express();

app.get('/sso-auth', (req, res) => {
const options = {
method: 'POST',
url: 'https://sso-provider.com/api/token',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
grant_type: 'password',
username: 'user',
password: 'pass',
}),
};

request(options, (error, response, body) => {
if (error) throw new Error(error);
console.log(body);
res.send('Authentication successful');
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Security: Remove hardcoded credentials and update dependencies

The current implementation has several security concerns:

  1. Hardcoded credentials in the code
  2. Usage of deprecated 'request' package
  3. Missing input validation

Update the example to use secure practices:

-const request = require('request');
+const axios = require('axios');  // Modern HTTP client
+require('dotenv').config();  // For environment variables

 app.get('/sso-auth', (req, res) => {
-  const options = {
-    method: 'POST',
-    url: 'https://sso-provider.com/api/token',
-    headers: {
-      'Content-Type': 'application/json',
-    },
-    body: JSON.stringify({
-      grant_type: 'password',
-      username: 'user',
-      password: 'pass',
-    }),
-  };
-
-  request(options, (error, response, body) => {
-    if (error) throw new Error(error);
-    console.log(body);
-    res.send('Authentication successful');
-  });
+  try {
+    // Validate input parameters
+    const { username, password } = req.body;
+    if (!username || !password) {
+      return res.status(400).json({ error: 'Missing credentials' });
+    }
+
+    const response = await axios.post(
+      process.env.SSO_PROVIDER_URL,
+      {
+        grant_type: 'password',
+        username,
+        password,
+      },
+      {
+        headers: {
+          'Content-Type': 'application/json',
+          'Authorization': `Basic ${Buffer.from(
+            `${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`
+          ).toString('base64')}`,
+        },
+      }
+    );
+
+    // Store token securely
+    req.session.token = response.data.access_token;
+    res.json({ message: 'Authentication successful' });
+  } catch (error) {
+    console.error('Authentication failed:', error);
+    res.status(401).json({ error: 'Authentication failed' });
+  }
});

Committable suggestion skipped: line range outside the PR's diff.


app.listen(3000, () => {
console.log('Server running on port 3000');
});
```
This script requests an access token from an OAuth2 server, showcasing a practical **REST API SSO authentication** example.

This server uses the Express framework to handle a route that authenticates users via an SSO provider, showcasing a practical **login REST API example**.

## Single Sign-On API Development in JavaScript

Developing SSO in **JavaScript** often involves integrating with a third-party SSO provider like Auth0. Here’s how to set up a simple SSO using Auth0:
Developing an SSO API in **JavaScript** can be achieved using Node.js. Here’s a simple example using OAuth2:

```javascript
const { auth } = require('express-openid-connect');

const config = {
authRequired: false,
auth0Logout: true,
secret: 'a long, randomly-generated string stored in env',
baseURL: 'http://your-application.com',
clientID: 'your-client-id',
issuerBaseURL: 'https://your-domain.auth0.com'
};

app.use(auth(config));
const OAuth2Server = require('oauth2-server');
const Request = OAuth2Server.Request;
const Response = OAuth2Server.Response;

let oauth = new OAuth2Server({
model: require('./model.js')
});

let request = new Request({
method: 'GET',
query: {},
headers: {Authorization: 'Bearer Access-Token-Here'}
});

let response = new Response({
headers: {}
});

oauth.authenticate(request, response)
.then((token) => {
console.log('The request was authenticated.');
})
.catch((err) => {
console.log('The request failed authentication.', err);
});
```
This code configures an Express.js application to use Auth0 for authentication, illustrating a **single sign-on API development in JavaScript**.

This snippet sets up an OAuth2 server that can authenticate requests based on the provided access tokens, illustrating a **single sign-on API development example** in JavaScript.

## Single Sign-On API Development in Python

For Python developers, implementing SSO can be achieved using libraries such as Flask and Flask-Dance. Here’s a basic setup for Google login using Flask-Dance:
For Python developers, implementing SSO can be done using libraries like Authlib. Here’s a basic example:

```python
from flask import Flask, redirect, url_for
from flask_dance.contrib.google import make_google_blueprint, google
from authlib.integrations.flask_client import OAuth

app = Flask(__name__)
app.secret_key = "supersekrit"
blueprint = make_google_blueprint(client_id="your-client-id", client_secret="your-client-secret")
app.register_blueprint(blueprint, url_prefix="/login")

@app.route("/")
def index():
if not google.authorized:
return redirect(url_for("google.login"))
resp = google.get("/oauth2/v1/userinfo")
assert resp.ok, resp.text
return "You are {email} on Google".format(email=resp.json()["email"])

if __name__ == "__main__":
app.run()
oauth = OAuth(app)

google = oauth.register(
name='google',
client_id='YOUR_CLIENT_ID',
client_secret='YOUR_CLIENT_SECRET',
access_token_url='https://accounts.google.com/o/oauth2/token',
access_token_params=None,
authorize_url='https://accounts.google.com/o/oauth2/auth',
authorize_params=None,
api_base_url='https://www.googleapis.com/oauth2/v1/',
client_kwargs={'scope': 'openid profile email'},
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Security: Protect sensitive configuration in Python example

The OAuth configuration contains sensitive information that should not be hardcoded.

Update the example to use environment variables:

 google = oauth.register(
     name='google',
-    client_id='YOUR_CLIENT_ID',
-    client_secret='YOUR_CLIENT_SECRET',
+    client_id=os.getenv('GOOGLE_CLIENT_ID'),
+    client_secret=os.getenv('GOOGLE_CLIENT_SECRET'),
     access_token_url='https://accounts.google.com/o/oauth2/token',
     access_token_params=None,
     authorize_url='https://accounts.google.com/o/oauth2/auth',
     authorize_params=None,
     api_base_url='https://www.googleapis.com/oauth2/v1/',
-    client_kwargs={'scope': 'openid profile email'},
+    client_kwargs={
+        'scope': 'openid profile email',
+        'verify': True  # Enable SSL verification
+    },
 )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
google = oauth.register(
name='google',
client_id='YOUR_CLIENT_ID',
client_secret='YOUR_CLIENT_SECRET',
access_token_url='https://accounts.google.com/o/oauth2/token',
access_token_params=None,
authorize_url='https://accounts.google.com/o/oauth2/auth',
authorize_params=None,
api_base_url='https://www.googleapis.com/oauth2/v1/',
client_kwargs={'scope': 'openid profile email'},
)
google = oauth.register(
name='google',
client_id=os.getenv('GOOGLE_CLIENT_ID'),
client_secret=os.getenv('GOOGLE_CLIENT_SECRET'),
access_token_url='https://accounts.google.com/o/oauth2/token',
access_token_params=None,
authorize_url='https://accounts.google.com/o/oauth2/auth',
authorize_params=None,
api_base_url='https://www.googleapis.com/oauth2/v1/',
client_kwargs={
'scope': 'openid profile email',
'verify': True # Enable SSL verification
},
)


@app.route('/login')
def login():
redirect_uri = url_for('authorize', _external=True)
return google.authorize_redirect(redirect_uri)

@app.route('/authorize')
def authorize():
token = google.authorize_access_token()
resp = google.get('userinfo')
user_info = resp.json()
# do something with the user_info
return 'Logged in successfully.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling and session management to authorization endpoint

The current implementation lacks proper error handling and session management.

 @app.route('/authorize')
 def authorize():
+    try:
         token = google.authorize_access_token()
         resp = google.get('userinfo')
         user_info = resp.json()
-        # do something with the user_info
-        return 'Logged in successfully.'
+        
+        # Validate user_info
+        if not user_info.get('email_verified'):
+            raise ValueError('Email not verified')
+            
+        # Store in session securely
+        session['user'] = {
+            'email': user_info['email'],
+            'name': user_info['name']
+        }
+        
+        return redirect(url_for('dashboard'))
+    except Exception as e:
+        logger.error(f"Authorization failed: {str(e)}")
+        return redirect(url_for('login', error='Authorization failed'))

Committable suggestion skipped: line range outside the PR's diff.

```
This example sets up a Flask application with Google SSO, redirecting to Google's login if not already authenticated, providing a clear **single sign-on API development example in Python**.

By understanding and implementing these concepts, developers can effectively utilize **Single Sign-On (SSO)** to enhance user authentication across multiple applications, improving both security and user experience.
This code configures a Flask app to authenticate users via Google's OAuth service, handling login and authorization, and serves as a **single sign-on API development example** in Python.

By understanding and implementing these concepts, API developers can effectively utilize Single Sign-On (SSO) to enhance user authentication across multiple applications, improving both security and user experience.
Loading