Blog | G5 Cyber Security

Login Token Schema

TL;DR

This guide explains how to design a secure login token schema for your application. We’ll cover the essential components, best practices, and example structures.

1. Understanding Login Tokens

Login tokens (also known as access tokens or JWTs) are credentials used to verify a user’s identity after they have successfully logged in. They allow users to access protected resources without repeatedly entering their username and password. A well-designed schema is crucial for security.

2. Essential Token Components

  1. Header: Contains metadata about the token, including the signing algorithm (e.g., HS256, RS256).
  2. Payload: Holds claims – statements about the user and the token itself. This is where you store information like user ID, roles, expiry time, etc.
  3. Signature: Ensures the token hasn’t been tampered with. It’s created using a secret key (or private key) and the header and payload.

3. Payload Claims – What to Include

Carefully consider what information you put in the payload. More data isn’t always better; it increases token size and potential security risks.

Example Payload (JSON):

{
  "sub": "1234567890",
  "iss": "https://example.com",
  "aud": "my-application",
  "exp": 1678886400,
  "iat": 1678882800,
  "roles": ["user", "admin"]
}

4. Choosing a Signing Algorithm

Select an algorithm appropriate for your security needs.

5. Token Storage (Client Side)

How you store tokens on the client side impacts security.

6. Refresh Tokens

Use refresh tokens to obtain new access tokens without requiring the user to re-enter their credentials.

7. Example Token Structure (JWT)

A JWT consists of three parts separated by dots:

Header.Payload.Signature

Example:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkJWT" . eyJzdWIiOiIxMjM0NTY3ODkwIiwiaXNzIjoihttps://example.com", "aud":"my-application","exp":1678886400,"iat":1678882800} . Sdfjklasdjfklasdjfklasdjfklasdjf

8. Security Considerations

Exit mobile version