audit

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeAuditEventNotFound     = "AUDIT_EVENT_NOT_FOUND"
	CodeAuditEventCreateFailed = "AUDIT_EVENT_CREATE_FAILED"
	CodeInvalidFilter          = "AUDIT_INVALID_FILTER"
	CodeInvalidTimeRange       = "AUDIT_INVALID_TIME_RANGE"
	CodeInvalidPagination      = "AUDIT_INVALID_PAGINATION"
	CodeQueryFailed            = "AUDIT_QUERY_FAILED"
)

Error codes for audit operations

Variables

This section is empty.

Functions

func AuditEventCreateFailed

func AuditEventCreateFailed(err error) *errs.AuthsomeError

AuditEventCreateFailed returns an error when creating an audit event fails

func AuditEventNotFound

func AuditEventNotFound(id string) *errs.AuthsomeError

AuditEventNotFound returns an error when an audit event is not found

func InvalidFilter

func InvalidFilter(field, reason string) *errs.AuthsomeError

InvalidFilter returns an error when filter parameters are invalid

func InvalidPagination

func InvalidPagination(reason string) *errs.AuthsomeError

InvalidPagination returns an error when pagination parameters are invalid

func InvalidTimeRange

func InvalidTimeRange(reason string) *errs.AuthsomeError

InvalidTimeRange returns an error when the time range is invalid

func QueryFailed

func QueryFailed(operation string, err error) *errs.AuthsomeError

QueryFailed returns an error when a query operation fails

Types

type CreateEventRequest

type CreateEventRequest struct {
	AppID     xid.ID  `json:"appId,omitempty"` // Optional - will be read from context if not provided
	UserID    *xid.ID `json:"userId,omitempty"`
	Action    string  `json:"action" validate:"required"`
	Resource  string  `json:"resource" validate:"required"`
	IPAddress string  `json:"ipAddress,omitempty"`
	UserAgent string  `json:"userAgent,omitempty"`
	Metadata  string  `json:"metadata,omitempty"`
}

CreateEventRequest represents a request to create an audit event

type CreateEventResponse

type CreateEventResponse struct {
	Event *Event `json:"event"`
}

CreateEventResponse represents the response after creating an audit event

type Event

type Event struct {
	ID        xid.ID    `json:"id"`
	AppID     xid.ID    `json:"appId"`
	UserID    *xid.ID   `json:"userId,omitempty"`
	Action    string    `json:"action"`
	Resource  string    `json:"resource"`
	IPAddress string    `json:"ipAddress,omitempty"`
	UserAgent string    `json:"userAgent,omitempty"`
	Metadata  string    `json:"metadata,omitempty"` // JSON string or plain text
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

Event represents an audit trail record DTO This is separate from schema.AuditEvent to maintain proper separation of concerns

func FromSchemaEvent

func FromSchemaEvent(ae *schema.AuditEvent) *Event

FromSchemaEvent converts a schema.AuditEvent model to Event DTO

func FromSchemaEvents

func FromSchemaEvents(events []*schema.AuditEvent) []*Event

FromSchemaEvents converts a slice of schema.AuditEvent to Event DTOs

func (*Event) ToSchema

func (e *Event) ToSchema() *schema.AuditEvent

ToSchema converts the Event DTO to a schema.AuditEvent model

type GetEventRequest

type GetEventRequest struct {
	ID xid.ID `json:"id" validate:"required"`
}

GetEventRequest represents a request to get an audit event by ID

type GetEventResponse

type GetEventResponse struct {
	Event *Event `json:"event"`
}

GetEventResponse represents the response for getting an audit event

type ListEventsFilter

type ListEventsFilter struct {
	pagination.PaginationParams

	// Filter by user
	UserID *xid.ID `json:"userId,omitempty" query:"user_id"`

	// Filter by action
	Action *string `json:"action,omitempty" query:"action"`

	// Filter by resource
	Resource *string `json:"resource,omitempty" query:"resource"`

	// Filter by IP address
	IPAddress *string `json:"ipAddress,omitempty" query:"ip_address"`

	// Time range filters
	Since *time.Time `json:"since,omitempty" query:"since"`
	Until *time.Time `json:"until,omitempty" query:"until"`

	// Sort order (default: created_at DESC)
	SortBy    *string `json:"sortBy,omitempty" query:"sort_by"`       // created_at, action, resource
	SortOrder *string `json:"sortOrder,omitempty" query:"sort_order"` // asc, desc
}

ListEventsFilter defines filters for listing audit events with pagination

type ListEventsResponse

type ListEventsResponse = pagination.PageResponse[*Event]

ListEventsResponse represents a paginated list of audit events

type Repository

type Repository interface {
	Create(ctx context.Context, e *schema.AuditEvent) error
	Get(ctx context.Context, id xid.ID) (*schema.AuditEvent, error)
	List(ctx context.Context, filter *ListEventsFilter) (*pagination.PageResponse[*schema.AuditEvent], error)
}

Repository defines persistence for audit events

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service handles audit logging

func NewService

func NewService(repo Repository) *Service

NewService creates a new audit service

func (*Service) Create

func (s *Service) Create(ctx context.Context, req *CreateEventRequest) (*Event, error)

Create creates a new audit event from a request

func (*Service) Get

func (s *Service) Get(ctx context.Context, req *GetEventRequest) (*Event, error)

Get retrieves an audit event by ID

func (*Service) List

func (s *Service) List(ctx context.Context, filter *ListEventsFilter) (*ListEventsResponse, error)

List returns paginated audit events with optional filters

func (*Service) Log

func (s *Service) Log(ctx context.Context, userID *xid.ID, action, resource, ip, ua, metadata string) error

Log creates an audit event with timestamps

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL