@@ -18,6 +18,11 @@ import (
1818 "k8s.io/klog/v2"
1919)
2020
21+ const (
22+ localIdentityProviderID = "zitadel-local"
23+ localIdentityProviderName = "Email"
24+ )
25+
2126type SDKConfig struct {
2227 // Domain is the ZITADEL gRPC/management host (host only, no scheme or path),
2328 // e.g. "auth.example.com".
@@ -230,6 +235,56 @@ func (c *SDKClient) ListIDPLinks(ctx context.Context, userID string) ([]IDPLink,
230235 IDPUserName : link .GetUserName (),
231236 })
232237 }
238+
239+ if len (out ) == 0 {
240+ localIdentity , err := c .getLocalIdentityLink (ctx , userID )
241+ if err != nil {
242+ klog .Errorf ("ListIDPLinks: failed to build local identity fallback for userID=%q: %v" , userID , err )
243+ return nil , err
244+ }
245+ if localIdentity != nil {
246+ out = append (out , * localIdentity )
247+ }
248+ }
233249 klog .V (2 ).Infof ("ListIDPLinks: found %d IDP link(s) for userID=%q" , len (out ), userID )
234250 return out , nil
235251}
252+
253+ func (c * SDKClient ) getLocalIdentityLink (ctx context.Context , userID string ) (* IDPLink , error ) {
254+ resp , err := c .user .GetUserByID (ctx , & userv2.GetUserByIDRequest {UserId : userID })
255+ if err != nil {
256+ return nil , err
257+ }
258+
259+ user := resp .GetUser ()
260+ if user == nil {
261+ return nil , nil
262+ }
263+
264+ username := localIdentityUsername (user )
265+ if username == "" {
266+ return nil , nil
267+ }
268+
269+ return & IDPLink {
270+ IDPID : localIdentityProviderID ,
271+ IDPName : localIdentityProviderName ,
272+ UserID : user .GetUserId (),
273+ IDPUserName : username ,
274+ }, nil
275+ }
276+
277+ func localIdentityUsername (user * userv2.User ) string {
278+ if user == nil {
279+ return ""
280+ }
281+ if preferred := strings .TrimSpace (user .GetPreferredLoginName ()); preferred != "" {
282+ return preferred
283+ }
284+ for _ , loginName := range user .GetLoginNames () {
285+ if loginName = strings .TrimSpace (loginName ); loginName != "" {
286+ return loginName
287+ }
288+ }
289+ return strings .TrimSpace (user .GetUsername ())
290+ }
0 commit comments