1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-11-29 22:48:19 +02:00

Extract api package

This is the first step towards genericizing the google_auth_proxy to support
OAuth2 providers other than Google as discussed in #65. The `api` package will
enable multiple providers to use the same `api.Request()` implementation.
This commit is contained in:
Mike Bland
2015-03-30 10:23:30 -04:00
parent fd74eae9c3
commit 9d8f932797
3 changed files with 102 additions and 24 deletions

View File

@@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"log"
"net"
"net/http"
@@ -16,6 +15,7 @@ import (
"strings"
"time"
"github.com/18F/google_auth_proxy/api"
"github.com/bitly/go-simplejson"
)
@@ -171,28 +171,6 @@ func (p *OauthProxy) GetLoginURL(host, redirect string) string {
return fmt.Sprintf("%s?%s", p.oauthLoginUrl, params.Encode())
}
func apiRequest(req *http.Request) (*simplejson.Json, error) {
httpclient := &http.Client{}
resp, err := httpclient.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
log.Printf("got response code %d - %s", resp.StatusCode, body)
return nil, errors.New("api request returned non 200 status code")
}
data, err := simplejson.NewJson(body)
if err != nil {
return nil, err
}
return data, nil
}
func (p *OauthProxy) displayCustomLoginForm() bool {
return p.HtpasswdFile != nil && p.DisplayHtpasswdForm
}
@@ -213,7 +191,7 @@ func (p *OauthProxy) redeemCode(host, code string) (string, string, error) {
return "", "", err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
json, err := apiRequest(req)
json, err := api.Request(req)
if err != nil {
log.Printf("failed making request %s", err)
return "", "", err