This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
192 lines (168 loc) · 5.41 KB
/
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package model
import (
"crypto"
"crypto/rsa"
"fmt"
"log"
"net/url"
"os"
"time"
"github.com/exlibris-fed/exlibris/key"
"github.com/dgrijalva/jwt-go"
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
"golang.org/x/crypto/bcrypt"
)
const (
// ContextKeyRequestedUser is the key to use for the username of the endpoint being requested.
ContextKeyRequestedUser ContextKey = "username"
// ContextKeyAuthenticatedUser is the key to use for a User object that was retrieved from a JWT. It should not be set until the JWT has been verified as being signed by the user specified in the `kid` field.
ContextKeyAuthenticatedUser ContextKey = "authuser"
// ContextKeyJWT is the key to use for a User's JWT in a context
ContextKeyJWT ContextKey = "jwt"
)
// A User is a person interacting with the app. They may not be registered on this server.
type User struct {
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
ID string `gorm:"primary_key"`
Username string `gorm:"unique;not null;index"`
DisplayName string `gorm:"not null"`
Email string `gorm:"not null"`
Password []byte `json:"-"`
PrivateKey []byte `json:"-"`
Summary string
CryptoPrivateKey crypto.PrivateKey `gorm:"-"`
Local bool `json:"-"`
}
// NewUser creates a user and handles generating the ID, key and hashed password.
func NewUser(username, password, email, displayName string) (*User, error) {
domain := os.Getenv("DOMAIN")
if domain == "" {
return nil, fmt.Errorf("DOMAIN env variable not set")
}
u := User{
ID: fmt.Sprintf("%s/@%s", domain, username),
Username: username,
Email: email,
DisplayName: displayName,
}
u.SetPassword(password)
u.GenerateKeys()
return &u, nil
}
// SetPassword is used to hash the password the user wishes to use.
func (u *User) SetPassword(password string) {
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
log.Println("error hashing password: " + err.Error())
return
}
u.Password = hashed
}
// GenerateKeys is used on user registration to generate a private key for a user. It can theoretically be used to invalidate all existing tokens/sessions.
func (u *User) GenerateKeys() error {
k, err := key.New()
if err != nil {
return err
}
bytes, err := key.SerializeRSAPrivateKey(k)
if err != nil {
return err
}
u.PrivateKey = bytes
return nil
}
// IRI returns a url representing the user's profile
func (u *User) IRI() *url.URL {
URL, err := url.Parse(fmt.Sprintf("https://%s", u.ID))
if err != nil {
log.Printf("error creating IRI for user %s (%s): %s", u.ID, u.Username, err)
return nil
}
return URL
}
// OutboxIRI returns a url representing the user's outbox
func (u *User) OutboxIRI() *url.URL {
URL, err := url.Parse(fmt.Sprintf("https://%s/outbox", u.ID))
if err != nil {
log.Printf("error creating outbox IRI for user %s (%s): %s", u.ID, u.Username, err)
return nil
}
return URL
}
// InboxIRI returns a url representing the user's inbox
func (u *User) InboxIRI() *url.URL {
URL, err := url.Parse(fmt.Sprintf("https://%s/inbox", u.ID))
if err != nil {
log.Printf("error creating inbox IRI for user %s (%s): %s", u.ID, u.Username, err)
return nil
}
return URL
}
// IsPassword verifies that the specified password matches what's in the database.
func (u *User) IsPassword(password string) bool {
return bcrypt.CompareHashAndPassword(u.Password, []byte(password)) == nil
}
func (u *User) ensureCryptoPrivateKey() {
if u.CryptoPrivateKey == nil {
pk, err := key.DeserializeRSAPrivateKey(u.PrivateKey)
if err != nil {
return
}
u.CryptoPrivateKey = pk
}
}
// GenerateJWT generates a JWT for the user.
func (u *User) GenerateJWT() (string, error) {
u.ensureCryptoPrivateKey()
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
"kid": u.Username, // TODO should we be generating urls as an ID?
})
t, err := token.SignedString(u.CryptoPrivateKey)
if err != nil {
return "", err
}
return t, nil
}
// ValidateJWT accepts a JWT and private key and verifies the token was signed by the key.
func (u *User) ValidateJWT(t string) bool {
u.ensureCryptoPrivateKey()
if u.CryptoPrivateKey == nil {
// this may not be a user persisted in the database
return false
}
token, err := jwt.Parse(t, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
pk, ok := u.CryptoPrivateKey.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("private key is %T, not *rsa.PrivateKey", u.CryptoPrivateKey)
}
return &pk.PublicKey, nil
})
if err != nil {
log.Println("error validating JWT: " + err.Error())
return false
}
return token.Valid
}
// ToType returns a representation of a user as an ActivityPub object.
func (u *User) ToType() vocab.Type {
user := streams.NewActivityStreamsPerson()
URL, err := url.Parse(u.ID)
if err == nil {
id := streams.NewJSONLDIdProperty()
id.SetIRI(URL)
user.SetJSONLDId(id)
}
name := streams.NewActivityStreamsNameProperty()
name.AppendXMLSchemaString(u.DisplayName)
user.SetActivityStreamsName(name)
username := streams.NewActivityStreamsPreferredUsernameProperty()
username.SetXMLSchemaString(u.Username)
user.SetActivityStreamsPreferredUsername(username)
return user
}