When using PingFederate with rolling tokens enabled, refresh tokens are managed to enhance security by invalidating previously issued refresh tokens after they are used. This ensures that only one valid refresh token exists at a time.
Here's how you can use a refresh token with PingFederate in such a scenario:
Step-by-Step Guide
1. Understand Rolling Tokens
- With rolling tokens enabled, every time a refresh token is used to obtain a new access token, PingFederate issues:
- A new refresh token (replacing the old one).
- A new access token.
- The previously issued refresh token becomes invalid, so the client must always use the latest refresh token received.
2. Enable Rolling Tokens in PingFederate
- In PingFederate, rolling refresh tokens are configured at the OAuth Client level.
- To enable rolling tokens:
- Go to OAuth Settings for the specific client in PingFederate Admin Console.
- Under Refresh Token Settings, select:
- Enable rolling tokens: Ensures old refresh tokens are invalidated after use.
- Revocation policy: Determines if older refresh tokens are immediately revoked or if they expire naturally.
3. Use the Refresh Token to Obtain New Tokens
- When the access token expires, the client application must send the refresh token to the Token Endpoint to obtain a new set of tokens.
Token Endpoint Request:
Parameters:
grant_type
: Set torefresh_token
.refresh_token
: The most recent refresh token issued by PingFederate.client_id
: The client ID of your application.client_secret
: The client secret associated with the client (if using client authentication).
4. Parse the Token Response
- After a successful refresh request, PingFederate will respond with:
- Important: Save the new refresh token (
<new_refresh_token>
). This is now the only valid refresh token for future requests.
5. Handle Errors Gracefully
- If the refresh token is invalid (e.g., due to being used already or expired), PingFederate will return an error:
- Resolution:
- Prompt the user to reauthenticate to obtain a new set of tokens.
- Ensure the application always uses the latest refresh token received.
6. Secure Token Storage
- Since refresh tokens are sensitive, store them securely:
- Use encrypted storage (e.g., secure server-side database).
- Never expose refresh tokens in client-side environments (e.g., browser or local storage).
Example Workflow
Initial Login:
- User authenticates, and PingFederate issues:
- Access Token (e.g., valid for 1 hour).
- Refresh Token (e.g., valid for 7 days).
- User authenticates, and PingFederate issues:
Access Token Expiry:
- After the access token expires, the client uses the refresh token to obtain a new access token and refresh token.
Refresh Token Usage:
- The client sends the current refresh token to PingFederate's Token Endpoint.
- PingFederate responds with a new access token and a new refresh token.
- The client replaces the old refresh token with the new one.
Token Revocation:
- If the refresh token is compromised or misused, rolling tokens ensure the impact is limited since only the latest token is valid.
Best Practices for Rolling Tokens
Always Use the Latest Refresh Token:
- After each token refresh, replace the old refresh token with the new one immediately.
Implement Graceful Error Handling:
- Handle scenarios where the refresh token is invalid or expired by reauthenticating the user.
Secure Storage:
- Store refresh tokens securely to prevent unauthorized access.
Short-Lived Access Tokens:
- Configure short access token lifetimes (e.g., 15–60 minutes) for better security and rely on refresh tokens to maintain session continuity.
Debugging Tips
If refresh token workflows aren't functioning as expected:
Check PingFederate Logs:
- Review logs in PingFederate Admin Console for error details during token refresh attempts.
Verify Client Configuration:
- Ensure rolling tokens are enabled in the OAuth client settings.
- Confirm token lifetimes and scopes are configured correctly.
Monitor Token Usage:
- Use PingFederate's Token Inspector to validate issued tokens and their status.
Inspect HTTP Requests/Responses:
- Use tools like Postman or Fiddler to examine the refresh token flow.
Configuring Rolling Refresh Tokens in PingFederate
1. Configure an OAuth Client in PingFederate
- Log into the PingFederate Admin Console.
- Navigate to Applications → OAuth Clients and select an existing client or create a new one.
- In the Client Settings:
- Enable Refresh Token Grant:
- Under the Allowed Grant Types, ensure
Refresh Token
is selected.
- Under the Allowed Grant Types, ensure
- Configure Rolling Tokens:
- Navigate to Refresh Token Settings and enable Rolling Refresh Tokens.
- Set the Token Lifetime (e.g., 7 days or less depending on your security policy).
- Select Revocation Policy:
- Immediate Revocation: Revokes the old refresh token as soon as a new one is issued.
- Non-Revocation: Allows the old refresh token to remain valid until it expires naturally.
- Enable Refresh Token Grant:
2. Configure Token Lifetimes
- Go to Authentication Policies → Access Token Manager.
- Configure the Access Token Lifetime (e.g., 15–60 minutes for security).
- Configure the Refresh Token Lifetime to define how long the refresh token remains valid (e.g., 7 days).
3. Test the Configuration
- Use a tool like Postman or cURL to simulate the OAuth flow:
- Authenticate to get the initial access token and refresh token.
- Use the refresh token to request a new access token.
- Verify that a new refresh token is issued during the refresh flow.
Testing the Refresh Token Flow
Step 1: Authenticate and Obtain Tokens
Send a request to PingFederate's Authorization Endpoint or Token Endpoint (depending on the grant type).
Example (Authorization Code Grant):
Response:
Step 2: Use the Refresh Token
Send the refresh token to the Token Endpoint to get new tokens:
Response (with rolling tokens enabled):
- Ensure that:
- A new refresh token is returned in the response.
- The previous refresh token is invalidated.
Troubleshooting Rolling Refresh Token Issues
1. Common Problems
Invalid Grant Error:
- Cause: Using an old/expired refresh token or a refresh token that has already been used.
- Solution: Always store and use the most recently issued refresh token.
Refresh Token Not Rolling:
- Cause: Rolling tokens may not be enabled for the client.
- Solution: Verify the Refresh Token Settings in the PingFederate Admin Console for the OAuth client.
Token Expired Errors:
- Cause: Using a refresh token beyond its configured lifetime.
- Solution: Check the refresh token lifetime in the PingFederate client settings and adjust if needed.
Token Revocation Issues:
- Cause: Old refresh tokens are still being accepted.
- Solution: Ensure that Immediate Revocation is enabled in the client’s Refresh Token Settings.
2. Debugging Tools
PingFederate Logs:
- Check the
pingfederate.log
file for errors or warnings related to token issuance and validation. - Located in the PingFederate installation directory under
log/
.
- Check the
Token Inspector:
- Go to Runtime → OAuth Access Tokens in the PingFederate Admin Console.
- Use the Token Inspector to verify the status, claims, and expiration times of issued tokens.
Postman or cURL:
- Use these tools to manually test the token exchange flow and ensure the expected tokens are being issued.
3. Validate Client Configuration
- Ensure the Refresh Token Grant is enabled in the client’s Allowed Grant Types.
- Verify the redirect_uri and scopes match those used during token requests.
4. Confirm Revocation Policy
- Navigate to the client’s Refresh Token Settings and confirm the selected Revocation Policy matches your expectations.
5. Enable Debug Logging
- Increase the logging level in PingFederate to capture detailed information about token exchanges:
- In the Admin Console, go to Server Settings → Runtime → Diagnostics.
- Set the logging level to
DEBUG
.
Best Practices
Always Store the Latest Refresh Token:
- Replace the old refresh token immediately upon receiving a new one.
Secure Token Storage:
- Store tokens in encrypted databases or secure server-side storage.
Monitor Token Usage:
- Use PingFederate's logging and monitoring features to detect misuse or anomalies.
Implement Token Rotation Logic in Clients:
- Ensure the client application is designed to handle rolling tokens by dynamically updating stored tokens.
Set Appropriate Token Lifetimes:
- Keep access tokens short-lived for security and rely on refresh tokens for session continuity.
Here’s an example of how to implement a rolling refresh token workflow using PingFederate in a Python client application. This sample demonstrates how to handle token exchange and properly manage refresh tokens.
Python Code Example
Dependencies
Install the required libraries:
Token Management with PingFederate
How It Works
Authenticate with Authorization Code:
- Use the authorization code to obtain an initial access token and refresh token.
Use the Refresh Token:
- Refresh the access token using the most recent refresh token.
Handle Token Expiry:
- Automatically refresh the access token when it expires.
Token Rotation:
- Update the stored refresh token whenever a new one is issued (rolling tokens).
API Requests:
- Attach the access token to API requests and retry if the token is invalid.
Best Practices
- Secure Storage: Store tokens securely (e.g., encrypted storage or a secure database).
- Handle Errors Gracefully: Implement retry logic and user reauthentication when needed.
- Minimize Lifetime: Use short-lived access tokens for security and rely on refresh tokens for continuity.
- Monitor Token Usage: Log and monitor token usage to detect anomalies.