diff --git a/.golangci.yml b/.golangci.yml index 867515a..5e88798 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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 diff --git a/internal/auth/delivery/http/auth_http.go b/internal/auth/delivery/http/auth_http.go index c71cdb1..375fe9e 100644 --- a/internal/auth/delivery/http/auth_http.go +++ b/internal/auth/delivery/http/auth_http.go @@ -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) diff --git a/internal/auth/delivery/http/auth_http_test.go b/internal/auth/delivery/http/auth_http_test.go index 55fab55..4b2d905 100644 --- a/internal/auth/delivery/http/auth_http_test.go +++ b/internal/auth/delivery/http/auth_http_test.go @@ -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, diff --git a/internal/client/delivery/http/client_http.go b/internal/client/delivery/http/client_http.go index 1b37615..b4a7d83 100644 --- a/internal/client/delivery/http/client_http.go +++ b/internal/client/delivery/http/client_http.go @@ -87,6 +87,7 @@ func (h *RequestHandler) handleRender(ctx *http.RequestCtx) { }) } +//nolint: funlen func (h *RequestHandler) handleCallback(ctx *http.RequestCtx) { ctx.SetContentType(common.MIMETextHTMLCharsetUTF8) diff --git a/internal/client/delivery/http/client_http_test.go b/internal/client/delivery/http/client_http_test.go index 12a4c6f..b46017b 100644 --- a/internal/client/delivery/http/client_http_test.go +++ b/internal/client/delivery/http/client_http_test.go @@ -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, diff --git a/internal/domain/code_challenge_method.go b/internal/domain/code_challenge_method.go index 3c555ab..b3cb355 100644 --- a/internal/domain/code_challenge_method.go +++ b/internal/domain/code_challenge_method.go @@ -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)) } diff --git a/internal/domain/code_challenge_method_test.go b/internal/domain/code_challenge_method_test.go index 4fb6782..ea3d781 100644 --- a/internal/domain/code_challenge_method_test.go +++ b/internal/domain/code_challenge_method_test.go @@ -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) } } diff --git a/internal/domain/token.go b/internal/domain/token.go index 9e3c011..30e9bde 100644 --- a/internal/domain/token.go +++ b/internal/domain/token.go @@ -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 diff --git a/internal/health/delivery/http/health_http.go b/internal/health/delivery/http/health_http.go index a0f4486..84658d3 100644 --- a/internal/health/delivery/http/health_http.go +++ b/internal/health/delivery/http/health_http.go @@ -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{} diff --git a/internal/metadata/delivery/http/metadata_http.go b/internal/metadata/delivery/http/metadata_http.go index d49f74f..a28b988 100644 --- a/internal/metadata/delivery/http/metadata_http.go +++ b/internal/metadata/delivery/http/metadata_http.go @@ -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 ( diff --git a/internal/metadata/repository/http/http_metadata.go b/internal/metadata/repository/http/http_metadata.go index 082f64d..88c7954 100644 --- a/internal/metadata/repository/http/http_metadata.go +++ b/internal/metadata/repository/http/http_metadata.go @@ -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, diff --git a/internal/profile/repository/http/http_profile.go b/internal/profile/repository/http/http_profile.go index 57a35ba..1107b13 100644 --- a/internal/profile/repository/http/http_profile.go +++ b/internal/profile/repository/http/http_profile.go @@ -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) diff --git a/internal/testing/httptest/httptest.go b/internal/testing/httptest/httptest.go index 0e205dc..b549cca 100644 --- a/internal/testing/httptest/httptest.go +++ b/internal/testing/httptest/httptest.go @@ -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" diff --git a/internal/testing/sqltest/sqltest.go b/internal/testing/sqltest/sqltest.go index feb7433..9816587 100644 --- a/internal/testing/sqltest/sqltest.go +++ b/internal/testing/sqltest/sqltest.go @@ -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{} diff --git a/internal/ticket/delivery/http/ticket_http.go b/internal/ticket/delivery/http/ticket_http.go index 38956be..09ecb96 100644 --- a/internal/ticket/delivery/http/ticket_http.go +++ b/internal/ticket/delivery/http/ticket_http.go @@ -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 { diff --git a/internal/ticket/delivery/http/ticket_http_test.go b/internal/ticket/delivery/http/ticket_http_test.go index bd07de5..8204f00 100644 --- a/internal/ticket/delivery/http/ticket_http_test.go +++ b/internal/ticket/delivery/http/ticket_http_test.go @@ -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, diff --git a/internal/token/delivery/http/token_http.go b/internal/token/delivery/http/token_http.go index 686f69c..d401ac9 100644 --- a/internal/token/delivery/http/token_http.go +++ b/internal/token/delivery/http/token_http.go @@ -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) diff --git a/internal/token/delivery/http/token_http_test.go b/internal/token/delivery/http/token_http_test.go index 2874f54..00cc148 100644 --- a/internal/token/delivery/http/token_http_test.go +++ b/internal/token/delivery/http/token_http_test.go @@ -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) } diff --git a/internal/token/usecase/token_ucase.go b/internal/token/usecase/token_ucase.go index 253c912..c2ee019 100644 --- a/internal/token/usecase/token_ucase.go +++ b/internal/token/usecase/token_ucase.go @@ -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 { diff --git a/internal/user/delivery/http/user_http.go b/internal/user/delivery/http/user_http.go index b08c7a9..99ad92b 100644 --- a/internal/user/delivery/http/user_http.go +++ b/internal/user/delivery/http/user_http.go @@ -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 } diff --git a/internal/user/delivery/http/user_http_test.go b/internal/user/delivery/http/user_http_test.go index 088547e..5eda63d 100644 --- a/internal/user/delivery/http/user_http_test.go +++ b/internal/user/delivery/http/user_http_test.go @@ -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), diff --git a/main.go b/main.go index 3178e8b..ee68a16 100644 --- a/main.go +++ b/main.go @@ -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) }