Skip to content

Commit

Permalink
Support OAuth servers using standard responses
Browse files Browse the repository at this point in the history
Co-authored-by: Mislav Marohnić <mislav@github.com>
  • Loading branch information
jamierocks and mislav committed Oct 15, 2021
1 parent 6dfce11 commit 5b3cff9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
36 changes: 24 additions & 12 deletions api/form.go
@@ -1,12 +1,14 @@
package api

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
"strings"
"strconv"
)

type httpClient interface {
Expand Down Expand Up @@ -71,7 +73,9 @@ func PostForm(c httpClient, u string, params url.Values) (*FormResponse, error)
requestURI: u,
}

if contentType(resp.Header.Get("Content-Type")) == formType {
mediaType, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type"))
switch mediaType {
case "application/x-www-form-urlencoded":
var bb []byte
bb, err = ioutil.ReadAll(resp.Body)
if err != nil {
Expand All @@ -82,7 +86,24 @@ func PostForm(c httpClient, u string, params url.Values) (*FormResponse, error)
if err != nil {
return r, err
}
} else {
case "application/json":
var values map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&values); err != nil {
return r, err
}

r.values = make(url.Values)
for key, value := range values {
switch v := value.(type) {
case string:
r.values.Set(key, v)
case int64:
r.values.Set(key, strconv.FormatInt(v, 10))
case float64:
r.values.Set(key, strconv.FormatFloat(v, 'f', -1, 64))
}
}
default:
_, err = io.Copy(ioutil.Discard, resp.Body)
if err != nil {
return r, err
Expand All @@ -91,12 +112,3 @@ func PostForm(c httpClient, u string, params url.Values) (*FormResponse, error)

return r, nil
}

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

func contentType(t string) string {
if i := strings.IndexRune(t, ';'); i >= 0 {
return t[0:i]
}
return t
}
22 changes: 21 additions & 1 deletion api/form_test.go
Expand Up @@ -141,7 +141,7 @@ func TestPostForm(t *testing.T) {
wantErr bool
}{
{
name: "success",
name: "success urlencoded",
args: args{
url: "https://github.com/oauth",
},
Expand All @@ -160,6 +160,26 @@ func TestPostForm(t *testing.T) {
},
wantErr: false,
},
{
name: "success JSON",
args: args{
url: "https://github.com/oauth",
},
http: apiClient{
body: `{"access_token":"123abc", "scopes":"repo gist"}`,
status: 200,
contentType: "application/json; charset=utf-8",
},
want: &FormResponse{
StatusCode: 200,
requestURI: "https://github.com/oauth",
values: url.Values{
"access_token": {"123abc"},
"scopes": {"repo gist"},
},
},
wantErr: false,
},
{
name: "HTML response",
args: args{
Expand Down

0 comments on commit 5b3cff9

Please sign in to comment.