2022-01-24 11:21:30 +01:00
|
|
|
package idp
|
|
|
|
|
|
|
|
import (
|
2022-06-06 12:05:44 +02:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"context"
|
2022-01-24 11:21:30 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2022-05-05 08:58:34 +02:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2022-01-24 11:21:30 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2022-05-05 08:58:34 +02:00
|
|
|
|
2023-04-03 15:09:35 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
|
|
|
|
2022-05-05 08:58:34 +02:00
|
|
|
"github.com/golang-jwt/jwt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-24 11:21:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Auth0Manager auth0 manager client instance
|
|
|
|
type Auth0Manager struct {
|
|
|
|
authIssuer string
|
|
|
|
httpClient ManagerHTTPClient
|
|
|
|
credentials ManagerCredentials
|
|
|
|
helper ManagerHelper
|
2022-10-22 13:29:39 +02:00
|
|
|
appMetrics telemetry.AppMetrics
|
2022-01-24 11:21:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Auth0ClientConfig auth0 manager client configurations
|
|
|
|
type Auth0ClientConfig struct {
|
2022-03-09 17:22:47 +01:00
|
|
|
Audience string
|
2023-05-29 13:48:19 +02:00
|
|
|
AuthIssuer string
|
2022-03-09 17:22:47 +01:00
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
2023-05-29 13:48:19 +02:00
|
|
|
GrantType string
|
2022-03-09 17:22:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// auth0JWTRequest payload struct to request a JWT Token
|
|
|
|
type auth0JWTRequest struct {
|
|
|
|
Audience string `json:"audience"`
|
2022-01-24 11:21:30 +01:00
|
|
|
AuthIssuer string `json:"auth_issuer"`
|
2022-03-09 17:22:47 +01:00
|
|
|
ClientID string `json:"client_id"`
|
2022-01-24 11:21:30 +01:00
|
|
|
ClientSecret string `json:"client_secret"`
|
|
|
|
GrantType string `json:"grant_type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth0Credentials auth0 authentication information
|
|
|
|
type Auth0Credentials struct {
|
|
|
|
clientConfig Auth0ClientConfig
|
|
|
|
helper ManagerHelper
|
|
|
|
httpClient ManagerHTTPClient
|
|
|
|
jwtToken JWTToken
|
|
|
|
mux sync.Mutex
|
2022-10-22 13:29:39 +02:00
|
|
|
appMetrics telemetry.AppMetrics
|
2022-01-24 11:21:30 +01:00
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
// createUserRequest is a user create request
|
|
|
|
type createUserRequest struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
AppMeta AppMetadata `json:"app_metadata"`
|
|
|
|
Connection string `json:"connection"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
VerifyEmail bool `json:"verify_email"`
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:05:44 +02:00
|
|
|
// userExportJobRequest is a user export request struct
|
|
|
|
type userExportJobRequest struct {
|
|
|
|
Format string `json:"format"`
|
|
|
|
Fields []map[string]string `json:"fields"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// userExportJobResponse is a user export response struct
|
|
|
|
type userExportJobResponse struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
ConnectionID string `json:"connection_id"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
Connection string `json:"connection"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// userExportJobStatusResponse is a user export status response struct
|
|
|
|
type userExportJobStatusResponse struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
ConnectionID string `json:"connection_id"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
Location string `json:"location"`
|
|
|
|
Connection string `json:"connection"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
|
2023-07-03 12:20:19 +02:00
|
|
|
// userVerificationJobRequest is a user verification request struct
|
|
|
|
type userVerificationJobRequest struct {
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:05:44 +02:00
|
|
|
// auth0Profile represents an Auth0 user profile response
|
|
|
|
type auth0Profile struct {
|
2022-10-13 18:26:31 +02:00
|
|
|
AccountID string `json:"wt_account_id"`
|
|
|
|
PendingInvite bool `json:"wt_pending_invite"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
LastLogin string `json:"last_login"`
|
2022-06-06 12:05:44 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 11:21:30 +01:00
|
|
|
// NewAuth0Manager creates a new instance of the Auth0Manager
|
2023-05-29 13:48:19 +02:00
|
|
|
func NewAuth0Manager(config Auth0ClientConfig, appMetrics telemetry.AppMetrics) (*Auth0Manager, error) {
|
2022-01-24 11:21:30 +01:00
|
|
|
httpTransport := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
httpTransport.MaxIdleConns = 5
|
|
|
|
|
|
|
|
httpClient := &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
Transport: httpTransport,
|
|
|
|
}
|
|
|
|
helper := JsonParser{}
|
2023-05-29 13:48:19 +02:00
|
|
|
|
|
|
|
if config.AuthIssuer == "" {
|
|
|
|
return nil, fmt.Errorf("auth0 IdP configuration is incomplete, AuthIssuer is missing")
|
|
|
|
}
|
2022-01-24 11:21:30 +01:00
|
|
|
|
2023-05-11 15:14:00 +02:00
|
|
|
if config.ClientID == "" {
|
2023-05-29 13:48:19 +02:00
|
|
|
return nil, fmt.Errorf("auth0 IdP configuration is incomplete, ClientID is missing")
|
2022-03-09 17:22:47 +01:00
|
|
|
}
|
|
|
|
|
2023-05-11 15:14:00 +02:00
|
|
|
if config.ClientSecret == "" {
|
|
|
|
return nil, fmt.Errorf("auth0 IdP configuration is incomplete, ClientSecret is missing")
|
2022-03-09 17:22:47 +01:00
|
|
|
}
|
|
|
|
|
2023-05-11 15:14:00 +02:00
|
|
|
if config.Audience == "" {
|
|
|
|
return nil, fmt.Errorf("auth0 IdP configuration is incomplete, Audience is missing")
|
2022-03-09 17:22:47 +01:00
|
|
|
}
|
|
|
|
|
2023-05-29 13:48:19 +02:00
|
|
|
if config.GrantType == "" {
|
|
|
|
return nil, fmt.Errorf("auth0 IdP configuration is incomplete, GrantType is missing")
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:21:30 +01:00
|
|
|
credentials := &Auth0Credentials{
|
|
|
|
clientConfig: config,
|
|
|
|
httpClient: httpClient,
|
|
|
|
helper: helper,
|
2022-10-22 13:29:39 +02:00
|
|
|
appMetrics: appMetrics,
|
2022-01-24 11:21:30 +01:00
|
|
|
}
|
2022-10-22 13:29:39 +02:00
|
|
|
|
2022-01-24 11:21:30 +01:00
|
|
|
return &Auth0Manager{
|
|
|
|
authIssuer: config.AuthIssuer,
|
|
|
|
credentials: credentials,
|
|
|
|
httpClient: httpClient,
|
|
|
|
helper: helper,
|
2022-10-22 13:29:39 +02:00
|
|
|
appMetrics: appMetrics,
|
2022-03-09 17:22:47 +01:00
|
|
|
}, nil
|
2022-01-24 11:21:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// jwtStillValid returns true if the token still valid and have enough time to be used and get a response from Auth0
|
|
|
|
func (c *Auth0Credentials) jwtStillValid() bool {
|
2023-04-10 10:54:23 +02:00
|
|
|
return !c.jwtToken.expiresInTime.IsZero() && time.Now().Add(5*time.Second).Before(c.jwtToken.expiresInTime)
|
2022-01-24 11:21:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// requestJWTToken performs request to get jwt token
|
|
|
|
func (c *Auth0Credentials) requestJWTToken() (*http.Response, error) {
|
|
|
|
var res *http.Response
|
2022-05-05 08:58:34 +02:00
|
|
|
reqURL := c.clientConfig.AuthIssuer + "/oauth/token"
|
2022-01-24 11:21:30 +01:00
|
|
|
|
2022-03-09 17:22:47 +01:00
|
|
|
p, err := c.helper.Marshal(auth0JWTRequest(c.clientConfig))
|
2022-01-24 11:21:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
payload := strings.NewReader(string(p))
|
|
|
|
|
2022-05-05 08:58:34 +02:00
|
|
|
req, err := http.NewRequest("POST", reqURL, payload)
|
2022-01-24 11:21:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("content-type", "application/json")
|
|
|
|
|
2022-03-09 17:22:47 +01:00
|
|
|
log.Debug("requesting new jwt token for idp manager")
|
|
|
|
|
2022-01-24 11:21:30 +01:00
|
|
|
res, err = c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2022-10-22 13:29:39 +02:00
|
|
|
if c.appMetrics != nil {
|
|
|
|
c.appMetrics.IDPMetrics().CountRequestError()
|
|
|
|
}
|
2022-01-24 11:21:30 +01:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return res, fmt.Errorf("unable to get token, statusCode %d", res.StatusCode)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseRequestJWTResponse parses jwt raw response body and extracts token and expires in seconds
|
|
|
|
func (c *Auth0Credentials) parseRequestJWTResponse(rawBody io.ReadCloser) (JWTToken, error) {
|
|
|
|
jwtToken := JWTToken{}
|
2022-10-13 18:26:31 +02:00
|
|
|
body, err := io.ReadAll(rawBody)
|
2022-01-24 11:21:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return jwtToken, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.helper.Unmarshal(body, &jwtToken)
|
|
|
|
if err != nil {
|
|
|
|
return jwtToken, err
|
|
|
|
}
|
|
|
|
if jwtToken.ExpiresIn == 0 && jwtToken.AccessToken == "" {
|
|
|
|
return jwtToken, fmt.Errorf("error while reading response body, expires_in: %d and access_token: %s", jwtToken.ExpiresIn, jwtToken.AccessToken)
|
|
|
|
}
|
|
|
|
data, err := jwt.DecodeSegment(strings.Split(jwtToken.AccessToken, ".")[1])
|
|
|
|
if err != nil {
|
|
|
|
return jwtToken, err
|
|
|
|
}
|
|
|
|
// Exp maps into exp from jwt token
|
|
|
|
var IssuedAt struct{ Exp int64 }
|
|
|
|
err = json.Unmarshal(data, &IssuedAt)
|
|
|
|
if err != nil {
|
|
|
|
return jwtToken, err
|
|
|
|
}
|
|
|
|
jwtToken.expiresInTime = time.Unix(IssuedAt.Exp, 0)
|
|
|
|
|
|
|
|
return jwtToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authenticate retrieves access token to use the Auth0 Management API
|
|
|
|
func (c *Auth0Credentials) Authenticate() (JWTToken, error) {
|
|
|
|
c.mux.Lock()
|
|
|
|
defer c.mux.Unlock()
|
|
|
|
|
2022-10-22 13:29:39 +02:00
|
|
|
if c.appMetrics != nil {
|
|
|
|
c.appMetrics.IDPMetrics().CountAuthenticate()
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:21:30 +01:00
|
|
|
// If jwtToken has an expires time and we have enough time to do a request return immediately
|
|
|
|
if c.jwtStillValid() {
|
|
|
|
return c.jwtToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := c.requestJWTToken()
|
|
|
|
if err != nil {
|
|
|
|
return c.jwtToken, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = res.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while closing get jwt token response body: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
jwtToken, err := c.parseRequestJWTResponse(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return c.jwtToken, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.jwtToken = jwtToken
|
|
|
|
|
|
|
|
return c.jwtToken, nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
func batchRequestUsersURL(authIssuer, accountID string, page int, perPage int) (string, url.Values, error) {
|
2022-05-05 08:58:34 +02:00
|
|
|
u, err := url.Parse(authIssuer + "/api/v2/users")
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
q := u.Query()
|
|
|
|
q.Set("page", strconv.Itoa(page))
|
|
|
|
q.Set("search_engine", "v3")
|
2022-10-13 18:26:31 +02:00
|
|
|
q.Set("per_page", strconv.Itoa(perPage))
|
2022-06-06 12:05:44 +02:00
|
|
|
q.Set("q", "app_metadata.wt_account_id:"+accountID)
|
2022-05-05 08:58:34 +02:00
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
return u.String(), q, nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:05:44 +02:00
|
|
|
func requestByUserIDURL(authIssuer, userID string) string {
|
|
|
|
return authIssuer + "/api/v2/users/" + userID
|
2022-05-05 08:58:34 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 12:05:44 +02:00
|
|
|
// GetAccount returns all the users for a given profile. Calls Auth0 API.
|
|
|
|
func (am *Auth0Manager) GetAccount(accountID string) ([]*UserData, error) {
|
2022-05-05 08:58:34 +02:00
|
|
|
jwtToken, err := am.credentials.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var list []*UserData
|
|
|
|
|
|
|
|
// https://auth0.com/docs/manage-users/user-search/retrieve-users-with-get-users-endpoint#limitations
|
|
|
|
// auth0 limitation of 1000 users via this endpoint
|
2022-10-13 18:26:31 +02:00
|
|
|
resultsPerPage := 50
|
2022-05-05 08:58:34 +02:00
|
|
|
for page := 0; page < 20; page++ {
|
2022-10-13 18:26:31 +02:00
|
|
|
reqURL, query, err := batchRequestUsersURL(am.authIssuer, accountID, page, resultsPerPage)
|
2022-05-05 08:58:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, reqURL, strings.NewReader(query.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("authorization", "Bearer "+jwtToken.AccessToken)
|
|
|
|
req.Header.Add("content-type", "application/json")
|
|
|
|
|
|
|
|
res, err := am.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestError()
|
|
|
|
}
|
2022-05-05 08:58:34 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountGetAccount()
|
|
|
|
}
|
|
|
|
|
2022-05-05 08:58:34 +02:00
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("failed requesting user data from IdP %s", string(body))
|
|
|
|
}
|
|
|
|
|
2022-05-05 08:58:34 +02:00
|
|
|
var batch []UserData
|
|
|
|
err = json.Unmarshal(body, &batch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
log.Debugf("returned user batch for accountID %s on page %d, %v", accountID, page, batch)
|
2022-05-05 08:58:34 +02:00
|
|
|
|
|
|
|
err = res.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
for user := range batch {
|
|
|
|
list = append(list, &batch[user])
|
2022-05-05 08:58:34 +02:00
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
if len(batch) == 0 || len(batch) < resultsPerPage {
|
|
|
|
log.Debugf("finished loading users for accountID %s", accountID)
|
2022-05-05 08:58:34 +02:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserDataByID requests user data from auth0 via ID
|
2022-06-06 12:05:44 +02:00
|
|
|
func (am *Auth0Manager) GetUserDataByID(userID string, appMetadata AppMetadata) (*UserData, error) {
|
2022-05-05 08:58:34 +02:00
|
|
|
jwtToken, err := am.credentials.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:05:44 +02:00
|
|
|
reqURL := requestByUserIDURL(am.authIssuer, userID)
|
2022-05-05 08:58:34 +02:00
|
|
|
req, err := http.NewRequest(http.MethodGet, reqURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Add("authorization", "Bearer "+jwtToken.AccessToken)
|
|
|
|
req.Header.Add("content-type", "application/json")
|
|
|
|
|
|
|
|
res, err := am.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestError()
|
|
|
|
}
|
2022-05-05 08:58:34 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountGetUserDataByID()
|
|
|
|
}
|
|
|
|
|
2022-05-05 08:58:34 +02:00
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var userData UserData
|
|
|
|
err = json.Unmarshal(body, &userData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = res.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while closing update user app metadata response body: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("unable to get UserData, statusCode %d", res.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &userData, nil
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:21:30 +01:00
|
|
|
// UpdateUserAppMetadata updates user app metadata based on userId and metadata map
|
2022-06-06 12:05:44 +02:00
|
|
|
func (am *Auth0Manager) UpdateUserAppMetadata(userID string, appMetadata AppMetadata) error {
|
2022-01-24 11:21:30 +01:00
|
|
|
|
|
|
|
jwtToken, err := am.credentials.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:05:44 +02:00
|
|
|
reqURL := am.authIssuer + "/api/v2/users/" + userID
|
2022-01-24 11:21:30 +01:00
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
data, err := am.helper.Marshal(map[string]any{"app_metadata": appMetadata})
|
2022-01-24 11:21:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
payload := strings.NewReader(string(data))
|
2022-01-24 11:21:30 +01:00
|
|
|
|
2022-05-05 08:58:34 +02:00
|
|
|
req, err := http.NewRequest("PATCH", reqURL, payload)
|
2022-01-24 11:21:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Add("authorization", "Bearer "+jwtToken.AccessToken)
|
|
|
|
req.Header.Add("content-type", "application/json")
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
log.Debugf("updating IdP metadata for user %s", userID)
|
2022-03-09 17:22:47 +01:00
|
|
|
|
2022-01-24 11:21:30 +01:00
|
|
|
res, err := am.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestError()
|
|
|
|
}
|
2022-01-24 11:21:30 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountUpdateUserAppMetadata()
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:21:30 +01:00
|
|
|
defer func() {
|
|
|
|
err = res.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while closing update user app metadata response body: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("unable to update the appMetadata, statusCode %d", res.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-06 12:05:44 +02:00
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
func buildCreateUserRequestPayload(email string, name string, accountID string) (string, error) {
|
2022-10-19 17:51:41 +02:00
|
|
|
invite := true
|
2022-10-13 18:26:31 +02:00
|
|
|
req := &createUserRequest{
|
|
|
|
Email: email,
|
|
|
|
Name: name,
|
|
|
|
AppMeta: AppMetadata{
|
|
|
|
WTAccountID: accountID,
|
2022-10-19 17:51:41 +02:00
|
|
|
WTPendingInvite: &invite,
|
2022-10-13 18:26:31 +02:00
|
|
|
},
|
|
|
|
Connection: "Username-Password-Authentication",
|
|
|
|
Password: GeneratePassword(8, 1, 1, 1),
|
|
|
|
VerifyEmail: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
str, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(str), nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:05:44 +02:00
|
|
|
func buildUserExportRequest() (string, error) {
|
|
|
|
req := &userExportJobRequest{}
|
|
|
|
fields := make([]map[string]string, 0)
|
|
|
|
|
|
|
|
for _, field := range []string{"created_at", "last_login", "user_id", "email", "name"} {
|
|
|
|
fields = append(fields, map[string]string{"name": field})
|
|
|
|
}
|
|
|
|
|
|
|
|
fields = append(fields, map[string]string{
|
|
|
|
"name": "app_metadata.wt_account_id",
|
|
|
|
"export_as": "wt_account_id",
|
|
|
|
})
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
fields = append(fields, map[string]string{
|
|
|
|
"name": "app_metadata.wt_pending_invite",
|
|
|
|
"export_as": "wt_pending_invite",
|
|
|
|
})
|
|
|
|
|
2022-06-06 12:05:44 +02:00
|
|
|
req.Format = "json"
|
|
|
|
req.Fields = fields
|
|
|
|
|
|
|
|
str, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(str), nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
func (am *Auth0Manager) createPostRequest(endpoint string, payloadStr string) (*http.Request, error) {
|
2022-06-06 12:05:44 +02:00
|
|
|
jwtToken, err := am.credentials.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
reqURL := am.authIssuer + endpoint
|
|
|
|
|
|
|
|
payload := strings.NewReader(payloadStr)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", reqURL, payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Add("authorization", "Bearer "+jwtToken.AccessToken)
|
|
|
|
req.Header.Add("content-type", "application/json")
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
|
|
|
|
}
|
2022-06-06 12:05:44 +02:00
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
// GetAllAccounts gets all registered accounts with corresponding user data.
|
|
|
|
// It returns a list of users indexed by accountID.
|
|
|
|
func (am *Auth0Manager) GetAllAccounts() (map[string][]*UserData, error) {
|
2022-06-06 12:05:44 +02:00
|
|
|
payloadString, err := buildUserExportRequest()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
exportJobReq, err := am.createPostRequest("/api/v2/jobs/users-exports", payloadString)
|
2022-06-06 12:05:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jobResp, err := am.httpClient.Do(exportJobReq)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Couldn't get job response %v", err)
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestError()
|
|
|
|
}
|
2022-06-06 12:05:44 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = jobResp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while closing update user app metadata response body: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if jobResp.StatusCode != 200 {
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestStatusError()
|
|
|
|
}
|
2022-06-06 12:05:44 +02:00
|
|
|
return nil, fmt.Errorf("unable to update the appMetadata, statusCode %d", jobResp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var exportJobResp userExportJobResponse
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
body, err := io.ReadAll(jobResp.Body)
|
2022-06-06 12:05:44 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Coudln't read export job response; %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.helper.Unmarshal(body, &exportJobResp)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Coudln't unmarshal export job response; %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exportJobResp.ID == "" {
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestStatusError()
|
|
|
|
}
|
2022-06-06 12:05:44 +02:00
|
|
|
return nil, fmt.Errorf("couldn't get an batch id status %d, %s, response body: %v", jobResp.StatusCode, jobResp.Status, exportJobResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("batch id status %d, %s, response body: %v", jobResp.StatusCode, jobResp.Status, exportJobResp)
|
|
|
|
|
|
|
|
done, downloadLink, err := am.checkExportJobStatus(exportJobResp.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Failed at getting status checks from exportJob; %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if done {
|
|
|
|
return am.downloadProfileExport(downloadLink)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed extracting user profiles from auth0")
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
// GetUserByEmail searches users with a given email. If no users have been found, this function returns an empty list.
|
|
|
|
// This function can return multiple users. This is due to the Auth0 internals - there could be multiple users with
|
|
|
|
// the same email but different connections that are considered as separate accounts (e.g., Google and username/password).
|
|
|
|
func (am *Auth0Manager) GetUserByEmail(email string) ([]*UserData, error) {
|
|
|
|
jwtToken, err := am.credentials.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-19 17:51:41 +02:00
|
|
|
reqURL := am.authIssuer + "/api/v2/users-by-email?email=" + url.QueryEscape(email)
|
2022-10-13 18:26:31 +02:00
|
|
|
body, err := doGetReq(am.httpClient, reqURL, jwtToken.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountGetUserByEmail()
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
userResp := []*UserData{}
|
|
|
|
|
|
|
|
err = am.helper.Unmarshal(body, &userResp)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Coudln't unmarshal export job response; %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return userResp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUser creates a new user in Auth0 Idp and sends an invite
|
|
|
|
func (am *Auth0Manager) CreateUser(email string, name string, accountID string) (*UserData, error) {
|
|
|
|
|
|
|
|
payloadString, err := buildCreateUserRequestPayload(email, name, accountID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req, err := am.createPostRequest("/api/v2/users", payloadString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountCreateUser()
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:26:31 +02:00
|
|
|
resp, err := am.httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Couldn't get job response %v", err)
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestError()
|
|
|
|
}
|
2022-10-13 18:26:31 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while closing create user response body: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if !(resp.StatusCode == 200 || resp.StatusCode == 201) {
|
2022-10-22 13:29:39 +02:00
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestStatusError()
|
|
|
|
}
|
2022-10-13 18:26:31 +02:00
|
|
|
return nil, fmt.Errorf("unable to create user, statusCode %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var createResp UserData
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Coudln't read export job response; %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.helper.Unmarshal(body, &createResp)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Coudln't unmarshal export job response; %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if createResp.ID == "" {
|
|
|
|
return nil, fmt.Errorf("couldn't create user: response %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("created user %s in account %s", createResp.ID, accountID)
|
|
|
|
|
|
|
|
return &createResp, nil
|
|
|
|
}
|
|
|
|
|
2023-07-03 12:20:19 +02:00
|
|
|
// InviteUserByID resend invitations to users who haven't activated,
|
|
|
|
// their accounts prior to the expiration period.
|
|
|
|
func (am *Auth0Manager) InviteUserByID(userID string) error {
|
|
|
|
userVerificationReq := userVerificationJobRequest{
|
|
|
|
UserID: userID,
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := am.helper.Marshal(userVerificationReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := am.createPostRequest("/api/v2/jobs/verification-email", string(payload))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := am.httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Couldn't get job response %v", err)
|
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestError()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while closing invite user response body: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if !(resp.StatusCode == 200 || resp.StatusCode == 201) {
|
|
|
|
if am.appMetrics != nil {
|
|
|
|
am.appMetrics.IDPMetrics().CountRequestStatusError()
|
|
|
|
}
|
|
|
|
return fmt.Errorf("unable to invite user, statusCode %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:05:44 +02:00
|
|
|
// checkExportJobStatus checks the status of the job created at CreateExportUsersJob.
|
|
|
|
// If the status is "completed", then return the downloadLink
|
|
|
|
func (am *Auth0Manager) checkExportJobStatus(jobID string) (bool, string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
retry := time.NewTicker(10 * time.Second)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Debugf("Export job status stopped...\n")
|
|
|
|
return false, "", ctx.Err()
|
|
|
|
case <-retry.C:
|
|
|
|
jwtToken, err := am.credentials.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
statusURL := am.authIssuer + "/api/v2/jobs/" + jobID
|
|
|
|
body, err := doGetReq(am.httpClient, statusURL, jwtToken.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var status userExportJobStatusResponse
|
|
|
|
err = am.helper.Unmarshal(body, &status)
|
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("current export job status is %v", status.Status)
|
|
|
|
|
|
|
|
if status.Status != "completed" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, status.Location, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// downloadProfileExport downloads user profiles from auth0 batch job
|
|
|
|
func (am *Auth0Manager) downloadProfileExport(location string) (map[string][]*UserData, error) {
|
|
|
|
body, err := doGetReq(am.httpClient, location, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyReader := bytes.NewReader(body)
|
|
|
|
|
|
|
|
gzipReader, err := gzip.NewReader(bodyReader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(gzipReader)
|
|
|
|
|
|
|
|
res := make(map[string][]*UserData)
|
|
|
|
for decoder.More() {
|
|
|
|
profile := auth0Profile{}
|
|
|
|
err = decoder.Decode(&profile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if profile.AccountID != "" {
|
|
|
|
if _, ok := res[profile.AccountID]; !ok {
|
|
|
|
res[profile.AccountID] = []*UserData{}
|
|
|
|
}
|
|
|
|
res[profile.AccountID] = append(res[profile.AccountID],
|
|
|
|
&UserData{
|
|
|
|
ID: profile.UserID,
|
|
|
|
Name: profile.Name,
|
|
|
|
Email: profile.Email,
|
2022-10-13 18:26:31 +02:00
|
|
|
AppMetadata: AppMetadata{
|
|
|
|
WTAccountID: profile.AccountID,
|
2022-10-19 17:51:41 +02:00
|
|
|
WTPendingInvite: &profile.PendingInvite,
|
2022-10-13 18:26:31 +02:00
|
|
|
},
|
2022-06-06 12:05:44 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Boilerplate implementation for Get Requests.
|
|
|
|
func doGetReq(client ManagerHTTPClient, url, accessToken string) ([]byte, error) {
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if accessToken != "" {
|
|
|
|
req.Header.Add("authorization", "Bearer "+accessToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = res.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while closing body for url %s: %v", url, err)
|
|
|
|
}
|
|
|
|
}()
|
2022-10-13 18:26:31 +02:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2022-06-06 12:05:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-19 17:51:41 +02:00
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("unable to get %s, statusCode %d", url, res.StatusCode)
|
|
|
|
}
|
2022-06-06 12:05:44 +02:00
|
|
|
return body, nil
|
|
|
|
}
|