-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
71 lines (58 loc) · 1.58 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package connector
import (
"context"
"errors"
"fmt"
"sync"
"github.com/conductorone/baton-jumpcloud/pkg/jcapi1"
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
)
var (
userCache sync.Map
errUserNotFoundForEmail = errors.New("user not found for email")
errMultipleUsersForEmail = errors.New("multiple users found for email")
)
func fmtResourceId(resourceTypeID string, id string) *v2.ResourceId {
return &v2.ResourceId{
ResourceType: resourceTypeID,
Resource: id,
}
}
func fmtResource(resourceID *v2.ResourceId, role string) string {
return fmt.Sprintf(
"%s:%s",
resourceID.ResourceType,
resourceID.Resource,
)
}
func fmtResourceGrant(resourceID *v2.ResourceId, principalId *v2.ResourceId, permission string) string {
return fmt.Sprintf(
"%s-grant:%s:%s:%s:%s",
resourceID.ResourceType,
resourceID.Resource,
principalId.ResourceType,
principalId.Resource,
permission,
)
}
func fetchUserByEmail(ctx context.Context, client *jcapi1.APIClient, email string) (*jcapi1.Systemuserreturn, error) {
if email == "" {
return nil, errors.New("email cannot be empty")
}
if u, ok := userCache.Load(email); ok {
return u.(*jcapi1.Systemuserreturn), nil
}
list, resp, err := client.SystemusersApi.SystemusersList(ctx).Filter(fmt.Sprintf("email:$eq:%s", email)).Execute()
if err != nil {
return nil, err
}
defer resp.Body.Close()
if len(list.Results) == 0 {
return nil, errUserNotFoundForEmail
}
if len(list.Results) != 1 {
return nil, errMultipleUsersForEmail
}
userCache.Store(email, &list.Results[0])
return &list.Results[0], nil
}