🚨 Fixed linters warnings

develop
Maxim Lebedev 9 months ago
parent 289fb8987d
commit 5d311b5c8d
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
  1. 12
      .golangci.yml
  2. 7
      internal/auth/delivery/http/auth_http.go
  3. 3
      internal/auth/delivery/http/auth_http_test.go
  4. 1
      internal/client/delivery/http/client_http.go
  5. 2
      internal/client/delivery/http/client_http_test.go
  6. 8
      internal/domain/code_challenge_method.go
  7. 12
      internal/domain/code_challenge_method_test.go
  8. 2
      internal/domain/token.go
  9. 2
      internal/health/delivery/http/health_http.go
  10. 2
      internal/metadata/delivery/http/metadata_http.go
  11. 1
      internal/metadata/repository/http/http_metadata.go
  12. 1
      internal/profile/repository/http/http_profile.go
  13. 4
      internal/testing/httptest/httptest.go
  14. 2
      internal/testing/sqltest/sqltest.go
  15. 5
      internal/ticket/delivery/http/ticket_http.go
  16. 2
      internal/ticket/delivery/http/ticket_http_test.go
  17. 6
      internal/token/delivery/http/token_http.go
  18. 3
      internal/token/delivery/http/token_http_test.go
  19. 6
      internal/token/usecase/token_ucase.go
  20. 10
      internal/user/delivery/http/user_http.go
  21. 12
      internal/user/delivery/http/user_http_test.go
  22. 24
      main.go

@ -12,14 +12,20 @@ output:
sort-results: true
linters-settings:
gci:
local-prefixes: source.toby3d.me
sections:
- standard
- default
- prefix(source.toby3d.me)
section-separators:
- newLine
goimports:
local-prefixes: source.toby3d.me
ireturn:
allow:
- "(Repository|UseCase)$"
- error
- stdlib
- "(Repository|UseCase)$"
- "sqlmock.Sqlmock"
lll:
tab-width: 8
varnamelen:
@ -31,7 +37,9 @@ linters-settings:
- db # dataBase
- err # error
- i # index
- id
- ip
- j # alt index
- ln # listener
- me
- ok

