71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package models
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/google/uuid"
|
|
"time"
|
|
)
|
|
|
|
type Session struct {
|
|
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"`
|
|
}
|
|
|
|
func (s Session) Valid() error {
|
|
if uuid.Validate(s.Id) != nil {
|
|
return errors.New("invalid session id")
|
|
}
|
|
if s.UserId == 0 {
|
|
return errors.New("empty user id")
|
|
}
|
|
if s.CreatedAt.IsZero() {
|
|
return errors.New("empty created at")
|
|
}
|
|
if s.ExpiresAt.IsZero() {
|
|
return errors.New("empty expires at")
|
|
}
|
|
//if s.UserAgent == "" {
|
|
// return errors.New("empty user agent")
|
|
//}
|
|
//if s.Ip == "" {
|
|
// return errors.New("empty ip")
|
|
//}
|
|
return nil
|
|
}
|
|
|
|
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")
|
|
}
|
|
if j.UserId == 0 {
|
|
return errors.New("empty user id")
|
|
}
|
|
if j.ExpiresAt == 0 {
|
|
return errors.New("empty expires at")
|
|
}
|
|
if j.IssuedAt == 0 {
|
|
return errors.New("empty issued at")
|
|
}
|
|
if j.NotBefore == 0 {
|
|
return errors.New("empty not before")
|
|
}
|
|
if len(j.Permissions) == 0 {
|
|
return errors.New("empty permissions")
|
|
}
|
|
return nil
|
|
}
|