-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
auth_session.go
988 lines (801 loc) Β· 25.2 KB
/
auth_session.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
package entity
import (
"encoding/json"
"fmt"
"net"
"net/http"
"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/server/limiter"
"github.com/photoprism/photoprism/pkg/authn"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/header"
"github.com/photoprism/photoprism/pkg/i18n"
"github.com/photoprism/photoprism/pkg/list"
"github.com/photoprism/photoprism/pkg/report"
"github.com/photoprism/photoprism/pkg/rnd"
"github.com/photoprism/photoprism/pkg/txt"
"github.com/photoprism/photoprism/pkg/unix"
)
// SessionPrefix for RefID.
const (
SessionPrefix = "sess"
UnknownIP = limiter.DefaultIP
)
// Sessions represents a list of sessions.
type Sessions []Session
// Session represents a User session.
type Session struct {
ID string `gorm:"type:VARBINARY(2048);primary_key;auto_increment:false;" json:"-" yaml:"ID"`
authToken string `gorm:"-" yaml:"-"`
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:"-"`
ClientUID string `gorm:"type:VARBINARY(42);index;default:'';" json:"ClientUID" yaml:"ClientUID,omitempty"`
ClientName string `gorm:"size:200;default:'';" json:"ClientName" yaml:"ClientName,omitempty"`
ClientIP string `gorm:"size:64;column:client_ip;index" json:"ClientIP" yaml:"ClientIP,omitempty"`
client *Client `gorm:"-" yaml:"-"`
AuthProvider string `gorm:"type:VARBINARY(128);default:'';" json:"AuthProvider" yaml:"AuthProvider,omitempty"`
AuthMethod string `gorm:"type:VARBINARY(128);default:'';" json:"AuthMethod" yaml:"AuthMethod,omitempty"`
AuthDomain string `gorm:"type:VARBINARY(255);default:'';" json:"AuthDomain" yaml:"AuthDomain,omitempty"`
AuthID string `gorm:"type:VARBINARY(255);index;default:'';" json:"-" yaml:"AuthID,omitempty"`
AuthScope string `gorm:"size:1024;default:'';" json:"AuthScope" yaml:"AuthScope,omitempty"`
GrantType string `gorm:"type:VARBINARY(64);default:'';" json:"GrantType" yaml:"GrantType,omitempty"`
LastActive int64 `json:"LastActive" yaml:"LastActive,omitempty"`
SessExpires int64 `gorm:"index" json:"Expires" yaml:"Expires,omitempty"`
SessTimeout int64 `json:"Timeout" yaml:"Timeout,omitempty"`
PreviewToken string `gorm:"type:VARBINARY(64);column:preview_token;default:'';" json:"-" yaml:"-"`
DownloadToken string `gorm:"type:VARBINARY(64);column:download_token;default:'';" json:"-" yaml:"-"`
AccessToken string `gorm:"type:VARBINARY(4096);column:access_token;default:'';" json:"-" yaml:"-"`
RefreshToken string `gorm:"type:VARBINARY(512);column:refresh_token;default:'';" json:"-" yaml:"-"`
IdToken string `gorm:"type:VARBINARY(1024);column:id_token;default:'';" json:"IdToken,omitempty" yaml:"IdToken,omitempty"`
UserAgent string `gorm:"size:512;" json:"UserAgent" yaml:"UserAgent,omitempty"`
DataJSON json.RawMessage `gorm:"type:VARBINARY(4096);" json:"-" yaml:"Data,omitempty"`
data *SessionData `gorm:"-" yaml:"-"`
RefID string `gorm:"type:VARBINARY(16);default:'';" json:"ID" yaml:"-"`
LoginIP string `gorm:"size:64;column:login_ip" json:"LoginIP" yaml:"-"`
LoginAt time.Time `json:"LoginAt" yaml:"-"`
CreatedAt time.Time `json:"CreatedAt" yaml:"CreatedAt"`
UpdatedAt time.Time `json:"UpdatedAt" yaml:"UpdatedAt"`
Status int `gorm:"-" json:"Status" yaml:"-"`
}
// TableName returns the entity table name.
func (Session) TableName() string {
return "auth_sessions"
}
// NewSession creates a new session with the expiration and idle time specified in seconds (-1 for infinite).
func NewSession(expiresIn, timeout int64) (m *Session) {
m = &Session{}
m.Regenerate()
// Set session expiration time in seconds (-1 for infinite).
m.SetExpiresIn(expiresIn)
// Set session idle time in seconds (-1 for infinite).
m.SetTimeout(timeout)
return m
}
// SessionStatusUnauthorized returns a session with status unauthorized (401).
func SessionStatusUnauthorized() *Session {
return &Session{Status: http.StatusUnauthorized}
}
// SessionStatusForbidden returns a session with status forbidden (403).
func SessionStatusForbidden() *Session {
return &Session{Status: http.StatusForbidden}
}
// SessionStatusTooManyRequests returns a session with status too many requests (429).
func SessionStatusTooManyRequests() *Session {
return &Session{Status: http.StatusTooManyRequests}
}
// FindSessionByRefID finds an existing session by ref ID.
func FindSessionByRefID(refId string) *Session {
if !rnd.IsRefID(refId) {
return nil
}
m := &Session{}
// Build query.
if err := UnscopedDb().Where("ref_id = ?", refId).First(m).Error; err != nil {
return nil
}
return m
}
// AuthToken returns the secret client authentication token.
func (m *Session) AuthToken() string {
return m.authToken
}
// SetAuthToken sets a custom authentication token.
func (m *Session) SetAuthToken(authToken string) *Session {
m.authToken = authToken
m.ID = rnd.SessionID(authToken)
return m
}
// AuthTokenType returns the authentication token type.
func (m *Session) AuthTokenType() string {
return header.AuthBearer
}
// Regenerate (re-)initializes the session with a random auth token, ID, and RefID.
func (m *Session) Regenerate() *Session {
if !rnd.IsSessionID(m.ID) {
// Skip deleting existing session if session ID is not set (or invalid).
} else if err := m.Delete(); err != nil {
// Failed to delete existing session.
event.AuditErr([]string{m.IP(), "session %s", "failed to delete", "%s"}, m.RefID, err)
} else {
// Successfully deleted existing session.
event.AuditErr([]string{m.IP(), "session %s", "deleted"}, m.RefID)
}
// Set new auth token and ref id.
m.SetAuthToken(rnd.AuthToken())
m.RefID = rnd.RefID(SessionPrefix)
// Get current time.
now := TimeStamp()
// Set timestamps to now.
m.CreatedAt = now
m.UpdatedAt = now
return m
}
// CacheDuration updates the session entity cache.
func (m *Session) CacheDuration(d time.Duration) {
if !rnd.IsSessionID(m.ID) {
return
}
CacheSession(m, d)
}
// Cache caches the session with the default expiration duration.
func (m *Session) Cache() {
m.CacheDuration(SessionCacheDuration)
}
// ClearCache deletes the session from the cache.
func (m *Session) ClearCache() {
DeleteFromSessionCache(m.ID)
}
// Create new entity in the database.
func (m *Session) Create() (err error) {
if err = Db().Create(m).Error; err == nil && rnd.IsSessionID(m.ID) {
m.Cache()
}
return err
}
// Save updates the record in the database or inserts a new record if it does not already exist.
func (m *Session) Save() error {
if err := Db().Save(m).Error; err != nil {
return err
} else if rnd.IsSessionID(m.ID) {
m.Cache()
}
// Limit the number of sessions that are created with an app password.
if !m.Method().IsSession() {
return nil
} else if !m.Provider().IsApplication() {
return nil
} else if client := m.Client(); client.NoName() || client.Tokens() < 1 {
return nil
} else if deleted := DeleteClientSessions(client, authn.MethodSession, client.Tokens()); deleted > 0 {
event.AuditInfo([]string{m.IP(), "session %s", "deleted %s"}, m.RefID, english.Plural(deleted, "previously created client session", "previously created client sessions"))
}
return nil
}
// Delete permanently deletes a session.
func (m *Session) Delete() error {
return DeleteSession(m)
}
// Updates multiple properties in the database.
func (m *Session) Updates(values interface{}) error {
return UnscopedDb().Model(m).Updates(values).Error
}
// BeforeCreate creates a random UID if needed before inserting a new row to the database.
func (m *Session) BeforeCreate(scope *gorm.Scope) error {
if rnd.InvalidRefID(m.RefID) {
m.RefID = rnd.RefID(SessionPrefix)
Log("session", "set ref id", scope.SetColumn("RefID", m.RefID))
}
if rnd.IsSessionID(m.ID) {
return nil
}
m.Regenerate()
return scope.SetColumn("ID", m.ID)
}
// SetClient updates the client of this session.
func (m *Session) SetClient(c *Client) *Session {
if c == nil {
return m
}
m.client = c
m.ClientUID = c.GetUID()
m.ClientName = c.ClientName
m.AuthProvider = c.Provider().String()
m.AuthMethod = c.Method().String()
m.AuthScope = c.Scope()
m.SetUser(c.User())
return m
}
// SetClientName changes the session's client name.
func (m *Session) SetClientName(s string) *Session {
if s == "" {
return m
}
m.ClientName = clean.Name(s)
return m
}
// Client returns the session's client.
func (m *Session) Client() *Client {
if m == nil {
return &Client{}
} else if m.client != nil {
return m.client
} else if c := FindClientByUID(m.ClientUID); c != nil {
m.SetClient(c)
return m.client
}
return &Client{
UserUID: m.UserUID,
UserName: m.UserName,
ClientUID: m.ClientUID,
ClientName: m.ClientName,
ClientRole: m.ClientRole().String(),
AuthScope: m.Scope(),
AuthMethod: m.AuthMethod,
}
}
// ClientRole returns the session's client ACL role.
func (m *Session) ClientRole() acl.Role {
if m.HasClient() {
return m.Client().AclRole()
} else if m.IsClient() {
return acl.RoleClient
}
return acl.RoleNone
}
// ClientInfo returns the session's client identifier string.
func (m *Session) ClientInfo() string {
if m.HasClient() {
return m.Client().String()
} else if m.ClientName != "" {
return m.ClientName
}
return report.NotAssigned
}
// HasClient checks if a client entity is assigned to the session.
func (m *Session) HasClient() bool {
if m == nil {
return false
}
return m.ClientUID != ""
}
// NoClient if this session has no client entity assigned.
func (m *Session) NoClient() bool {
return !m.HasClient()
}
// IsClient checks if this session authenticates an API client.
func (m *Session) IsClient() bool {
return authn.Provider(m.AuthProvider).IsClient()
}
// User returns the session's user entity.
func (m *Session) User() *User {
if m == nil {
return &User{}
} else if m.user != nil {
return m.user
} else if m.UserUID == "" {
return &User{}
}
if u := FindUserByUID(m.UserUID); u != nil {
m.SetUser(u)
return m.user
}
return &User{}
}
// UserRole returns the session's user ACL role.
func (m *Session) UserRole() acl.Role {
return m.User().AclRole()
}
// UserInfo returns the session's user information.
func (m *Session) UserInfo() string {
name := m.Username()
if name != "" {
return name
}
return m.UserRole().String()
}
// SetUser updates the user entity of this session.
func (m *Session) SetUser(u *User) *Session {
if u == nil {
return m
}
// Update user.
m.user = u
m.UserUID = u.UserUID
m.UserName = u.UserName
// Update tokens.
m.SetPreviewToken(u.PreviewToken)
m.SetDownloadToken(u.DownloadToken)
return m
}
// HasUser checks if a user entity is assigned to the session.
func (m *Session) HasUser() bool {
if m == nil {
return false
}
return m.UserUID != ""
}
// NoUser checks if this session has no user entity assigned.
func (m *Session) NoUser() bool {
return !m.HasUser()
}
// RefreshUser updates the cached user data.
func (m *Session) RefreshUser() *Session {
// Remove user if uid is nil.
if m.UserUID == "" {
m.user = nil
return m
}
// Fetch matching record.
if u := FindUserByUID(m.UserUID); u != nil {
m.SetUser(u)
}
return m
}
// Username returns the login name.
func (m *Session) Username() string {
return m.UserName
}
// AuthInfo returns information about the authentication type.
func (m *Session) AuthInfo() string {
provider := m.Provider()
method := m.Method()
if method.IsDefault() {
return provider.Pretty()
}
return fmt.Sprintf("%s (%s)", provider.Pretty(), method.Pretty())
}
// SetAuthID sets a custom authentication identifier.
func (m *Session) SetAuthID(id string) *Session {
if id == "" {
return m
}
m.AuthID = clean.Auth(id)
return m
}
// Provider returns the authentication provider.
func (m *Session) Provider() authn.ProviderType {
return authn.Provider(m.AuthProvider)
}
// SetProvider updates the session's authentication provider.
func (m *Session) SetProvider(provider authn.ProviderType) *Session {
if provider == "" {
return m
}
m.AuthProvider = provider.String()
return m
}
// Method returns the authentication method.
func (m *Session) Method() authn.MethodType {
return authn.Method(m.AuthMethod)
}
// Is2FA checks if 2-Factor Authentication (2FA) was used to log in.
func (m *Session) Is2FA() bool {
return m.Method().Is(authn.Method2FA)
}
// SetMethod sets a custom authentication method.
func (m *Session) SetMethod(method authn.MethodType) *Session {
if method == "" {
return m
}
m.AuthMethod = method.String()
return m
}
// Scope returns the authorization scope as a sanitized string.
func (m *Session) Scope() string {
return clean.Scope(m.AuthScope)
}
// ValidateScope checks if the scope does not exclude access to specified resource.
func (m *Session) ValidateScope(resource acl.Resource, perms acl.Permissions) bool {
// Get scope string.
scope := m.Scope()
// Skip detailed check and allow all if scope is "*".
if scope == list.All {
return true
}
// Skip resource check if scope includes all read operations.
if scope == acl.ScopeRead.String() {
return !acl.GrantScopeRead.DenyAny(perms)
}
// Parse scope to check for resources and permissions.
attr := list.ParseAttr(scope)
// Check if resource is within scope.
if granted := attr.Contains(resource.String()); !granted {
return false
}
// Check if permission is within scope.
if len(perms) == 0 {
return true
}
// Check if scope is limited to read or write operations.
if a := attr.Find(acl.ScopeRead.String()); a.Value == list.True && acl.GrantScopeRead.DenyAny(perms) {
return false
} else if a = attr.Find(acl.ScopeWrite.String()); a.Value == list.True && acl.GrantScopeWrite.DenyAny(perms) {
return false
}
return true
}
// InsufficientScope checks if the scope does not include access to specified resource.
func (m *Session) InsufficientScope(resource acl.Resource, perms acl.Permissions) bool {
return !m.ValidateScope(resource, perms)
}
// SetScope sets a custom authentication scope.
func (m *Session) SetScope(scope string) *Session {
if scope == "" {
return m
}
m.AuthScope = clean.Scope(scope)
return m
}
// AuthGrantType returns the session's grant type as authn.GrantType.
func (m *Session) AuthGrantType() authn.GrantType {
return authn.Grant(m.GrantType)
}
// SetGrantType sets the session's grant type if no type has been set yet.
func (m *Session) SetGrantType(t authn.GrantType) *Session {
if t.IsUndefined() || m.GrantType != "" {
return m
}
m.GrantType = t.String()
return m
}
// ChangePassword changes the password of the current user.
func (m *Session) ChangePassword(newPw string) (err error) {
u := m.User()
if u == nil {
return fmt.Errorf("unknown user")
}
// Change password.
err = u.SetPassword(newPw)
m.SetPreviewToken(u.PreviewToken)
m.SetDownloadToken(u.DownloadToken)
return nil
}
// SetPreviewToken updates the preview token if not empty.
func (m *Session) SetPreviewToken(token string) *Session {
if m.ID == "" {
return m
}
if token != "" {
m.PreviewToken = token
PreviewToken.Set(token, m.ID)
} else if m.PreviewToken == "" {
m.PreviewToken = GenerateToken()
PreviewToken.Set(token, m.ID)
}
return m
}
// SetDownloadToken updates the download token if not empty.
func (m *Session) SetDownloadToken(token string) *Session {
if m.ID == "" {
return m
}
if token != "" {
m.DownloadToken = token
DownloadToken.Set(token, m.ID)
} else if m.DownloadToken == "" {
m.DownloadToken = GenerateToken()
DownloadToken.Set(token, m.ID)
}
return m
}
// Data returns the session's data.
func (m *Session) Data() (data *SessionData) {
if m.data != nil {
data = m.data
}
data = NewSessionData()
if len(m.DataJSON) == 0 {
return data
} else if err := json.Unmarshal(m.DataJSON, data); err != nil {
log.Errorf("failed parsing session json: %s", err)
} else {
data.RefreshShares()
m.data = data
}
return data
}
// SetData updates the session's data.
func (m *Session) SetData(data *SessionData) *Session {
if data == nil {
log.Debugf("auth: empty data passed to session %s", m.RefID)
return m
}
// Refresh session data.
data.RefreshShares()
if j, err := json.Marshal(data); err != nil {
log.Debugf("auth: %s", err)
} else {
m.DataJSON = j
}
m.data = data
return m
}
// SetContext sets the session request context.
func (m *Session) SetContext(c *gin.Context) *Session {
if c == nil || m == nil {
return &Session{}
}
// Set client ip address from request context.
if clientIp := header.ClientIP(c); clientIp != "" {
m.SetClientIP(clientIp)
} else if m.ClientIP == "" {
// Unit tests often do not set a client IP.
m.SetClientIP(UnknownIP)
}
// Set client user agent from request context.
if ua := header.UserAgent(c); ua != "" {
m.SetUserAgent(ua)
}
return m
}
// UpdateContext sets the session request context and updates the session entry in the database if it has changed.
func (m *Session) UpdateContext(c *gin.Context) *Session {
if c == nil || m == nil {
return &Session{}
}
changed := false
// Set client ip address from request context.
if clientIp := header.ClientIP(c); clientIp != "" && (clientIp != m.ClientIP || m.LoginIP == "") {
m.SetClientIP(clientIp)
changed = true
} else if m.ClientIP == "" {
// Unit tests often do not set a client IP.
m.SetClientIP(UnknownIP)
changed = true
}
// Set client user agent from request context.
if ua := header.UserAgent(c); ua != "" && ua != m.UserAgent {
m.SetUserAgent(ua)
changed = true
}
if !changed {
return m
} else if err := m.Save(); err != nil {
log.Debugf("auth: %s while updating session context", err)
}
return m
}
// IsVisitor checks if the session belongs to a sharing link visitor.
func (m *Session) IsVisitor() bool {
return m.User().IsVisitor()
}
// IsSuperAdmin checks if the session belongs to a registered super admin user.
func (m *Session) IsSuperAdmin() bool {
if !m.IsRegistered() {
return false
}
return m.User().IsSuperAdmin()
}
// IsRegistered checks if the session belongs to a registered user account.
func (m *Session) IsRegistered() bool {
if m == nil || m.user == nil || rnd.InvalidUID(m.UserUID, UserUID) {
return false
}
return m.User().IsRegistered()
}
// NotRegistered checks if the user is not registered with an own account.
func (m *Session) NotRegistered() bool {
return !m.IsRegistered()
}
// NoShares checks if the session has no shares yet.
func (m *Session) NoShares() bool {
return !m.HasShares()
}
// HasShares checks if the session has any shares.
func (m *Session) HasShares() bool {
if user := m.User(); user.IsRegistered() {
return user.HasShares()
} else if data := m.Data(); data == nil {
return false
} else {
return data.HasShares()
}
}
// HasRegisteredUser checks if the session belongs to a registered user.
func (m *Session) HasRegisteredUser() bool {
if !m.HasUser() {
return false
}
return m.User().IsRegistered()
}
// HasShare if the session includes the specified share
func (m *Session) HasShare(uid string) bool {
if user := m.User(); user.IsRegistered() {
return user.HasShare(uid)
} else if data := m.Data(); data == nil {
return false
} else {
return data.HasShare(uid)
}
}
// SharedUIDs returns shared entity UIDs.
func (m *Session) SharedUIDs() UIDs {
if user := m.User(); user.IsRegistered() {
return user.SharedUIDs()
} else if data := m.Data(); data == nil {
return UIDs{}
} else {
return data.SharedUIDs()
}
}
// RedeemToken updates shared entity UIDs using the specified token.
func (m *Session) RedeemToken(token string) (n int) {
if user := m.User(); user.IsRegistered() {
return user.RedeemToken(token)
} else if data := m.Data(); data == nil {
return 0
} else {
return data.RedeemToken(token)
}
}
// Expires sets an explicit expiration time.
func (m *Session) Expires(t time.Time) *Session {
if t.IsZero() {
return m
}
m.SessExpires = t.Unix()
return m
}
// ExpiresAt returns the time when the session expires.
func (m *Session) ExpiresAt() time.Time {
if m.SessExpires <= 0 {
return time.Time{}
}
return time.Unix(m.SessExpires, 0)
}
// ExpiresIn returns the expiration time in seconds.
func (m *Session) ExpiresIn() int64 {
if m.SessExpires <= 0 {
return 0
}
return m.SessExpires - unix.Now()
}
// SetExpiresIn sets the session lifetime in seconds (-1 for infinite).
func (m *Session) SetExpiresIn(expiresIn int64) *Session {
if expiresIn < 0 {
m.SessExpires = -1
} else if expiresIn > 0 {
m.SessExpires = unix.Now() + expiresIn
}
return m
}
// SetTimeout sets the session idle time in seconds (-1 for infinite).
func (m *Session) SetTimeout(timeout int64) *Session {
if timeout < 0 {
m.SessTimeout = -1
} else if timeout > 0 {
m.SessTimeout = timeout
}
return m
}
// Expired checks if the session has expired.
func (m *Session) Expired() bool {
if m.SessExpires <= 0 {
return m.TimedOut()
} else if at := m.ExpiresAt(); at.IsZero() {
return false
} else {
return at.Before(UTC())
}
}
// TimeoutAt returns the time at which the session will expire due to inactivity.
func (m *Session) TimeoutAt() time.Time {
if m.SessTimeout <= 0 || m.LastActive <= 0 {
return m.ExpiresAt()
} else if t := m.LastActive + m.SessTimeout; t <= m.SessExpires || m.SessExpires <= 0 {
return time.Unix(m.LastActive+m.SessTimeout, 0)
} else {
return m.ExpiresAt()
}
}
// TimedOut checks if the session has expired due to inactivity..
func (m *Session) TimedOut() bool {
if at := m.TimeoutAt(); at.IsZero() {
return false
} else {
return at.Before(UTC())
}
}
// UpdateLastActive sets the time of last activity to now and optionally also updates the auth_sessions table.
func (m *Session) UpdateLastActive(save bool) *Session {
if m == nil {
return &Session{}
} else if m.Invalid() || m.ID == "" {
return m
}
// Set time of last activity to now (Unix timestamp).
m.LastActive = unix.Now()
// Update activity timestamp of this session in the auth_sessions table.
if !save {
return m
} else if err := Db().Model(m).UpdateColumn("last_active", m.LastActive).Error; err != nil {
event.AuditWarn([]string{m.IP(), "session %s", "failed to update activity timestamp", "%s"}, m.RefID, err)
}
// Update the activity timestamp of the parent session, if any.
if m.Method().IsNot(authn.MethodSession) || m.AuthID == "" || m.AuthID == m.ID {
return m
} else if err := Db().Table(Session{}.TableName()).Where("id = ?", m.AuthID).UpdateColumn("last_active", m.LastActive).Error; err != nil {
event.AuditWarn([]string{m.IP(), "session %s", "failed to update activity timestamp of parent session", "%s"}, m.RefID, err)
}
return m
}
// Invalid checks if the session does not belong to a registered user or a visitor with shares.
func (m *Session) Invalid() bool {
return !m.Valid()
}
// Valid checks whether the session belongs to a registered user or a visitor with shares.
func (m *Session) Valid() bool {
if m.IsClient() {
return true
}
return m.User().IsRegistered() || m.IsVisitor() && m.HasShares()
}
// Abort aborts the request with the appropriate error code if access to the requested resource is denied.
func (m *Session) Abort(c *gin.Context) bool {
if m.Valid() {
return false
}
// Abort the request with the appropriate HTTP error code and message.
switch m.Status {
case http.StatusUnauthorized:
c.AbortWithStatusJSON(m.Status, i18n.NewResponse(m.Status, i18n.ErrUnauthorized))
case http.StatusTooManyRequests:
c.AbortWithStatusJSON(m.Status, gin.H{"error": "rate limit exceeded", "code": http.StatusTooManyRequests})
default:
c.AbortWithStatusJSON(http.StatusForbidden, i18n.NewResponse(http.StatusForbidden, i18n.ErrForbidden))
}
return true
}
// SetUserAgent sets the client user agent.
func (m *Session) SetUserAgent(ua string) {
if m == nil || ua == "" {
return
} else if ua = txt.Clip(ua, 512); ua == "" {
return
} else if m.UserAgent != "" && m.UserAgent != ua {
event.AuditWarn([]string{m.IP(), "session %s", "user agent has changed from %s to %s"}, m.RefID, clean.LogQuote(m.UserAgent), clean.LogQuote(ua))
}
m.UserAgent = ua
return
}
// SetClientIP sets the client IP address.
func (m *Session) SetClientIP(ip string) {
if m == nil || ip == "" {
return
} else if parsed := net.ParseIP(ip); parsed == nil {
return
} else if ip = parsed.String(); ip == "" {
return
} else if m.ClientIP != "" && m.ClientIP != ip {
event.AuditWarn([]string{ip, "session %s", "client address has changed from %s to %s"}, m.RefID, clean.LogQuote(m.ClientIP), clean.LogQuote(ip))
}
m.ClientIP = ip
if m.LoginIP == "" {
m.LoginIP = ip
m.LoginAt = TimeStamp()
}
return
}
// IP returns the client IP address, or "unknown" if it is unknown.
func (m *Session) IP() string {
if m.ClientIP != "" {
return m.ClientIP
} else {
return UnknownIP
}
}
// HttpStatus returns the session status as HTTP code for the client.
func (m *Session) HttpStatus() int {
if m.Status > 0 {
return m.Status
} else if m.Valid() {
return http.StatusOK
}
return http.StatusUnauthorized
}