Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions api/form.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package api

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -19,11 +19,23 @@ type FormResponse struct {

requestURI string
values url.Values

json map[string]interface{}

contentType string
}

// Get the response value named k.
func (f FormResponse) Get(k string) string {
return f.values.Get(k)
if f.contentType == formType {
return f.values.Get(k)
} else {
if v, ok := f.json[k]; ok {
return v.(string)
}

return ""
}
}

// Err returns an Error object extracted from the response.
Expand Down Expand Up @@ -71,19 +83,23 @@ func PostForm(c httpClient, u string, params url.Values) (*FormResponse, error)
requestURI: u,
}

if contentType(resp.Header.Get("Content-Type")) == formType {
var bb []byte
bb, err = ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}
var bb []byte
bb, err = ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}

ct := contentType(resp.Header.Get("Content-Type"))
r.contentType = ct
if ct == formType {
r.values, err = url.ParseQuery(string(bb))
if err != nil {
return r, err
}
} else {
_, err = io.Copy(ioutil.Discard, resp.Body)
} else if ct == jsonType {
jObj := make(map[string]interface{})
err := json.Unmarshal(bb, &jObj)

This comment was marked as spam.

r.json = jObj
if err != nil {
return r, err
}
Expand All @@ -93,6 +109,7 @@ func PostForm(c httpClient, u string, params url.Values) (*FormResponse, error)
}

const formType = "application/x-www-form-urlencoded"
const jsonType = "application/json"

func contentType(t string) string {
if i := strings.IndexRune(t, ';'); i >= 0 {
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/cli/browser v1.0.0 h1:RIleZgXrhdiCVgFBSjtWwkLPUCWyhhhN5k5HGSBt1js=
github.com/cli/browser v1.0.0/go.mod h1:IEWkHYbLjkhtjwwWlwTHW2lGxeS5gezEQBMLTwDHf5Q=
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
25 changes: 16 additions & 9 deletions webapp/webapp_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ func (flow *Flow) StartServer(writeSuccess func(io.Writer)) error {
return flow.server.Serve()
}

// AccessToken blocks until the browser flow has completed and returns the access token.
func (flow *Flow) AccessToken(c httpClient, tokenURL, clientSecret string) (*api.AccessToken, error) {
func (flow *Flow) AccessTokenWithParams(c httpClient, tokenURL, clientSecret string, postParams url.Values) (*api.AccessToken, error) {
code, err := flow.server.WaitForCode()
if err != nil {
return nil, err
Expand All @@ -94,20 +93,28 @@ func (flow *Flow) AccessToken(c httpClient, tokenURL, clientSecret string) (*api
return nil, errors.New("state mismatch")
}

resp, err := api.PostForm(c, tokenURL,
url.Values{
"client_id": {flow.clientID},
"client_secret": {clientSecret},
"code": {code.Code},
"state": {flow.state},
})
if postParams == nil {
postParams = url.Values{}
}

postParams["client_id"] = []string{flow.clientID}
postParams["client_secret"] = []string{clientSecret}
postParams["code"] = []string{code.Code}
postParams["state"] = []string{flow.state}

resp, err := api.PostForm(c, tokenURL, postParams)
if err != nil {
return nil, err
}

return resp.AccessToken()
}

// AccessToken blocks until the browser flow has completed and returns the access token.
func (flow *Flow) AccessToken(c httpClient, tokenURL, clientSecret string) (*api.AccessToken, error) {
return flow.AccessTokenWithParams(c, tokenURL, clientSecret, nil)
}

func randomString(length int) (string, error) {
b := make([]byte, length/2)
_, err := rand.Read(b)
Expand Down