Skip to content

Commit b4eb66f

Browse files
authored
Remove pointer map used to get AuthProperties (#87)
* Remove pointer map used to get AuthProperties Fixes #86 Signed-off-by: Calvin McLean <calvinlmc@gmail.com> * Improve testing for Unmarshal AuthProperties Signed-off-by: Calvin McLean <calvinlmc@gmail.com> Signed-off-by: Calvin McLean <calvinlmc@gmail.com>
1 parent 4be1862 commit b4eb66f

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

model/auth.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,16 @@ const (
7070
GrantTypeTokenExchange GrantType = "tokenExchange"
7171
)
7272

73-
// authTypesMapping map to support JSON unmarshalling when guessing the auth scheme
74-
var authTypesMapping = map[AuthType]AuthProperties{
75-
AuthTypeBasic: &BasicAuthProperties{},
76-
AuthTypeBearer: &BearerAuthProperties{},
77-
AuthTypeOAuth2: &OAuth2AuthProperties{},
73+
func getAuthProperties(authType AuthType) (AuthProperties, bool) {
74+
switch authType {
75+
case AuthTypeBasic:
76+
return &BasicAuthProperties{}, true
77+
case AuthTypeBearer:
78+
return &BearerAuthProperties{}, true
79+
case AuthTypeOAuth2:
80+
return &OAuth2AuthProperties{}, true
81+
}
82+
return nil, false
7883
}
7984

8085
// Auth ...
@@ -150,11 +155,12 @@ func (a *Auth) UnmarshalJSON(data []byte) error {
150155
if len(a.Scheme) == 0 {
151156
a.Scheme = AuthTypeBasic
152157
}
153-
if _, ok := authTypesMapping[a.Scheme]; !ok {
154-
return fmt.Errorf("authentication scheme %s not supported", authTypesMapping["type"])
158+
authProperties, ok := getAuthProperties(a.Scheme)
159+
if !ok {
160+
return fmt.Errorf("authentication scheme %s not supported", a.Scheme)
155161
}
162+
156163
// we take the type we want to unmarshal based on the scheme
157-
authProperties := authTypesMapping[a.Scheme]
158164
if err := unmarshalKey("properties", auth, authProperties); err != nil {
159165
return err
160166
}

model/auth_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package model
1616

1717
import (
18+
"encoding/json"
1819
"testing"
1920

2021
"github.com/stretchr/testify/assert"
@@ -92,3 +93,70 @@ func TestAuthDefinitionsStructLevelValidation(t *testing.T) {
9293
})
9394
}
9495
}
96+
97+
func TestUnmarshalJSONMultipleAuthProperties(t *testing.T) {
98+
t.Run("BearerAuthProperties", func(t *testing.T) {
99+
a1JSON := `{
100+
"name": "a1",
101+
"scheme": "bearer",
102+
"properties": {
103+
"token": "token1"
104+
}
105+
}`
106+
a2JSON := `{
107+
"name": "a2",
108+
"scheme": "bearer",
109+
"properties": {
110+
"token": "token2"
111+
}
112+
}`
113+
114+
var a1 Auth
115+
err := json.Unmarshal([]byte(a1JSON), &a1)
116+
assert.NoError(t, err)
117+
118+
var a2 Auth
119+
err = json.Unmarshal([]byte(a2JSON), &a2)
120+
assert.NoError(t, err)
121+
122+
a1Properties := a1.Properties.(*BearerAuthProperties)
123+
a2Properties := a2.Properties.(*BearerAuthProperties)
124+
125+
assert.Equal(t, "token1", a1Properties.Token)
126+
assert.Equal(t, "token2", a2Properties.Token)
127+
assert.NotEqual(t, a1Properties, a2Properties)
128+
})
129+
130+
t.Run("OAuth2AuthProperties", func(t *testing.T) {
131+
a1JSON := `{
132+
"name": "a1",
133+
"scheme": "oauth2",
134+
"properties": {
135+
"clientSecret": "secret1"
136+
}
137+
}`
138+
139+
a2JSON := `{
140+
"name": "a2",
141+
"scheme": "oauth2",
142+
"properties": {
143+
"clientSecret": "secret2"
144+
}
145+
}`
146+
147+
var a1 Auth
148+
err := json.Unmarshal([]byte(a1JSON), &a1)
149+
assert.NoError(t, err)
150+
151+
var a2 Auth
152+
err = json.Unmarshal([]byte(a2JSON), &a2)
153+
assert.NoError(t, err)
154+
155+
a1Properties := a1.Properties.(*OAuth2AuthProperties)
156+
a2Properties := a2.Properties.(*OAuth2AuthProperties)
157+
158+
assert.Equal(t, "secret1", a1Properties.ClientSecret)
159+
assert.Equal(t, "secret2", a2Properties.ClientSecret)
160+
assert.NotEqual(t, a1Properties, a2Properties)
161+
})
162+
}

0 commit comments

Comments
 (0)