Skip to content

Commit 5b3cff9

Browse files
jamierocksmislav
andcommitted
Support OAuth servers using standard responses
Co-authored-by: Mislav Marohnić <[email protected]>
1 parent 6dfce11 commit 5b3cff9

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

api/form.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package api
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"io/ioutil"
8+
"mime"
79
"net/http"
810
"net/url"
9-
"strings"
11+
"strconv"
1012
)
1113

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

74-
if contentType(resp.Header.Get("Content-Type")) == formType {
76+
mediaType, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type"))
77+
switch mediaType {
78+
case "application/x-www-form-urlencoded":
7579
var bb []byte
7680
bb, err = ioutil.ReadAll(resp.Body)
7781
if err != nil {
@@ -82,7 +86,24 @@ func PostForm(c httpClient, u string, params url.Values) (*FormResponse, error)
8286
if err != nil {
8387
return r, err
8488
}
85-
} else {
89+
case "application/json":
90+
var values map[string]interface{}
91+
if err := json.NewDecoder(resp.Body).Decode(&values); err != nil {
92+
return r, err
93+
}
94+
95+
r.values = make(url.Values)
96+
for key, value := range values {
97+
switch v := value.(type) {
98+
case string:
99+
r.values.Set(key, v)
100+
case int64:
101+
r.values.Set(key, strconv.FormatInt(v, 10))
102+
case float64:
103+
r.values.Set(key, strconv.FormatFloat(v, 'f', -1, 64))
104+
}
105+
}
106+
default:
86107
_, err = io.Copy(ioutil.Discard, resp.Body)
87108
if err != nil {
88109
return r, err
@@ -91,12 +112,3 @@ func PostForm(c httpClient, u string, params url.Values) (*FormResponse, error)
91112

92113
return r, nil
93114
}
94-
95-
const formType = "application/x-www-form-urlencoded"
96-
97-
func contentType(t string) string {
98-
if i := strings.IndexRune(t, ';'); i >= 0 {
99-
return t[0:i]
100-
}
101-
return t
102-
}

api/form_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestPostForm(t *testing.T) {
141141
wantErr bool
142142
}{
143143
{
144-
name: "success",
144+
name: "success urlencoded",
145145
args: args{
146146
url: "https://github.com/oauth",
147147
},
@@ -160,6 +160,26 @@ func TestPostForm(t *testing.T) {
160160
},
161161
wantErr: false,
162162
},
163+
{
164+
name: "success JSON",
165+
args: args{
166+
url: "https://github.com/oauth",
167+
},
168+
http: apiClient{
169+
body: `{"access_token":"123abc", "scopes":"repo gist"}`,
170+
status: 200,
171+
contentType: "application/json; charset=utf-8",
172+
},
173+
want: &FormResponse{
174+
StatusCode: 200,
175+
requestURI: "https://github.com/oauth",
176+
values: url.Values{
177+
"access_token": {"123abc"},
178+
"scopes": {"repo gist"},
179+
},
180+
},
181+
wantErr: false,
182+
},
163183
{
164184
name: "HTML response",
165185
args: args{

0 commit comments

Comments
 (0)