...
1 package bsl
2
3 import (
4 "context"
5 "fmt"
6 "strings"
7
8 "edge-infra.dev/pkg/edge/api/bsl/types"
9 )
10
11 const (
12 SharedKey = "shared_key"
13 SecretKey = "secret_key"
14 OrgDelimiter = "/"
15 NepOrganization = "Nep-Organization"
16 getUsersGroups = "/security/group-memberships/groups?username=%s"
17 )
18
19 type AccessKeySecret func(ctx context.Context, organization string) (*AccessKey, error)
20
21 func WithRootOrg(root, organization string) string {
22 if !strings.Contains(organization, TrimOrg(root)) {
23 organization = fmt.Sprintf("%s%s/", root, TrimOrg(organization))
24 } else {
25 organization = fmt.Sprintf("/%s/", TrimOrg(organization))
26 }
27 return organization
28 }
29
30 func TrimOrg(org string) string {
31 return strings.Trim(org, OrgDelimiter)
32 }
33
34 func GetOrgShortName(org string) string {
35 splitPath := strings.Split(TrimOrg(org), OrgDelimiter)
36 return splitPath[len(splitPath)-1]
37 }
38
39 func CreateFullAccountName(user *types.AuthUser) string {
40 if user.AuthProvider == "okta" {
41 return fmt.Sprintf("acct:commerce@%s-%s", user.Username, user.Email)
42 }
43 return fmt.Sprintf("acct:%s@%s", GetOrgShortName(user.Organization), user.Username)
44 }
45
46 func WithOrganizationPrefix(b types.BSPConfig, org string) string {
47 if b.OrganizationPrefix == "" {
48 return org
49 }
50 orgParts := strings.Split(org, OrgDelimiter)
51 orgIndex := findOrgIndex(orgParts)
52 if !strings.HasPrefix(orgParts[orgIndex], b.OrganizationPrefix) {
53 orgParts[orgIndex] = b.OrganizationPrefix + orgParts[orgIndex]
54 }
55 return strings.Join(orgParts, OrgDelimiter)
56 }
57
58 func GetTenant(root, orgName string) string {
59 orgWithoutRoot := strings.TrimPrefix(orgName, root)
60 orgParts := strings.Split(orgWithoutRoot, OrgDelimiter)
61 if len(orgParts) > 0 {
62 return orgParts[0]
63 }
64 return ""
65 }
66
67 func findOrgIndex(orgParts []string) int {
68 i := len(orgParts) - 1
69 for ; i >= 0; i-- {
70 if len(orgParts[i]) > 0 {
71 break
72 }
73 }
74 return i
75 }
76
View as plain text