Skip to content

Commit f310f48

Browse files
committed
start handling external email identifiers
1 parent 363108d commit f310f48

3 files changed

Lines changed: 39 additions & 16 deletions

File tree

‎.secrets.baseline‎

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/auth/auth.go‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,7 @@ func (a *Auth) applySignUpExternal(context storage.TransactionContext, supported
364364
return nil, errors.WrapErrorAction(logutils.ActionRegister, model.TypeAccount, nil, err)
365365
}
366366

367-
externalAuthTypeID := account.AuthTypes[0].ID
368367
for i, id := range account.Identifiers {
369-
account.Identifiers[i].AccountAuthTypeID = &externalAuthTypeID
370-
371368
primary := (id.Identifier == externalUser.Identifier)
372369
account.Identifiers[i].Primary = &primary
373370
}
@@ -391,10 +388,7 @@ func (a *Auth) applySignUpAdminExternal(context storage.TransactionContext, auth
391388
return nil, errors.WrapErrorAction(logutils.ActionRegister, "admin account", nil, err)
392389
}
393390

394-
externalAuthTypeID := account.AuthTypes[0].ID
395391
for i, id := range account.Identifiers {
396-
account.Identifiers[i].AccountAuthTypeID = &externalAuthTypeID
397-
398392
primary := (id.Identifier == externalUser.Identifier)
399393
account.Identifiers[i].Primary = &primary
400394
}
@@ -440,12 +434,17 @@ func (a *Auth) prepareExternalUserData(authType model.AuthType, appOrg model.App
440434
params := map[string]interface{}{"user": externalUser}
441435

442436
//3. create the account identifiers
437+
now := time.Now().UTC()
443438
accountID := uuid.NewString()
444439
accountIdentifiers := make([]model.AccountIdentifier, 0)
445440
for k, v := range externalUser.ExternalIDs {
446441
primary := (v == externalUser.Identifier)
447442
accountIdentifiers = append(accountIdentifiers, model.AccountIdentifier{ID: uuid.NewString(), Code: k, Identifier: v,
448-
Verified: true, Primary: &primary, Account: model.Account{ID: accountID}, DateCreated: time.Now().UTC()})
443+
Verified: true, Primary: &primary, Account: model.Account{ID: accountID}, DateCreated: now})
444+
}
445+
if externalUser.Email != "" {
446+
accountIdentifiers = append(accountIdentifiers, model.AccountIdentifier{ID: uuid.NewString(), Code: IdentifierTypeEmail, Identifier: externalUser.Email,
447+
Account: model.Account{ID: accountID}, DateCreated: now})
449448
}
450449

451450
return accountIdentifiers, params, &profile, preferences, nil
@@ -515,6 +514,7 @@ func (a *Auth) updateExternalUserIfNeeded(accountAuthType model.AccountAuthType,
515514
now := time.Now().UTC()
516515
accountAuthType.DateUpdated = &now
517516

517+
//TODO: make sure external identifiers get updated in storage and in account memory
518518
transaction := func(context storage.TransactionContext) error {
519519
//1. first find the account record
520520
account, err := a.storage.FindAccountByAuthTypeID(context, accountAuthType.ID)
@@ -2559,12 +2559,13 @@ func (a *Auth) updateExternalAccountGroups(account *model.Account, newExternalGr
25592559

25602560
func (a *Auth) updateExternalIdentifiers(account *model.Account, accountAuthTypeID string, externalUser *model.ExternalSystemUser, linked bool) bool {
25612561
updated := false
2562+
now := time.Now().UTC()
25622563
for k, v := range externalUser.ExternalIDs {
25632564
accountIdentifier := account.GetAccountIdentifier(k, "")
25642565
if accountIdentifier == nil {
25652566
primary := (v == externalUser.Identifier)
25662567
newIdentifier := model.AccountIdentifier{ID: uuid.NewString(), Code: k, Identifier: v, Verified: true, Linked: linked, AccountAuthTypeID: &accountAuthTypeID,
2567-
Primary: &primary, Account: model.Account{ID: account.ID}, DateCreated: time.Now().UTC()}
2568+
Primary: &primary, Account: model.Account{ID: account.ID}, DateCreated: now}
25682569
account.Identifiers = append(account.Identifiers, newIdentifier)
25692570
updated = true
25702571
} else if accountIdentifier.Identifier != v {
@@ -2575,6 +2576,25 @@ func (a *Auth) updateExternalIdentifiers(account *model.Account, accountAuthType
25752576
}
25762577
}
25772578

2579+
if externalUser.Email != "" {
2580+
hasExternalEmail := false
2581+
for i, identifier := range account.Identifiers {
2582+
if identifier.AccountAuthTypeID != nil && *identifier.AccountAuthTypeID == accountAuthTypeID && identifier.Code == IdentifierTypeEmail {
2583+
hasExternalEmail = true
2584+
if identifier.Identifier != externalUser.Email {
2585+
account.Identifiers[i].Identifier = externalUser.Email
2586+
updated = true
2587+
break
2588+
}
2589+
}
2590+
}
2591+
if !hasExternalEmail {
2592+
primary := (externalUser.Email == externalUser.Identifier)
2593+
account.Identifiers = append(account.Identifiers, model.AccountIdentifier{ID: uuid.NewString(), Code: IdentifierTypeEmail, Identifier: externalUser.Email,
2594+
Linked: linked, Sensitive: true, AccountAuthTypeID: &accountAuthTypeID, Primary: &primary, Account: model.Account{ID: account.ID}, DateCreated: now})
2595+
}
2596+
}
2597+
25782598
return updated
25792599
}
25802600

‎driver/web/conversions_user.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,18 @@ func accountAuthTypesToDefLegacy(account *model.Account) []Def.AccountAuthType {
144144
code := id.Code
145145
identifier := id.Identifier
146146
legacyAat := resAat
147-
if utils.Contains(aat.SupportedAuthType.AuthType.Aliases, id.Code) {
147+
if id.AccountAuthTypeID != nil && *id.AccountAuthTypeID == aat.ID {
148+
legacyAat.Identifier = &identifier // old clients expect the identifier in the auth types
148149

150+
aats = append(aats, legacyAat)
151+
addedLegacy = true
152+
} else if utils.Contains(aat.SupportedAuthType.AuthType.Aliases, id.Code) {
149153
if code == "phone" {
150154
code = "twilio_" + code
151155
}
152156
legacyAat.Code = &code // old clients will not understand new auth type codes
153157
legacyAat.Identifier = &identifier // old clients expect the identifier in the auth types
154158

155-
aats = append(aats, legacyAat)
156-
addedLegacy = true
157-
} else if id.AccountAuthTypeID != nil && *id.AccountAuthTypeID == aat.ID {
158-
legacyAat.Identifier = &identifier // old clients expect the identifier in the auth types
159-
160159
aats = append(aats, legacyAat)
161160
addedLegacy = true
162161
}

0 commit comments

Comments
 (0)