more consolidation

v0.1.13 v0.1.13
Vernon Keenan 2021-01-18 11:46:19 -08:00
parent cda4c20de9
commit f5aa06ed82
3 changed files with 61 additions and 1 deletions

View File

@ -8,10 +8,38 @@ import (
"strings" "strings"
"time" "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_client/user"
"code.tnxs.net/taxnexus/lib/api/auth0/auth0_models" "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 // GetAuth0UserByEmail looks up Auth0 user by email
func GetAuth0UserByEmail(email string) (string, error) { func GetAuth0UserByEmail(email string) (string, error) {
sugar.Info("app.GetAuth0UserByEmail: 📥") sugar.Info("app.GetAuth0UserByEmail: 📥")

View File

@ -12,7 +12,7 @@ import (
) )
// StoreAuth0UserActivity creates a new Auth0 user tied to a User record // 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) auth0UserID, err := app.GetAuth0UserByEmail(w.Obj.Email)
if err != nil { if err != nil {
return err return err

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"html/template" "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_client/user"
"code.tnxs.net/taxnexus/lib/api/devops/devops_models" "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: 👍 📤") sugar.Info("workflow.notifyUser: 👍 📤")
return nil 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
}