-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
auth_client.go
601 lines (483 loc) Β· 14.1 KB
/
auth_client.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package entity
import (
"errors"
"fmt"
"time"
"github.com/dustin/go-humanize/english"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/internal/acl"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/pkg/authn"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/report"
"github.com/photoprism/photoprism/pkg/rnd"
"github.com/photoprism/photoprism/pkg/unix"
)
// ClientUID is the unique ID prefix.
const (
ClientUID = byte('c')
)
// Clients represents a list of client applications.
type Clients []Client
// Client represents a client application.
type Client struct {
ClientUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"-" yaml:"ClientUID"`
UserUID string `gorm:"type:VARBINARY(42);index;default:'';" json:"UserUID" yaml:"UserUID,omitempty"`
UserName string `gorm:"size:200;index;" json:"UserName" yaml:"UserName,omitempty"`
user *User `gorm:"-" yaml:"-"`
ClientName string `gorm:"size:200;" json:"ClientName" yaml:"ClientName,omitempty"`
ClientRole string `gorm:"size:64;default:'';" json:"ClientRole" yaml:"ClientRole,omitempty"`
ClientType string `gorm:"type:VARBINARY(16)" json:"ClientType" yaml:"ClientType,omitempty"`
ClientURL string `gorm:"type:VARBINARY(255);default:'';column:client_url;" json:"ClientURL" yaml:"ClientURL,omitempty"`
CallbackURL string `gorm:"type:VARBINARY(255);default:'';column:callback_url;" json:"CallbackURL" yaml:"CallbackURL,omitempty"`
AuthProvider string `gorm:"type:VARBINARY(128);default:'';" json:"AuthProvider" yaml:"AuthProvider,omitempty"`
AuthMethod string `gorm:"type:VARBINARY(128);default:'';" json:"AuthMethod" yaml:"AuthMethod,omitempty"`
AuthScope string `gorm:"size:1024;default:'';" json:"AuthScope" yaml:"AuthScope,omitempty"`
AuthExpires int64 `json:"AuthExpires" yaml:"AuthExpires,omitempty"`
AuthTokens int64 `json:"AuthTokens" yaml:"AuthTokens,omitempty"`
AuthEnabled bool `json:"AuthEnabled" yaml:"AuthEnabled,omitempty"`
LastActive int64 `json:"LastActive" yaml:"LastActive,omitempty"`
CreatedAt time.Time `json:"CreatedAt" yaml:"-"`
UpdatedAt time.Time `json:"UpdatedAt" yaml:"-"`
}
// TableName returns the entity table name.
func (Client) TableName() string {
return "auth_clients"
}
// NewClient returns a new client application instance.
func NewClient() *Client {
return &Client{
UserUID: "",
ClientName: "",
ClientRole: acl.RoleClient.String(),
ClientType: authn.ClientConfidential,
ClientURL: "",
CallbackURL: "",
AuthProvider: authn.ProviderClient.String(),
AuthMethod: authn.MethodOAuth2.String(),
AuthScope: "",
AuthExpires: unix.Hour,
AuthTokens: 5,
AuthEnabled: true,
LastActive: 0,
}
}
// BeforeCreate creates a random UID if needed before inserting a new row to the database.
func (m *Client) BeforeCreate(scope *gorm.Scope) error {
if rnd.IsUID(m.ClientUID, ClientUID) {
return nil
}
m.ClientUID = rnd.GenerateUID(ClientUID)
return scope.SetColumn("ClientUID", m.ClientUID)
}
// FindClientByUID returns the matching client or nil if it was not found.
func FindClientByUID(uid string) *Client {
if rnd.InvalidUID(uid, ClientUID) {
return nil
}
m := &Client{}
// Find matching record.
if err := UnscopedDb().First(m, "client_uid = ?", uid).Error; err != nil {
return nil
}
return m
}
// GetUID returns the client uid string.
func (m *Client) GetUID() string {
return m.ClientUID
}
// HasUID tests if the client has a valid uid.
func (m *Client) HasUID() bool {
return rnd.IsUID(m.ClientUID, ClientUID)
}
// NoUID tests if the client does not have a valid uid.
func (m *Client) NoUID() bool {
return !m.HasUID()
}
// Name returns the client name string.
func (m *Client) Name() string {
return m.ClientName
}
// HasName tests if the client has a name.
func (m *Client) HasName() bool {
return m.ClientName != ""
}
// NoName tests if the client does not have a name.
func (m *Client) NoName() bool {
return !m.HasName()
}
// String returns the client id or name for use in logs and reports.
func (m *Client) String() string {
if m == nil {
return report.NotAssigned
} else if m.HasUID() {
return m.GetUID()
} else if m.HasName() {
return m.Name()
}
return report.NotAssigned
}
// SetName sets a custom client name.
func (m *Client) SetName(s string) *Client {
if s = clean.Name(s); s != "" {
m.ClientName = s
}
return m
}
// SetRole sets the client role specified as string.
func (m *Client) SetRole(role string) *Client {
if role != "" {
m.ClientRole = acl.ClientRoles[clean.Role(role)].String()
}
return m
}
// HasRole checks the client role specified as string.
func (m *Client) HasRole(role acl.Role) bool {
return m.AclRole() == role
}
// AclRole returns the client role for ACL permission checks.
func (m *Client) AclRole() acl.Role {
if m == nil {
return acl.RoleNone
}
if role, ok := acl.ClientRoles[clean.Role(m.ClientRole)]; ok {
return role
}
return acl.RoleNone
}
// User returns the user who owns the client, if any.
func (m *Client) User() *User {
if m.user != nil {
return m.user
} else if m.UserUID == "" {
return &User{}
}
if u := FindUserByUID(m.UserUID); u != nil {
m.user = u
return m.user
}
return &User{}
}
// HasUser checks the client belongs to a user.
func (m *Client) HasUser() bool {
return rnd.IsUID(m.UserUID, UserUID)
}
// SetUser sets the user to which the client belongs.
func (m *Client) SetUser(u *User) *Client {
if u == nil {
return m
}
// Update user references.
m.user = u
m.UserUID = u.UserUID
m.UserName = u.UserName
return m
}
// UserInfo reports the user that is assigned to this client.
func (m *Client) UserInfo() string {
if m == nil {
return ""
} else if m.UserUID == "" {
return report.NotAssigned
} else if m.UserName != "" {
return m.UserName
}
return m.UserUID
}
// AuthInfo reports the authentication configured for this client.
func (m *Client) AuthInfo() string {
if m == nil {
return ""
}
provider := m.Provider()
method := m.Method()
if method.IsDefault() {
return provider.Pretty()
}
if provider.IsDefault() {
return method.Pretty()
}
return fmt.Sprintf("%s (%s)", provider.Pretty(), method.Pretty())
}
// Create new entity in the database.
func (m *Client) Create() error {
return Db().Create(m).Error
}
// Save updates the record in the database or inserts a new record if it does not already exist.
func (m *Client) Save() error {
if err := Db().Save(m).Error; err != nil {
return err
}
// Delete related sessions if authentication is disabled.
if m.AuthEnabled {
return nil
} else if _, err := m.DeleteSessions(); err != nil {
return err
}
return nil
}
// Delete marks the entity as deleted.
func (m *Client) Delete() (err error) {
if m.ClientUID == "" {
return fmt.Errorf("client uid is empty")
}
if _, err = m.DeleteSessions(); err != nil {
return err
}
err = Db().Delete(m).Error
return err
}
// DeleteSessions deletes all sessions that belong to this client.
func (m *Client) DeleteSessions() (deleted int, err error) {
if m.ClientUID == "" {
return 0, fmt.Errorf("client uid is empty")
}
if deleted = DeleteClientSessions(m, "", 0); deleted > 0 {
event.AuditInfo([]string{"client %s", "deleted %s"}, m.String(), english.Plural(deleted, "session", "sessions"))
}
return deleted, nil
}
// Deleted checks if the client has been deleted.
func (m *Client) Deleted() bool {
if m == nil {
return true
}
return false
}
// Disabled checks if the client authentication has been disabled.
func (m *Client) Disabled() bool {
if m == nil {
return true
}
return !m.AuthEnabled
}
// Updates multiple properties in the database.
func (m *Client) Updates(values interface{}) error {
return UnscopedDb().Model(m).Updates(values).Error
}
// NewSecret sets a random client secret and returns it if successful.
func (m *Client) NewSecret() (secret string, err error) {
if !m.HasUID() {
return "", fmt.Errorf("invalid client uid")
}
secret = rnd.ClientSecret()
if err = m.SetSecret(secret); err != nil {
return "", err
}
return secret, nil
}
// SetSecret updates the current client secret or returns an error otherwise.
func (m *Client) SetSecret(secret string) (err error) {
if !m.HasUID() {
return fmt.Errorf("invalid client uid")
} else if !rnd.IsClientSecret(secret) {
return fmt.Errorf("invalid client secret")
}
pw := NewPassword(m.ClientUID, secret, false)
if err = pw.Save(); err != nil {
return err
}
return nil
}
// VerifySecret checks if the given client secret is correct.
func (m *Client) VerifySecret(s string) bool {
return !m.InvalidSecret(s)
}
// InvalidSecret checks if the given client secret is invalid.
func (m *Client) InvalidSecret(s string) bool {
// Check client UID.
if !m.HasUID() {
// Invalid, ID is missing.
return true
}
// Check if secret is empty.
if s == "" {
// Invalid, no secret provided.
return true
}
// Find secret.
pw := FindPassword(m.ClientUID)
// Found?
if pw == nil {
// Invalid, not found.
return true
}
// Matches?
if pw.Invalid(s) {
// Invalid, does not match.
return true
}
return false
}
// Provider returns the client authentication provider.
func (m *Client) Provider() authn.ProviderType {
return authn.Provider(m.AuthProvider)
}
// SetProvider sets a custom client authentication provider.
func (m *Client) SetProvider(provider authn.ProviderType) *Client {
if !provider.IsDefault() {
m.AuthProvider = provider.String()
}
return m
}
// Method returns the client authentication method.
func (m *Client) Method() authn.MethodType {
return authn.Method(m.AuthMethod)
}
// SetMethod sets a custom client authentication method.
func (m *Client) SetMethod(method authn.MethodType) *Client {
if !method.IsDefault() {
m.AuthMethod = method.String()
}
return m
}
// Scope returns the client authorization scope.
func (m *Client) Scope() string {
return clean.Scope(m.AuthScope)
}
// SetScope sets a custom client authorization scope.
func (m *Client) SetScope(s string) *Client {
if s = clean.Scope(s); s != "" {
m.AuthScope = clean.Scope(s)
}
return m
}
// UpdateLastActive sets the time of last activity to now and optionally also updates the auth_clients table.
func (m *Client) UpdateLastActive(save bool) *Client {
if m == nil {
return &Client{}
} else if !m.HasUID() {
return m
}
// Set time of last activity to now (Unix timestamp).
m.LastActive = unix.Now()
// Update activity timestamp of this client in the auth_clients table.
if !save {
return m
} else if err := Db().Model(m).UpdateColumn("last_active", m.LastActive).Error; err != nil {
event.AuditWarn([]string{"client %s", "failed to update activity timestamp", "%s"}, m.ClientUID, err)
}
return m
}
// NewSession creates a new client session.
func (m *Client) NewSession(c *gin.Context, t authn.GrantType) *Session {
// Create, initialize, and return new session.
return NewSession(m.AuthExpires, 0).SetContext(c).SetClient(m).SetGrantType(t)
}
// EnforceAuthTokenLimit deletes client sessions above the configured limit and returns the number of deleted sessions.
func (m *Client) EnforceAuthTokenLimit() (deleted int) {
if m == nil {
return 0
} else if !m.HasUID() {
return 0
} else if m.AuthTokens < 0 {
return 0
}
return DeleteClientSessions(m, authn.MethodOAuth2, m.AuthTokens)
}
// Expires returns the auth expiration duration.
func (m *Client) Expires() time.Duration {
return time.Duration(m.AuthExpires) * time.Second
}
// SetExpires sets a custom auth expiration time in seconds.
func (m *Client) SetExpires(i int64) *Client {
if i != 0 {
m.AuthExpires = i
}
return m
}
// Tokens returns maximum number of access tokens this client can create.
func (m *Client) Tokens() int64 {
if m.AuthTokens == 0 {
return 1
}
return m.AuthTokens
}
// SetTokens sets a custom access token limit for this client.
func (m *Client) SetTokens(i int64) *Client {
if i != 0 {
m.AuthTokens = i
}
return m
}
// Report returns the entity values as rows.
func (m *Client) Report(skipEmpty bool) (rows [][]string, cols []string) {
cols = []string{"Name", "Value"}
// Extract model values.
values, _, err := ModelValues(m, "ClientUID")
// Ok?
if err != nil {
return rows, cols
}
rows = make([][]string, 0, len(values))
for k, v := range values {
s := fmt.Sprintf("%#v", v)
// Skip empty values?
if !skipEmpty || s != "" {
rows = append(rows, []string{k, s})
}
}
return rows, cols
}
// SetFormValues sets the values specified in the form.
func (m *Client) SetFormValues(frm form.Client) *Client {
if frm.UserUID == "" && frm.UserName == "" {
// Client does not belong to a specific user or the user remains unchanged.
} else if u := FindUser(User{UserUID: frm.UserUID, UserName: frm.UserName}); u != nil {
m.SetUser(u)
}
// Set custom client UID?
if id := frm.ID(); m.ClientUID == "" && id != "" {
m.ClientUID = id
}
// Set values from form.
m.SetName(frm.Name())
m.SetProvider(frm.Provider())
m.SetMethod(frm.Method())
m.SetScope(frm.Scope())
m.SetTokens(frm.Tokens())
m.SetExpires(frm.Expires())
// Enable authentication?
if frm.AuthEnabled {
m.AuthEnabled = true
}
// Replace empty values with defaults.
if m.AuthProvider == "" {
m.AuthProvider = authn.ProviderClient.String()
}
if m.AuthMethod == "" {
m.AuthMethod = authn.MethodOAuth2.String()
}
if m.AuthScope == "" {
m.AuthScope = "*"
}
if m.AuthExpires <= 0 {
m.AuthExpires = unix.Hour
}
if m.AuthTokens <= 0 {
m.AuthTokens = -1
}
return m
}
// Validate checks the client application properties before saving them.
func (m *Client) Validate() (err error) {
// Empty client name?
if m.ClientName == "" {
return errors.New("client name must not be empty")
}
// Empty client type?
if m.ClientType == "" {
return errors.New("client type must not be empty")
}
// Empty authorization method?
if m.AuthMethod == "" {
return errors.New("authorization method must not be empty")
}
// Empty authorization scope?
if m.AuthScope == "" {
return errors.New("authorization scope must not be empty")
}
return nil
}