-
Notifications
You must be signed in to change notification settings - Fork 41
/
users_helpers.go
164 lines (148 loc) · 3.41 KB
/
users_helpers.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package utils
import (
"bytes"
"fmt"
"io"
"math"
"math/rand"
"net/mail"
"regexp"
"strings"
"time"
"unicode"
"github.com/rigdev/rig-go-api/api/v1/user"
"github.com/rigdev/rig/pkg/errors"
"github.com/rigdev/rig/pkg/uuid"
)
var ErrInvalidUpdatedAt = errors.InvalidArgumentErrorf("invalid updated at timestamp")
func ValidatePassword(password string) error {
if strings.TrimSpace(password) != password {
return errors.InvalidArgumentErrorf("invalid password; starts or ends with a whitespace")
}
if password == "" {
return errors.InvalidArgumentErrorf("invalid password; empty")
}
var (
num, sym bool
tot uint8
)
for _, char := range password {
switch {
case unicode.IsUpper(char):
tot++
case unicode.IsLower(char):
tot++
case unicode.IsNumber(char):
num = true
tot++
case unicode.IsPunct(char) || unicode.IsSymbol(char):
sym = true
tot++
default:
return errors.InvalidArgumentErrorf("invalid password; contains invalid characters")
}
}
err := errors.InvalidArgumentErrorf(
"invalid password; must contain a number, normal symbol and be at least 8 chars long",
)
if !num || tot < 8 || !sym {
return err
}
return nil
}
func ValidateEmail(email string) error {
_, err := mail.ParseAddress(email)
if err != nil && email != "" {
return err
}
return nil
}
//nolint:lll
const validPhoneRegexp = `^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$`
func ValidatePhone(phone string) error {
re := regexp.MustCompile(validPhoneRegexp)
if !re.MatchString(phone) && phone != "" {
return errors.InvalidArgumentErrorf("invalid phone number")
}
return nil
}
func GetIdentifierFromIdentifier(userID uuid.UUID) (string, string) {
return "id", userID.String()
}
func GetExponentialBackoff(backoff float64, factor float64) time.Duration {
min := 100 * time.Millisecond
max := 30 * time.Second
minf := float64(min)
durf := minf * math.Pow(factor, backoff)
durf = rand.Float64()*(durf-minf) + minf
dur := time.Duration(durf)
// keep within bounds
if dur < min {
return min
}
if dur > max {
return max
}
return dur
}
var ErrFileIsTooLarge = errors.InvalidArgumentErrorf("file exceeds maximum file size")
type DataReceive func() ([]byte, error)
func (d DataReceive) Receive() ([]byte, error) {
return d()
}
func GetData(stream interface {
Receive() ([]byte, error)
}, maxSize int,
) ([]byte, error) {
size := 0
data := bytes.Buffer{}
defer data.Reset()
for {
req, err := stream.Receive()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
size += len(req)
_, err = data.Write(req)
if err != nil {
return nil, err
}
if size > maxSize {
return nil, ErrFileIsTooLarge
}
}
return data.Bytes(), nil
}
func UserName(u *user.User) string {
if n := u.GetProfile().GetFirstName(); n != "" {
if l := u.GetProfile().GetLastName(); l != "" {
n = fmt.Sprint(n, " ", l)
}
return n
}
if n := u.GetUserInfo().GetUsername(); n != "" {
return n
}
if n := u.GetUserInfo().GetEmail(); n != "" {
return n
}
if n := u.GetUserInfo().GetPhoneNumber(); n != "" {
return n
}
return ""
}
func UserIdentifier(u *user.User) string {
if n := u.GetUserInfo().GetUsername(); n != "" {
return n
}
if n := u.GetUserInfo().GetEmail(); n != "" {
return n
}
if n := u.GetUserInfo().GetPhoneNumber(); n != "" {
return n
}
return ""
}