Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
add password flow
Browse files Browse the repository at this point in the history
  • Loading branch information
kisom committed Mar 3, 2013
1 parent ad630ec commit 749edec
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
)

type Application struct {
Id string
Secret string
RedirectURI string
Scopes Scopes
Id string
Secret string
RedirectURI string
Scopes Scopes
PasswordSecret string
}

var DefaultApplication = &Application{}
Expand Down Expand Up @@ -132,8 +133,33 @@ func (c *Application) AccessToken(code string) (string, error) {
BodyType: "application/x-www-form-urlencoded",
}

//{"error": "This code has already been used."}
//{"access_token": "x", "username": "whee", "user_id": 19058}
resp := &struct {
AccessToken string `json:"access_token"`
Error string
}{}
err := c.Do(r, "get access token", EpArgs{}, resp)
if err != nil {
return "", err
}
if resp.Error != "" {
return "", errors.New(resp.Error)
}
return resp.AccessToken, nil
}

func (c *Application) PasswordToken(username, password string) (string, error) {
data := url.Values{}
data.Set("client_id", c.Id)
data.Set("password_grant_secret", c.PasswordSecret)
data.Set("grant_type", "password")
data.Set("username", username)
data.Set("password", password)
data.Set("scopes", c.Scopes.Spaced())

r := &Request{
Body: strings.NewReader(data.Encode()),
BodyType: "application/x-www-form-urlencoded",
}

resp := &struct {
AccessToken string `json:"access_token"`
Expand Down

0 comments on commit 749edec

Please sign in to comment.