Skip to main content

session.go

Overview

This file defines the authentication and session management logic for the DiceDB server. It includes structures for managing users, sessions, and their statuses, as well as functions for user creation, authentication, and session validation.

Detailed Documentation

Constants

  • Cmd: The authentication command string.
  • SessionStatusPending: Represents a pending session status (0).
  • SessionStatusActive: Represents an active session status (1).
  • SessionStatusExpired: Represents an expired session status (2).

Variables

  • UserStore: A global variable of type *Users that stores user information. Initialized using NewUsersStore().

Type Definitions

  • SessionStatusT: An alias for uint8 representing the session's status.
  • Session: Represents a user session.
    • ID: Unique session ID (uint64).
    • User: Pointer to the User associated with the session.
    • CreatedAt: Session creation timestamp (time.Time).
    • LastAccessedAt: Last access timestamp (time.Time).
    • Status: Session status (SessionStatusT).
  • Users: Represents a collection of users.
    • store: A map that stores users, with usernames as keys and *User as values.
    • stLock: A read-write mutex (*sync.RWMutex) to protect the store from concurrent access.
  • User: Represents a user.
    • Username: Username (string).
    • Passwords: A slice of hashed passwords (string).
    • IsPasswordEnabled: Indicates if the password is enabled for the user (bool).

Function: NewUsersStore

  • Purpose: Creates and returns a new Users store.
  • Returns: A pointer to a new Users struct.

Method: (*Users) Get

  • Purpose: Retrieves a user from the store by username.
  • Parameters:
    • username (string): The username of the user to retrieve.
  • Returns:
    • user (*User): The user if found, otherwise nil.
    • err (error): An error if the user is not found.

Method: (*Users) Add

  • Purpose: Adds a new user to the store.
  • Parameters:
    • username (string): The username of the new user.
  • Returns:
    • user (*User): The newly created user.
    • err (error): Returns nothing.

Method: (*User) SetPassword

  • Purpose: Sets the password for a user, hashing it using bcrypt.
  • Parameters:
    • password (string): The plain text password to set.
  • Returns:
    • err (error): An error if the password hashing fails.

Function: NewSession

  • Purpose: Creates a new session with a pending status.
  • Returns: A pointer to a new Session struct.

Method: (*Session) IsActive

  • Purpose: Checks if the session is active. If password authentication is disabled and the session is not active, it activates the session.
  • Returns:
    • isActive (bool): True if the session is active, false otherwise.

Method: (*Session) Activate

  • Purpose: Activates a session by setting the user, status, and timestamps.
  • Parameters:
    • user (*User): The user to associate with the session.
  • Returns: None

Method: (*Session) Validate

  • Purpose: Validates a user's credentials against stored credentials and activates the session upon successful validation.
  • Parameters:
    • username (string): The username to validate.
    • password (string): The password to validate.
  • Returns:
    • err (error): An error if validation fails.

Method: (*Session) Expire

  • Purpose: Expires a session by setting its status to SessionStatusExpired.
  • Parameters: None
  • Returns: None

Code Examples

// Example of creating a new user store
const userStore = NewUsersStore();

// Example of creating a new session
const session = NewSession();

Getting Started Relevance