Skip to content

Commit

Permalink
Merge pull request #21 from shamus/master
Browse files Browse the repository at this point in the history
Improve client deserialization.
  • Loading branch information
joefitzgerald authored Jul 25, 2019
2 parents 0dc8535 + 8fedbed commit da1d1fc
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 17 deletions.
47 changes: 30 additions & 17 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)

Expand All @@ -21,30 +22,42 @@ type paginatedClientList struct {
// Client is a UAA client
// http://docs.cloudfoundry.org/api/uaa/version/4.19.0/index.html#clients.
type Client struct {
ClientID string `json:"client_id,omitempty" generator:"id"`
AuthorizedGrantTypes []string `json:"authorized_grant_types,omitempty"`
RedirectURI []string `json:"redirect_uri,omitempty"`
Scope []string `json:"scope,omitempty"`
ResourceIDs []string `json:"resource_ids,omitempty"`
Authorities []string `json:"authorities,omitempty"`
AutoApprove []string `json:"autoapprove,omitempty"`
AccessTokenValidity int64 `json:"access_token_validity,omitempty"`
RefreshTokenValidity int64 `json:"refresh_token_validity,omitempty"`
AllowedProviders []string `json:"allowedproviders,omitempty"`
DisplayName string `json:"name,omitempty"`
TokenSalt string `json:"token_salt,omitempty"`
CreatedWith string `json:"createdwith,omitempty"`
ApprovalsDeleted bool `json:"approvals_deleted,omitempty"`
RequiredUserGroups []string `json:"required_user_groups,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
LastModified int64 `json:"lastModified,omitempty"`
ClientID string `json:"client_id,omitempty" generator:"id"`
AuthorizedGrantTypes []string `json:"authorized_grant_types,omitempty"`
RedirectURI []string `json:"redirect_uri,omitempty"`
Scope []string `json:"scope,omitempty"`
ResourceIDs []string `json:"resource_ids,omitempty"`
Authorities []string `json:"authorities,omitempty"`
AutoApproveRaw interface{} `json:"autoapprove,omitempty"`
AccessTokenValidity int64 `json:"access_token_validity,omitempty"`
RefreshTokenValidity int64 `json:"refresh_token_validity,omitempty"`
AllowedProviders []string `json:"allowedproviders,omitempty"`
DisplayName string `json:"name,omitempty"`
TokenSalt string `json:"token_salt,omitempty"`
CreatedWith string `json:"createdwith,omitempty"`
ApprovalsDeleted bool `json:"approvals_deleted,omitempty"`
RequiredUserGroups []string `json:"required_user_groups,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
LastModified int64 `json:"lastModified,omitempty"`
}

// Identifier returns the field used to uniquely identify a Client.
func (c Client) Identifier() string {
return c.ClientID
}

func (c Client) AutoApprove() []string {
switch t := c.AutoApproveRaw.(type) {
case bool:
return []string{strconv.FormatBool(t)}
case string:
return []string{t}
case []string:
return t
}
return []string{}
}

// GrantType is a type of oauth2 grant.
type GrantType string

Expand Down
88 changes: 88 additions & 0 deletions clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

uaa "github.com/cloudfoundry-community/go-uaa"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"github.com/sclevine/spec"
)

Expand Down Expand Up @@ -52,6 +53,93 @@ func testClientExtra(t *testing.T, when spec.G, it spec.S) {
RegisterTestingT(t)
})

when("GetClient()", func() {
var (
server *ghttp.Server
a *uaa.API
)

it.Before(func() {
server = ghttp.NewServer()

c := &http.Client{Transport: http.DefaultTransport}
u, _ := url.Parse(server.URL())
a = &uaa.API{
TargetURL: u,
AuthenticatedClient: c,
UnauthenticatedClient: c,
}
})

it.After(func() {
if server != nil {
server.Close()
}
})

when("the client returned from the server contains an autoapprove value that is a boolean", func() {
response := `{
"scope" : [ "clients.read", "clients.write" ],
"client_id" : "00000000-0000-0000-0000-000000000001",
"resource_ids" : [ "none" ],
"authorized_grant_types" : [ "client_credentials" ],
"redirect_uri" : [ "http://ant.path.wildcard/**/passback/*", "http://test1.com" ],
"autoapprove" : true,
"authorities" : [ "clients.read", "clients.write" ],
"token_salt" : "1SztLL",
"allowedproviders" : [ "uaa", "ldap", "my-saml-provider" ],
"name" : "My Client Name",
"lastModified" : 1502816030525,
"required_user_groups" : [ ]
}`

it.Before(func() {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", uaa.ClientsEndpoint + "/00000000-0000-0000-0000-000000000001"),
ghttp.VerifyHeaderKV("Accept", "application/json"),
ghttp.RespondWith(http.StatusOK, response),
))
})

it("decodes the autoapprove value", func() {
client, err := a.GetClient("00000000-0000-0000-0000-000000000001")
Expect(err).NotTo(HaveOccurred())
Expect(client.AutoApprove()).To(Equal([]string{"true"}))
})
})

when("the client returned from the server contains an autoapprove value that is a string", func() {
response := `{
"scope" : [ "clients.read", "clients.write" ],
"client_id" : "00000000-0000-0000-0000-000000000001",
"resource_ids" : [ "none" ],
"authorized_grant_types" : [ "client_credentials" ],
"redirect_uri" : [ "http://ant.path.wildcard/**/passback/*", "http://test1.com" ],
"autoapprove" : "scope",
"authorities" : [ "clients.read", "clients.write" ],
"token_salt" : "1SztLL",
"allowedproviders" : [ "uaa", "ldap", "my-saml-provider" ],
"name" : "My Client Name",
"lastModified" : 1502816030525,
"required_user_groups" : [ ]
}`

it.Before(func() {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", uaa.ClientsEndpoint + "/00000000-0000-0000-0000-000000000001"),
ghttp.VerifyHeaderKV("Accept", "application/json"),
ghttp.RespondWith(http.StatusOK, response),
))
})

it("decodes the autoapprove value", func() {
client, err := a.GetClient("00000000-0000-0000-0000-000000000001")
Expect(err).NotTo(HaveOccurred())
Expect(client.AutoApprove()).To(Equal([]string{"scope"}))
})
})
})

when("Client.Validate()", func() {
it("rejects empty grant types", func() {
client := uaa.Client{}
Expand Down

0 comments on commit da1d1fc

Please sign in to comment.