Skip to content

Commit 4112a4c

Browse files
committed
Tolerate string-typed allowpublic values from UAA
UAA stores allowpublic in a loosely-typed additionalInformation map, so the JSON type on the wire depends on how the client was originally created (bool or string). The strict bool field caused json.Unmarshal to fail for clients with a string allowpublic value, breaking ListAllClients/GetClient. Mirrors the existing AutoApproveRaw pattern used for the same class of issue with autoapprove.
1 parent b85d541 commit 4112a4c

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

clients.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Client struct {
3939
RequiredUserGroups []string `json:"required_user_groups,omitempty"`
4040
ClientSecret string `json:"client_secret,omitempty"`
4141
LastModified int64 `json:"lastModified,omitempty"`
42-
AllowPublic bool `json:"allowpublic,omitempty"`
42+
AllowPublicRaw interface{} `json:"allowpublic,omitempty"`
4343
JwksURI string `json:"jwks_uri,omitempty"`
4444
Jwks json.RawMessage `json:"jwks,omitempty"`
4545
}
@@ -61,6 +61,22 @@ func (c Client) AutoApprove() []string {
6161
return []string{}
6262
}
6363

64+
// AllowPublic returns whether the client allows public access, tolerating
65+
// UAA responses that encode allowpublic as either a JSON boolean or string.
66+
func (c Client) AllowPublic() bool {
67+
switch t := c.AllowPublicRaw.(type) {
68+
case bool:
69+
return t
70+
case string:
71+
b, err := strconv.ParseBool(t)
72+
if err != nil {
73+
return false
74+
}
75+
return b
76+
}
77+
return false
78+
}
79+
6480
// GrantType is a type of oauth2 grant.
6581
type GrantType string
6682

clients_test.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const clientListResponse = `{
4242
}`
4343

4444
var testClientValue uaa.Client = uaa.Client{
45-
ClientID: "00000000-0000-0000-0000-000000000001",
46-
ClientSecret: "new_secret",
47-
AllowPublic: true,
45+
ClientID: "00000000-0000-0000-0000-000000000001",
46+
ClientSecret: "new_secret",
47+
AllowPublicRaw: true,
4848
}
4949

5050
const testClientJSON string = `{"client_id": "00000000-0000-0000-0000-000000000001", "client_secret": "new_secret", "allowpublic": true}`
@@ -134,6 +134,44 @@ func testClientExtra(t *testing.T, when spec.G, it spec.S) {
134134
Expect(err).NotTo(HaveOccurred())
135135
Expect(client.AutoApprove()).To(Equal([]string{"scope"}))
136136
})
137+
138+
it("decodes the allowpublic value", func() {
139+
client, err := a.GetClient("00000000-0000-0000-0000-000000000001")
140+
Expect(err).NotTo(HaveOccurred())
141+
Expect(client.AllowPublic()).To(BeTrue())
142+
})
143+
})
144+
145+
when("the client returned from the server contains an allowpublic value that is a string", func() {
146+
response := `{
147+
"scope" : [ "clients.read", "clients.write" ],
148+
"client_id" : "00000000-0000-0000-0000-000000000001",
149+
"resource_ids" : [ "none" ],
150+
"authorized_grant_types" : [ "client_credentials" ],
151+
"redirect_uri" : [ "http://ant.path.wildcard/**/passback/*", "http://test1.com" ],
152+
"autoapprove" : [ "true" ],
153+
"authorities" : [ "clients.read", "clients.write" ],
154+
"token_salt" : "1SztLL",
155+
"allowedproviders" : [ "uaa", "ldap", "my-saml-provider" ],
156+
"name" : "My Client Name",
157+
"lastModified" : 1502816030525,
158+
"required_user_groups" : [ ],
159+
"allowpublic" : "true"
160+
}`
161+
162+
it.Before(func() {
163+
server.AppendHandlers(ghttp.CombineHandlers(
164+
ghttp.VerifyRequest("GET", uaa.ClientsEndpoint+"/00000000-0000-0000-0000-000000000001"),
165+
ghttp.VerifyHeaderKV("Accept", "application/json"),
166+
ghttp.RespondWith(http.StatusOK, response),
167+
))
168+
})
169+
170+
it("decodes the allowpublic value", func() {
171+
client, err := a.GetClient("00000000-0000-0000-0000-000000000001")
172+
Expect(err).NotTo(HaveOccurred())
173+
Expect(client.AllowPublic()).To(BeTrue())
174+
})
137175
})
138176
})
139177

0 commit comments

Comments
 (0)