ms-auth/internal/models/session.go

72 lines
1.5 KiB
Go
Raw Normal View History

2024-08-14 15:36:43 +05:00
package models
import (
2024-12-30 20:04:26 +05:00
"errors"
2024-08-14 15:36:43 +05:00
"github.com/google/uuid"
2024-12-30 20:04:26 +05:00
"time"
2024-08-14 15:36:43 +05:00
)
type Session struct {
2025-02-25 18:33:15 +05:00
Id string `json:"id"`
UserId int32 `json:"user_id"`
Role Role `json:"role"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
UserAgent string `json:"user_agent"`
Ip string `json:"ip"`
2024-08-14 15:36:43 +05:00
}
func (s Session) Valid() error {
2024-12-30 20:04:26 +05:00
if uuid.Validate(s.Id) != nil {
return errors.New("invalid session id")
2024-08-14 15:36:43 +05:00
}
2024-12-30 20:04:26 +05:00
if s.UserId == 0 {
return errors.New("empty user id")
}
if s.CreatedAt.IsZero() {
return errors.New("empty created at")
}
2025-02-25 18:33:15 +05:00
if s.ExpiresAt.IsZero() {
return errors.New("empty expires at")
2024-08-14 15:36:43 +05:00
}
2025-02-25 18:33:15 +05:00
//if s.UserAgent == "" {
// return errors.New("empty user agent")
//}
//if s.Ip == "" {
// return errors.New("empty ip")
//}
2024-08-14 15:36:43 +05:00
return nil
}
2025-02-25 18:33:15 +05:00
type JWT struct {
SessionId string `json:"session_id"`
UserId int32 `json:"user_id"`
Role Role `json:"role"`
ExpiresAt int64 `json:"exp"`
IssuedAt int64 `json:"iat"`
NotBefore int64 `json:"nbf"`
Permissions []grant `json:"permissions"`
}
func (j JWT) Valid() error {
if uuid.Validate(j.SessionId) != nil {
return errors.New("invalid session id")
2024-12-30 20:04:26 +05:00
}
2025-02-25 18:33:15 +05:00
if j.UserId == 0 {
return errors.New("empty user id")
2024-08-14 15:36:43 +05:00
}
2025-02-25 18:33:15 +05:00
if j.ExpiresAt == 0 {
return errors.New("empty expires at")
2024-08-14 15:36:43 +05:00
}
2025-02-25 18:33:15 +05:00
if j.IssuedAt == 0 {
return errors.New("empty issued at")
2024-08-14 15:36:43 +05:00
}
2025-02-25 18:33:15 +05:00
if j.NotBefore == 0 {
return errors.New("empty not before")
2024-08-14 15:36:43 +05:00
}
2025-02-25 18:33:15 +05:00
if len(j.Permissions) == 0 {
return errors.New("empty permissions")
}
return nil
2024-08-14 15:36:43 +05:00
}