mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* add users metadata Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add users metadata Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add metadata to users Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add metadata to users Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * run.sh Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add metadata to users Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add default value for metadata Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add default value for metadata Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * when metadata is not set dont save 'null' string Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * when metadata is not set dont save 'null' string Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change metadata type, add error handling Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add pause Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove extra char Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * retype from string to []byte Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add wait logic for gnatsd Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * few small fixes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix identityRes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add users metadata Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add users metadata Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert run.sh for now as gnats availability check is solved in other PR Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * revert changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change metadata database/sql handling Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix commit issues Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * small change to errors handling Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor comment change Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
//
|
|
// Copyright (c) 2019
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package users
|
|
|
|
import (
|
|
"context"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
userRegexp = regexp.MustCompile("^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+$")
|
|
hostRegexp = regexp.MustCompile("^[^\\s]+\\.[^\\s]+$")
|
|
userDotRegexp = regexp.MustCompile("(^[.]{1})|([.]{1}$)|([.]{2,})")
|
|
)
|
|
|
|
// User represents a Mainflux user account. Each user is identified given its
|
|
// email and password.
|
|
type User struct {
|
|
Email string
|
|
Password string
|
|
Metadata map[string]interface{}
|
|
}
|
|
|
|
// Validate returns an error if user representation is invalid.
|
|
func (u User) Validate() error {
|
|
if u.Email == "" || u.Password == "" {
|
|
return ErrMalformedEntity
|
|
}
|
|
|
|
if !isEmail(u.Email) {
|
|
return 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(context.Context, User) error
|
|
|
|
// RetrieveByID retrieves user by its unique identifier (i.e. email).
|
|
RetrieveByID(context.Context, string) (User, error)
|
|
}
|
|
|
|
func isEmail(email string) bool {
|
|
if len(email) < 6 || len(email) > 254 {
|
|
return false
|
|
}
|
|
|
|
at := strings.LastIndex(email, "@")
|
|
if at <= 0 || at > len(email)-3 {
|
|
return false
|
|
}
|
|
|
|
user := email[:at]
|
|
host := email[at+1:]
|
|
|
|
if len(user) > 64 {
|
|
return false
|
|
}
|
|
|
|
if userDotRegexp.MatchString(user) || !userRegexp.MatchString(user) || !hostRegexp.MatchString(host) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|