Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sso-proxy: remove default provider and unused functions #87

Merged
merged 1 commit into from
Nov 5, 2018
Merged
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
11 changes: 6 additions & 5 deletions internal/proxy/oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,20 +423,21 @@ func (p *OAuthProxy) GetRedirectURL(host string) *url.URL {
return &u
}

func (p *OAuthProxy) redeemCode(host, code string) (s *providers.SessionState, err error) {
func (p *OAuthProxy) redeemCode(host, code string) (*providers.SessionState, error) {
if code == "" {
return nil, errors.New("missing code")
}
redirectURL := p.GetRedirectURL(host)
s, err = p.provider.Redeem(redirectURL.String(), code)
s, err := p.provider.Redeem(redirectURL.String(), code)
if err != nil {
return
return s, err
}

if s.Email == "" {
s.Email, err = p.provider.GetEmailAddress(s)
return s, errors.New("invalid email address")
}
return

return s, nil
}

// MakeSessionCookie constructs a session cookie given the request, an expiration time and the current time.
Expand Down
83 changes: 15 additions & 68 deletions internal/proxy/oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,51 +438,12 @@ func TestFavicon(t *testing.T) {
testutil.Equal(t, http.StatusNotFound, rw.Code)
}

type TestProvider struct {
*providers.ProviderData
EmailAddress string
ValidToken bool
}

func NewTestProvider(providerURL *url.URL, emailAddress string) *TestProvider {
return &TestProvider{
ProviderData: &providers.ProviderData{
ProviderName: "Test Provider",
SignInURL: &url.URL{
Scheme: "http",
Host: providerURL.Host,
Path: "/oauth/authorize",
},
RedeemURL: &url.URL{
Scheme: "http",
Host: providerURL.Host,
Path: "/oauth/token",
},
ProfileURL: &url.URL{
Scheme: "http",
Host: providerURL.Host,
Path: "/api/v1/profile",
},
Scope: "profile.email",
},
EmailAddress: emailAddress,
}
}

func (tp *TestProvider) GetEmailAddress(session *providers.SessionState) (string, error) {
return tp.EmailAddress, nil
}

func (tp *TestProvider) ValidateSessionState(session *providers.SessionState, g []string) bool {
return tp.ValidToken
}

type ProcessCookieTest struct {
opts *Options
proxy *OAuthProxy
rw *httptest.ResponseRecorder
req *http.Request
provider TestProvider
provider providers.TestProvider
responseCode int
validateUser bool
}
Expand All @@ -508,8 +469,8 @@ func NewProcessCookieTest(opts ProcessCookieTestOpts) *ProcessCookieTest {
return nil
})

