@@ -1,7 +1,6 @@
11package main
22
33import (
4− "bytes"
54 "crypto/aes"
65 "crypto/cipher"
76 "encoding/base64"
@@ -17,7 +16,6 @@ import (
1716 "strings"
1817 "time"
1918
20− "github.com/bitly/google_auth_proxy/api"
2119 "github.com/bitly/google_auth_proxy/providers"
2220)
2321
@@ -39,7 +37,6 @@ type OauthProxy struct {
3937
4038 redirectUrl *url.URL // the url to receive requests at
4139 provider providers.Provider
42− oauthRedemptionUrl *url.URL // endpoint to redeem the code
4340 oauthLoginUrl *url.URL // to redirect the user to
4441 oauthValidateUrl *url.URL // to validate the access token
4542 oauthScope string
@@ -143,21 +140,20 @@ func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
143140 CookieRefresh: opts.CookieRefresh,
144141 Validator: validator,
145142
146− clientID: opts.ClientID,
147− clientSecret: opts.ClientSecret,
148− oauthScope: opts.provider.Data().Scope,
149− provider: opts.provider,
150− oauthRedemptionUrl: opts.provider.Data().RedeemUrl,
151− oauthLoginUrl: opts.provider.Data().LoginUrl,
152− oauthValidateUrl: opts.provider.Data().ValidateUrl,
153− serveMux: serveMux,
154− redirectUrl: redirectUrl,
155− skipAuthRegex: opts.SkipAuthRegex,
156− compiledRegex: opts.CompiledRegex,
157− PassBasicAuth: opts.PassBasicAuth,
158− PassAccessToken: opts.PassAccessToken,
159− AesCipher: aes_cipher,
160− templates: loadTemplates(opts.CustomTemplatesDir),
143+ clientID: opts.ClientID,
144+ clientSecret: opts.ClientSecret,
145+ oauthScope: opts.provider.Data().Scope,
146+ provider: opts.provider,
147+ oauthLoginUrl: opts.provider.Data().LoginUrl,
148+ oauthValidateUrl: opts.provider.Data().ValidateUrl,
149+ serveMux: serveMux,
150+ redirectUrl: redirectUrl,
151+ skipAuthRegex: opts.SkipAuthRegex,
152+ compiledRegex: opts.CompiledRegex,
153+ PassBasicAuth: opts.PassBasicAuth,
154+ PassAccessToken: opts.PassAccessToken,
155+ AesCipher: aes_cipher,
156+ templates: loadTemplates(opts.CustomTemplatesDir),
161157 }
162158}
163159
@@ -200,29 +196,13 @@ func (p *OauthProxy) redeemCode(host, code string) (string, string, error) {
200196 if code == "" {
201197 return "", "", errors.New("missing code")
202198 }
203− params := url.Values{}
204− params.Add("redirect_uri", p.GetRedirectUrl(host))
205− params.Add("client_id", p.clientID)
206− params.Add("client_secret", p.clientSecret)
207− params.Add("code", code)
208− params.Add("grant_type", "authorization_code")
209− req, err := http.NewRequest("POST", p.oauthRedemptionUrl.String(), bytes.NewBufferString(params.Encode()))
210− if err != nil {
211− log.Printf("failed building request %s", err.Error())
212− return "", "", err
213− }
214− req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
215− json, err := api.Request(req)
216− if err != nil {
217− log.Printf("failed making request %s", err)
218− return "", "", err
219− }
220− access_token, err := json.Get("access_token").String()
199+ redirectUri := p.GetRedirectUrl(host)
200+ body, access_token, err := p.provider.Redeem(redirectUri, code)
221201 if err != nil {
222202 return "", "", err
223203 }
224204
225− email, err := p.provider.GetEmailAddress(json, access_token)
205+ email, err := p.provider.GetEmailAddress(body, access_token)
226206 if err != nil {
227207 return "", "", err
228208 }