Authentication Security & Policy Implementation
This page describes how the sample application handles tokens, device information, and requests.
See Basic implementation using the Auth API for the sign-in flow.
Token storage and refresh
The sample stores ID, access, and refresh tokens in HttpOnly cookies.
The HttpOnly attribute prevents JavaScript from reading cookie values.
Cookies use SameSite=Strict and Path=/.
The sample sets Secure unless APP_ENV=local, so use HTTPS outside local development.
The ID and access token cookies have a one-hour lifetime.
The refresh token cookie lifetime comes from the SaaSus Platform setting and is capped at 400 days.
Browsers may delete cookies earlier, and the cookie lifetime is separate from the token lifetime.
Refreshing does not extend the refresh token cookie's expiration.
Protected endpoints check the ID token using GetUserInfo.
On a 401 response, the frontend attempts a refresh once.
On sign-out, RevokeToken invalidates the refresh token; the sample then deletes the cookies after a successful response.
When device remembering is used, token refresh may require the device key, so the sample saves it with the refresh token if the access token contains one.
The sample joins the two values in the same format used by the SaaSus Platform provided login screen.
CSRF protection
Browsers send cookies automatically, so applications using cookies need CSRF protection.
In addition to SameSite=Strict, the sample uses common middleware to check the Origin and JSON Content-Type of POST requests.
It does not use a CSRF token.
Local development allows http://localhost:3000.
Outside local development, set APP_ORIGIN to the frontend origin.
The backend will not start if it is unset.
Consider additional protections such as CSRF tokens according to your application's architecture.
For example, the following configures CSRF middleware in Go's Echo framework. This example is not included in the sample application.
// Example CSRF middleware configuration
e.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{
TokenLookup: "header:X-CSRF-Token",
CookiePath: "/",
}))
The application must also provide the token to the frontend and include it in the header of state-changing requests.
Choose cookie attributes and token handling based on the application's architecture.
Device information
The sample stores the device key, device group key, and device password in browser localStorage.
The device password is generated separately from the user's password and used to generate a signature in the browser; the password itself is not sent.
JavaScript on the same origin can read the localStorage used to store device information, so an XSS vulnerability could expose the device password.
Choose where to store the information and how long to retain it based on the application's architecture.