-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
71 lines (60 loc) · 1.54 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package yeahapi
import (
"context"
"time"
"github.com/gofrs/uuid"
)
type Session struct {
ID uuid.UUID `json:"id"`
UserID UserID `json:"-"`
Active bool `json:"-"`
ClientID ClientID `json:"-"`
UserAgent string `json:"-"`
IP string `json:"-"`
}
type Auth struct {
User *User `json:"user"`
Session *Session `json:"session"`
}
type Otp struct {
ID uuid.UUID
Code string
Hash string
Confirmed bool
ExpiresAt time.Time
Identifier string
}
type LoginToken struct {
Sig []byte
Payload []byte
Token string
ExpiresAt time.Time
}
type AuthService interface {
CreateOtp(ctx context.Context, otp *Otp) (*Otp, error)
VerifyOtp(ctx context.Context, otp *Otp) error
Otp(ctx context.Context, hash string, confirmed bool) (*Otp, error)
CreateAuth(ctx context.Context, auth *Auth) (*Auth, error)
DeleteAuth(ctx context.Context, sessionID uuid.UUID) error
Session(ctx context.Context, sessionID uuid.UUID) (*Session, error)
CreateLoginToken(expiresAt time.Time) (*LoginToken, error)
VerifyLoginToken(token string) error
}
func (o *Otp) Ok() error {
if len(o.Identifier) == 0 {
return E(EInvalid, "Otp identifier is required")
}
if o.ExpiresAt.IsZero() {
return E(EInternal, "Otp expiration is required")
}
return nil
}
func (a *Auth) Ok() error {
if a.Session.ClientID.IsNil() {
return E(EInvalid, "Session client id is required")
}
if a.Session.UserID.IsNil() && a.User == nil {
return E(EInvalid, "Session user id is required if user not passed")
}
return nil
}