-
Notifications
You must be signed in to change notification settings - Fork 3
/
mocks_properties.go
143 lines (123 loc) · 4.85 KB
/
mocks_properties.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package sectest
import (
"encoding/json"
"fmt"
"github.com/cisco-open/go-lanai/pkg/bootstrap"
"github.com/cisco-open/go-lanai/pkg/utils"
"github.com/pkg/errors"
"strings"
"time"
)
const (
MockingPropertiesPrefix = "mocking"
)
type MockingProperties struct {
Accounts MockedPropertiesAccounts `json:"accounts"`
Tenants MockedPropertiesTenants `json:"tenants"`
Clients MockedPropertiesClients `json:"clients"`
FederatedUsers MockedPropertiesFederatedUsers `json:"fed-users"`
}
// BindMockingProperties is a FX provider that bind all mocked properties as MockingProperties.
// All mocked properties should be under the yaml section defined as MockingPropertiesPrefix
// e.g. "mocking.accounts" defines all account mocks
func BindMockingProperties(ctx *bootstrap.ApplicationContext) (MockingProperties, error) {
return MockedPropertiesBinder[MockingProperties]("")(ctx)
}
type MockedProperties[T any] map[string]*T
func (p MockedProperties[T]) Values() []*T {
values := make([]*T, 0, len(p))
for _, v := range p {
values = append(values, v)
}
return values
}
func (p MockedProperties[T]) MapValues() map[string]*T {
return p
}
func (p *MockedProperties[T]) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, (*map[string]*T)(p))
}
// MockedPropertiesBinder returns a FX provider that bind specific mocked properties type from the properties sub-section
// specified by "prefix". The root section prefix is defined by MockingPropertiesPrefix
// e.g. MockedPropertiesBinder[MockedPropertiesAccounts]("accounts"):
// The returned binder binds MockedPropertiesAccounts from "mocking.accounts"
func MockedPropertiesBinder[T any](prefix string) func(ctx *bootstrap.ApplicationContext) (T, error) {
return func(ctx *bootstrap.ApplicationContext) (T, error) {
prefix = MockingPropertiesPrefix + "." + prefix
prefix = strings.Trim(prefix, ".")
var props T
if err := ctx.Config().Bind(&props, prefix); err != nil {
return props, errors.Wrap(err, fmt.Sprintf("failed to bind mocking properties %T from [%s]", props, prefix))
}
return props, nil
}
}
type MockedPropertiesClients struct {
MockedProperties[MockedClientProperties]
}
type MockedClientProperties struct {
ClientID string `json:"id"`
Secret string `json:"secret"`
GrantTypes utils.CommaSeparatedSlice `json:"grant-types"`
Scopes utils.CommaSeparatedSlice `json:"scopes"`
RedirectUris utils.CommaSeparatedSlice `json:"redirect-uris"`
ATValidity utils.Duration `json:"access-token-validity"`
RTValidity utils.Duration `json:"refresh-token-validity"`
AssignedTenantIds utils.CommaSeparatedSlice `json:"tenants"`
}
type MockedPropertiesAccounts struct {
MockedProperties[MockedAccountProperties]
}
type MockedAccountProperties struct {
UserId string `json:"id"` // optional field
Username string `json:"username"`
Password string `json:"password"`
DefaultTenant string `json:"default-tenant"`
Tenants []string `json:"tenants"`
Perms []string `json:"permissions"`
}
type MockedPropertiesFederatedUsers struct {
MockedProperties[MockedFederatedUserProperties]
}
type MockedFederatedUserProperties struct {
MockedAccountProperties
ExtIdpName string `json:"ext-idp-name"`
ExtIdName string `json:"ext-id-name"`
ExtIdValue string `json:"ext-id-value"`
}
type MockedPropertiesTenants struct {
MockedProperties[MockedTenantProperties]
}
type MockedTenantProperties struct {
ID string `json:"id"` // optional field
ExternalId string `json:"external-id"`
Perms map[string][]string `json:"permissions"` // permissions are MockedAccountProperties.UserId to permissions
}
type scopeMockingProperties struct {
MockingProperties
TokenValidity utils.Duration `json:"token-validity"`
}
func bindScopeMockingProperties(ctx *bootstrap.ApplicationContext) *scopeMockingProperties {
props := scopeMockingProperties{
TokenValidity: utils.Duration(120 * time.Second),
}
if err := ctx.Config().Bind(&props, MockingPropertiesPrefix); err != nil {
panic(errors.Wrap(err, "failed to bind mocking properties"))
}
return &props
}