Skip to content

Commit

Permalink
CLI | Change get tenant functionality in group wrapper (AST-31008) (#780
Browse files Browse the repository at this point in the history
)

* Change get tenant functionality in group wrapper

* Change get tenant functionality in group wrapper

---------

Co-authored-by: AlvoBen <alvo@post.bgu.ac.il>
  • Loading branch information
AlvoBen and BenAlvo1 authored Jun 23, 2024
1 parent d78cef0 commit 369f615
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/wrappers/groups-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewHTTPGroupsWrapper(path string) GroupsWrapper {

func (g *GroupsHTTPWrapper) Get(groupName string) ([]Group, error) {
clientTimeout := viper.GetUint(commonParams.ClientTimeoutKey)
tenant := viper.GetString(commonParams.TenantKey)
tenant := getTenant()
tenantPath := strings.Replace(g.path, "organization", strings.ToLower(tenant), 1)
groupMap := make(map[string]string)
groupMap["groupName"] = groupName
Expand Down Expand Up @@ -62,3 +62,11 @@ func (g *GroupsHTTPWrapper) Get(groupName string) ([]Group, error) {
return nil, errors.Errorf("response status code %d", resp.StatusCode)
}
}

func getTenant() string {
tenant, tenantErr := NewJwtWrapper().ExtractTenantFromToken()
if tenantErr != nil {
return viper.GetString(commonParams.TenantKey)
}
return tenant
}
14 changes: 14 additions & 0 deletions internal/wrappers/jwt-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// JWTStruct model used to get all jwt fields
type JWTStruct struct {
Tenant string `json:"tenant_name"`
AstLicense struct {
LicenseData struct {
AllowedEngines []string `json:"allowedEngines"`
Expand All @@ -30,6 +31,7 @@ var defaultEngines = map[string]bool{

type JWTWrapper interface {
GetAllowedEngines(featureFlagsWrapper FeatureFlagsWrapper) (allowedEngines map[string]bool, err error)
ExtractTenantFromToken() (tenant string, err error)
}

func NewJwtWrapper() JWTWrapper {
Expand Down Expand Up @@ -82,3 +84,15 @@ func extractFromTokenToJwtStruct(accessToken string) (*JWTStruct, error) {

return claims, nil
}

func (*JWTStruct) ExtractTenantFromToken() (tenant string, err error) {
accessToken, err := GetAccessToken()
if err != nil {
return "", err
}
jwtStruct, err := extractFromTokenToJwtStruct(accessToken)
if err != nil {
return "", err
}
return jwtStruct.Tenant, nil
}
4 changes: 4 additions & 0 deletions internal/wrappers/mock/jwt-helper-mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ func (*JWTMockWrapper) GetAllowedEngines(featureFlagsWrapper wrappers.FeatureFla
}
return allowedEngines, nil
}

func (*JWTMockWrapper) ExtractTenantFromToken() (tenant string, err error) {
return "test-tenant", nil
}

0 comments on commit 369f615

Please sign in to comment.