Skip to content

Commit

Permalink
Moves GetUserToken off OAuthService and onto UsersService
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmoran committed Mar 23, 2015
1 parent 05e1d4e commit bbb6015
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 119 deletions.
2 changes: 1 addition & 1 deletion acceptance/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ = Describe("Tokens", func() {

By("retrieving a user token", func() {
var err error
userToken, err = client.OAuth.GetUserToken("username", "password", "cf", "https://uaa.cloudfoundry.com/redirect/cf")
userToken, err = client.Users.GetToken("username", "password", "cf", "https://uaa.cloudfoundry.com/redirect/cf")
Expect(err).NotTo(HaveOccurred())
})

Expand Down
2 changes: 0 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ type response struct {
type Warrant struct {
config Config
Users UsersService
OAuth OAuthService
Clients ClientsService
}

func New(config Config) Warrant {
return Warrant{
config: config,
Users: NewUsersService(config),
OAuth: NewOAuthService(config),
Clients: NewClientsService(config),
}
}
Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ var _ = Describe("Client", func() {
Expect(client.Users).To(BeAssignableToTypeOf(warrant.UsersService{}))
})

It("has an oauth service", func() {
Expect(client.OAuth).To(BeAssignableToTypeOf(warrant.OAuthService{}))
It("has an clients service", func() {
Expect(client.Clients).To(BeAssignableToTypeOf(warrant.ClientsService{}))
})

Describe("makeRequest", func() {
Expand Down
57 changes: 0 additions & 57 deletions oauth_service.go

This file was deleted.

57 changes: 0 additions & 57 deletions oauth_service_test.go

This file was deleted.

42 changes: 42 additions & 0 deletions users_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/pivotal-cf-experimental/warrant/internal/documents"
Expand Down Expand Up @@ -142,6 +143,47 @@ func (us UsersService) ChangePassword(id, oldPassword, password, token string) e
return nil
}

func (us UsersService) GetToken(username, password, client, redirectURI string) (string, error) {
query := url.Values{
"client_id": []string{"cf"},
"redirect_uri": []string{redirectURI},
"response_type": []string{"token"},
}

requestPath := url.URL{
Path: "/oauth/authorize",
RawQuery: query.Encode(),
}
req := requestArguments{
Method: "POST",
Path: requestPath.String(),
Body: formRequestBody{
"username": []string{username},
"password": []string{password},
"source": []string{"credentials"},
},
AcceptableStatusCodes: []int{http.StatusFound},
DoNotFollowRedirects: true,
}

resp, err := New(us.config).makeRequest(req)
if err != nil {
return "", err
}

locationURL, err := url.Parse(resp.Headers.Get("Location"))
if err != nil {
return "", err
}

locationQuery, err := url.ParseQuery(locationURL.Fragment)
if err != nil {
return "", err
}

return locationQuery.Get("access_token"), nil
}

func newUpdateUserDocumentFromUser(user User) documents.UpdateUserRequest {
var emails []documents.Email
for _, email := range user.Emails {
Expand Down
32 changes: 32 additions & 0 deletions users_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,36 @@ var _ = Describe("UsersService", func() {
//Expect(score).To(Equal(8))
})
})

Describe("GetToken", func() {
var user warrant.User

BeforeEach(func() {
var err error
user, err = service.Create("username", "user@example.com", token)
Expect(err).NotTo(HaveOccurred())

err = service.SetPassword(user.ID, "password", token)
Expect(err).NotTo(HaveOccurred())
})

It("returns a valid token given a username and password", func() {
token, err := service.GetToken("username", "password", "cf", "https://cf.example.com/redirect")
Expect(err).NotTo(HaveOccurred())
Expect(token).NotTo(BeEmpty())

decodedToken := fakeUAAServer.Tokenizer.Decrypt(token)
Expect(decodedToken.UserID).To(Equal(user.ID))
})

It("returns an error when the request does not succeed", func() {
_, err := service.GetToken("unknown-user", "password", "cf", "https://cf.example.com/redirect")
Expect(err).To(BeAssignableToTypeOf(warrant.NotFoundError{}))
})

It("returns an error when the response is not parsable", func() {
_, err := service.GetToken("username", "password", "cf", "%%%")
Expect(err).To(MatchError(`parse %%%: invalid URL escape "%%%"`))
})
})
})

0 comments on commit bbb6015

Please sign in to comment.