Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/go/user.go
2498 views
1
// Copyright (c) 2024 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package protocol
6
7
import (
8
"cmp"
9
"slices"
10
)
11
12
// GetSSOEmail returns the email of the user's last-used SSO identity, if any. It mirros the funcationality we have implemeted in TS here: https://github.com/gitpod-io/gitpod/blob/e4ccbf0b4d224714ffd16719a3b5c50630d6edbc/components/public-api/typescript-common/src/user-utils.ts#L24-L35
13
func (u *User) GetSSOEmail() string {
14
var ssoIdentities []*Identity
15
for _, id := range u.Identities {
16
// LastSigninTime is empty for non-SSO identities, and used as a filter here.
17
if id == nil || id.Deleted || id.LastSigninTime == "" {
18
continue
19
}
20
ssoIdentities = append(ssoIdentities, id)
21
}
22
if len(ssoIdentities) == 0 {
23
return ""
24
}
25
26
// We are looking for the latest-used SSO identity.
27
slices.SortFunc(ssoIdentities, func(i, j *Identity) int {
28
return cmp.Compare(j.LastSigninTime, i.LastSigninTime)
29
})
30
return ssoIdentities[0].PrimaryEmail
31
}
32
33
// GetRandomEmail returns an email address of any of the user's identities.
34
func (u *User) GetRandomEmail() string {
35
for _, id := range u.Identities {
36
if id == nil || id.Deleted || id.PrimaryEmail == "" {
37
continue
38
}
39
return id.PrimaryEmail
40
}
41
return ""
42
}
43
44