Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Aug 3, 2023
1 parent 397d9a1 commit 9601a90
Show file tree
Hide file tree
Showing 40 changed files with 223 additions and 262 deletions.
15 changes: 5 additions & 10 deletions access_request_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package fosite_test

import (
"context"
"encoding/base64"
"fmt"
"net/http"
Expand All @@ -29,8 +28,6 @@ func TestNewAccessRequest(t *testing.T) {
hasher := internal.NewMockHasher(ctrl)
defer ctrl.Finish()

ctx := gomock.AssignableToTypeOf(context.WithValue(context.TODO(), ContextKey("test"), nil))

client := &DefaultClient{}
config := &Config{ClientSecretsHasher: hasher, AudienceMatchingStrategy: DefaultAudienceMatchingStrategy}
fosite := &Fosite{Store: store, Config: config}
Expand Down Expand Up @@ -121,7 +118,7 @@ func TestNewAccessRequest(t *testing.T) {
store.EXPECT().GetClient(gomock.Any(), gomock.Eq("foo")).Return(client, nil)
client.Public = false
client.Secret = []byte("foo")
hasher.EXPECT().Compare(ctx, gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(errors.New(""))
hasher.EXPECT().Compare(gomock.Any(), gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(errors.New(""))
},
handlers: TokenEndpointHandlers{handler},
},
Expand All @@ -138,7 +135,7 @@ func TestNewAccessRequest(t *testing.T) {
store.EXPECT().GetClient(gomock.Any(), gomock.Eq("foo")).Return(client, nil)
client.Public = false
client.Secret = []byte("foo")
hasher.EXPECT().Compare(ctx, gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil)
hasher.EXPECT().Compare(gomock.Any(), gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil)
handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(ErrServerError)
},
handlers: TokenEndpointHandlers{handler},
Expand All @@ -155,7 +152,7 @@ func TestNewAccessRequest(t *testing.T) {
store.EXPECT().GetClient(gomock.Any(), gomock.Eq("foo")).Return(client, nil)
client.Public = false
client.Secret = []byte("foo")
hasher.EXPECT().Compare(ctx, gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil)
hasher.EXPECT().Compare(gomock.Any(), gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil)
handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
},
handlers: TokenEndpointHandlers{handler},
Expand Down Expand Up @@ -355,8 +352,6 @@ func TestNewAccessRequestWithMixedClientAuth(t *testing.T) {
hasher := internal.NewMockHasher(ctrl)
defer ctrl.Finish()

ctx := gomock.AssignableToTypeOf(context.WithValue(context.TODO(), ContextKey("test"), nil))

client := &DefaultClient{}
config := &Config{ClientSecretsHasher: hasher, AudienceMatchingStrategy: DefaultAudienceMatchingStrategy}
fosite := &Fosite{Store: store, Config: config}
Expand All @@ -380,7 +375,7 @@ func TestNewAccessRequestWithMixedClientAuth(t *testing.T) {
store.EXPECT().GetClient(gomock.Any(), gomock.Eq("foo")).Return(client, nil)
client.Public = false
client.Secret = []byte("foo")
hasher.EXPECT().Compare(ctx, gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(errors.New("hash err"))
hasher.EXPECT().Compare(gomock.Any(), gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(errors.New("hash err"))
handlerWithoutClientAuth.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
},
method: "POST",
Expand All @@ -398,7 +393,7 @@ func TestNewAccessRequestWithMixedClientAuth(t *testing.T) {
store.EXPECT().GetClient(gomock.Any(), gomock.Eq("foo")).Return(client, nil)
client.Public = false
client.Secret = []byte("foo")
hasher.EXPECT().Compare(ctx, gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil)
hasher.EXPECT().Compare(gomock.Any(), gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil)
handlerWithoutClientAuth.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
handlerWithClientAuth.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any()).Return(nil)
},
Expand Down
4 changes: 2 additions & 2 deletions authorize_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package fosite_test
import (
"bytes"
"context"
"io/ioutil"
"io"
"net/url"
"strings"
"testing"
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestWriteAuthorizeFormPostResponse(t *testing.T) {
redirectURL := "https://localhost:8080/cb"
//parameters :=
fosite.WriteAuthorizeFormPostResponse(redirectURL, c.parameters, fosite.DefaultFormPostTemplate, &responseBuffer)
code, state, _, _, customParams, _, err := internal.ParseFormPostResponse(redirectURL, ioutil.NopCloser(bytes.NewReader(responseBuffer.Bytes())))
code, state, _, _, customParams, _, err := internal.ParseFormPostResponse(redirectURL, io.NopCloser(bytes.NewReader(responseBuffer.Bytes())))
assert.NoError(t, err, "case %d", d)
c.check(code, state, customParams, d)

Expand Down
4 changes: 2 additions & 2 deletions authorize_request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package fosite
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"

Expand Down Expand Up @@ -76,7 +76,7 @@ func (f *Fosite) authorizeRequestParametersFromOpenIDConnectRequest(ctx context.
return errorsx.WithStack(ErrInvalidRequestURI.WithHintf("Unable to fetch OpenID Connect request parameters from 'request_uri' because status code '%d' was expected, but got '%d'.", http.StatusOK, response.StatusCode))
}

body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
return errorsx.WithStack(ErrInvalidRequestURI.WithHintf("Unable to fetch OpenID Connect request parameters from 'request_uri' because body parsing failed with: %s.", err).WithWrap(err).WithDebug(err.Error()))
}
Expand Down
7 changes: 3 additions & 4 deletions client_authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ func TestAuthenticateClient(t *testing.T) {
},
}

var h http.HandlerFunc
h = func(w http.ResponseWriter, r *http.Request) {
var h http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, json.NewEncoder(w).Encode(rsaJwks))
}
ts := httptest.NewServer(h)
Expand Down Expand Up @@ -586,12 +585,12 @@ func TestAuthenticateClientTwice(t *testing.T) {
"aud": "token-url",
}, key, "kid-foo")}, "client_assertion_type": []string{at}}

c, err := f.AuthenticateClient(nil, new(http.Request), formValues)
c, err := f.AuthenticateClient(context.Background(), new(http.Request), formValues)
require.NoError(t, err, "%#v", err)
assert.Equal(t, client, c)

// replay the request and expect it to fail
c, err = f.AuthenticateClient(nil, new(http.Request), formValues)
c, err = f.AuthenticateClient(context.Background(), new(http.Request), formValues)
require.Error(t, err)
assert.EqualError(t, err, ErrJTIKnown.Error())
assert.Nil(t, c)
Expand Down
3 changes: 2 additions & 1 deletion handler/oauth2/flow_authorize_code_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package oauth2

import (
"context"
"net/url"
"strings"
"testing"
Expand Down Expand Up @@ -161,7 +162,7 @@ func TestAuthorizeCode_HandleAuthorizeEndpointRequest(t *testing.T) {
} {
t.Run("case="+c.description, func(t *testing.T) {
aresp := fosite.NewAuthorizeResponse()
err := c.handler.HandleAuthorizeEndpointRequest(nil, c.areq, aresp)
err := c.handler.HandleAuthorizeEndpointRequest(context.Background(), c.areq, aresp)
if c.expectErr != nil {
require.EqualError(t, err, c.expectErr.Error())
} else {
Expand Down
44 changes: 22 additions & 22 deletions handler/oauth2/flow_authorize_code_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestAuthorizeCode_PopulateTokenEndpointResponse(t *testing.T) {
},
description: "should fail because authcode not found",
setup: func(t *testing.T, areq *fosite.AccessRequest, config *fosite.Config) {
code, _, err := strategy.GenerateAuthorizeCode(nil, nil)
code, _, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
areq.Form.Set("code", code)
},
Expand All @@ -79,7 +79,7 @@ func TestAuthorizeCode_PopulateTokenEndpointResponse(t *testing.T) {
},
description: "should fail because validation failed",
setup: func(t *testing.T, areq *fosite.AccessRequest, config *fosite.Config) {
require.NoError(t, store.CreateAuthorizeCodeSession(nil, "bar", areq))
require.NoError(t, store.CreateAuthorizeCodeSession(context.Background(), "bar", areq))
},
expectErr: fosite.ErrInvalidRequest,
},
Expand All @@ -97,11 +97,11 @@ func TestAuthorizeCode_PopulateTokenEndpointResponse(t *testing.T) {
},
},
setup: func(t *testing.T, areq *fosite.AccessRequest, config *fosite.Config) {
code, sig, err := strategy.GenerateAuthorizeCode(nil, nil)
code, sig, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
areq.Form.Add("code", code)

require.NoError(t, store.CreateAuthorizeCodeSession(nil, sig, areq))
require.NoError(t, store.CreateAuthorizeCodeSession(context.Background(), sig, areq))
},
description: "should pass with offline scope and refresh token",
check: func(t *testing.T, aresp *fosite.AccessResponse) {
Expand All @@ -127,11 +127,11 @@ func TestAuthorizeCode_PopulateTokenEndpointResponse(t *testing.T) {
},
setup: func(t *testing.T, areq *fosite.AccessRequest, config *fosite.Config) {
config.RefreshTokenScopes = []string{}
code, sig, err := strategy.GenerateAuthorizeCode(nil, nil)
code, sig, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
areq.Form.Add("code", code)

require.NoError(t, store.CreateAuthorizeCodeSession(nil, sig, areq))
require.NoError(t, store.CreateAuthorizeCodeSession(context.Background(), sig, areq))
},
description: "should pass with refresh token always provided",
check: func(t *testing.T, aresp *fosite.AccessResponse) {
Expand All @@ -157,11 +157,11 @@ func TestAuthorizeCode_PopulateTokenEndpointResponse(t *testing.T) {
},
setup: func(t *testing.T, areq *fosite.AccessRequest, config *fosite.Config) {
config.RefreshTokenScopes = []string{}
code, sig, err := strategy.GenerateAuthorizeCode(nil, nil)
code, sig, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
areq.Form.Add("code", code)

require.NoError(t, store.CreateAuthorizeCodeSession(nil, sig, areq))
require.NoError(t, store.CreateAuthorizeCodeSession(context.Background(), sig, areq))
},
description: "should pass with no refresh token",
check: func(t *testing.T, aresp *fosite.AccessResponse) {
Expand All @@ -186,11 +186,11 @@ func TestAuthorizeCode_PopulateTokenEndpointResponse(t *testing.T) {
},
},
setup: func(t *testing.T, areq *fosite.AccessRequest, config *fosite.Config) {
code, sig, err := strategy.GenerateAuthorizeCode(nil, nil)
code, sig, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
areq.Form.Add("code", code)

require.NoError(t, store.CreateAuthorizeCodeSession(nil, sig, areq))
require.NoError(t, store.CreateAuthorizeCodeSession(context.Background(), sig, areq))
},
description: "should not have refresh token",
check: func(t *testing.T, aresp *fosite.AccessResponse) {
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestAuthorizeCode_PopulateTokenEndpointResponse(t *testing.T) {
}

aresp := fosite.NewAccessResponse()
err := h.PopulateTokenEndpointResponse(nil, c.areq, aresp)
err := h.PopulateTokenEndpointResponse(context.Background(), c.areq, aresp)

if c.expectErr != nil {
require.EqualError(t, err, c.expectErr.Error(), "%+v", err)
Expand Down Expand Up @@ -294,7 +294,7 @@ func TestAuthorizeCode_HandleTokenEndpointRequest(t *testing.T) {
},
description: "should fail because authcode could not be retrieved (1)",
setup: func(t *testing.T, areq *fosite.AccessRequest, authreq *fosite.AuthorizeRequest) {
token, _, err := strategy.GenerateAuthorizeCode(nil, nil)
token, _, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
areq.Form = url.Values{"code": {token}}
},
Expand Down Expand Up @@ -330,11 +330,11 @@ func TestAuthorizeCode_HandleTokenEndpointRequest(t *testing.T) {
},
description: "should fail because client mismatch",
setup: func(t *testing.T, areq *fosite.AccessRequest, authreq *fosite.AuthorizeRequest) {
token, signature, err := strategy.GenerateAuthorizeCode(nil, nil)
token, signature, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
areq.Form = url.Values{"code": {token}}

require.NoError(t, store.CreateAuthorizeCodeSession(nil, signature, authreq))
require.NoError(t, store.CreateAuthorizeCodeSession(context.Background(), signature, authreq))
},
expectErr: fosite.ErrInvalidGrant,
},
Expand All @@ -356,11 +356,11 @@ func TestAuthorizeCode_HandleTokenEndpointRequest(t *testing.T) {
},
description: "should fail because redirect uri was set during /authorize call, but not in /token call",
setup: func(t *testing.T, areq *fosite.AccessRequest, authreq *fosite.AuthorizeRequest) {
token, signature, err := strategy.GenerateAuthorizeCode(nil, nil)
token, signature, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
areq.Form = url.Values{"code": {token}}

require.NoError(t, store.CreateAuthorizeCodeSession(nil, signature, authreq))
require.NoError(t, store.CreateAuthorizeCodeSession(context.Background(), signature, authreq))
},
expectErr: fosite.ErrInvalidGrant,
},
Expand All @@ -384,11 +384,11 @@ func TestAuthorizeCode_HandleTokenEndpointRequest(t *testing.T) {
},
description: "should pass",
setup: func(t *testing.T, areq *fosite.AccessRequest, authreq *fosite.AuthorizeRequest) {
token, signature, err := strategy.GenerateAuthorizeCode(nil, nil)
token, signature, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)

areq.Form = url.Values{"code": {token}}
require.NoError(t, store.CreateAuthorizeCodeSession(nil, signature, authreq))
require.NoError(t, store.CreateAuthorizeCodeSession(context.Background(), signature, authreq))
},
},
{
Expand All @@ -409,12 +409,12 @@ func TestAuthorizeCode_HandleTokenEndpointRequest(t *testing.T) {
assert.Equal(t, time.Now().Add(time.Minute).UTC().Round(time.Second), areq.GetSession().GetExpiresAt(fosite.RefreshToken))
},
setup: func(t *testing.T, areq *fosite.AccessRequest, authreq *fosite.AuthorizeRequest) {
code, sig, err := strategy.GenerateAuthorizeCode(nil, nil)
code, sig, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
areq.Form.Add("code", code)

require.NoError(t, store.CreateAuthorizeCodeSession(nil, sig, areq))
require.NoError(t, store.InvalidateAuthorizeCodeSession(nil, sig))
require.NoError(t, store.CreateAuthorizeCodeSession(context.Background(), sig, areq))
require.NoError(t, store.InvalidateAuthorizeCodeSession(context.Background(), sig))
},
description: "should fail because code has been used already",
expectErr: fosite.ErrInvalidGrant,
Expand Down Expand Up @@ -457,7 +457,7 @@ func TestAuthorizeCodeTransactional_HandleTokenEndpointRequest(t *testing.T) {
RequestedAt: time.Now().UTC(),
},
}
token, _, err := strategy.GenerateAuthorizeCode(nil, nil)
token, _, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
request.Form = url.Values{"code": {token}}
response := fosite.NewAccessResponse()
Expand Down
17 changes: 9 additions & 8 deletions handler/oauth2/flow_authorize_implicit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package oauth2

import (
"context"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestAuthorizeImplicit_EndpointHandler(t *testing.T) {
GrantTypes: fosite.Arguments{"implicit"},
ResponseTypes: fosite.Arguments{"token"},
}
chgen.EXPECT().GenerateAccessToken(nil, areq).Return("", "", errors.New(""))
chgen.EXPECT().GenerateAccessToken(gomock.Any(), areq).Return("", "", errors.New(""))
},
expectErr: fosite.ErrServerError,
},
Expand Down Expand Up @@ -80,8 +81,8 @@ func TestAuthorizeImplicit_EndpointHandler(t *testing.T) {
description: "should fail because persistence failed",
setup: func() {
areq.RequestedAudience = fosite.Arguments{"https://www.ory.sh/api"}
chgen.EXPECT().GenerateAccessToken(nil, areq).AnyTimes().Return("access.ats", "ats", nil)
store.EXPECT().CreateAccessTokenSession(nil, "ats", gomock.Eq(areq.Sanitize([]string{}))).Return(errors.New(""))
chgen.EXPECT().GenerateAccessToken(gomock.Any(), areq).AnyTimes().Return("access.ats", "ats", nil)
store.EXPECT().CreateAccessTokenSession(gomock.Any(), "ats", gomock.Eq(areq.Sanitize([]string{}))).Return(errors.New(""))
},
expectErr: fosite.ErrServerError,
},
Expand All @@ -91,7 +92,7 @@ func TestAuthorizeImplicit_EndpointHandler(t *testing.T) {
areq.State = "state"
areq.GrantedScope = fosite.Arguments{"scope"}

store.EXPECT().CreateAccessTokenSession(nil, "ats", gomock.Eq(areq.Sanitize([]string{}))).AnyTimes().Return(nil)
store.EXPECT().CreateAccessTokenSession(gomock.Any(), "ats", gomock.Eq(areq.Sanitize([]string{}))).AnyTimes().Return(nil)

aresp.EXPECT().AddParameter("access_token", "access.ats")
aresp.EXPECT().AddParameter("expires_in", gomock.Any())
Expand All @@ -104,7 +105,7 @@ func TestAuthorizeImplicit_EndpointHandler(t *testing.T) {
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
c.setup()
err := h.HandleAuthorizeEndpointRequest(nil, areq, aresp)
err := h.HandleAuthorizeEndpointRequest(context.Background(), areq, aresp)
if c.expectErr != nil {
require.EqualError(t, err, c.expectErr.Error())
} else {
Expand Down Expand Up @@ -151,16 +152,16 @@ func TestDefaultResponseMode_AuthorizeImplicit_EndpointHandler(t *testing.T) {
TokenLifespans: &internal.TestLifespans,
}

store.EXPECT().CreateAccessTokenSession(nil, "ats", gomock.Eq(areq.Sanitize([]string{}))).AnyTimes().Return(nil)
store.EXPECT().CreateAccessTokenSession(gomock.Any(), "ats", gomock.Eq(areq.Sanitize([]string{}))).AnyTimes().Return(nil)

aresp.EXPECT().AddParameter("access_token", "access.ats")
aresp.EXPECT().AddParameter("expires_in", gomock.Any())
aresp.EXPECT().AddParameter("token_type", "bearer")
aresp.EXPECT().AddParameter("state", "state")
aresp.EXPECT().AddParameter("scope", "scope")
chgen.EXPECT().GenerateAccessToken(nil, areq).AnyTimes().Return("access.ats", "ats", nil)
chgen.EXPECT().GenerateAccessToken(gomock.Any(), areq).AnyTimes().Return("access.ats", "ats", nil)

err := h.HandleAuthorizeEndpointRequest(nil, areq, aresp)
err := h.HandleAuthorizeEndpointRequest(context.Background(), areq, aresp)
assert.NoError(t, err)
assert.Equal(t, fosite.ResponseModeFragment, areq.GetResponseMode())

Expand Down
Loading

0 comments on commit 9601a90

Please sign in to comment.