/
identity.go
46 lines (38 loc) · 1.44 KB
/
identity.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
package types
import (
"fmt"
"strings"
"github.com/ellcrys/util"
)
// Identity defines a user
type Identity struct {
Email string `json:"email,omitempty" structs:"email,omitempty" mapstructure:"email,omitempty"`
Password string `json:"password,omitempty" structs:"password,omitempty" mapstructure:"password,omitempty"`
Cocoons []string `json:"cocoons,omitempty" structs:"cocoons,omitempty" mapstructure:"cocoons,omitempty"`
ClientSessions []string `json:"clientSessions,omitempty" structs:"clientSessions,omitempty" mapstructure:"clientSessions,omitempty"`
CreatedAt string `json:"createdAt,omitempty" structs:"createdAt,omitempty" mapstructure:"createdAt,omitempty"`
}
// NewIdentity creates a new Identity
func NewIdentity(email, password string) *Identity {
return &Identity{
Email: email,
Password: password,
}
}
// GetID returns the hashed version of the email
func (i *Identity) GetID() string {
return util.Sha256(strings.ToLower(strings.TrimSpace(i.Email)))
}
// ToJSON returns the json equivalent of this object
func (i *Identity) ToJSON() []byte {
json, _ := util.ToJSON(i)
return json
}
// MakeIdentityKey constructs an identity key
func MakeIdentityKey(id string) string {
return fmt.Sprintf("identity;%s", id)
}
// MakePrivateIdentityKey returns a key for storing private identity fields
func MakePrivateIdentityKey(identityID string) string {
return fmt.Sprintf("identity_private;%s", identityID)
}