Safety doesn’t happen by accident
swift networking token Estimated reading time: 8 minutesAn authentication system is a critical component of any application or system that requires secure user interaction. It ensures that only authorized individuals or entities can access specific resources, data, or functionalities.
We often use this process in our apps, but it’s also a good idea to understand all key-aspects of it. One of such aspects - it’s a token(s).
Access tokens and ID tokens are both commonly used in modern authentication and authorization frameworks such as OAuth 2.0 and OpenID Connect (OIDC). While they might seem similar, their purposes, usage, and contents are distinct.
The abstract protocol scheme of Bearer
-like auth:
+--------+ +---------------+
| |--(A)- Authorization Request ->| Resource |
| | | Owner |
| |<-(B)-- Authorization Grant ---| |
| | +---------------+
| |
| | +---------------+
| |--(C)-- Authorization Grant -->| Authorization |
| Client | | Server |
| |<-(D)----- Access Token -------| |
| | +---------------+
| |
| | +---------------+
| |--(E)----- Access Token ------>| Resource |
| | | Server |
| |<-(F)--- Protected Resource ---| |
+--------+ +---------------+
There is also refresh token, whose primary purpose is to refresh id or access token. See RFC 6750 for more.
Comparison
Let’s review the key-diference between id and access tokens.
1. Purpose
Aspect | Access Token | ID Token |
---|---|---|
Definition | A token used to authorize access to APIs or resources. | A token that provides information about the authenticated user. |
Main Purpose | Grants permissions to access a resource on behalf of the user. | Confirms the user’s identity and carries user profile information. |
Use Case | Sent to APIs to validate access and permissions. | Sent to the client (e.g., web app) to authenticate the user. |
2. Issued By
Aspect | Access Token | ID Token |
---|---|---|
Issuer | Authorization Server (OAuth 2.0) | Authorization Server (OIDC) |
Standard Protocol | Part of OAuth 2.0 | Part of OpenID Connect (OIDC), an identity layer built on OAuth 2.0. |
3. Content
Aspect | Access Token | ID Token |
---|---|---|
Data Included | - Scopes granted (e.g., read , write ). - Expiry ( exp ) and issuer (iss ). - Metadata for authorization. |
- User’s unique identifier (sub ). - User claims (e.g., email , name ). - Token metadata ( exp , iat , iss ). |
Structure | Typically a JWT (JSON Web Token) or opaque string. | Always a JWT under OIDC. |
4. Expiry and Scope
Aspect | Access Token | ID Token |
---|---|---|
Expiration | Short-lived (minutes to hours) to reduce security risks. | Typically short-lived but can match session duration. |
Scope of Use | Limited to accessing specific APIs or resources. | Limited to the identity verification context. |
5. Transmission
Aspect | Access Token | ID Token |
---|---|---|
Sent To | Resource servers (APIs) to validate access. | The client application for user authentication. |
Storage | Stored securely on the client (e.g., in-memory, secure storage). | Stored on the client, often in conjunction with a session or cookie. |
6. Validation
Aspect | Access Token | ID Token |
---|---|---|
Who Validates? | Resource server (API) validates it. | Client application validates it. |
Validation Data | Signature, expiry, and scopes. | Signature, expiry, and audience (aud ). |
7. Example Use Cases
Use Case | Access Token | ID Token |
---|---|---|
Mobile App API | Used to authorize API requests like fetching user data. | Used to display the user’s profile info in the app. |
Web App Login | Authorizes API calls for resources like dashboards. | Verifies the logged-in user’s identity. |
Key Differences in Summary
Feature | Access Token | ID Token |
---|---|---|
What it represents | Permissions to access resources. | Identity of the user. |
Who consumes it? | APIs or resource servers. | Client applications (e.g., SPAs, mobile apps). |
Standard | OAuth 2.0 | OpenID Connect (OIDC). |
When to Use Which?
Bearer
authentication can use both Access Tokens and ID Tokens because they serve complementary purposes in modern authentication and authorization systems, especially in protocols like OAuth 2.0 and OpenID Connect (OIDC).
- Access Token: Use it whenever you need to call APIs or access a resource on behalf of a user.
- ID Token: Use it to authenticate the user and retrieve their identity-related claims.
Together, access tokens and ID tokens work seamlessly to provide a secure and user-friendly experience in modern applications.
Bearer
authentication uses both tokens because they address different aspects of security:
- The Access Token focuses on authorizing access to resources.
- The ID Token focuses on verifying and communicating user identity to the client.
This dual-token approach ensures better security, flexibility, and user experience in modern distributed systems.
Sometimes we can faced with systems, where id and access tokens are mixed in their responsibilities… but this is a bit another story.
Model
In apps, I developing, I like to use a model, that not only wrap token itself, but also provide some usefull info from it, like jwt.io does.
Note: this is not applicable for
refreshToken
kind, we can only wrap this token. Why? Because of design - it’s simple created not for u, so no information available without special key.. - it’s for one, that provide this token to u. For us - it’s just a shortcut to refresh access to some resources and information.
The model looks like this
Using it, we can easely inspect different aspect of the token at any moment like if it’s expired - token.isExpired
.
Conclusion
In ideal world, we must use both tokens:
- Separation of Concerns: Ensures distinct roles for authentication and authorization.
- Improved Security: Limits the exposure of sensitive identity data.
- Enhanced Flexibility: Supports diverse use cases across distributed systems.
are a key reasons.
This dual-token system is foundational in modern secure and scalable authentication architectures.
P.S:
“Safety doesn’t happen by accident” - These words by author and motivational speaker, Zig Ziglar.
Resources
Share on: