Vetspire API Auth Tokens
View SourceWhat are Auth Tokens?
Auth tokens are very similar to API keys in that they grant access to our API, however their purpose is to identify a real user and not a machine.
You obtain an auth token upon logging in to Vetspire, but there are some programmatic ways of getting it (see below).
We want to distinguish between programmatic use and human use of our API, allowing us to:
- Route API users through read replica where possible by default
- Monitor API usage and flag any weird usage
- Prioritize real user's web experience over API (e.g. when investigating issues in Sentry)
Usage
For the most part, no one should be generating and using the auth tokens. Where possible, we should advise against this practice, as this bypasses our ability to monitor usage and protect performance of the real users.
Historically, there have been some exceptions:
- Heart & Paw use the auth token to access private APIs under our
/reportsroutes to access reporting data otherwise not available in our GraphQL API - Bond Vet use auth tokens for their Chrome browser extension (TBC: clarify their use case)
- Potentially more! TBC
Examples
Example auth token:
eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ2ZXRzcGlyZSIsImV4cCI6MTY5NTE5ODE0MCwiaWF0IjoxNjk1MTExNzQwLCJpc3MiOiJ2ZXRzcGlyZSIsImp0aSI6ImI3NjA0ZTAwLTZiM2YtNGM4My04Y2JjLWM3NjM4ODgxZWJhNCIsIm5iZiI6MTY5NTExMTczOSwicHJvdmlkZXIiOnsiaWQiOjQyLCJpc19vcmdfYWRtaW4iOnRydWUsImlzX3ZldGVyaW5hcmlhbiI6ZmFsc2UsIm9yZ19pZCI6MTJ9LCJzdWIiOiJQcm92aWRlcjo0MiIsInN1cHBvcnRfdXNlciI6bnVsbCwidHlwIjoidG9rZW4ifQ.l1SHRC7MojTK__LF2dySo05zBp4c0RQ_qZQQNbJq_mCctpUuiej69MXKA90valVVDhbhlFO1E7hVaAjrRs1IrgWhen base64 decoded:
{"alg":"HS512","typ":"JWT"}{"aud":"vetspire","exp":1695198140,"iat":1695111740,"iss":"vetspire","jti":"b7604e00-6b3f-4c83-8cbc-c7638881eba4","nbf":1695111739,"provider":{"id":42,"is_org_admin":true,"is_veterinarian":false,"org_id":12},"sub":"Provider:42","support_user":null,"typ":"token"}<redacted binary>Normal API key:
SFMyNTY.g2gDbQAAAAcxMjo0OHMzbgYAZ7QCdnoBYgYBUYA.4C8RQ-2t-TTZcUTb6hXeYZ2pS18xEj-x2QzwQLTbOEdWhen base64 decoded:
HS256 <redacted binary>As you can see, auth tokens contain more information about the user.
Creating Auth tokens
You can execute this in any deployed environment to get the token for a given user. It will be valid as long as the TTL entered and there's no way to revoke these as they're not persisted in the database.
provider = Vetspire.Accounts.get_provider_by_username(12, "vetspire")
{:ok, token, _claims} = Vetspire.Guardian.encode_and_sign(
provider,
%{
provider: %{
id: provider.id,
org_id: provider.org_id,
is_org_admin: provider.is_org_admin,
is_veterinarian: provider.is_veterinarian
},
support_user: nil
},
token_type: :token,
ttl: {52, :weeks}
)