-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nick Calugar and Ryan Moran
committed
Mar 20, 2015
1 parent
2df580b
commit 05e1d4e
Showing
18 changed files
with
415 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package acceptance | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"time" | ||
|
||
"github.com/pivotal-cf-experimental/warrant" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Client Lifecycle", func() { | ||
var ( | ||
warrantClient warrant.Warrant | ||
token string | ||
client warrant.Client | ||
) | ||
|
||
BeforeEach(func() { | ||
token = os.Getenv("UAA_TOKEN") | ||
client = warrant.Client{ | ||
ID: "warrant-client", | ||
Scope: []string{"openid"}, | ||
ResourceIDs: []string{"none"}, | ||
Authorities: []string{"scim.read", "scim.write"}, | ||
AuthorizedGrantTypes: []string{"client_credentials"}, | ||
AccessTokenValidity: 5000 * time.Second, | ||
} | ||
|
||
warrantClient = warrant.New(warrant.Config{ | ||
Host: os.Getenv("UAA_HOST"), | ||
SkipVerifySSL: true, | ||
}) | ||
}) | ||
|
||
AfterEach(func() { | ||
// TODO: replace with implementation that does not call out to UAAC | ||
cmd := exec.Command("uaac", "client", "delete", client.ID) | ||
output, err := cmd.Output() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(output).To(ContainSubstring("client registration deleted")) | ||
}) | ||
|
||
It("creates, and retrieves a client", func() { | ||
By("creating a client", func() { | ||
err := warrantClient.Clients.Create(client, "secret", token) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
By("find the client", func() { | ||
fetchedClient, err := warrantClient.Clients.Get(client.ID, token) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(fetchedClient).To(Equal(client)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package warrant | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pivotal-cf-experimental/warrant/internal/documents" | ||
) | ||
|
||
type Client struct { | ||
ID string | ||
Scope []string | ||
ResourceIDs []string | ||
Authorities []string | ||
AuthorizedGrantTypes []string | ||
AccessTokenValidity time.Duration | ||
} | ||
|
||
type ClientsService struct { | ||
config Config | ||
} | ||
|
||
func NewClientsService(config Config) ClientsService { | ||
return ClientsService{ | ||
config: config, | ||
} | ||
} | ||
|
||
func (cs ClientsService) Create(client Client, secret, token string) error { | ||
_, err := New(cs.config).makeRequest(requestArguments{ | ||
Method: "POST", | ||
Path: "/oauth/clients", | ||
Token: token, | ||
Body: jsonRequestBody{client.ToDocument(secret)}, | ||
AcceptableStatusCodes: []int{http.StatusCreated}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cs ClientsService) Get(id, token string) (Client, error) { | ||
resp, err := New(cs.config).makeRequest(requestArguments{ | ||
Method: "GET", | ||
Path: fmt.Sprintf("/oauth/clients/%s", id), | ||
Token: token, | ||
AcceptableStatusCodes: []int{http.StatusOK}, | ||
}) | ||
if err != nil { | ||
return Client{}, err | ||
} | ||
|
||
var document documents.ClientResponse | ||
err = json.Unmarshal(resp.Body, &document) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return newClientFromDocument(document), nil | ||
} | ||
|
||
func newClientFromDocument(document documents.ClientResponse) Client { | ||
return Client{ | ||
ID: document.ClientID, | ||
Scope: document.Scope, | ||
ResourceIDs: document.ResourceIDs, | ||
Authorities: document.Authorities, | ||
AuthorizedGrantTypes: document.AuthorizedGrantTypes, | ||
AccessTokenValidity: time.Duration(document.AccessTokenValidity) * time.Second, | ||
} | ||
} | ||
|
||
func (c Client) ToDocument(secret string) documents.CreateClientRequest { | ||
client := documents.CreateClientRequest{ | ||
ClientID: c.ID, | ||
ClientSecret: secret, | ||
AccessTokenValidity: int(c.AccessTokenValidity.Seconds()), | ||
Scope: make([]string, 0), | ||
ResourceIDs: make([]string, 0), | ||
Authorities: make([]string, 0), | ||
AuthorizedGrantTypes: make([]string, 0), | ||
} | ||
client.Scope = append(client.Scope, c.Scope...) | ||
client.ResourceIDs = append(client.ResourceIDs, c.ResourceIDs...) | ||
client.Authorities = append(client.Authorities, c.Authorities...) | ||
client.AuthorizedGrantTypes = append(client.AuthorizedGrantTypes, c.AuthorizedGrantTypes...) | ||
|
||
return client | ||
} |
Oops, something went wrong.