-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.go
94 lines (74 loc) · 2.21 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package twitter
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"
)
var (
UsernameMinLength = 2
PasswordMinLength = 6
)
var emailRegexp = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
type AuthService interface {
Register(ctx context.Context, input RegisterInput) (AuthResponse, error)
Login(ctx context.Context, input LoginInput) (AuthResponse, error)
}
type AuthTokenService interface {
CreateAccessToken(ctx context.Context, user User) (string, error)
CreateRefreshToken(ctx context.Context, user User, tokenID string) (string, error)
ParseToken(ctx context.Context, payload string) (AuthToken, error)
ParseTokenFromRequest(ctx context.Context, r *http.Request) (AuthToken, error)
}
type AuthToken struct {
ID string
Sub string
}
type AuthResponse struct {
AccessToken string
User User
}
type RegisterInput struct {
Email string
Username string
Password string
ConfirmPassword string
}
func (in *RegisterInput) Sanitize() {
in.Email = strings.TrimSpace(in.Email)
in.Email = strings.ToLower(in.Email)
in.Username = strings.TrimSpace(in.Username)
}
func (in RegisterInput) Validate() error {
if len(in.Username) < UsernameMinLength {
return fmt.Errorf("%w: username not long enough, (%d) characters at least", ErrValidation, UsernameMinLength)
}
if !emailRegexp.MatchString(in.Email) {
return fmt.Errorf("%w: email not valid", ErrValidation)
}
if len(in.Password) < PasswordMinLength {
return fmt.Errorf("%w: password not long enough, (%d) characters at least", ErrValidation, PasswordMinLength)
}
if in.Password != in.ConfirmPassword {
return fmt.Errorf("%w: confirm password must match the password", ErrValidation)
}
return nil
}
type LoginInput struct {
Email string
Password string
}
func (in *LoginInput) Sanitize() {
in.Email = strings.TrimSpace(in.Email)
in.Email = strings.ToLower(in.Email)
}
func (in LoginInput) Validate() error {
if !emailRegexp.MatchString(in.Email) {
return fmt.Errorf("%w: email not valid", ErrValidation)
}
if len(in.Password) < 1 {
return fmt.Errorf("%w: password required", ErrValidation)
}
return nil
}