/
listing.go
149 lines (126 loc) · 4.17 KB
/
listing.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
package sessionlisting
import (
"sort"
"time"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/oauth"
"github.com/authgear/authgear-server/pkg/lib/session"
"github.com/authgear/authgear-server/pkg/lib/session/idpsession"
"github.com/authgear/authgear-server/pkg/util/setutil"
)
//go:generate mockgen -source=listing.go -destination=listing_mock_test.go -package sessionlisting_test
// Session in the sessionlisting package wrapped the model.Session to provide
// extra information for internal display
type Session struct {
*model.Session
// IsDevice has a different meaning for IDP session and offline grant
// For IDP session, IsDevice is true only if it has active sso enabled offline grant.
// So it may change.
// All offline grant's IsDevice is true.
IsDevice bool `json:"-"`
// IsCurrent indicates if the session is current session
IsCurrent bool `json:"-"`
}
type IDPSessionProvider interface {
CheckSessionExpired(session *idpsession.IDPSession) (expired bool)
}
type OfflineGrantService interface {
CheckSessionExpired(session *oauth.OfflineGrant) (bool, time.Time, error)
}
type SessionListingService struct {
OAuthConfig *config.OAuthConfig
IDPSessions IDPSessionProvider
OfflineGrants OfflineGrantService
}
func (s *SessionListingService) FilterForDisplay(sessions []session.Session, currentSession session.Session) ([]*Session, error) {
sess := make([]session.Session, len(sessions))
copy(sess, sessions)
sortSessions(sess)
offlineGrants := []*oauth.OfflineGrant{}
idpSessions := []*idpsession.IDPSession{}
for _, ses := range sess {
if offlineGrant, ok := ses.(*oauth.OfflineGrant); ok {
offlineGrants = append(offlineGrants, offlineGrant)
} else if idpSession, ok := ses.(*idpsession.IDPSession); ok {
idpSessions = append(idpSessions, idpSession)
}
}
// construct third-party app client id set
thirdPartyClientIDSet := make(setutil.Set[string])
for _, c := range s.OAuthConfig.Clients {
if c.IsThirdParty() {
thirdPartyClientIDSet[c.ClientID] = struct{}{}
}
}
result := []*Session{}
idpSessionToDisplayNameMap := map[string]string{}
for _, offlineGrant := range offlineGrants {
// remove third-party app refresh token
if _, ok := thirdPartyClientIDSet[offlineGrant.ClientID]; ok {
continue
}
expired, _, err := s.OfflineGrants.CheckSessionExpired(offlineGrant)
if err != nil {
return nil, err
}
if expired {
continue
}
apiModel := &Session{
Session: offlineGrant.ToAPIModel(),
IsDevice: true,
}
// construct a map for replacing idp session's display name in the SSO group
if offlineGrant.SSOGroupIDPSessionID() != "" {
if _, ok := idpSessionToDisplayNameMap[offlineGrant.SSOGroupIDPSessionID()]; !ok {
idpSessionToDisplayNameMap[offlineGrant.SSOGroupIDPSessionID()] = apiModel.DisplayName
}
continue
}
if currentSession != nil {
apiModel.IsCurrent = offlineGrant.IsSameSSOGroup(currentSession)
}
result = append(result, apiModel)
}
for _, idpSession := range idpSessions {
expired := s.IDPSessions.CheckSessionExpired(idpSession)
if expired {
continue
}
// replace idp session display name with the last accessed refresh token's display name
apiModel := &Session{
Session: idpSession.ToAPIModel(),
}
if displayName, ok := idpSessionToDisplayNameMap[idpSession.ID]; ok {
apiModel.DisplayName = displayName
apiModel.IsDevice = true
}
if currentSession != nil {
apiModel.IsCurrent = idpSession.IsSameSSOGroup(currentSession)
}
result = append(result, apiModel)
}
sortSessionModels(result)
return result, nil
}
func sortSessions(sessions []session.Session) {
sort.Slice(sessions, func(i, j int) bool {
a := time.Time{}
if accessInfo := sessions[i].GetAccessInfo(); accessInfo != nil {
a = accessInfo.LastAccess.Timestamp
}
b := time.Time{}
if accessInfo := sessions[j].GetAccessInfo(); accessInfo != nil {
b = accessInfo.LastAccess.Timestamp
}
return a.After(b)
})
}
func sortSessionModels(sessions []*Session) {
sort.Slice(sessions, func(i, j int) bool {
a := sessions[i]
b := sessions[j]
return a.LastAccessedAt.After(b.LastAccessedAt)
})
}