🚨 Fixed linters warnings

This commit is contained in:
Maxim Lebedev 2022-03-25 23:07:52 +05:00
parent 289fb8987d
commit 5d311b5c8d
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
22 changed files with 71 additions and 56 deletions

View File

@ -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

View File

@ -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)

View File

@ -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,

View File

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

View File

@ -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,

View File

@ -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))
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -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{}

View File

@ -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 (

View File

@ -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,

View File

@ -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)

View File

@ -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"

View File

@ -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{}

View File

@ -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 {

View File

@ -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,

View File

@ -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)

View File

@ -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)
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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),

24
main.go
View File

@ -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)
}