86 lines
1.4 KiB
Go
86 lines
1.4 KiB
Go
|
package domain
|
||
|
|
||
|
import (
|
||
|
"encoding/base64"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
serverMaxLen = 10
|
||
|
)
|
||
|
|
||
|
type Session struct {
|
||
|
ID int64
|
||
|
UserID int64
|
||
|
ServerKey string
|
||
|
SID string
|
||
|
CreatedAt time.Time
|
||
|
UpdatedAt time.Time
|
||
|
}
|
||
|
|
||
|
type CreateSessionParams struct {
|
||
|
userID int64
|
||
|
serverKey string
|
||
|
sid string
|
||
|
}
|
||
|
|
||
|
func NewCreateSessionParams(serverKey, sid string, userID int64) (CreateSessionParams, error) {
|
||
|
if serverKey == "" {
|
||
|
return CreateSessionParams{}, ValidationError{
|
||
|
Field: "ServerKey",
|
||
|
Err: ErrRequired,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if len(serverKey) > serverMaxLen {
|
||
|
return CreateSessionParams{}, ValidationError{
|
||
|
Field: "ServerKey",
|
||
|
Err: MaxLengthError{
|
||
|
Max: serverMaxLen,
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if sid == "" {
|
||
|
return CreateSessionParams{}, ValidationError{
|
||
|
Field: "SID",
|
||
|
Err: ErrRequired,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if !isBase64(sid) {
|
||
|
return CreateSessionParams{}, ValidationError{
|
||
|
Field: "SID",
|
||
|
Err: ErrBase64,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if userID < userIDMin {
|
||
|
return CreateSessionParams{}, ValidationError{
|
||
|
Field: "UserID",
|
||
|
Err: MinError{
|
||
|
Min: userIDMin,
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return CreateSessionParams{userID: userID, serverKey: serverKey, sid: sid}, nil
|
||
|
}
|
||
|
|
||
|
func (c CreateSessionParams) UserID() int64 {
|
||
|
return c.userID
|
||
|
}
|
||
|
|
||
|
func (c CreateSessionParams) ServerKey() string {
|
||
|
return c.serverKey
|
||
|
}
|
||
|
|
||
|
func (c CreateSessionParams) SID() string {
|
||
|
return c.sid
|
||
|
}
|
||
|
|
||
|
func isBase64(s string) bool {
|
||
|
_, err := base64.StdEncoding.DecodeString(s)
|
||
|
return err == nil
|
||
|
}
|