Skip to content

Commit

Permalink
Cleaned up source to make golangci-lint pass (oauth2-proxy#418)
Browse files Browse the repository at this point in the history
* cleaned up source to make golangci-lint pass

* providers/azure_test.go: use build in POST constant

* options_test.go: do not export unnecessary variables

Co-authored-by: Joel Speed <[email protected]>
  • Loading branch information
theonewolf and JoelSpeed authored Mar 14, 2020
1 parent b1c81e2 commit fad6fff
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 33 deletions.
22 changes: 13 additions & 9 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ import (
"github.com/stretchr/testify/assert"
)

const cookieSecret = "foobar"
const clientID = "bazquux"
const clientSecret = "xyzzyplugh"

func testOptions() *Options {
o := NewOptions()
o.Upstreams = append(o.Upstreams, "http://127.0.0.1:8080/")
o.CookieSecret = "foobar"
o.ClientID = "bazquux"
o.ClientSecret = "xyzzyplugh"
o.CookieSecret = cookieSecret
o.ClientID = clientID
o.ClientSecret = clientSecret
o.EmailDomains = []string{"*"}
return o
}
Expand All @@ -45,15 +49,15 @@ func TestNewOptions(t *testing.T) {

func TestClientSecretFileOptionFails(t *testing.T) {
o := NewOptions()
o.CookieSecret = "foobar"
o.ClientID = "bazquux"
o.ClientSecretFile = "xyzzyplugh"
o.CookieSecret = cookieSecret
o.ClientID = clientID
o.ClientSecretFile = clientSecret
o.EmailDomains = []string{"*"}
err := o.Validate()
assert.NotEqual(t, nil, err)

p := o.provider.Data()
assert.Equal(t, "xyzzyplugh", p.ClientSecretFile)
assert.Equal(t, clientSecret, p.ClientSecretFile)
assert.Equal(t, "", p.ClientSecret)

s, err := p.GetClientSecret()
Expand All @@ -75,8 +79,8 @@ func TestClientSecretFileOption(t *testing.T) {
defer os.Remove(clientSecretFileName)

o := NewOptions()
o.CookieSecret = "foobar"
o.ClientID = "bazquux"
o.CookieSecret = cookieSecret
o.ClientID = clientID
o.ClientSecretFile = clientSecretFileName
o.EmailDomains = []string{"*"}
err = o.Validate()
Expand Down
1 change: 0 additions & 1 deletion providers/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (p *AzureProvider) Redeem(redirectURL, code string) (s *sessions.SessionSta
return
}


params := url.Values{}
params.Add("redirect_uri", redirectURL)
params.Add("client_id", p.ClientID)
Expand Down
4 changes: 2 additions & 2 deletions providers/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ func testAzureBackend(payload string) *httptest.Server {

return httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if (r.URL.Path != path || r.URL.RawQuery != query) && r.Method != "POST" {
if (r.URL.Path != path || r.URL.RawQuery != query) && r.Method != http.MethodPost {
w.WriteHeader(404)
} else if r.Method == "POST" && r.Body != nil {
} else if r.Method == http.MethodPost && r.Body != nil {
w.WriteHeader(200)
w.Write([]byte(payload))
} else if !IsAuthorizedInHeader(r.Header) {
Expand Down
7 changes: 3 additions & 4 deletions providers/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ func (p *OIDCProvider) findVerifiedIDToken(ctx context.Context, token *oauth2.To
}

if rawIDToken, present := getIDToken(); present {
verifiedIdToken, err := p.Verifier.Verify(ctx, rawIDToken)
return verifiedIdToken, err
} else {
return nil, nil
verifiedIDToken, err := p.Verifier.Verify(ctx, rawIDToken)
return verifiedIDToken, err
}
return nil, nil
}

func (p *OIDCProvider) createSessionState(token *oauth2.Token, idToken *oidc.IDToken) (*sessions.SessionState, error) {
Expand Down
33 changes: 17 additions & 16 deletions providers/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"

"golang.org/x/oauth2"

"github.com/bmizerany/assert"
Expand Down Expand Up @@ -58,7 +59,7 @@ var defaultIDToken idTokenClaims = idTokenClaims{
},
}

type fakeKeySetStub struct {}
type fakeKeySetStub struct{}

func (fakeKeySetStub) VerifySignature(_ context.Context, jwt string) (payload []byte, err error) {
decodeString, err := base64.RawURLEncoding.DecodeString(strings.Split(jwt, ".")[1])
Expand Down Expand Up @@ -98,7 +99,7 @@ func newOIDCProvider(serverURL *url.URL) *OIDCProvider {

p := &OIDCProvider{
ProviderData: providerData,
Verifier: oidc.NewVerifier(
Verifier: oidc.NewVerifier(
"https://issuer.example.com",
fakeKeySetStub{},
&oidc.Config{ClientID: clientID},
Expand Down Expand Up @@ -235,30 +236,30 @@ func TestOIDCProvider_findVerifiedIdToken(t *testing.T) {
defer server.Close()

token := newOauth2Token()
signedIdToken, _ := newSignedTestIDToken(defaultIDToken)
tokenWithIdToken := token.WithExtra(map[string]interface{}{
"id_token": signedIdToken,
signedIDToken, _ := newSignedTestIDToken(defaultIDToken)
tokenWithIDToken := token.WithExtra(map[string]interface{}{
"id_token": signedIDToken,
})

verifiedIdToken, err := provider.findVerifiedIDToken(context.Background(), tokenWithIdToken)
verifiedIDToken, err := provider.findVerifiedIDToken(context.Background(), tokenWithIDToken)
assert.Equal(t, true, err == nil)
assert.Equal(t, true, verifiedIdToken != nil)
assert.Equal(t, defaultIDToken.Issuer, verifiedIdToken.Issuer)
assert.Equal(t, defaultIDToken.Subject, verifiedIdToken.Subject)
assert.Equal(t, true, verifiedIDToken != nil)
assert.Equal(t, defaultIDToken.Issuer, verifiedIDToken.Issuer)
assert.Equal(t, defaultIDToken.Subject, verifiedIDToken.Subject)

// When the validation fails the response should be nil
defaultIDToken.Id = "this-id-fails-validation"
signedIdToken, _ = newSignedTestIDToken(defaultIDToken)
tokenWithIdToken = token.WithExtra(map[string]interface{}{
"id_token": signedIdToken,
signedIDToken, _ = newSignedTestIDToken(defaultIDToken)
tokenWithIDToken = token.WithExtra(map[string]interface{}{
"id_token": signedIDToken,
})

verifiedIdToken, err = provider.findVerifiedIDToken(context.Background(), tokenWithIdToken)
verifiedIDToken, err = provider.findVerifiedIDToken(context.Background(), tokenWithIDToken)
assert.Equal(t, errors.New("failed to verify signature: the validation failed for subject [123456789]"), err)
assert.Equal(t, true, verifiedIdToken == nil)
assert.Equal(t, true, verifiedIDToken == nil)

// When there is no id token in the oauth token
verifiedIdToken, err = provider.findVerifiedIDToken(context.Background(), newOauth2Token())
verifiedIDToken, err = provider.findVerifiedIDToken(context.Background(), newOauth2Token())
assert.Equal(t, nil, err)
assert.Equal(t, true, verifiedIdToken == nil)
assert.Equal(t, true, verifiedIDToken == nil)
}
3 changes: 2 additions & 1 deletion providers/provider_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package providers

import (
"errors"
"github.com/pusher/oauth2_proxy/pkg/logger"
"io/ioutil"
"net/url"

"github.com/pusher/oauth2_proxy/pkg/logger"
)

// ProviderData contains information required to configure all implementations
Expand Down

0 comments on commit fad6fff

Please sign in to comment.