Back to Documentation

Service Accounts vs OAuth Applications

Understanding when to use Service Accounts versus OAuth Applications with Client Credentials for machine-to-machine authentication.

Overview

OAuth42 provides two approaches for machine-to-machine (M2M) authentication using theclient_credentialsgrant type: Service Accounts and OAuth Applications. While both can authenticate backend services and APIs, they serve different use cases and have distinct architectures.

Quick Recommendation

Use Service Accounts for simple machine-to-machine authentication. Use OAuth Applications when you need flexibility or plan to add user authentication later.

Technical Architecture

Both approaches create entries in the oauth_clients table, but they differ in configuration and capabilities:

Service Accounts

Purpose-built for machine-to-machine authentication with minimal configuration.

Database Structure:

  • ✓Creates entry in service_accounts table
  • ✓Links to oauth_clients via service_account_id
  • ✓Client ID prefixed with sa_
  • ✓Grant types hardcoded to ["client_credentials"]
  • ✓No redirect URIs (not needed for M2M)
  • ✓Built-in audit logging and metadata tracking
  • ✓Optional expiration dates for security

OAuth Applications

Flexible OAuth2 clients that support multiple grant types and can evolve with your needs.

Database Structure:

  • ✓Creates entry in applications table
  • ✓Links to oauth_clients via oauth_client_id
  • ✓Client ID prefixed with app_
  • ✓Configurable grant types (authorization_code,client_credentials,refresh_token)
  • ✓Supports redirect URIs for user authentication flows
  • ✓Advanced features: access control, first-party apps, custom settings
  • ✓Can combine M2M and user authentication in one client

Feature Comparison

FeatureService AccountsOAuth Applications
Primary Use CaseMachine-to-machine onlyFlexible (M2M + user auth)
Grant Typesclient_credentials onlyMultiple (configurable)
Setup ComplexitySimple (3 fields)More complex (10+ fields)
Redirect URIsNot supportedSupported
Expiration DatesBuilt-in supportManual management
Audit LoggingAutomaticStandard OAuth logs
Future EvolutionLimited (M2M only)Flexible (can add flows)
Client ID Prefixsa_app_
Best ForBackend services, APIs, cron jobsApps that may add user auth later

When to Use Each Approach

✓Choose Service Accounts When:

  • You only need machine-to-machine authentication (backend to backend, service to service)
  • You want simpler setup with fewer configuration options
  • You need built-in expiration dates for security compliance
  • You want enhanced audit logging specific to service accounts
  • Your service will never need user authentication

Common Use Cases:

  • • Backend microservices accessing OAuth42 APIs
  • • Cron jobs that need to create groups or manage users
  • • CI/CD pipelines automating user provisioning
  • • Server-side scripts syncing data between systems

✓Choose OAuth Applications When:

  • You might add user authentication in the future (even if not using it now)
  • You need to combine multiple grant types in one client
  • You want maximum flexibility and advanced configuration options
  • You need redirect URIs for browser-based flows
  • Your app is customer-facing and may evolve to need user login

Common Use Cases:

  • • SaaS applications that start with M2M but plan to add user login
  • • Mobile/web apps using client_credentials for backend APIs temporarily
  • • Applications needing both user auth and service-to-service communication
  • • First-party apps requiring special privileges or access control

Code Examples

Creating a Service Account

Create a service account via the OAuth42 Portal or API:

POST /service-accounts
Content-Type: application/json

{
  "name": "Backend Microservice",
  "description": "Production API service for user management",
  "scopes": ["admin", "read", "write"],
  "expires_at": "2025-12-31T23:59:59Z"
}

Response includes client_id (prefixed with sa_) and client_secret. Store these securely!

Authenticate using client credentials:

POST /oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&
client_id=sa_abc123&
client_secret=secret_xyz789&
scope=admin

Creating an OAuth Application

Create an OAuth application with client_credentials support:

POST /applications
Content-Type: application/json

{
  "name": "My SaaS Platform",
  "description": "Multi-purpose application",
  "grant_types": ["client_credentials", "authorization_code", "refresh_token"],
  "redirect_uris": ["https://myapp.com/callback"],
  "scopes": ["admin", "read", "write", "openid", "profile"],
  "is_first_party": false
}

Response includes client_id (prefixed with app_) and client_secret.

Same authentication flow as service accounts:

POST /oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&
client_id=app_abc123&
client_secret=secret_xyz789&
scope=admin

Later, you can add user authentication using authorization_codegrant without creating a new client.

Using the Access Token

Both approaches produce the same token format for API calls:

GET /groups
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

// Both service accounts and OAuth apps with client_credentials
// get tokens with the "scope" field in JWT claims:
{
  "sub": "sa_abc123",  // or "app_abc123"
  "scope": "admin read write",
  "iat": 1703001600,
  "exp": 1703005200
}

Migration Between Approaches

Important Note

Service accounts cannot be upgraded to OAuth applications. They exist in separate database tables with different architectures. If you need to add user authentication later, you must create a new OAuth application.

However, migrating is straightforward:

  1. Create a new OAuth application with client_credentials grant
  2. Test authentication with the new client credentials
  3. Update your service configuration to use the new client_id and client_secret
  4. Deactivate or delete the old service account after confirming the migration
  5. Later, add authorization_code grant to enable user authentication

Because both use the same token endpoint and scopes, the migration requires minimal code changes.

Security Best Practices

Rotate Client Secrets Regularly

Both service accounts and OAuth applications support secret rotation. Rotate secrets every 90 days and immediately if compromised.

Request Minimum Scopes

Only request the scopes your service actually needs. Use readinstead of admin when read-only access is sufficient.

Use Environment Variables

Never hardcode client secrets. Store them in environment variables or secrets management systems (AWS Secrets Manager, HashiCorp Vault, etc.).

Monitor Service Account Usage

Service accounts provide enhanced audit logging. Review logs regularly for suspicious activity or unauthorized access patterns.

Set Expiration Dates (Service Accounts)

For temporary integrations or testing, set expiration dates on service accounts. This provides automatic cleanup and reduces security risks.