pcTest.proxy.provider = &TestProvider{
ValidToken: opts.providerValidateCookieResponse,
pcTest.proxy.provider = &providers.TestProvider{
ValidateSessionFunc: func(*providers.SessionState, []string) bool { return opts.providerValidateCookieResponse },
}

pcTest.rw = httptest.NewRecorder()
Expand Down Expand Up @@ -695,7 +656,7 @@ func TestAuthSkippedForPreflightRequests(t *testing.T) {
opts.Validate()

upstreamURL, _ := url.Parse(upstream.URL)
opts.provider = NewTestProvider(upstreamURL, "")
opts.provider = providers.NewTestProvider(upstreamURL, "")

proxy, _ := NewOAuthProxy(opts)
rw := httptest.NewRecorder()
Expand Down Expand Up @@ -749,7 +710,7 @@ func TestAuthSkipRequests(t *testing.T) {
opts.Validate()

upstreamURL, _ := url.Parse(upstream.URL)
opts.provider = NewTestProvider(upstreamURL, "")
opts.provider = providers.NewTestProvider(upstreamURL, "")

proxy, _ := NewOAuthProxy(opts)

Expand Down Expand Up @@ -829,7 +790,7 @@ func TestMultiAuthSkipRequests(t *testing.T) {
opts.Validate()

upstreamFooURL, _ := url.Parse(upstreamFoo.URL)
opts.provider = NewTestProvider(upstreamFooURL, "")
opts.provider = providers.NewTestProvider(upstreamFooURL, "")

proxy, _ := NewOAuthProxy(opts)

Expand Down Expand Up @@ -921,7 +882,7 @@ func NewSignatureTest(key string) *SignatureTest {
}
provider := httptest.NewServer(http.HandlerFunc(providerHandler))
providerURL, _ := url.Parse(provider.URL)
opts.provider = NewTestProvider(providerURL, "email1@example.com")
opts.provider = providers.NewTestProvider(providerURL, "email1@example.com")
opts.upstreamConfigs = generateSignatureTestUpstreamConfigs(key, upstream.URL)
opts.Validate()

Expand Down Expand Up @@ -1041,7 +1002,7 @@ func TestHeadersSentToUpstreams(t *testing.T) {
opts.upstreamConfigs = generateTestUpstreamConfigs(upstream.URL)
opts.Validate()
providerURL, _ := url.Parse("http://sso-auth.example.com/")
opts.provider = NewTestProvider(providerURL, "")
opts.provider = providers.NewTestProvider(providerURL, "")

state := testSession()
state.Email = "foo@example.com"
Expand Down Expand Up @@ -1098,20 +1059,6 @@ func TestHeadersSentToUpstreams(t *testing.T) {

}

type testAuthenticateProvider struct {
*providers.ProviderData
refreshSessionFunc func(*providers.SessionState, []string) (bool, error)
validateSessionFunc func(*providers.SessionState, []string) bool
}

func (tap *testAuthenticateProvider) RefreshSession(s *providers.SessionState, g []string) (bool, error) {
return tap.refreshSessionFunc(s, g)
}

func (tap *testAuthenticateProvider) ValidateSessionState(s *providers.SessionState, g []string) bool {
return tap.validateSessionFunc(s, g)
}

func TestAuthenticate(t *testing.T) {
// Constants to represent possible cookie behaviors.
const (
Expand Down Expand Up @@ -1261,9 +1208,9 @@ func TestAuthenticate(t *testing.T) {
opts.upstreamConfigs = generateTestUpstreamConfigs("foo-internal.sso.dev")
opts.Validate()
proxy, _ := NewOAuthProxy(opts, testValidatorFunc(true), testCookieCipher(tc.Cipher))
proxy.provider = &testAuthenticateProvider{
refreshSessionFunc: tc.RefreshSessionFunc,
validateSessionFunc: tc.ValidateSessionFunc,
proxy.provider = &providers.TestProvider{
RefreshSessionFunc: tc.RefreshSessionFunc,
ValidateSessionFunc: tc.ValidateSessionFunc,
}

value, err := providers.MarshalSession(tc.Session, proxy.CookieCipher)
Expand Down Expand Up @@ -1518,7 +1465,7 @@ func TestPing(t *testing.T) {
opts.Validate()

providerURL, _ := url.Parse("http://sso-auth.example.com/")
opts.provider = NewTestProvider(providerURL, "")
opts.provider = providers.NewTestProvider(providerURL, "")

proxy, _ := NewOAuthProxy(opts)
state := testSession()
Expand Down Expand Up @@ -1597,7 +1544,7 @@ func TestSecurityHeaders(t *testing.T) {
opts.Validate()

providerURL, _ := url.Parse("http://sso-auth.example.com/")
opts.provider = NewTestProvider(providerURL, "")
opts.provider = providers.NewTestProvider(providerURL, "")

proxy, _ := NewOAuthProxy(opts, testValidatorFunc(true))

Expand Down Expand Up @@ -1741,7 +1688,7 @@ func TestHeaderOverrides(t *testing.T) {
opts.Validate()

providerURL, _ := url.Parse("http://sso-auth.example.com/")
opts.provider = NewTestProvider(providerURL, "")
opts.provider = providers.NewTestProvider(providerURL, "")

proxy, _ := NewOAuthProxy(opts, testValidatorFunc(true))

Expand Down Expand Up @@ -1785,7 +1732,7 @@ func TestHTTPSRedirect(t *testing.T) {
defer upstream.Close()

providerURL, _ := url.Parse("http://sso-auth.example.com/")
provider := NewTestProvider(providerURL, "")
provider := providers.NewTestProvider(providerURL, "")
state := testSession()

testCases := []struct {
Expand Down
40 changes: 0 additions & 40 deletions internal/proxy/providers/internal_util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package providers

import (
"io/ioutil"
"net/http"
"net/url"

log "github.com/buzzfeed/sso/internal/pkg/logging"
Expand Down Expand Up @@ -45,41 +43,3 @@ func stripParam(param, endpoint string) string {

return endpoint
}

// validateToken returns true if token is valid
func validateToken(p Provider, accessToken string, header http.Header) bool {
logger := log.NewLogEntry()

if accessToken == "" || p.Data().ValidateURL == nil {
return false
}
endpoint := p.Data().ValidateURL.String()
if len(header) == 0 {
params := url.Values{"access_token": {accessToken}}
endpoint = endpoint + "?" + params.Encode()
}

req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
logger.Error(err, "token validation request failed")
return false
}
req.Header = header

resp, err := httpClient.Do(req)
if err != nil {
logger.Error(err, "token validation request failed")
return false
}

body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
logger.Printf("%d GET %s %s", resp.StatusCode, stripToken(endpoint), body)

if resp.StatusCode == 200 {
return true
}
logger.WithHTTPStatus(resp.StatusCode).WithResponseBody(body).Info(
"token validation request failed")
return false
}
132 changes: 0 additions & 132 deletions internal/proxy/providers/internal_util_test.go

This file was deleted.

Loading