-
Notifications
You must be signed in to change notification settings - Fork 13
/
users.go
141 lines (113 loc) · 3.16 KB
/
users.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
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package users
import (
"context"
"fmt"
"regexp"
"strings"
"github.com/MainfluxLabs/mainflux/pkg/errors"
"golang.org/x/net/idna"
)
const (
maxLocalLen = 64
maxDomainLen = 255
maxTLDLen = 24 // longest TLD currently in existence
atSeparator = "@"
dotSeparator = "."
)
var (
userRegexp = regexp.MustCompile("^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+$")
hostRegexp = regexp.MustCompile(`^[^\s]+\.[^\s]+$`)
userDotRegexp = regexp.MustCompile("(^[.]{1})|([.]{1}$)|([.]{2,})")
)
// Metadata to be used for Mainflux thing or channel for customized
// describing of particular thing or channel.
type Metadata map[string]interface{}
// User represents a Mainflux user account. Each user is identified given its
// email and password.
type User struct {
ID string
Email string
Password string
Metadata Metadata
Status string
Role string
}
// Validate returns an error if user representation is invalid.
func (u User) Validate() error {
if !isEmail(u.Email) {
return errors.ErrMalformedEntity
}
return nil
}
// UserRepository specifies an account persistence API.
type UserRepository interface {
// Save persists the user account. A non-nil error is returned to indicate
// operation failure.
Save(ctx context.Context, u User) (string, error)
// UpdateUser updates the user metadata.
UpdateUser(ctx context.Context, u User) error
// RetrieveByEmail retrieves user by its unique identifier (i.e. email).
RetrieveByEmail(ctx context.Context, email string) (User, error)
// RetrieveByID retrieves user by its unique identifier ID.
RetrieveByID(ctx context.Context, id string) (User, error)
// RetrieveByIDs retrieves all users for given array of userIDs.
RetrieveByIDs(ctx context.Context, userIDs []string, pm PageMetadata) (UserPage, error)
// UpdatePassword updates password for user with given email
UpdatePassword(ctx context.Context, email, password string) error
// ChangeStatus changes users status to enabled or disabled
ChangeStatus(ctx context.Context, id, status string) error
// RetrieveAll retrieves all users.
RetrieveAll(ctx context.Context) ([]User, error)
}
func isEmail(email string) bool {
if email == "" {
return false
}
es := strings.Split(email, atSeparator)
if len(es) != 2 {
return false
}
local, host := es[0], es[1]
if local == "" || len(local) > maxLocalLen {
return false
}
hs := strings.Split(host, dotSeparator)
if len(hs) < 2 {
return false
}
domain, ext := hs[0], hs[1]
// Check subdomain and validate
if len(hs) > 2 {
if domain == "" {
return false
}
for i := 1; i < len(hs)-1; i++ {
sub := hs[i]
if sub == "" {
return false
}
domain = fmt.Sprintf("%s.%s", domain, sub)
}
ext = hs[len(hs)-1]
}
if domain == "" || len(domain) > maxDomainLen {
return false
}
if ext == "" || len(ext) > maxTLDLen {
return false
}
punyLocal, err := idna.ToASCII(local)
if err != nil {
return false
}
punyHost, err := idna.ToASCII(host)
if err != nil {
return false
}
if userDotRegexp.MatchString(punyLocal) || !userRegexp.MatchString(punyLocal) || !hostRegexp.MatchString(punyHost) {
return false
}
return true
}