How to audit if application authentication tokens have been used for login?
-
We have authentication tokens enabled for some applications in FusionAuth, and we want to determine if these tokens have actually been used for authentication in the last 30/90/365 days before disabling the feature.
We have approximately 225 users with authentication tokens assigned to their registrations. We can identify which users have tokens using an Elasticsearch query:
{ "bool": { "must": [ { "nested": { "path": "registrations", "query": { "exists": { "field": "registrations.authenticationToken" } } } } ] } }However, we need to confirm whether these tokens have been actively used for authentication. Is there a way to audit authentication token usage through login records or other mechanisms in FusionAuth?
-
Login records in FusionAuth (via the Login Report) don't distinguish between authentication methods. The response fields include
applicationId,applicationName,loginId,loginIdType,instant,ipAddress, anduserId, but none indicate the credential type used (token vs. password vs. SSO).Recommended Approach
You can determine token usage by checking the
lastLoginInstantfield on user registrations:- Use the Retrieve User Registration endpoint (
GET /api/user/registration/{userId}/{applicationId}) or search for users with tokens - For each registration with an
authenticationToken, check thelastLoginInstantagainst your time windows (30 days, 90 days, 1 year) - If
lastLoginInstantfalls within your time window for any user, someone authenticated to that application during that period
Here's a Python script approach:
import requests from datetime import datetime, timedelta # Define time windows windows = { '30d': datetime.now() - timedelta(days=30), '90d': datetime.now() - timedelta(days=90), '1y': datetime.now() - timedelta(days=365) } # Pull registrations for your application # Check each registration's lastLoginInstant against cutoffs # Count how many fall within each windowIf all time windows return 0 users: Nobody authenticated to that application during those periods, so the tokens weren't used.
If users show up: The
lastLoginInstantcan't distinguish whether they used the token, password, or SSO. In this case, you have two additional options:-
Check proxy/application logs for
/api/loginAPI calls and examine the request body to see ifloginIdandpasswordwere provided, or if it was a token-based authentication -
Check webhook events: If you have webhooks configured, examine
user.login.successevents for theauthenticationTypefield. When a user authenticates with an application authentication token, the value will beAPPLICATION_TOKEN. Other possible values includePASSWORD,REFRESH_TOKEN, identity providers likeGOOGLEorFACEBOOK, and many more. -
Check JWTs: If you capture or log JWTs, the access token contains an
authenticationTypeclaim that indicates the authentication method used. For authentication tokens, this will be set toAPPLICATION_TOKEN.
Related Documentation
- Application Authentication Tokens - Overview of application authentication tokens and how they work
- Retrieve a User Registration API - API documentation for retrieving user registration details including
authenticationTokenandlastLoginInstant - Search Login Records API - API for searching login records (note: does not include
authenticationType) - User Login Success Event - Webhook event that includes
authenticationTypefield - Authentication Types Reference - Complete list of authentication types including
APPLICATION_TOKEN,PASSWORD, and others - Login API - API for authenticating users with various methods including authentication tokens
- Use the Retrieve User Registration endpoint (
-
D dan has marked this topic as solved