@ -13,14 +13,14 @@ import (
"golang.org/x/text/language"
"golang.org/x/text/message"
"source.toby3d.me/toby3d/form"
"source.toby3d.me/toby3d/middleware"
"source.toby3d.me/toby3d/auth/internal/auth"
"source.toby3d.me/toby3d/auth/internal/client"
"source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/auth/internal/domain"
"source.toby3d.me/toby3d/auth/internal/profile"
"source.toby3d.me/toby3d/auth/web"
"source.toby3d.me/toby3d/form"
"source.toby3d.me/toby3d/middleware"
)
type (
@ -349,6 +349,7 @@ func NewAuthAuthorizationRequest() *AuthAuthorizationRequest {
}
}
//nolint: cyclop
func (r *AuthAuthorizationRequest) bind(ctx *http.RequestCtx) error {
indieAuthError := new(domain.Error)
if err := form.Unmarshal(ctx.QueryArgs(), r); err != nil {
@ -424,7 +425,7 @@ func NewAuthVerifyRequest() *AuthVerifyRequest {
}
}
//nolint: funlen
//nolint: funlen,cyclop
func (r *AuthVerifyRequest) bind(ctx *http.RequestCtx) error {
indieAuthError := new(domain.Error)

@ -49,7 +49,8 @@ func TestAuthorize(t *testing.T) {
deps.store.Store(path.Join(profilerepo.DefaultPathPrefix, me.String()), user.Profile)
deps.store.Store(path.Join(userrepo.DefaultPathPrefix, me.String()), user)
r := router.New() //nolint: varnamelen
r := router.New()
//nolint: exhaustivestruct
delivery.NewRequestHandler(delivery.NewRequestHandlerOptions{
Auth: deps.authService,
Clients: deps.clientService,

@ -87,6 +87,7 @@ func (h *RequestHandler) handleRender(ctx *http.RequestCtx) {
})
}
//nolint: funlen
func (h *RequestHandler) handleCallback(ctx *http.RequestCtx) {
ctx.SetContentType(common.MIMETextHTMLCharsetUTF8)

@ -37,7 +37,7 @@ func TestRead(t *testing.T) {
deps := NewDependencies(t)
r := router.New() //nolint: varnamelen
r := router.New()
delivery.NewRequestHandler(delivery.NewRequestHandlerOptions{
Client: deps.client,
Config: deps.config,

@ -127,12 +127,12 @@ func (ccm CodeChallengeMethod) Validate(codeChallenge, verifier string) bool {
return codeChallenge == verifier
}
h := ccm.hash
h.Reset() // WARN(toby3d): even hash.New contains something.
hash := ccm.hash
hash.Reset() // WARN(toby3d): even hash.New contains something.
if _, err := h.Write([]byte(verifier)); err != nil {
if _, err := hash.Write([]byte(verifier)); err != nil {
return false
}
return codeChallenge == base64.RawURLEncoding.EncodeToString(h.Sum(nil))
return codeChallenge == base64.RawURLEncoding.EncodeToString(hash.Sum(nil))
}

@ -144,14 +144,14 @@ func TestCodeChallengeMethod_Validate(t *testing.T) {
case domain.CodeChallengeMethodUndefined, domain.CodeChallengeMethodPLAIN:
codeChallenge = verifier
default:
h := tc.hash
h.Reset()
hash := tc.hash
hash.Reset()
if _, err := h.Write([]byte(verifier)); err != nil {
if _, err := hash.Write([]byte(verifier)); err != nil {
t.Error(err)
}
codeChallenge = base64.RawURLEncoding.EncodeToString(h.Sum(nil))
codeChallenge = base64.RawURLEncoding.EncodeToString(hash.Sum(nil))
}
if result := tc.in.Validate(codeChallenge, verifier); result != !tc.expError {
@ -169,7 +169,7 @@ func TestCodeChallengeMethod_Validate_IndieAuth(t *testing.T) {
"6f535c952339f0670311b4bbec5c41c00805e83291fc7eb15ca4963f82a4d57595787dcc6ee90571fb7789cbd521fe0178ed",
); !ok {
t.Errorf("Validate(%s, %s) = %t, want %t", "ALiMNf5FvF_LIWLhSkd9tjPKh3PEmai2OrdDBzrVZ3M",
"6f535c952339f0670311b4bbec5c41c00805e83291fc7eb15ca4963f82a4d57595787dcc6ee90571fb7789cbd521fe0178ed", ok, true,
)
"6f535c952339f0670311b4bbec5c41c00805e83291fc7eb15ca4963f82a4d57595787dcc6ee90571fb7789cbd521"+
"fe0178ed", ok, true)
}
}

@ -49,7 +49,7 @@ var DefaultNewTokenOptions = NewTokenOptions{
}
// NewToken create a new token by provided options.
//nolint: funlen,cyclop
//nolint: cyclop
func NewToken(opts NewTokenOptions) (*Token, error) {
if opts.NonceLength == 0 {
opts.NonceLength = DefaultNewTokenOptions.NonceLength

@ -4,8 +4,8 @@ import (
"github.com/fasthttp/router"
http "github.com/valyala/fasthttp"
"source.toby3d.me/toby3d/middleware"
"source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/middleware"
)
type RequestHandler struct{}

@ -5,9 +5,9 @@ import (
"github.com/goccy/go-json"
http "github.com/valyala/fasthttp"
"source.toby3d.me/toby3d/middleware"
"source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/auth/internal/domain"
"source.toby3d.me/toby3d/middleware"
)
type (

@ -72,6 +72,7 @@ func (repo *httpMetadataRepository) Get(ctx context.Context, me *domain.Me) (*do
return nil, fmt.Errorf("cannot unmarshal metadata configuration: %w", err)
}
//nolint: exhaustivestruct // TODO(toby3d)
return &domain.Metadata{
AuthorizationEndpoint: data.AuthorizationEndpoint,
AuthorizationResponseIssParameterSupported: data.AuthorizationResponseIssParameterSupported,

@ -32,6 +32,7 @@ func NewHTPPClientRepository(client *http.Client) profile.Repository {
}
}
//nolint: cyclop
func (repo *httpProfileRepository) Get(ctx context.Context, me *domain.Me) (*domain.Profile, error) {
req := http.AcquireRequest()
defer http.ReleaseRequest(req)

@ -3,9 +3,7 @@ package httptest
import (
"crypto/tls"
// used for running tests.
_ "embed"
_ "embed" // used for running tests without same import in "god object"
"net"
"testing"
"time"

@ -7,7 +7,7 @@ import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
_ "modernc.org/sqlite"
_ "modernc.org/sqlite" // used for running tests without same import in "god object"
)
type Time struct{}

@ -12,13 +12,13 @@ import (
"golang.org/x/text/language"
"golang.org/x/text/message"
"source.toby3d.me/toby3d/form"
"source.toby3d.me/toby3d/middleware"
"source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/auth/internal/domain"
"source.toby3d.me/toby3d/auth/internal/random"
"source.toby3d.me/toby3d/auth/internal/ticket"
"source.toby3d.me/toby3d/auth/web"
"source.toby3d.me/toby3d/form"
"source.toby3d.me/toby3d/middleware"
)
type (
@ -57,6 +57,7 @@ func NewRequestHandler(tickets ticket.UseCase, matcher language.Matcher, config
}
func (h *RequestHandler) Register(r *router.Router) {
//nolint: exhaustivestruct
chain := middleware.Chain{
middleware.CSRFWithConfig(middleware.CSRFConfig{
Skipper: func(ctx *http.RequestCtx) bool {

@ -79,7 +79,7 @@ func NewDependencies(tb testing.TB) Dependencies {
ticket := domain.TestTicket(tb)
token := domain.TestToken(tb)
r := router.New() //nolint: varnamelen
r := router.New()
// NOTE(toby3d): private resource
r.GET(ticket.Resource.URL().EscapedPath(), func(ctx *http.RequestCtx) {
ctx.SuccessString(common.MIMETextHTMLCharsetUTF8,

@ -9,12 +9,12 @@ import (
"github.com/lestrrat-go/jwx/jwa"
http "github.com/valyala/fasthttp"
"source.toby3d.me/toby3d/form"
"source.toby3d.me/toby3d/middleware"
"source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/auth/internal/domain"
"source.toby3d.me/toby3d/auth/internal/ticket"
"source.toby3d.me/toby3d/auth/internal/token"
"source.toby3d.me/toby3d/form"
"source.toby3d.me/toby3d/middleware"
)
type (
@ -138,6 +138,7 @@ func NewRequestHandler(tokens token.UseCase, tickets ticket.UseCase, config *dom
func (h *RequestHandler) Register(r *router.Router) {
chain := middleware.Chain{
//nolint: exhaustivestruct
middleware.JWTWithConfig(middleware.JWTConfig{
AuthScheme: "Bearer",
ContextKey: "token",
@ -227,6 +228,7 @@ func (h *RequestHandler) handleAction(ctx *http.RequestCtx) {
}
}
//nolint: funlen
func (h *RequestHandler) handleExchange(ctx *http.RequestCtx) {
ctx.SetContentType(common.MIMEApplicationJSONCharsetUTF8)

@ -76,9 +76,6 @@ func TestIntrospection(t *testing.T) {
result := new(delivery.TokenIntrospectResponse)
if err := json.Unmarshal(resp.Body(), result); err != nil {
e := err.(*json.SyntaxError)
t.Logf("%s\noffset: %d", resp.Body(), e.Offset)
t.Fatal(err)
}

@ -41,8 +41,10 @@ func NewTokenUseCase(config Config) token.UseCase {
}
}
//nolint: cyclop
func (uc *tokenUseCase) Exchange(ctx context.Context, opts token.ExchangeOptions) (*domain.Token, *domain.Profile,
error) {
error,
) {
session, err := uc.sessions.GetAndDelete(ctx, opts.Code)
if err != nil {
return nil, nil, fmt.Errorf("cannot get session from store: %w", err)
@ -127,7 +129,7 @@ func (uc *tokenUseCase) Verify(ctx context.Context, accessToken string) (*domain
profile, err := uc.profiles.Get(ctx, result.Me)
if err != nil {
return result, nil, nil
return result, nil, nil //nolint: nilerr // it's okay to return result without profile
}
if !result.Scope.Has(domain.ScopeEmail) && len(profile.Email) > 0 {

@ -8,10 +8,10 @@ import (
"github.com/lestrrat-go/jwx/jwa"
http "github.com/valyala/fasthttp"
"source.toby3d.me/toby3d/middleware"
"source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/auth/internal/domain"
"source.toby3d.me/toby3d/auth/internal/token"
"source.toby3d.me/toby3d/middleware"
)
type (
@ -37,6 +37,7 @@ func NewRequestHandler(tokens token.UseCase, config *domain.Config) *RequestHand
func (h *RequestHandler) Register(r *router.Router) {
chain := middleware.Chain{
//nolint: exhaustivestruct
middleware.JWTWithConfig(middleware.JWTConfig{
AuthScheme: "Bearer",
ContextKey: "token",
@ -62,7 +63,7 @@ func (h *RequestHandler) handleUserInformation(ctx *http.RequestCtx) {
if err != nil || tkn == nil {
// WARN(toby3d): If the token is not valid, the endpoint still
// MUST return a 200 Response.
_ = encoder.Encode(err)
_ = encoder.Encode(err) //nolint: errchkjson
return
}
@ -70,6 +71,7 @@ func (h *RequestHandler) handleUserInformation(ctx *http.RequestCtx) {
if !tkn.Scope.Has(domain.ScopeProfile) {
ctx.SetStatusCode(http.StatusForbidden)
//nolint: errchkjson
_ = encoder.Encode(domain.NewError(
domain.ErrorCodeInsufficientScope,
"token with 'profile' scope is required to view profile data",
@ -81,7 +83,7 @@ func (h *RequestHandler) handleUserInformation(ctx *http.RequestCtx) {
resp := new(UserInformationResponse)
if userInfo == nil {
_ = encoder.Encode(resp)
_ = encoder.Encode(resp) //nolint: errchkjson
return
}
@ -102,5 +104,5 @@ func (h *RequestHandler) handleUserInformation(ctx *http.RequestCtx) {
resp.Email = userInfo.GetEmail().String()
}
_ = encoder.Encode(resp)
_ = encoder.Encode(resp) //nolint: errchkjson
}

@ -78,11 +78,13 @@ func NewDependencies(tb testing.TB) Dependencies {
config := domain.TestConfig(tb)
return Dependencies{
profile: domain.TestProfile(tb),
token: domain.TestToken(tb),
config: config,
store: store,
tokens: tokenrepo.NewMemoryTokenRepository(store),
config: config,
profile: domain.TestProfile(tb),
profiles: profilerepo.NewMemoryProfileRepository(store),
sessions: sessionrepo.NewMemorySessionRepository(store, config),
store: store,
token: domain.TestToken(tb),
tokens: tokenrepo.NewMemoryTokenRepository(store),
tokenService: tokenucase.NewTokenUseCase(tokenucase.Config{
Config: config,
Profiles: profilerepo.NewMemoryProfileRepository(store),

@ -42,6 +42,7 @@ import (
metadatahttpdelivery "source.toby3d.me/toby3d/auth/internal/metadata/delivery/http"
"source.toby3d.me/toby3d/auth/internal/profile"
profilehttprepo "source.toby3d.me/toby3d/auth/internal/profile/repository/http"
profileucase "source.toby3d.me/toby3d/auth/internal/profile/usecase"
"source.toby3d.me/toby3d/auth/internal/session"
sessionmemoryrepo "source.toby3d.me/toby3d/auth/internal/session/repository/memory"
sessionsqlite3repo "source.toby3d.me/toby3d/auth/internal/session/repository/sqlite3"
@ -66,6 +67,7 @@ type (
matcher language.Matcher
sessions session.UseCase
tickets ticket.UseCase
profiles profile.UseCase
tokens token.UseCase
}
@ -193,7 +195,7 @@ func main() {
opts.Clients = clienthttprepo.NewHTTPClientRepository(opts.Client)
opts.Profiles = profilehttprepo.NewHTPPClientRepository(opts.Client)
r := router.New() //nolint: varnamelen
r := router.New()
NewApp(opts).Register(r)
//nolint: exhaustivestruct// too many options
r.ServeFilesCustom(path.Join(config.Server.StaticURLPrefix, "{filepath:*}"), &http.FS{
@ -275,6 +277,7 @@ func NewApp(opts NewAppOptions) *App {
auth: authucase.NewAuthUseCase(opts.Sessions, opts.Profiles, config),
clients: clientucase.NewClientUseCase(opts.Clients),
matcher: language.NewMatcher(message.DefaultCatalog.Languages()),
profiles: profileucase.NewProfileUseCase(opts.Profiles),
sessions: sessionucase.NewSessionUseCase(opts.Sessions),
tickets: ticketucase.NewTicketUseCase(opts.Tickets, opts.Client, config),
tokens: tokenucase.NewTokenUseCase(tokenucase.Config{
@ -316,14 +319,8 @@ func (app *App) Register(r *router.Router) {
domain.ScopeRead,
domain.ScopeUpdate,
},
ResponseTypesSupported: []domain.ResponseType{
domain.ResponseTypeCode,
domain.ResponseTypeID,
},
GrantTypesSupported: []domain.GrantType{
domain.GrantTypeAuthorizationCode,
domain.GrantTypeTicket,
},
ResponseTypesSupported: []domain.ResponseType{domain.ResponseTypeCode, domain.ResponseTypeID},
GrantTypesSupported: []domain.GrantType{domain.GrantTypeAuthorizationCode, domain.GrantTypeTicket},
CodeChallengeMethodsSupported: []domain.CodeChallengeMethod{
domain.CodeChallengeMethodMD5,
domain.CodeChallengeMethodPLAIN,
@ -341,10 +338,11 @@ func (app *App) Register(r *router.Router) {
Tokens: app.tokens,
}).Register(r)
authhttpdelivery.NewRequestHandler(authhttpdelivery.NewRequestHandlerOptions{
Auth: app.auth,
Clients: app.clients,
Config: config,
Matcher: app.matcher,
Auth: app.auth,
Clients: app.clients,
Config: config,
Matcher: app.matcher,
Profiles: app.profiles,
}).Register(r)
userhttpdelivery.NewRequestHandler(app.tokens, config).Register(r)
}

Loading…
Cancel
Save