@@ -17,6 +17,7 @@ package model
17
17
import (
18
18
"encoding/json"
19
19
"fmt"
20
+ "strings"
20
21
)
21
22
22
23
// AuthType ...
@@ -43,18 +44,6 @@ const (
43
44
GrantTypeTokenExchange GrantType = "tokenExchange"
44
45
)
45
46
46
- func getAuthProperties (authType AuthType ) (AuthProperties , bool ) {
47
- switch authType {
48
- case AuthTypeBasic :
49
- return & BasicAuthProperties {}, true
50
- case AuthTypeBearer :
51
- return & BearerAuthProperties {}, true
52
- case AuthTypeOAuth2 :
53
- return & OAuth2AuthProperties {}, true
54
- }
55
- return nil , false
56
- }
57
-
58
47
// Auth ...
59
48
type Auth struct {
60
49
// Name Unique auth definition name
@@ -90,74 +79,73 @@ func (a *Auth) UnmarshalJSON(data []byte) error {
90
79
if len (a .Scheme ) == 0 {
91
80
a .Scheme = AuthTypeBasic
92
81
}
93
- authProperties , ok := getAuthProperties (a .Scheme )
94
- if ! ok {
95
- return fmt .Errorf ("authentication scheme %s not supported" , a .Scheme )
96
- }
97
82
98
- // we take the type we want to unmarshal based on the scheme
99
- if err := unmarshalKey ("properties" , auth , authProperties ); err != nil {
100
- return err
101
- }
83
+ switch a .Scheme {
84
+ case AuthTypeBasic :
85
+ authProperties := & BasicAuthProperties {}
102
86
103
- a .Properties = authProperties
104
- return nil
105
- }
87
+ if err := unmarshalKey ("properties" , auth , authProperties ); err != nil {
88
+ return err
89
+ }
90
+ a .Properties .Basic = authProperties
106
91
107
- // AuthProperties ...
108
- type AuthProperties interface {
109
- // GetMetadata ...
110
- GetMetadata () * Metadata
111
- // GetSecret ...
112
- GetSecret () string
113
- // DeepCopyAuthProperties fixes in.Properties.DeepCopyAuthProperties undefined (type AuthProperties has no
114
- // field or method DeepCopyAuthProperties)
115
- DeepCopyAuthProperties () AuthProperties
116
- }
92
+ return nil
117
93
118
- // BaseAuthProperties ...
119
- type BaseAuthProperties struct {
120
- Common `json:",inline"`
121
- // Secret Expression referencing a workflow secret that contains all needed auth info
122
- Secret string `json:"secret,omitempty"`
123
- }
94
+ case AuthTypeBearer :
95
+ authProperties := & BearerAuthProperties {}
96
+ if err := unmarshalKey ("properties" , auth , authProperties ); err != nil {
97
+ return err
98
+ }
99
+ a .Properties .Bearer = authProperties
100
+ return nil
124
101
125
- // UnmarshalJSON ...
126
- func (b * BaseAuthProperties ) UnmarshalJSON (data []byte ) error {
127
- properties := make (map [string ]json.RawMessage )
128
- if err := json .Unmarshal (data , & properties ); err != nil {
129
- b .Secret , err = unmarshalString (data )
130
- if err != nil {
102
+ case AuthTypeOAuth2 :
103
+ authProperties := & OAuth2AuthProperties {}
104
+ if err := unmarshalKey ("properties" , auth , authProperties ); err != nil {
131
105
return err
132
106
}
107
+ a .Properties .OAuth2 = authProperties
133
108
return nil
134
- }
135
- if err := unmarshalKey ("metadata" , properties , & b .Metadata ); err != nil {
136
- return err
137
- }
138
- if err := unmarshalKey ("secret" , properties , & b .Secret ); err != nil {
139
- return err
140
- }
141
- return nil
142
- }
143
109
144
- // GetMetadata ...
145
- func ( b * BaseAuthProperties ) GetMetadata () * Metadata {
146
- return & b . Metadata
110
+ default :
111
+ return fmt . Errorf ( "failed to parse auth properties" )
112
+ }
147
113
}
148
114
149
- // GetSecret ...
150
- func (b * BaseAuthProperties ) GetSecret () string {
151
- return b .Secret
115
+ func (a * Auth ) MarshalJSON () ([]byte , error ) {
116
+ custom , err := json .Marshal (& struct {
117
+ Name string `json:"name" validate:"required"`
118
+ Scheme AuthType `json:"scheme,omitempty" validate:"omitempty,min=1"`
119
+ Properties AuthProperties `json:"properties" validate:"required"`
120
+ }{
121
+ Name : a .Name ,
122
+ Scheme : a .Scheme ,
123
+ Properties : a .Properties ,
124
+ })
125
+ if err != nil {
126
+ fmt .Println (err )
127
+ }
128
+ st := strings .Replace (string (custom ), "null," , "" , 1 )
129
+ st = strings .Replace (st , "\" Basic\" :" , "" , 1 )
130
+ st = strings .Replace (st , "\" Oauth2\" :" , "" , 1 )
131
+ st = strings .Replace (st , "\" Bearer\" :" , "" , 1 )
132
+ st = strings .Replace (st , "{{" , "{" , 1 )
133
+ st = strings .TrimSuffix (st , "}" )
134
+ return []byte (st ), nil
152
135
}
153
136
154
- func (b * BasicAuthProperties ) DeepCopyAuthProperties () AuthProperties {
155
- return b
137
+ // AuthProperties ...
138
+ type AuthProperties struct {
139
+ Basic * BasicAuthProperties `json:",omitempty"`
140
+ Bearer * BearerAuthProperties `json:",omitempty"`
141
+ OAuth2 * OAuth2AuthProperties `json:",omitempty"`
156
142
}
157
143
158
144
// BasicAuthProperties Basic Auth Info
159
145
type BasicAuthProperties struct {
160
- BaseAuthProperties `json:",inline"`
146
+ Common `json:",inline"`
147
+ // Secret Expression referencing a workflow secret that contains all needed auth info
148
+ Secret string `json:"secret,omitempty"`
161
149
// Username String or a workflow expression. Contains the username
162
150
Username string `json:"username" validate:"required"`
163
151
// Password String or a workflow expression. Contains the user password
@@ -168,11 +156,7 @@ type BasicAuthProperties struct {
168
156
func (b * BasicAuthProperties ) UnmarshalJSON (data []byte ) error {
169
157
properties := make (map [string ]json.RawMessage )
170
158
if err := json .Unmarshal (data , & properties ); err != nil {
171
- err = json .Unmarshal (data , & b .BaseAuthProperties )
172
- if err != nil {
173
- return err
174
- }
175
- return nil
159
+ return err
176
160
}
177
161
if err := unmarshalKey ("username" , properties , & b .Username ); err != nil {
178
162
return err
@@ -183,42 +167,44 @@ func (b *BasicAuthProperties) UnmarshalJSON(data []byte) error {
183
167
if err := unmarshalKey ("metadata" , properties , & b .Metadata ); err != nil {
184
168
return err
185
169
}
170
+ if err := unmarshalKey ("secret" , properties , & b .Secret ); err != nil {
171
+ return err
172
+ }
186
173
return nil
187
174
}
188
175
189
176
// BearerAuthProperties Bearer auth information
190
177
type BearerAuthProperties struct {
191
- BaseAuthProperties `json:",inline"`
178
+ Common `json:",inline"`
179
+ // Secret Expression referencing a workflow secret that contains all needed auth info
180
+ Secret string `json:"secret,omitempty"`
192
181
// Token String or a workflow expression. Contains the token
193
182
Token string `json:"token" validate:"required"`
194
183
}
195
184
196
- func (b * BearerAuthProperties ) DeepCopyAuthProperties () AuthProperties {
197
- return b
198
- }
199
-
200
185
// UnmarshalJSON ...
201
186
func (b * BearerAuthProperties ) UnmarshalJSON (data []byte ) error {
202
187
properties := make (map [string ]json.RawMessage )
203
188
if err := json .Unmarshal (data , & properties ); err != nil {
204
- err = json .Unmarshal (data , & b .BaseAuthProperties )
205
- if err != nil {
206
- return err
207
- }
208
- return nil
189
+ return err
209
190
}
210
191
if err := unmarshalKey ("token" , properties , & b .Token ); err != nil {
211
192
return err
212
193
}
213
194
if err := unmarshalKey ("metadata" , properties , & b .Metadata ); err != nil {
214
195
return err
215
196
}
197
+ if err := unmarshalKey ("secret" , properties , & b .Secret ); err != nil {
198
+ return err
199
+ }
216
200
return nil
217
201
}
218
202
219
203
// OAuth2AuthProperties OAuth2 information
220
204
type OAuth2AuthProperties struct {
221
- BaseAuthProperties `json:",inline"`
205
+ Common `json:",inline"`
206
+ // Secret Expression referencing a workflow secret that contains all needed auth info
207
+ Secret string `json:"secret,omitempty"`
222
208
// Authority String or a workflow expression. Contains the authority information
223
209
Authority string `json:"authority,omitempty" validate:"omitempty,min=1"`
224
210
// GrantType Defines the grant type
@@ -243,21 +229,13 @@ type OAuth2AuthProperties struct {
243
229
RequestedIssuer string `json:"requestedIssuer,omitempty" validate:"omitempty,min=1"`
244
230
}
245
231
246
- func (b * OAuth2AuthProperties ) DeepCopyAuthProperties () AuthProperties {
247
- return b
248
- }
249
-
250
232
// TODO: use reflection to unmarshal the keys and think on a generic approach to handle them
251
233
252
234
// UnmarshalJSON ...
253
235
func (b * OAuth2AuthProperties ) UnmarshalJSON (data []byte ) error {
254
236
properties := make (map [string ]json.RawMessage )
255
237
if err := json .Unmarshal (data , & properties ); err != nil {
256
- err = json .Unmarshal (data , & b .BaseAuthProperties )
257
- if err != nil {
258
- return err
259
- }
260
- return nil
238
+ return err
261
239
}
262
240
if err := unmarshalKey ("authority" , properties , & b .Authority ); err != nil {
263
241
return err
@@ -295,5 +273,8 @@ func (b *OAuth2AuthProperties) UnmarshalJSON(data []byte) error {
295
273
if err := unmarshalKey ("metadata" , properties , & b .Metadata ); err != nil {
296
274
return err
297
275
}
276
+ if err := unmarshalKey ("secret" , properties , & b .Secret ); err != nil {
277
+ return err
278
+ }
298
279
return nil
299
280
}
0 commit comments