/
app_user.go
63 lines (53 loc) · 1.09 KB
/
app_user.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
package domain
import (
"time"
"github.com/friendsofgo/errors"
"golang.org/x/crypto/ssh"
)
type AvatarBaseURL string
func NewUser(name string) *User {
return &User{
ID: NewID(),
Name: name,
Admin: false,
}
}
type User struct {
ID string
Name string
Admin bool
}
func (u *User) AvatarURL(baseURL AvatarBaseURL) string {
return string(baseURL) + u.Name
}
type UserKey struct {
ID string
UserID string
PublicKey string
Name string
CreatedAt time.Time
}
func NewUserKey(userID string, publicKey string, name string) (*UserKey, error) {
key := &UserKey{
ID: NewID(),
UserID: userID,
PublicKey: publicKey,
Name: name,
CreatedAt: time.Now(),
}
if err := key.Validate(); err != nil {
return nil, err
}
return key, nil
}
func (u *UserKey) MarshalKey() []byte {
out, _, _, _, _ := ssh.ParseAuthorizedKey([]byte(u.PublicKey))
return out.Marshal()
}
func (u *UserKey) Validate() error {
_, _, _, _, err := ssh.ParseAuthorizedKey([]byte(u.PublicKey))
if err != nil {
return errors.Wrap(err, "invalid public key format")
}
return nil
}