diff --git a/app/auth0.go b/app/auth0.go index 5c4fcf8..9325ece 100644 --- a/app/auth0.go +++ b/app/auth0.go @@ -8,10 +8,38 @@ import ( "strings" "time" + "code.tnxs.net/taxnexus/lib/api/auth0/auth0_client/auth" "code.tnxs.net/taxnexus/lib/api/auth0/auth0_client/user" "code.tnxs.net/taxnexus/lib/api/auth0/auth0_models" ) +var auth0AuthToken string +var auth0Expires time.Time + +// GetAuth0AuthToken is a func +func GetAuth0AuthToken() string { + sugar.Infof("board.getAuth0AuthToken: 📥") + if auth0Expires.After(time.Now()) { + return auth0AuthToken + } + apiParams := auth.NewPostCredentialsParamsWithTimeout(getTimeout) + apiParams.CredentialsRequest = &auth0_models.CredentialsRequest{ + ClientID: GetServiceAccount("auth0").ClientID, + ClientSecret: GetServiceAccount("auth0").ClientSecret, + Audience: "https://taxnexus.auth0.com/api/v2/", + GrantType: "client_credentials", + } + response, err := auth0Client.Auth.PostCredentials(apiParams) + if err != nil { + sugar.Errorf("board.getAuth0AuthToken: 💣 ⛔ : %w", err) + } + auth0Expires = time.Now().Add(time.Duration(response.Payload.ExpiresIn) * time.Second) + auth0AuthToken = response.Payload.AccessToken + sugar.Debugf("board.getAuth0AuthToken: 📏 new token: %s", auth0AuthToken) + sugar.Infof("board.getAuth0AuthToken: 👍 📤 new token retrieved") + return auth0AuthToken +} + // GetAuth0UserByEmail looks up Auth0 user by email func GetAuth0UserByEmail(email string) (string, error) { sugar.Info("app.GetAuth0UserByEmail: 📥") diff --git a/rules/auth0.go b/rules/auth0.go index 9d274a8..0113829 100644 --- a/rules/auth0.go +++ b/rules/auth0.go @@ -12,7 +12,7 @@ import ( ) // StoreAuth0UserActivity creates a new Auth0 user tied to a User record -func StoreAuth0UserActivity(ctx workflow.Context, w app.UserChannelWrapper) error { +func StoreAuth0UserActivity(ctx workflow.Context, w app.UserChannelWrapper) error { //nolint:gocritic // don't care auth0UserID, err := app.GetAuth0UserByEmail(w.Obj.Email) if err != nil { return err diff --git a/rules/user.go b/rules/user.go index 96796a6..c2c74a3 100644 --- a/rules/user.go +++ b/rules/user.go @@ -6,6 +6,7 @@ import ( "fmt" "html/template" + "code.tnxs.net/taxnexus/lib/api/auth0/auth0_models" "code.tnxs.net/taxnexus/lib/api/devops/devops_client/user" "code.tnxs.net/taxnexus/lib/api/devops/devops_models" @@ -114,3 +115,34 @@ Alert! New User Inquiry from Taxnexus.io website. sugar.Info("workflow.notifyUser: 👍 📤") return nil } + +// EnrichUsers synchronizes contact sources +func EnrichUsers(m *devops_models.User, r *auth0_models.User) (*devops_models.User, *auth0_models.User) { //nolint:gocritic,lll // shut up! + // parse first/last name + nm := *m + nr := *r + nm.LastIP = r.LastIP + nm.LastLogin = r.LastLogin + nm.LoginCount = r.LoginsCount + nm.Auth0UserID = r.UserID + + // enrich taxnexus user (m->nm) + if m.CommunityNickname == "" && r.Nickname != "" { + nm.CommunityNickname = r.Nickname + } + if m.Email == "" && r.Email != "" { + nm.Email = r.Email + } + if m.SmallPhotoURL == "" && r.Picture != "" { + nm.SmallPhotoURL = r.Picture + } + // enrich auth0 user (r->nr) + if r.Nickname == "" && m.CommunityNickname != "" { + nr.Nickname = m.CommunityNickname + } + if r.Picture == "" && m.SmallPhotoURL != "" { + nr.Picture = m.SmallPhotoURL + } + + return &nm, &nr +}