Skip to content

Commit

Permalink
Feature/refactor get profile to use form data (#40)
Browse files Browse the repository at this point in the history
* add form data
  • Loading branch information
maxchehab authored Mar 19, 2020
1 parent 8ad2e26 commit 26052ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 8 additions & 7 deletions pkg/sso/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,24 @@ type Profile struct {
func (c *Client) GetProfile(ctx context.Context, opts GetProfileOptions) (Profile, error) {
c.once.Do(c.init)

form := make(url.Values, 5)
form.Set("client_id", c.ProjectID)
form.Set("client_secret", c.APIKey)
form.Set("grant_type", "authorization_code")
form.Set("code", opts.Code)

req, err := http.NewRequest(
http.MethodPost,
c.Endpoint+"/sso/token",
nil,
strings.NewReader(form.Encode()),
)
if err != nil {
return Profile{}, err
}
req = req.WithContext(ctx)
req.Header.Set("User-Agent", "workos-go/"+workos.Version)

query := make(url.Values, 5)
query.Set("client_id", c.ProjectID)
query.Set("client_secret", c.APIKey)
query.Set("grant_type", "authorization_code")
query.Set("code", opts.Code)
req.URL.RawQuery = query.Encode()
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

res, err := c.HTTPClient.Do(req)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/sso/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ func profileTestHandler(w http.ResponseWriter, r *http.Request) {
return
}

if clientSecret := r.URL.Query().Get("client_secret"); clientSecret != "test" {
r.ParseForm()

if clientSecret := r.Form.Get("client_secret"); clientSecret != "test" {
w.WriteHeader(http.StatusForbidden)
return
}
Expand Down

0 comments on commit 26052ff

Please sign in to comment.