// Copyright (c) 2024 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package protocol56import (7"cmp"8"slices"9)1011// 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-L3512func (u *User) GetSSOEmail() string {13var ssoIdentities []*Identity14for _, id := range u.Identities {15// LastSigninTime is empty for non-SSO identities, and used as a filter here.16if id == nil || id.Deleted || id.LastSigninTime == "" {17continue18}19ssoIdentities = append(ssoIdentities, id)20}21if len(ssoIdentities) == 0 {22return ""23}2425// We are looking for the latest-used SSO identity.26slices.SortFunc(ssoIdentities, func(i, j *Identity) int {27return cmp.Compare(j.LastSigninTime, i.LastSigninTime)28})29return ssoIdentities[0].PrimaryEmail30}3132// GetRandomEmail returns an email address of any of the user's identities.33func (u *User) GetRandomEmail() string {34for _, id := range u.Identities {35if id == nil || id.Deleted || id.PrimaryEmail == "" {36continue37}38return id.PrimaryEmail39}40return ""41}424344