diff --git a/auth/api/http/keys/transport.go b/auth/api/http/keys/transport.go index 30e3e11deec..7ba25f60dd6 100644 --- a/auth/api/http/keys/transport.go +++ b/auth/api/http/keys/transport.go @@ -10,7 +10,7 @@ import ( "net/http" "strings" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/auth" "github.com/absmach/supermq/pkg/errors" @@ -23,27 +23,27 @@ const contentType = "application/json" // MakeHandler returns a HTTP handler for API endpoints. func MakeHandler(svc auth.Service, mux *chi.Mux, logger *slog.Logger) *chi.Mux { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } mux.Route("/keys", func(r chi.Router) { r.Post("/", kithttp.NewServer( issueEndpoint(svc), decodeIssue, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Get("/{id}", kithttp.NewServer( (retrieveEndpoint(svc)), decodeKeyReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Delete("/{id}", kithttp.NewServer( (revokeEndpoint(svc)), decodeKeyReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) }) diff --git a/auth/api/http/pats/transport.go b/auth/api/http/pats/transport.go index 910bf323be7..8aa46b7f340 100644 --- a/auth/api/http/pats/transport.go +++ b/auth/api/http/pats/transport.go @@ -10,7 +10,7 @@ import ( "net/http" "strings" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/auth" "github.com/absmach/supermq/pkg/errors" @@ -27,20 +27,20 @@ const ( // MakeHandler returns a HTTP handler for API endpoints. func MakeHandler(svc auth.Service, mux *chi.Mux, logger *slog.Logger) *chi.Mux { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } mux.Route("/pats", func(r chi.Router) { r.Post("/", kithttp.NewServer( createPATEndpoint(svc), decodeCreatePATRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Get("/", kithttp.NewServer( listPATSEndpoint(svc), decodeListPATSRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) @@ -48,28 +48,28 @@ func MakeHandler(svc auth.Service, mux *chi.Mux, logger *slog.Logger) *chi.Mux { r.Get("/", kithttp.NewServer( retrievePATEndpoint(svc), decodeRetrievePATRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Patch("/name", kithttp.NewServer( updatePATNameEndpoint(svc), decodeUpdatePATNameRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Patch("/description", kithttp.NewServer( updatePATDescriptionEndpoint(svc), decodeUpdatePATDescriptionRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Delete("/", kithttp.NewServer( deletePATEndpoint(svc), decodeDeletePATRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) @@ -77,14 +77,14 @@ func MakeHandler(svc auth.Service, mux *chi.Mux, logger *slog.Logger) *chi.Mux { r.Patch("/reset", kithttp.NewServer( resetPATSecretEndpoint(svc), decodeResetPATSecretRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Patch("/revoke", kithttp.NewServer( revokePATSecretEndpoint(svc), decodeRevokePATSecretRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) }) @@ -93,21 +93,21 @@ func MakeHandler(svc auth.Service, mux *chi.Mux, logger *slog.Logger) *chi.Mux { r.Patch("/add", kithttp.NewServer( addPATScopeEntryEndpoint(svc), decodeAddPATScopeEntryRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Patch("/remove", kithttp.NewServer( removePATScopeEntryEndpoint(svc), decodeRemovePATScopeEntryRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Delete("/", kithttp.NewServer( clearPATAllScopeEntryEndpoint(svc), decodeClearPATAllScopeEntryRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) }) @@ -182,11 +182,11 @@ func decodeUpdatePATDescriptionRequest(_ context.Context, r *http.Request) (inte } func decodeListPATSRequest(_ context.Context, r *http.Request) (interface{}, error) { - l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + l, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + o, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } diff --git a/bootstrap/api/endpoint.go b/bootstrap/api/endpoint.go index 7889c1e98a6..77ff1a1ce97 100644 --- a/bootstrap/api/endpoint.go +++ b/bootstrap/api/endpoint.go @@ -6,7 +6,7 @@ package api import ( "context" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/bootstrap" "github.com/absmach/supermq/pkg/authn" @@ -22,7 +22,7 @@ func addEndpoint(svc bootstrap.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -65,7 +65,7 @@ func updateCertEndpoint(svc bootstrap.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -93,7 +93,7 @@ func viewEndpoint(svc bootstrap.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -134,7 +134,7 @@ func updateEndpoint(svc bootstrap.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -165,7 +165,7 @@ func updateConnEndpoint(svc bootstrap.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -190,7 +190,7 @@ func listEndpoint(svc bootstrap.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -240,7 +240,7 @@ func removeEndpoint(svc bootstrap.Service) endpoint.Endpoint { return removeRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -276,7 +276,7 @@ func stateEndpoint(svc bootstrap.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } diff --git a/bootstrap/api/transport.go b/bootstrap/api/transport.go index c5c9baacfff..923823c187f 100644 --- a/bootstrap/api/transport.go +++ b/bootstrap/api/transport.go @@ -12,7 +12,7 @@ import ( "strings" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/bootstrap" smqauthn "github.com/absmach/supermq/pkg/authn" @@ -42,64 +42,64 @@ var ( // MakeHandler returns a HTTP handler for API endpoints. func MakeHandler(svc bootstrap.Service, authn smqauthn.Authentication, reader bootstrap.ConfigReader, logger *slog.Logger, instanceID string) http.Handler { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } r := chi.NewRouter() r.Route("/{domainID}/clients", func(r chi.Router) { r.Group(func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, true)) + r.Use(httpapi.AuthenticateMiddleware(authn, true)) r.Route("/configs", func(r chi.Router) { r.Post("/", otelhttp.NewHandler(kithttp.NewServer( addEndpoint(svc), decodeAddRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "add").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( listEndpoint(svc), decodeListRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "list").ServeHTTP) r.Get("/{configID}", otelhttp.NewHandler(kithttp.NewServer( viewEndpoint(svc), decodeEntityRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "view").ServeHTTP) r.Put("/{configID}", otelhttp.NewHandler(kithttp.NewServer( updateEndpoint(svc), decodeUpdateRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "update").ServeHTTP) r.Delete("/{configID}", otelhttp.NewHandler(kithttp.NewServer( removeEndpoint(svc), decodeEntityRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "remove").ServeHTTP) r.Patch("/certs/{certID}", otelhttp.NewHandler(kithttp.NewServer( updateCertEndpoint(svc), decodeUpdateCertRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "update_cert").ServeHTTP) r.Put("/connections/{connID}", otelhttp.NewHandler(kithttp.NewServer( updateConnEndpoint(svc), decodeUpdateConnRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "update_connections").ServeHTTP) }) }) - r.With(api.AuthenticateMiddleware(authn, true)).Put("/state/{clientID}", otelhttp.NewHandler(kithttp.NewServer( + r.With(httpapi.AuthenticateMiddleware(authn, true)).Put("/state/{clientID}", otelhttp.NewHandler(kithttp.NewServer( stateEndpoint(svc), decodeStateRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "update_state").ServeHTTP) }) @@ -107,12 +107,12 @@ func MakeHandler(svc bootstrap.Service, authn smqauthn.Authentication, reader bo r.Get("/", otelhttp.NewHandler(kithttp.NewServer( bootstrapEndpoint(svc, reader, false), decodeBootstrapRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "bootstrap").ServeHTTP) r.Get("/{externalID}", otelhttp.NewHandler(kithttp.NewServer( bootstrapEndpoint(svc, reader, false), decodeBootstrapRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts...), "bootstrap").ServeHTTP) r.Get("/secure/{externalID}", otelhttp.NewHandler(kithttp.NewServer( bootstrapEndpoint(svc, reader, true), diff --git a/certs/api/transport.go b/certs/api/transport.go index 726e5379370..98c5975cba6 100644 --- a/certs/api/transport.go +++ b/certs/api/transport.go @@ -10,7 +10,7 @@ import ( "net/http" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/certs" smqauthn "github.com/absmach/supermq/pkg/authn" @@ -34,38 +34,38 @@ const ( // MakeHandler returns a HTTP handler for API endpoints. func MakeHandler(svc certs.Service, authn smqauthn.Authentication, logger *slog.Logger, instanceID string) http.Handler { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } r := chi.NewRouter() r.Group(func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, true)) + r.Use(httpapi.AuthenticateMiddleware(authn, true)) r.Route("/{domainID}", func(r chi.Router) { r.Route("/certs", func(r chi.Router) { r.Post("/", otelhttp.NewHandler(kithttp.NewServer( issueCert(svc), decodeCerts, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "issue").ServeHTTP) r.Get("/{certID}", otelhttp.NewHandler(kithttp.NewServer( viewCert(svc), decodeViewCert, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view").ServeHTTP) r.Delete("/{certID}", otelhttp.NewHandler(kithttp.NewServer( revokeCert(svc), decodeRevokeCerts, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "revoke").ServeHTTP) }) r.Get("/serials/{clientID}", otelhttp.NewHandler(kithttp.NewServer( listSerials(svc), decodeListCerts, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_serials").ServeHTTP) }) diff --git a/channels/api/http/decode.go b/channels/api/http/decode.go index ea662f8da0d..200e6b86f99 100644 --- a/channels/api/http/decode.go +++ b/channels/api/http/decode.go @@ -9,7 +9,7 @@ import ( "net/http" "strings" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" smqclients "github.com/absmach/supermq/clients" "github.com/absmach/supermq/pkg/errors" @@ -25,7 +25,7 @@ func decodeViewChannel(_ context.Context, r *http.Request) (interface{}, error) } func decodeCreateChannelReq(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -38,7 +38,7 @@ func decodeCreateChannelReq(_ context.Context, r *http.Request) (interface{}, er } func decodeCreateChannelsReq(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -51,40 +51,40 @@ func decodeCreateChannelsReq(_ context.Context, r *http.Request) (interface{}, e } func decodeListChannels(_ context.Context, r *http.Request) (interface{}, error) { - s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefClientStatus) + s, err := apiutil.ReadStringQuery(r, httpapi.StatusKey, httpapi.DefClientStatus) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + o, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + l, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - m, err := apiutil.ReadMetadataQuery(r, api.MetadataKey, nil) + m, err := apiutil.ReadMetadataQuery(r, httpapi.MetadataKey, nil) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - n, err := apiutil.ReadStringQuery(r, api.NameKey, "") + n, err := apiutil.ReadStringQuery(r, httpapi.NameKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - t, err := apiutil.ReadStringQuery(r, api.TagKey, "") + t, err := apiutil.ReadStringQuery(r, httpapi.TagKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - id, err := apiutil.ReadStringQuery(r, api.IDOrder, "") + id, err := apiutil.ReadStringQuery(r, httpapi.IDOrder, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - p, err := apiutil.ReadStringQuery(r, api.PermissionKey, api.DefPermission) + p, err := apiutil.ReadStringQuery(r, httpapi.PermissionKey, httpapi.DefPermission) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - lp, err := apiutil.ReadBoolQuery(r, api.ListPerms, api.DefListPerms) + lp, err := apiutil.ReadBoolQuery(r, httpapi.ListPerms, httpapi.DefListPerms) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } @@ -108,7 +108,7 @@ func decodeListChannels(_ context.Context, r *http.Request) (interface{}, error) } func decodeUpdateChannel(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -123,7 +123,7 @@ func decodeUpdateChannel(_ context.Context, r *http.Request) (interface{}, error } func decodeUpdateChannelTags(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -138,7 +138,7 @@ func decodeUpdateChannelTags(_ context.Context, r *http.Request) (interface{}, e } func decodeSetChannelParentGroupStatus(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -175,7 +175,7 @@ func decodeDeleteChannelReq(_ context.Context, r *http.Request) (interface{}, er } func decodeConnectChannelClientRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := connectChannelClientsRequest{ @@ -189,7 +189,7 @@ func decodeConnectChannelClientRequest(_ context.Context, r *http.Request) (inte } func decodeDisconnectChannelClientsRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := disconnectChannelClientsRequest{ @@ -203,7 +203,7 @@ func decodeDisconnectChannelClientsRequest(_ context.Context, r *http.Request) ( } func decodeConnectRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -216,7 +216,7 @@ func decodeConnectRequest(_ context.Context, r *http.Request) (interface{}, erro } func decodeDisconnectRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } diff --git a/channels/api/http/endpoints.go b/channels/api/http/endpoints.go index cd33b30ca56..a12a545b1ab 100644 --- a/channels/api/http/endpoints.go +++ b/channels/api/http/endpoints.go @@ -6,7 +6,7 @@ package http import ( "context" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/channels" "github.com/absmach/supermq/pkg/authn" @@ -22,7 +22,7 @@ func createChannelEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -46,7 +46,7 @@ func createChannelsEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -77,7 +77,7 @@ func viewChannelEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -98,7 +98,7 @@ func listChannelsEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -142,7 +142,7 @@ func updateChannelEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -168,7 +168,7 @@ func updateChannelTagsEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -193,7 +193,7 @@ func setChannelParentGroupEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -213,7 +213,7 @@ func removeChannelParentGroupEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -233,7 +233,7 @@ func enableChannelEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -254,7 +254,7 @@ func disableChannelEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -275,7 +275,7 @@ func connectChannelClientEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -295,7 +295,7 @@ func disconnectChannelClientsEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -315,7 +315,7 @@ func connectEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -335,7 +335,7 @@ func disconnectEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -355,7 +355,7 @@ func deleteChannelEndpoint(svc channels.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } diff --git a/channels/api/http/requests.go b/channels/api/http/requests.go index cd5b64e2ce3..8ba4c547797 100644 --- a/channels/api/http/requests.go +++ b/channels/api/http/requests.go @@ -6,7 +6,7 @@ package http import ( "strings" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/channels" smqclients "github.com/absmach/supermq/clients" @@ -18,7 +18,7 @@ type createChannelReq struct { } func (req createChannelReq) validate() error { - if len(req.Channel.Name) > api.MaxNameSize { + if len(req.Channel.Name) > httpapi.MaxNameSize { return apiutil.ErrNameSize } if req.Channel.ID != "" { @@ -44,7 +44,7 @@ func (req createChannelsReq) validate() error { return apiutil.ErrMissingChannelID } } - if len(channel.Name) > api.MaxNameSize { + if len(channel.Name) > httpapi.MaxNameSize { return apiutil.ErrNameSize } } @@ -78,16 +78,16 @@ type listChannelsReq struct { } func (req listChannelsReq) validate() error { - if req.limit > api.MaxLimitSize || req.limit < 1 { + if req.limit > httpapi.MaxLimitSize || req.limit < 1 { return apiutil.ErrLimitSize } if req.visibility != "" && - req.visibility != api.AllVisibility && - req.visibility != api.MyVisibility && - req.visibility != api.SharedVisibility { + req.visibility != httpapi.AllVisibility && + req.visibility != httpapi.MyVisibility && + req.visibility != httpapi.SharedVisibility { return apiutil.ErrInvalidVisibilityType } - if len(req.name) > api.MaxNameSize { + if len(req.name) > httpapi.MaxNameSize { return apiutil.ErrNameSize } @@ -105,7 +105,7 @@ func (req updateChannelReq) validate() error { if req.id == "" { return apiutil.ErrMissingID } - if len(req.Name) > api.MaxNameSize { + if len(req.Name) > httpapi.MaxNameSize { return apiutil.ErrNameSize } @@ -181,7 +181,7 @@ func (req *connectChannelClientsRequest) validate() error { } for _, tid := range req.ClientIDs { - if err := api.ValidateUUID(tid); err != nil { + if err := httpapi.ValidateUUID(tid); err != nil { return err } } @@ -204,7 +204,7 @@ func (req *disconnectChannelClientsRequest) validate() error { return apiutil.ErrMissingID } - if err := api.ValidateUUID(req.channelID); err != nil { + if err := httpapi.ValidateUUID(req.channelID); err != nil { return err } @@ -213,7 +213,7 @@ func (req *disconnectChannelClientsRequest) validate() error { } for _, tid := range req.ClientIds { - if err := api.ValidateUUID(tid); err != nil { + if err := httpapi.ValidateUUID(tid); err != nil { return err } } @@ -269,7 +269,7 @@ func (req *disconnectRequest) validate() error { return apiutil.ErrMissingID } for _, cid := range req.ChannelIds { - if err := api.ValidateUUID(cid); err != nil { + if err := httpapi.ValidateUUID(cid); err != nil { return err } } @@ -279,7 +279,7 @@ func (req *disconnectRequest) validate() error { } for _, tid := range req.ClientIds { - if err := api.ValidateUUID(tid); err != nil { + if err := httpapi.ValidateUUID(tid); err != nil { return err } } diff --git a/channels/api/http/requests_test.go b/channels/api/http/requests_test.go index 42a91d0d03a..ced9ed5cccf 100644 --- a/channels/api/http/requests_test.go +++ b/channels/api/http/requests_test.go @@ -8,7 +8,7 @@ import ( "strings" "testing" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/channels" "github.com/absmach/supermq/internal/testsutil" @@ -35,7 +35,7 @@ func TestCreateChannelReqValidation(t *testing.T) { desc: "long name", req: createChannelReq{ Channel: channels.Channel{ - Name: strings.Repeat("a", api.MaxNameSize+1), + Name: strings.Repeat("a", httpapi.MaxNameSize+1), }, }, err: apiutil.ErrNameSize, @@ -79,7 +79,7 @@ func TestCreateChannelsReqValidation(t *testing.T) { req: createChannelsReq{ Channels: []channels.Channel{ { - Name: strings.Repeat("a", api.MaxNameSize+1), + Name: strings.Repeat("a", httpapi.MaxNameSize+1), }, }, }, @@ -162,7 +162,7 @@ func TestListChannelsReqValidation(t *testing.T) { { desc: "limit is greater than max limit", req: listChannelsReq{ - limit: api.MaxLimitSize + 1, + limit: httpapi.MaxLimitSize + 1, }, err: apiutil.ErrLimitSize, }, @@ -170,7 +170,7 @@ func TestListChannelsReqValidation(t *testing.T) { desc: "name is too long", req: listChannelsReq{ limit: 10, - name: strings.Repeat("a", api.MaxNameSize+1), + name: strings.Repeat("a", httpapi.MaxNameSize+1), }, err: apiutil.ErrNameSize, }, @@ -213,7 +213,7 @@ func TestUpdateChannelReqValidate(t *testing.T) { desc: "name is too long", req: updateChannelReq{ id: valid, - Name: strings.Repeat("a", api.MaxNameSize+1), + Name: strings.Repeat("a", httpapi.MaxNameSize+1), }, err: apiutil.ErrNameSize, }, diff --git a/channels/api/http/transport.go b/channels/api/http/transport.go index 562fe7b945c..14f4cdee3fb 100644 --- a/channels/api/http/transport.go +++ b/channels/api/http/transport.go @@ -7,7 +7,7 @@ import ( "log/slog" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/channels" smqauthn "github.com/absmach/supermq/pkg/authn" @@ -20,43 +20,43 @@ import ( // MakeHandler returns a HTTP handler for Channels API endpoints. func MakeHandler(svc channels.Service, authn smqauthn.Authentication, mux *chi.Mux, logger *slog.Logger, instanceID string) *chi.Mux { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } mux.Route("/{domainID}/channels", func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, true)) + r.Use(httpapi.AuthenticateMiddleware(authn, true)) r.Post("/", otelhttp.NewHandler(kithttp.NewServer( createChannelEndpoint(svc), decodeCreateChannelReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "create_channel").ServeHTTP) r.Post("/bulk", otelhttp.NewHandler(kithttp.NewServer( createChannelsEndpoint(svc), decodeCreateChannelsReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "create_channels").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( listChannelsEndpoint(svc), decodeListChannels, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_channels").ServeHTTP) r.Post("/connect", otelhttp.NewHandler(kithttp.NewServer( connectEndpoint(svc), decodeConnectRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "connect").ServeHTTP) r.Post("/disconnect", otelhttp.NewHandler(kithttp.NewServer( disconnectEndpoint(svc), decodeDisconnectRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "disconnect").ServeHTTP) @@ -64,70 +64,70 @@ func MakeHandler(svc channels.Service, authn smqauthn.Authentication, mux *chi.M r.Get("/", otelhttp.NewHandler(kithttp.NewServer( viewChannelEndpoint(svc), decodeViewChannel, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view_channel").ServeHTTP) r.Patch("/", otelhttp.NewHandler(kithttp.NewServer( updateChannelEndpoint(svc), decodeUpdateChannel, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_channel_name_and_metadata").ServeHTTP) r.Patch("/tags", otelhttp.NewHandler(kithttp.NewServer( updateChannelTagsEndpoint(svc), decodeUpdateChannelTags, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_channel_tag").ServeHTTP) r.Delete("/", otelhttp.NewHandler(kithttp.NewServer( deleteChannelEndpoint(svc), decodeDeleteChannelReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_channel").ServeHTTP) r.Post("/enable", otelhttp.NewHandler(kithttp.NewServer( enableChannelEndpoint(svc), decodeChangeChannelStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "enable_channel").ServeHTTP) r.Post("/disable", otelhttp.NewHandler(kithttp.NewServer( disableChannelEndpoint(svc), decodeChangeChannelStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "disable_channel").ServeHTTP) r.Post("/parent", otelhttp.NewHandler(kithttp.NewServer( setChannelParentGroupEndpoint(svc), decodeSetChannelParentGroupStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "set_channel_parent_group").ServeHTTP) r.Delete("/parent", otelhttp.NewHandler(kithttp.NewServer( removeChannelParentGroupEndpoint(svc), decodeRemoveChannelParentGroupStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "remove_channel_parent_group").ServeHTTP) r.Post("/connect", otelhttp.NewHandler(kithttp.NewServer( connectChannelClientEndpoint(svc), decodeConnectChannelClientRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "connect_channel_client").ServeHTTP) r.Post("/disconnect", otelhttp.NewHandler(kithttp.NewServer( disconnectChannelClientsEndpoint(svc), decodeDisconnectChannelClientsRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "disconnect_channel_client").ServeHTTP) }) diff --git a/channels/postgres/channels.go b/channels/postgres/channels.go index 0905ea00fd3..bcaf3be670a 100644 --- a/channels/postgres/channels.go +++ b/channels/postgres/channels.go @@ -11,7 +11,7 @@ import ( "strings" "time" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/channels" clients "github.com/absmach/supermq/clients" @@ -573,7 +573,7 @@ func applyOrdering(emq string, pm channels.PageMetadata) string { switch pm.Order { case "name", "created_at", "updated_at": emq = fmt.Sprintf("%s ORDER BY %s", emq, pm.Order) - if pm.Dir == api.AscDir || pm.Dir == api.DescDir { + if pm.Dir == httpapi.AscDir || pm.Dir == httpapi.DescDir { emq = fmt.Sprintf("%s %s", emq, pm.Dir) } } diff --git a/clients/api/http/clients.go b/clients/api/http/clients.go index 29cd6ff32d1..8c5bd852313 100644 --- a/clients/api/http/clients.go +++ b/clients/api/http/clients.go @@ -6,7 +6,7 @@ package http import ( "log/slog" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/clients" smqauthn "github.com/absmach/supermq/pkg/authn" @@ -18,32 +18,32 @@ import ( func clientsHandler(svc clients.Service, authn smqauthn.Authentication, r *chi.Mux, logger *slog.Logger) *chi.Mux { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } d := roleManagerHttp.NewDecoder("clientID") r.Group(func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, true)) + r.Use(httpapi.AuthenticateMiddleware(authn, true)) r.Route("/{domainID}/clients", func(r chi.Router) { r.Post("/", otelhttp.NewHandler(kithttp.NewServer( createClientEndpoint(svc), decodeCreateClientReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "create_client").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( listClientsEndpoint(svc), decodeListClients, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_clients").ServeHTTP) r.Post("/bulk", otelhttp.NewHandler(kithttp.NewServer( createClientsEndpoint(svc), decodeCreateClientsReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "create_clients").ServeHTTP) r = roleManagerHttp.EntityAvailableActionsRouter(svc, d, r, opts) @@ -52,63 +52,63 @@ func clientsHandler(svc clients.Service, authn smqauthn.Authentication, r *chi.M r.Get("/", otelhttp.NewHandler(kithttp.NewServer( viewClientEndpoint(svc), decodeViewClient, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view_client").ServeHTTP) r.Patch("/", otelhttp.NewHandler(kithttp.NewServer( updateClientEndpoint(svc), decodeUpdateClient, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_client").ServeHTTP) r.Patch("/tags", otelhttp.NewHandler(kithttp.NewServer( updateClientTagsEndpoint(svc), decodeUpdateClientTags, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_client_tags").ServeHTTP) r.Patch("/secret", otelhttp.NewHandler(kithttp.NewServer( updateClientSecretEndpoint(svc), decodeUpdateClientCredentials, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_client_credentials").ServeHTTP) r.Post("/enable", otelhttp.NewHandler(kithttp.NewServer( enableClientEndpoint(svc), decodeChangeClientStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "enable_client").ServeHTTP) r.Post("/disable", otelhttp.NewHandler(kithttp.NewServer( disableClientEndpoint(svc), decodeChangeClientStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "disable_client").ServeHTTP) r.Post("/parent", otelhttp.NewHandler(kithttp.NewServer( setClientParentGroupEndpoint(svc), decodeSetClientParentGroupStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "set_client_parent_group").ServeHTTP) r.Delete("/parent", otelhttp.NewHandler(kithttp.NewServer( removeClientParentGroupEndpoint(svc), decodeRemoveClientParentGroupStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "remove_client_parent_group").ServeHTTP) r.Delete("/", otelhttp.NewHandler(kithttp.NewServer( deleteClientEndpoint(svc), decodeDeleteClientReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_client").ServeHTTP) roleManagerHttp.EntityRoleMangerRouter(svc, d, r, opts) @@ -118,7 +118,7 @@ func clientsHandler(svc clients.Service, authn smqauthn.Authentication, r *chi.M r.Get("/{domainID}/users/{userID}/clients", otelhttp.NewHandler(kithttp.NewServer( listClientsEndpoint(svc), decodeListClients, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_user_clients").ServeHTTP) }) diff --git a/clients/api/http/decode.go b/clients/api/http/decode.go index 4c2377bbb65..9b9fdc6e021 100644 --- a/clients/api/http/decode.go +++ b/clients/api/http/decode.go @@ -9,7 +9,7 @@ import ( "net/http" "strings" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/clients" "github.com/absmach/supermq/pkg/errors" @@ -27,40 +27,40 @@ func decodeViewClient(_ context.Context, r *http.Request) (interface{}, error) { } func decodeListClients(_ context.Context, r *http.Request) (interface{}, error) { - s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefClientStatus) + s, err := apiutil.ReadStringQuery(r, httpapi.StatusKey, httpapi.DefClientStatus) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + o, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + l, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - m, err := apiutil.ReadMetadataQuery(r, api.MetadataKey, nil) + m, err := apiutil.ReadMetadataQuery(r, httpapi.MetadataKey, nil) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - n, err := apiutil.ReadStringQuery(r, api.NameKey, "") + n, err := apiutil.ReadStringQuery(r, httpapi.NameKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - t, err := apiutil.ReadStringQuery(r, api.TagKey, "") + t, err := apiutil.ReadStringQuery(r, httpapi.TagKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - id, err := apiutil.ReadStringQuery(r, api.IDOrder, "") + id, err := apiutil.ReadStringQuery(r, httpapi.IDOrder, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - p, err := apiutil.ReadStringQuery(r, api.PermissionKey, api.DefPermission) + p, err := apiutil.ReadStringQuery(r, httpapi.PermissionKey, httpapi.DefPermission) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - lp, err := apiutil.ReadBoolQuery(r, api.ListPerms, api.DefListPerms) + lp, err := apiutil.ReadBoolQuery(r, httpapi.ListPerms, httpapi.DefListPerms) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } @@ -84,7 +84,7 @@ func decodeListClients(_ context.Context, r *http.Request) (interface{}, error) } func decodeUpdateClient(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -99,7 +99,7 @@ func decodeUpdateClient(_ context.Context, r *http.Request) (interface{}, error) } func decodeUpdateClientTags(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -114,7 +114,7 @@ func decodeUpdateClientTags(_ context.Context, r *http.Request) (interface{}, er } func decodeUpdateClientCredentials(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -129,7 +129,7 @@ func decodeUpdateClientCredentials(_ context.Context, r *http.Request) (interfac } func decodeCreateClientReq(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -145,7 +145,7 @@ func decodeCreateClientReq(_ context.Context, r *http.Request) (interface{}, err } func decodeCreateClientsReq(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -166,7 +166,7 @@ func decodeChangeClientStatus(_ context.Context, r *http.Request) (interface{}, } func decodeSetClientParentGroupStatus(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } diff --git a/clients/api/http/endpoints.go b/clients/api/http/endpoints.go index cb8936f8860..99868705110 100644 --- a/clients/api/http/endpoints.go +++ b/clients/api/http/endpoints.go @@ -6,7 +6,7 @@ package http import ( "context" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/clients" "github.com/absmach/supermq/pkg/authn" @@ -22,7 +22,7 @@ func createClientEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -46,7 +46,7 @@ func createClientsEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -77,7 +77,7 @@ func viewClientEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -98,7 +98,7 @@ func listClientsEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -142,7 +142,7 @@ func updateClientEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -168,7 +168,7 @@ func updateClientTagsEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -193,7 +193,7 @@ func updateClientSecretEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -214,7 +214,7 @@ func enableClientEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -235,7 +235,7 @@ func disableClientEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -256,7 +256,7 @@ func setClientParentGroupEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -275,7 +275,7 @@ func removeClientParentGroupEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -294,7 +294,7 @@ func deleteClientEndpoint(svc clients.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } diff --git a/clients/api/http/endpoints_test.go b/clients/api/http/endpoints_test.go index 5fd8491a2a4..01026abba0a 100644 --- a/clients/api/http/endpoints_test.go +++ b/clients/api/http/endpoints_test.go @@ -13,7 +13,7 @@ import ( "testing" "github.com/0x6flab/namegenerator" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/clients" clientsapi "github.com/absmach/supermq/clients/api/http" @@ -527,7 +527,7 @@ func TestListClients(t *testing.T) { token: validToken, domainID: domainID, authnRes: smqauthn.Session{UserID: validID, DomainID: domainID, DomainUserID: domainID + "_" + validID, SuperAdmin: false}, - query: fmt.Sprintf("limit=%d", api.MaxLimitSize+1), + query: fmt.Sprintf("limit=%d", httpapi.MaxLimitSize+1), status: http.StatusBadRequest, err: apiutil.ErrValidation, }, @@ -940,7 +940,7 @@ func TestUpdateClient(t *testing.T) { desc: "update client with name that is too long", id: client.ID, authnRes: smqauthn.Session{DomainUserID: domainID + "_" + validID, UserID: validID, DomainID: domainID}, - data: fmt.Sprintf(`{"name":"%s","tags":["%s"],"metadata":%s}`, strings.Repeat("a", api.MaxNameSize+1), newTag, toJSON(newMetadata)), + data: fmt.Sprintf(`{"name":"%s","tags":["%s"],"metadata":%s}`, strings.Repeat("a", httpapi.MaxNameSize+1), newTag, toJSON(newMetadata)), domainID: domainID, token: validToken, contentType: contentType, diff --git a/clients/api/http/requests.go b/clients/api/http/requests.go index ae2e586f21b..f1d51b32838 100644 --- a/clients/api/http/requests.go +++ b/clients/api/http/requests.go @@ -4,7 +4,7 @@ package http import ( - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/clients" ) @@ -14,11 +14,11 @@ type createClientReq struct { } func (req createClientReq) validate() error { - if len(req.client.Name) > api.MaxNameSize { + if len(req.client.Name) > httpapi.MaxNameSize { return apiutil.ErrNameSize } if req.client.ID != "" { - return api.ValidateUUID(req.client.ID) + return httpapi.ValidateUUID(req.client.ID) } return nil @@ -34,11 +34,11 @@ func (req createClientsReq) validate() error { } for _, c := range req.Clients { if c.ID != "" { - if err := api.ValidateUUID(c.ID); err != nil { + if err := httpapi.ValidateUUID(c.ID); err != nil { return err } } - if len(c.Name) > api.MaxNameSize { + if len(c.Name) > httpapi.MaxNameSize { return apiutil.ErrNameSize } } @@ -85,16 +85,16 @@ type listClientsReq struct { } func (req listClientsReq) validate() error { - if req.limit > api.MaxLimitSize || req.limit < 1 { + if req.limit > httpapi.MaxLimitSize || req.limit < 1 { return apiutil.ErrLimitSize } if req.visibility != "" && - req.visibility != api.AllVisibility && - req.visibility != api.MyVisibility && - req.visibility != api.SharedVisibility { + req.visibility != httpapi.AllVisibility && + req.visibility != httpapi.MyVisibility && + req.visibility != httpapi.SharedVisibility { return apiutil.ErrInvalidVisibilityType } - if len(req.name) > api.MaxNameSize { + if len(req.name) > httpapi.MaxNameSize { return apiutil.ErrNameSize } @@ -126,7 +126,7 @@ func (req updateClientReq) validate() error { return apiutil.ErrMissingID } - if len(req.Name) > api.MaxNameSize { + if len(req.Name) > httpapi.MaxNameSize { return apiutil.ErrNameSize } diff --git a/clients/api/http/requests_test.go b/clients/api/http/requests_test.go index cc9fcc0d5ab..be41c9a6950 100644 --- a/clients/api/http/requests_test.go +++ b/clients/api/http/requests_test.go @@ -7,7 +7,7 @@ import ( "strings" "testing" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/clients" "github.com/absmach/supermq/internal/testsutil" @@ -43,7 +43,7 @@ func TestCreateClientReqValidate(t *testing.T) { req: createClientReq{ client: clients.Client{ ID: validID, - Name: strings.Repeat("a", api.MaxNameSize+1), + Name: strings.Repeat("a", httpapi.MaxNameSize+1), }, }, err: apiutil.ErrNameSize, @@ -98,7 +98,7 @@ func TestCreateClientsReqValidate(t *testing.T) { Clients: []clients.Client{ { ID: validID, - Name: strings.Repeat("a", api.MaxNameSize+1), + Name: strings.Repeat("a", httpapi.MaxNameSize+1), }, }, }, @@ -199,7 +199,7 @@ func TestListClientsReqValidate(t *testing.T) { { desc: "limit too big", req: listClientsReq{ - limit: api.MaxLimitSize + 1, + limit: httpapi.MaxLimitSize + 1, }, err: apiutil.ErrLimitSize, }, @@ -222,7 +222,7 @@ func TestListClientsReqValidate(t *testing.T) { desc: "name too long", req: listClientsReq{ limit: 10, - name: strings.Repeat("a", api.MaxNameSize+1), + name: strings.Repeat("a", httpapi.MaxNameSize+1), }, err: apiutil.ErrNameSize, }, @@ -290,7 +290,7 @@ func TestUpdateClientReqValidate(t *testing.T) { desc: "name too long", req: updateClientReq{ id: validID, - Name: strings.Repeat("a", api.MaxNameSize+1), + Name: strings.Repeat("a", httpapi.MaxNameSize+1), }, err: apiutil.ErrNameSize, }, diff --git a/clients/postgres/clients.go b/clients/postgres/clients.go index 62717f9edf9..d14f0b28cad 100644 --- a/clients/postgres/clients.go +++ b/clients/postgres/clients.go @@ -11,7 +11,7 @@ import ( "strings" "time" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/clients" "github.com/absmach/supermq/pkg/connections" @@ -575,7 +575,7 @@ func applyOrdering(emq string, pm clients.Page) string { switch pm.Order { case "name", "identity", "created_at", "updated_at": emq = fmt.Sprintf("%s ORDER BY %s", emq, pm.Order) - if pm.Dir == api.AscDir || pm.Dir == api.DescDir { + if pm.Dir == httpapi.AscDir || pm.Dir == httpapi.DescDir { emq = fmt.Sprintf("%s %s", emq, pm.Dir) } } diff --git a/consumers/notifiers/api/transport.go b/consumers/notifiers/api/transport.go index 605833fe0a8..1c67cf48d28 100644 --- a/consumers/notifiers/api/transport.go +++ b/consumers/notifiers/api/transport.go @@ -11,7 +11,7 @@ import ( "strings" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/consumers/notifiers" "github.com/absmach/supermq/pkg/errors" @@ -34,7 +34,7 @@ const ( // MakeHandler returns a HTTP handler for API endpoints. func MakeHandler(svc notifiers.Service, logger *slog.Logger, instanceID string) http.Handler { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } mux := chi.NewRouter() @@ -43,35 +43,35 @@ func MakeHandler(svc notifiers.Service, logger *slog.Logger, instanceID string) r.Post("/", otelhttp.NewHandler(kithttp.NewServer( createSubscriptionEndpoint(svc), decodeCreate, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "create").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( listSubscriptionsEndpoint(svc), decodeList, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list").ServeHTTP) r.Delete("/", otelhttp.NewHandler(kithttp.NewServer( deleteSubscriptionEndpint(svc), decodeSubscription, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete").ServeHTTP) r.Get("/{subID}", otelhttp.NewHandler(kithttp.NewServer( viewSubscriptionEndpint(svc), decodeSubscription, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view").ServeHTTP) r.Delete("/{subID}", otelhttp.NewHandler(kithttp.NewServer( deleteSubscriptionEndpint(svc), decodeSubscription, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete").ServeHTTP) }) diff --git a/domains/api/http/decode.go b/domains/api/http/decode.go index 13999acf1d1..b7537af4ce3 100644 --- a/domains/api/http/decode.go +++ b/domains/api/http/decode.go @@ -9,7 +9,7 @@ import ( "net/http" "strings" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/domains" "github.com/absmach/supermq/pkg/errors" @@ -17,7 +17,7 @@ import ( ) func decodeCreateDomainRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := createDomainReq{} @@ -36,7 +36,7 @@ func decodeRetrieveDomainRequest(_ context.Context, r *http.Request) (interface{ } func decodeUpdateDomainRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -85,7 +85,7 @@ func decodeFreezeDomainRequest(_ context.Context, r *http.Request) (interface{}, } func decodePageRequest(_ context.Context, r *http.Request) (page, error) { - s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefClientStatus) + s, err := apiutil.ReadStringQuery(r, httpapi.StatusKey, httpapi.DefClientStatus) if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } @@ -93,36 +93,36 @@ func decodePageRequest(_ context.Context, r *http.Request) (page, error) { if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } - o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + o, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } - or, err := apiutil.ReadStringQuery(r, api.OrderKey, api.DefOrder) + or, err := apiutil.ReadStringQuery(r, httpapi.OrderKey, httpapi.DefOrder) if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } - dir, err := apiutil.ReadStringQuery(r, api.DirKey, api.DefDir) + dir, err := apiutil.ReadStringQuery(r, httpapi.DirKey, httpapi.DefDir) if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } - l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + l, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } - m, err := apiutil.ReadMetadataQuery(r, api.MetadataKey, nil) + m, err := apiutil.ReadMetadataQuery(r, httpapi.MetadataKey, nil) if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } - n, err := apiutil.ReadStringQuery(r, api.NameKey, "") + n, err := apiutil.ReadStringQuery(r, httpapi.NameKey, "") if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } - t, err := apiutil.ReadStringQuery(r, api.TagKey, "") + t, err := apiutil.ReadStringQuery(r, httpapi.TagKey, "") if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } - allActions, err := apiutil.ReadStringQuery(r, api.ActionsKey, "") + allActions, err := apiutil.ReadStringQuery(r, httpapi.ActionsKey, "") if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } @@ -133,12 +133,12 @@ func decodePageRequest(_ context.Context, r *http.Request) (page, error) { if allActions != "" { actions = strings.Split(allActions, ",") } - roleID, err := apiutil.ReadStringQuery(r, api.RoleIDKey, "") + roleID, err := apiutil.ReadStringQuery(r, httpapi.RoleIDKey, "") if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } - roleName, err := apiutil.ReadStringQuery(r, api.RoleNameKey, "") + roleName, err := apiutil.ReadStringQuery(r, httpapi.RoleNameKey, "") if err != nil { return page{}, errors.Wrap(apiutil.ErrValidation, err) } diff --git a/domains/api/http/endpoint.go b/domains/api/http/endpoint.go index 40fb2c1182b..7f998dc8466 100644 --- a/domains/api/http/endpoint.go +++ b/domains/api/http/endpoint.go @@ -6,7 +6,7 @@ package http import ( "context" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/domains" "github.com/absmach/supermq/pkg/authn" @@ -22,7 +22,7 @@ func createDomainEndpoint(svc domains.Service) endpoint.Endpoint { return nil, err } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -49,7 +49,7 @@ func retrieveDomainEndpoint(svc domains.Service) endpoint.Endpoint { return nil, err } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -69,7 +69,7 @@ func updateDomainEndpoint(svc domains.Service) endpoint.Endpoint { return nil, err } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -100,7 +100,7 @@ func listDomainsEndpoint(svc domains.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -133,7 +133,7 @@ func enableDomainEndpoint(svc domains.Service) endpoint.Endpoint { return nil, err } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -152,7 +152,7 @@ func disableDomainEndpoint(svc domains.Service) endpoint.Endpoint { return nil, err } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -171,7 +171,7 @@ func freezeDomainEndpoint(svc domains.Service) endpoint.Endpoint { return nil, err } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } diff --git a/domains/api/http/endpoint_test.go b/domains/api/http/endpoint_test.go index e3360cfe9bd..456439bf4fe 100644 --- a/domains/api/http/endpoint_test.go +++ b/domains/api/http/endpoint_test.go @@ -13,7 +13,7 @@ import ( "testing" "time" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/domains" domainsapi "github.com/absmach/supermq/domains/api/http" @@ -274,10 +274,10 @@ func TestListDomains(t *testing.T) { desc: "list domains with valid token", token: validToken, page: domains.Page{ - Offset: api.DefOffset, - Limit: api.DefLimit, - Order: api.DefOrder, - Dir: api.DefDir, + Offset: httpapi.DefOffset, + Limit: httpapi.DefLimit, + Order: httpapi.DefOrder, + Dir: httpapi.DefDir, }, status: http.StatusOK, listDomainsResp: domains.DomainsPage{ @@ -305,9 +305,9 @@ func TestListDomains(t *testing.T) { query: "offset=1", page: domains.Page{ Offset: 1, - Limit: api.DefLimit, - Order: api.DefOrder, - Dir: api.DefDir, + Limit: httpapi.DefLimit, + Order: httpapi.DefOrder, + Dir: httpapi.DefDir, }, listDomainsResp: domains.DomainsPage{ Total: 1, @@ -328,10 +328,10 @@ func TestListDomains(t *testing.T) { token: validToken, query: "limit=1", page: domains.Page{ - Offset: api.DefOffset, + Offset: httpapi.DefOffset, Limit: 1, - Order: api.DefOrder, - Dir: api.DefDir, + Order: httpapi.DefOrder, + Dir: httpapi.DefDir, }, listDomainsResp: domains.DomainsPage{ Total: 1, @@ -356,10 +356,10 @@ func TestListDomains(t *testing.T) { }, query: "name=domainname", page: domains.Page{ - Offset: api.DefOffset, - Limit: api.DefLimit, - Order: api.DefOrder, - Dir: api.DefDir, + Offset: httpapi.DefOffset, + Limit: httpapi.DefLimit, + Order: httpapi.DefOrder, + Dir: httpapi.DefDir, Name: "domainname", }, status: http.StatusOK, @@ -388,10 +388,10 @@ func TestListDomains(t *testing.T) { }, query: "status=enabled", page: domains.Page{ - Offset: api.DefOffset, - Limit: api.DefLimit, - Order: api.DefOrder, - Dir: api.DefDir, + Offset: httpapi.DefOffset, + Limit: httpapi.DefLimit, + Order: httpapi.DefOrder, + Dir: httpapi.DefDir, Status: domains.EnabledStatus, }, status: http.StatusOK, @@ -420,10 +420,10 @@ func TestListDomains(t *testing.T) { }, query: "tag=tag1", page: domains.Page{ - Offset: api.DefOffset, - Limit: api.DefLimit, - Order: api.DefOrder, - Dir: api.DefDir, + Offset: httpapi.DefOffset, + Limit: httpapi.DefLimit, + Order: httpapi.DefOrder, + Dir: httpapi.DefDir, Tag: "tag1", }, status: http.StatusOK, @@ -448,10 +448,10 @@ func TestListDomains(t *testing.T) { token: validToken, query: "metadata=%7B%22domain%22%3A%20%22example.com%22%7D&", page: domains.Page{ - Offset: api.DefOffset, - Limit: api.DefLimit, - Order: api.DefOrder, - Dir: api.DefDir, + Offset: httpapi.DefOffset, + Limit: httpapi.DefLimit, + Order: httpapi.DefOrder, + Dir: httpapi.DefDir, Metadata: domains.Metadata{ "domain": "example.com", }, @@ -482,10 +482,10 @@ func TestListDomains(t *testing.T) { token: validToken, query: "role_name=view", page: domains.Page{ - Offset: api.DefOffset, - Limit: api.DefLimit, - Order: api.DefOrder, - Dir: api.DefDir, + Offset: httpapi.DefOffset, + Limit: httpapi.DefLimit, + Order: httpapi.DefOrder, + Dir: httpapi.DefDir, RoleName: "view", }, listDomainsResp: domains.DomainsPage{ @@ -513,10 +513,10 @@ func TestListDomains(t *testing.T) { desc: "list domains with order", token: validToken, page: domains.Page{ - Offset: api.DefOffset, - Limit: api.DefLimit, + Offset: httpapi.DefOffset, + Limit: httpapi.DefLimit, Order: "name", - Dir: api.DefDir, + Dir: httpapi.DefDir, }, query: "order=name", listDomainsResp: domains.DomainsPage{ @@ -543,9 +543,9 @@ func TestListDomains(t *testing.T) { desc: "list domains with dir", token: validToken, page: domains.Page{ - Offset: api.DefOffset, - Limit: api.DefLimit, - Order: api.DefOrder, + Offset: httpapi.DefOffset, + Limit: httpapi.DefLimit, + Order: httpapi.DefOrder, Dir: "asc", }, query: "dir=asc", @@ -573,10 +573,10 @@ func TestListDomains(t *testing.T) { desc: "list domains with service error", token: validToken, page: domains.Page{ - Offset: api.DefOffset, - Limit: api.DefLimit, - Order: api.DefOrder, - Dir: api.DefDir, + Offset: httpapi.DefOffset, + Limit: httpapi.DefLimit, + Order: httpapi.DefOrder, + Dir: httpapi.DefDir, }, status: http.StatusBadRequest, listDomainsResp: domains.DomainsPage{}, diff --git a/domains/api/http/transport.go b/domains/api/http/transport.go index fa68fe2f34d..1d3cccddfac 100644 --- a/domains/api/http/transport.go +++ b/domains/api/http/transport.go @@ -7,7 +7,7 @@ import ( "log/slog" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/domains" "github.com/absmach/supermq/pkg/authn" @@ -20,24 +20,24 @@ import ( func MakeHandler(svc domains.Service, authn authn.Authentication, mux *chi.Mux, logger *slog.Logger, instanceID string) *chi.Mux { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } d := roleManagerHttp.NewDecoder("domainID") mux.Route("/domains", func(r chi.Router) { r.Group(func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, false)) + r.Use(httpapi.AuthenticateMiddleware(authn, false)) r.Post("/", otelhttp.NewHandler(kithttp.NewServer( createDomainEndpoint(svc), decodeCreateDomainRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "create_domain").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( listDomainsEndpoint(svc), decodeListDomainRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_domains").ServeHTTP) @@ -45,39 +45,39 @@ func MakeHandler(svc domains.Service, authn authn.Authentication, mux *chi.Mux, }) r.Route("/{domainID}", func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, true)) + r.Use(httpapi.AuthenticateMiddleware(authn, true)) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( retrieveDomainEndpoint(svc), decodeRetrieveDomainRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view_domain").ServeHTTP) r.Patch("/", otelhttp.NewHandler(kithttp.NewServer( updateDomainEndpoint(svc), decodeUpdateDomainRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_domain").ServeHTTP) r.Post("/enable", otelhttp.NewHandler(kithttp.NewServer( enableDomainEndpoint(svc), decodeEnableDomainRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "enable_domain").ServeHTTP) r.Post("/disable", otelhttp.NewHandler(kithttp.NewServer( disableDomainEndpoint(svc), decodeDisableDomainRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "disable_domain").ServeHTTP) r.Post("/freeze", otelhttp.NewHandler(kithttp.NewServer( freezeDomainEndpoint(svc), decodeFreezeDomainRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "freeze_domain").ServeHTTP) roleManagerHttp.EntityRoleMangerRouter(svc, d, r, opts) diff --git a/domains/postgres/domains.go b/domains/postgres/domains.go index 8610576774e..ce99758c311 100644 --- a/domains/postgres/domains.go +++ b/domains/postgres/domains.go @@ -11,7 +11,7 @@ import ( "strings" "time" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" "github.com/absmach/supermq/domains" "github.com/absmach/supermq/pkg/errors" repoerr "github.com/absmach/supermq/pkg/errors/repository" @@ -423,7 +423,7 @@ func applyOrdering(emq string, pm domains.Page) string { switch pm.Order { case "name", "created_at", "updated_at": emq = fmt.Sprintf("%s ORDER BY d.%s", emq, pm.Order) - if pm.Dir == api.AscDir || pm.Dir == api.DescDir { + if pm.Dir == httpapi.AscDir || pm.Dir == httpapi.DescDir { emq = fmt.Sprintf("%s %s", emq, pm.Dir) } } diff --git a/groups/api/http/decode.go b/groups/api/http/decode.go index 7ca6cc36a69..bd621101923 100644 --- a/groups/api/http/decode.go +++ b/groups/api/http/decode.go @@ -9,7 +9,7 @@ import ( "net/http" "strings" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" groups "github.com/absmach/supermq/groups" "github.com/absmach/supermq/pkg/errors" @@ -17,7 +17,7 @@ import ( ) func DecodeGroupCreate(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } var g groups.Group @@ -37,12 +37,12 @@ func DecodeListGroupsRequest(_ context.Context, r *http.Request) (interface{}, e return nil, err } - userID, err := apiutil.ReadStringQuery(r, api.UserKey, "") + userID, err := apiutil.ReadStringQuery(r, httpapi.UserKey, "") if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - groupID, err := apiutil.ReadStringQuery(r, api.GroupKey, "") + groupID, err := apiutil.ReadStringQuery(r, httpapi.GroupKey, "") if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } @@ -56,7 +56,7 @@ func DecodeListGroupsRequest(_ context.Context, r *http.Request) (interface{}, e } func DecodeGroupUpdate(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := updateGroupReq{ @@ -96,7 +96,7 @@ func decodeRetrieveGroupHierarchy(_ context.Context, r *http.Request) (interface } func decodeAddParentGroupRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -117,7 +117,7 @@ func decodeRemoveParentGroupRequest(_ context.Context, r *http.Request) (interfa } func decodeAddChildrenGroupsRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := addChildrenGroupsReq{ @@ -130,7 +130,7 @@ func decodeAddChildrenGroupsRequest(_ context.Context, r *http.Request) (interfa } func decodeRemoveChildrenGroupsRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := removeChildrenGroupsReq{ @@ -155,12 +155,12 @@ func decodeListChildrenGroupsRequest(_ context.Context, r *http.Request) (interf return nil, err } - startLevel, err := apiutil.ReadNumQuery[int64](r, api.StartLevelKey, api.DefStartLevel) + startLevel, err := apiutil.ReadNumQuery[int64](r, httpapi.StartLevelKey, httpapi.DefStartLevel) if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - endLevel, err := apiutil.ReadNumQuery[int64](r, api.EndLevelKey, api.DefEndLevel) + endLevel, err := apiutil.ReadNumQuery[int64](r, httpapi.EndLevelKey, httpapi.DefEndLevel) if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } @@ -175,16 +175,16 @@ func decodeListChildrenGroupsRequest(_ context.Context, r *http.Request) (interf } func decodeHierarchyPageMeta(r *http.Request) (groups.HierarchyPageMeta, error) { - level, err := apiutil.ReadNumQuery[uint64](r, api.LevelKey, api.DefLevel) + level, err := apiutil.ReadNumQuery[uint64](r, httpapi.LevelKey, httpapi.DefLevel) if err != nil { return groups.HierarchyPageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - tree, err := apiutil.ReadBoolQuery(r, api.TreeKey, false) + tree, err := apiutil.ReadBoolQuery(r, httpapi.TreeKey, false) if err != nil { return groups.HierarchyPageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - hierarchyDir, err := apiutil.ReadNumQuery[int64](r, api.DirKey, -1) + hierarchyDir, err := apiutil.ReadNumQuery[int64](r, httpapi.DirKey, -1) if err != nil { return groups.HierarchyPageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } @@ -197,7 +197,7 @@ func decodeHierarchyPageMeta(r *http.Request) (groups.HierarchyPageMeta, error) } func decodePageMeta(r *http.Request) (groups.PageMeta, error) { - s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefGroupStatus) + s, err := apiutil.ReadStringQuery(r, httpapi.StatusKey, httpapi.DefGroupStatus) if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } @@ -205,28 +205,28 @@ func decodePageMeta(r *http.Request) (groups.PageMeta, error) { if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - offset, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + offset, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - limit, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + limit, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - name, err := apiutil.ReadStringQuery(r, api.NameKey, "") + name, err := apiutil.ReadStringQuery(r, httpapi.NameKey, "") if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - id, err := apiutil.ReadStringQuery(r, api.IDOrder, "") + id, err := apiutil.ReadStringQuery(r, httpapi.IDOrder, "") if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - meta, err := apiutil.ReadMetadataQuery(r, api.MetadataKey, nil) + meta, err := apiutil.ReadMetadataQuery(r, httpapi.MetadataKey, nil) if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - allActions, err := apiutil.ReadStringQuery(r, api.ActionsKey, "") + allActions, err := apiutil.ReadStringQuery(r, httpapi.ActionsKey, "") if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } @@ -237,17 +237,17 @@ func decodePageMeta(r *http.Request) (groups.PageMeta, error) { if allActions != "" { actions = strings.Split(allActions, ",") } - roleID, err := apiutil.ReadStringQuery(r, api.RoleIDKey, "") + roleID, err := apiutil.ReadStringQuery(r, httpapi.RoleIDKey, "") if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - roleName, err := apiutil.ReadStringQuery(r, api.RoleNameKey, "") + roleName, err := apiutil.ReadStringQuery(r, httpapi.RoleNameKey, "") if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } - accessType, err := apiutil.ReadStringQuery(r, api.AccessTypeKey, "") + accessType, err := apiutil.ReadStringQuery(r, httpapi.AccessTypeKey, "") if err != nil { return groups.PageMeta{}, errors.Wrap(apiutil.ErrValidation, err) } diff --git a/groups/api/http/decode_test.go b/groups/api/http/decode_test.go index 5bbc9d2aed0..bbdfd1ca78a 100644 --- a/groups/api/http/decode_test.go +++ b/groups/api/http/decode_test.go @@ -11,7 +11,7 @@ import ( "strings" "testing" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/groups" "github.com/absmach/supermq/pkg/errors" @@ -307,7 +307,7 @@ func TestDecodeGroupCreate(t *testing.T) { body: `{"name": "random", "description": "random"}`, header: map[string][]string{ "Authorization": {"Bearer 123"}, - "Content-Type": {api.ContentType}, + "Content-Type": {httpapi.ContentType}, }, resp: createGroupReq{ Group: groups.Group{ @@ -332,7 +332,7 @@ func TestDecodeGroupCreate(t *testing.T) { body: `data`, header: map[string][]string{ "Authorization": {"Bearer 123"}, - "Content-Type": {api.ContentType}, + "Content-Type": {httpapi.ContentType}, }, resp: nil, err: errors.ErrMalformedEntity, @@ -362,7 +362,7 @@ func TestDecodeGroupUpdate(t *testing.T) { body: `{"name": "random", "description": "random"}`, header: map[string][]string{ "Authorization": {"Bearer 123"}, - "Content-Type": {api.ContentType}, + "Content-Type": {httpapi.ContentType}, }, resp: updateGroupReq{ Name: "random", @@ -385,7 +385,7 @@ func TestDecodeGroupUpdate(t *testing.T) { body: `data`, header: map[string][]string{ "Authorization": {"Bearer 123"}, - "Content-Type": {api.ContentType}, + "Content-Type": {httpapi.ContentType}, }, resp: nil, err: errors.ErrMalformedEntity, diff --git a/groups/api/http/endpoints.go b/groups/api/http/endpoints.go index 4a5f238c2df..d8ddb84aba4 100644 --- a/groups/api/http/endpoints.go +++ b/groups/api/http/endpoints.go @@ -6,7 +6,7 @@ package api import ( "context" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/groups" "github.com/absmach/supermq/pkg/authn" @@ -22,7 +22,7 @@ func CreateGroupEndpoint(svc groups.Service) endpoint.Endpoint { return createGroupRes{created: false}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return createGroupRes{created: false}, svcerr.ErrAuthentication } @@ -43,7 +43,7 @@ func ViewGroupEndpoint(svc groups.Service) endpoint.Endpoint { return viewGroupRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return viewGroupRes{}, svcerr.ErrAuthentication } @@ -64,7 +64,7 @@ func UpdateGroupEndpoint(svc groups.Service) endpoint.Endpoint { return updateGroupRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return updateGroupRes{}, svcerr.ErrAuthentication } @@ -92,7 +92,7 @@ func EnableGroupEndpoint(svc groups.Service) endpoint.Endpoint { return changeStatusRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return changeStatusRes{}, svcerr.ErrAuthentication } @@ -112,7 +112,7 @@ func DisableGroupEndpoint(svc groups.Service) endpoint.Endpoint { return changeStatusRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return changeStatusRes{}, svcerr.ErrAuthentication } @@ -133,7 +133,7 @@ func ListGroupsEndpoint(svc groups.Service) endpoint.Endpoint { return groupPageRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return groupPageRes{}, svcerr.ErrAuthentication } @@ -173,7 +173,7 @@ func DeleteGroupEndpoint(svc groups.Service) endpoint.Endpoint { return deleteGroupRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return deleteGroupRes{}, svcerr.ErrAuthentication } @@ -191,7 +191,7 @@ func retrieveGroupHierarchyEndpoint(svc groups.Service) endpoint.Endpoint { return retrieveGroupHierarchyRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return changeStatusRes{}, svcerr.ErrAuthentication } @@ -219,7 +219,7 @@ func addParentGroupEndpoint(svc groups.Service) endpoint.Endpoint { return addParentGroupRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return changeStatusRes{}, svcerr.ErrAuthentication } @@ -238,7 +238,7 @@ func removeParentGroupEndpoint(svc groups.Service) endpoint.Endpoint { return removeParentGroupRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return changeStatusRes{}, svcerr.ErrAuthentication } @@ -257,7 +257,7 @@ func addChildrenGroupsEndpoint(svc groups.Service) endpoint.Endpoint { return addChildrenGroupsRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return changeStatusRes{}, svcerr.ErrAuthentication } @@ -276,7 +276,7 @@ func removeChildrenGroupsEndpoint(svc groups.Service) endpoint.Endpoint { return removeChildrenGroupsRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return changeStatusRes{}, svcerr.ErrAuthentication } @@ -295,7 +295,7 @@ func removeAllChildrenGroupsEndpoint(svc groups.Service) endpoint.Endpoint { return removeAllChildrenGroupsRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return changeStatusRes{}, svcerr.ErrAuthentication } @@ -314,7 +314,7 @@ func listChildrenGroupsEndpoint(svc groups.Service) endpoint.Endpoint { return listChildrenGroupsRes{}, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return changeStatusRes{}, svcerr.ErrAuthentication } diff --git a/groups/api/http/requests.go b/groups/api/http/requests.go index 59a0e59f346..37572b450ca 100644 --- a/groups/api/http/requests.go +++ b/groups/api/http/requests.go @@ -4,7 +4,7 @@ package api import ( - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/groups" ) @@ -14,7 +14,7 @@ type createGroupReq struct { } func (req createGroupReq) validate() error { - if len(req.Name) > api.MaxNameSize || req.Name == "" { + if len(req.Name) > httpapi.MaxNameSize || req.Name == "" { return apiutil.ErrNameSize } @@ -32,7 +32,7 @@ func (req updateGroupReq) validate() error { if req.id == "" { return apiutil.ErrMissingID } - if len(req.Name) > api.MaxNameSize { + if len(req.Name) > httpapi.MaxNameSize { return apiutil.ErrNameSize } return nil @@ -45,7 +45,7 @@ type listGroupsReq struct { } func (req listGroupsReq) validate() error { - if req.Limit > api.MaxLimitSize || req.Limit < 1 { + if req.Limit > httpapi.MaxLimitSize || req.Limit < 1 { return apiutil.ErrLimitSize } @@ -103,7 +103,7 @@ func (req addParentGroupReq) validate() error { if req.id == "" { return apiutil.ErrMissingID } - if err := api.ValidateUUID(req.ParentID); err != nil { + if err := httpapi.ValidateUUID(req.ParentID); err != nil { return err } if req.id == req.ParentID { @@ -136,7 +136,7 @@ func (req addChildrenGroupsReq) validate() error { return apiutil.ErrMissingChildrenGroupIDs } for _, childID := range req.ChildrenIDs { - if err := api.ValidateUUID(childID); err != nil { + if err := httpapi.ValidateUUID(childID); err != nil { return err } if req.id == childID { @@ -159,7 +159,7 @@ func (req removeChildrenGroupsReq) validate() error { return apiutil.ErrMissingChildrenGroupIDs } for _, childID := range req.ChildrenIDs { - if err := api.ValidateUUID(childID); err != nil { + if err := httpapi.ValidateUUID(childID); err != nil { return err } } @@ -188,7 +188,7 @@ func (req listChildrenGroupsReq) validate() error { if req.id == "" { return apiutil.ErrMissingID } - if req.Limit > api.MaxLimitSize || req.Limit < 1 { + if req.Limit > httpapi.MaxLimitSize || req.Limit < 1 { return apiutil.ErrLimitSize } return nil diff --git a/groups/api/http/requests_test.go b/groups/api/http/requests_test.go index 8fc3712e135..3d58dacda0b 100644 --- a/groups/api/http/requests_test.go +++ b/groups/api/http/requests_test.go @@ -8,7 +8,7 @@ import ( "strings" "testing" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/groups" "github.com/absmach/supermq/internal/testsutil" @@ -36,7 +36,7 @@ func TestCreateGroupReqValidation(t *testing.T) { desc: "long name", req: createGroupReq{ Group: groups.Group{ - Name: strings.Repeat("a", api.MaxNameSize+1), + Name: strings.Repeat("a", httpapi.MaxNameSize+1), }, }, err: apiutil.ErrNameSize, @@ -74,7 +74,7 @@ func TestUpdateGroupReqValidation(t *testing.T) { desc: "long name", req: updateGroupReq{ id: valid, - Name: strings.Repeat("a", api.MaxNameSize+1), + Name: strings.Repeat("a", httpapi.MaxNameSize+1), }, err: apiutil.ErrNameSize, }, @@ -121,7 +121,7 @@ func TestListGroupReqValidation(t *testing.T) { desc: "invalid upper limit", req: listGroupsReq{ PageMeta: groups.PageMeta{ - Limit: api.MaxLimitSize + 1, + Limit: httpapi.MaxLimitSize + 1, }, }, err: apiutil.ErrLimitSize, @@ -479,7 +479,7 @@ func TestListChildrenGroupsReqValidation(t *testing.T) { req: listChildrenGroupsReq{ id: validID, PageMeta: groups.PageMeta{ - Limit: api.MaxLimitSize + 1, + Limit: httpapi.MaxLimitSize + 1, }, }, err: apiutil.ErrLimitSize, diff --git a/groups/api/http/transport.go b/groups/api/http/transport.go index 36a0f462a7e..3423723a20c 100644 --- a/groups/api/http/transport.go +++ b/groups/api/http/transport.go @@ -7,7 +7,7 @@ import ( "log/slog" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/groups" "github.com/absmach/supermq/pkg/authn" @@ -21,23 +21,23 @@ import ( // MakeHandler returns a HTTP handler for Groups API endpoints. func MakeHandler(svc groups.Service, authn authn.Authentication, mux *chi.Mux, logger *slog.Logger, instanceID string) *chi.Mux { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } d := roleManagerHttp.NewDecoder("groupID") mux.Route("/{domainID}/groups", func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, true)) + r.Use(httpapi.AuthenticateMiddleware(authn, true)) r.Post("/", otelhttp.NewHandler(kithttp.NewServer( CreateGroupEndpoint(svc), DecodeGroupCreate, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "create_group").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( ListGroupsEndpoint(svc), DecodeListGroupsRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_groups").ServeHTTP) r = roleManagerHttp.EntityAvailableActionsRouter(svc, d, r, opts) @@ -46,35 +46,35 @@ func MakeHandler(svc groups.Service, authn authn.Authentication, mux *chi.Mux, l r.Get("/", otelhttp.NewHandler(kithttp.NewServer( ViewGroupEndpoint(svc), DecodeGroupRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view_group").ServeHTTP) r.Put("/", otelhttp.NewHandler(kithttp.NewServer( UpdateGroupEndpoint(svc), DecodeGroupUpdate, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_group").ServeHTTP) r.Delete("/", otelhttp.NewHandler(kithttp.NewServer( DeleteGroupEndpoint(svc), DecodeGroupRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_group").ServeHTTP) r.Post("/enable", otelhttp.NewHandler(kithttp.NewServer( EnableGroupEndpoint(svc), DecodeChangeGroupStatusRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "enable_group").ServeHTTP) r.Post("/disable", otelhttp.NewHandler(kithttp.NewServer( DisableGroupEndpoint(svc), DecodeChangeGroupStatusRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "disable_group").ServeHTTP) @@ -83,7 +83,7 @@ func MakeHandler(svc groups.Service, authn authn.Authentication, mux *chi.Mux, l r.Get("/hierarchy", otelhttp.NewHandler(kithttp.NewServer( retrieveGroupHierarchyEndpoint(svc), decodeRetrieveGroupHierarchy, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "retrieve_group_hierarchy").ServeHTTP) @@ -91,14 +91,14 @@ func MakeHandler(svc groups.Service, authn authn.Authentication, mux *chi.Mux, l r.Post("/", otelhttp.NewHandler(kithttp.NewServer( addParentGroupEndpoint(svc), decodeAddParentGroupRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "add_parent_group").ServeHTTP) r.Delete("/", otelhttp.NewHandler(kithttp.NewServer( removeParentGroupEndpoint(svc), decodeRemoveParentGroupRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "remove_parent_group").ServeHTTP) }) @@ -107,28 +107,28 @@ func MakeHandler(svc groups.Service, authn authn.Authentication, mux *chi.Mux, l r.Post("/", otelhttp.NewHandler(kithttp.NewServer( addChildrenGroupsEndpoint(svc), decodeAddChildrenGroupsRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "add_children_groups").ServeHTTP) r.Delete("/", otelhttp.NewHandler(kithttp.NewServer( removeChildrenGroupsEndpoint(svc), decodeRemoveChildrenGroupsRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "remove_children_groups").ServeHTTP) r.Delete("/all", otelhttp.NewHandler(kithttp.NewServer( removeAllChildrenGroupsEndpoint(svc), decodeRemoveAllChildrenGroupsRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "remove_all_children_groups").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( listChildrenGroupsEndpoint(svc), decodeListChildrenGroupsRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_children_groups").ServeHTTP) }) diff --git a/http/api/transport.go b/http/api/transport.go index 56fee979fee..67565c561d9 100644 --- a/http/api/transport.go +++ b/http/api/transport.go @@ -10,7 +10,7 @@ import ( "net/http" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/pkg/errors" "github.com/absmach/supermq/pkg/messaging" @@ -29,21 +29,21 @@ const ( // MakeHandler returns a HTTP handler for API endpoints. func MakeHandler(logger *slog.Logger, instanceID string) http.Handler { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } r := chi.NewRouter() r.Post("/channels/{chanID}/messages", otelhttp.NewHandler(kithttp.NewServer( sendMessageEndpoint(), decodeRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "publish").ServeHTTP) r.Post("/channels/{chanID}/messages/*", otelhttp.NewHandler(kithttp.NewServer( sendMessageEndpoint(), decodeRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "publish").ServeHTTP) r.Get("/health", supermq.Health("http", instanceID)) diff --git a/invitations/api/endpoint.go b/invitations/api/endpoint.go index efde368a700..371e1c94f18 100644 --- a/invitations/api/endpoint.go +++ b/invitations/api/endpoint.go @@ -6,7 +6,7 @@ package api import ( "context" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/invitations" "github.com/absmach/supermq/pkg/authn" @@ -24,7 +24,7 @@ func sendInvitationEndpoint(svc invitations.Service) endpoint.Endpoint { if err := req.validate(); err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -52,7 +52,7 @@ func viewInvitationEndpoint(svc invitations.Service) endpoint.Endpoint { if err := req.validate(); err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -75,7 +75,7 @@ func listInvitationsEndpoint(svc invitations.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -99,7 +99,7 @@ func acceptInvitationEndpoint(svc invitations.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -119,7 +119,7 @@ func rejectInvitationEndpoint(svc invitations.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -139,7 +139,7 @@ func deleteInvitationEndpoint(svc invitations.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } diff --git a/invitations/api/transport.go b/invitations/api/transport.go index 06eae217790..fac2771c75e 100644 --- a/invitations/api/transport.go +++ b/invitations/api/transport.go @@ -11,7 +11,7 @@ import ( "strings" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/invitations" smqauthn "github.com/absmach/supermq/pkg/authn" @@ -32,51 +32,51 @@ const ( func MakeHandler(svc invitations.Service, logger *slog.Logger, authn smqauthn.Authentication, instanceID string) http.Handler { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } mux := chi.NewRouter() mux.Group(func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, false)) + r.Use(httpapi.AuthenticateMiddleware(authn, false)) r.Route("/invitations", func(r chi.Router) { r.Post("/", otelhttp.NewHandler(kithttp.NewServer( sendInvitationEndpoint(svc), decodeSendInvitationReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "send_invitation").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( listInvitationsEndpoint(svc), decodeListInvitationsReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_invitations").ServeHTTP) r.Route("/{user_id}/{domain_id}", func(r chi.Router) { r.Get("/", otelhttp.NewHandler(kithttp.NewServer( viewInvitationEndpoint(svc), decodeInvitationReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view_invitations").ServeHTTP) r.Delete("/", otelhttp.NewHandler(kithttp.NewServer( deleteInvitationEndpoint(svc), decodeInvitationReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_invitation").ServeHTTP) }) r.Post("/accept", otelhttp.NewHandler(kithttp.NewServer( acceptInvitationEndpoint(svc), decodeAcceptInvitationReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "accept_invitation").ServeHTTP) r.Post("/reject", otelhttp.NewHandler(kithttp.NewServer( rejectInvitationEndpoint(svc), decodeAcceptInvitationReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "reject_invitation").ServeHTTP) }) @@ -89,7 +89,7 @@ func MakeHandler(svc invitations.Service, logger *slog.Logger, authn smqauthn.Au } func decodeSendInvitationReq(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -102,11 +102,11 @@ func decodeSendInvitationReq(_ context.Context, r *http.Request) (interface{}, e } func decodeListInvitationsReq(_ context.Context, r *http.Request) (interface{}, error) { - offset, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + offset, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - limit, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + limit, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } @@ -150,7 +150,7 @@ func decodeListInvitationsReq(_ context.Context, r *http.Request) (interface{}, } func decodeAcceptInvitationReq(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } diff --git a/journal/api/endpoint.go b/journal/api/endpoint.go index 2042aeba3f0..cfdc9b8b974 100644 --- a/journal/api/endpoint.go +++ b/journal/api/endpoint.go @@ -6,7 +6,7 @@ package api import ( "context" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/journal" "github.com/absmach/supermq/pkg/authn" @@ -22,7 +22,7 @@ func retrieveJournalsEndpoint(svc journal.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } diff --git a/journal/api/requests.go b/journal/api/requests.go index 8d52fd8ecfb..597a3194dcc 100644 --- a/journal/api/requests.go +++ b/journal/api/requests.go @@ -4,7 +4,7 @@ package api import ( - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/journal" ) @@ -18,10 +18,10 @@ func (req retrieveJournalsReq) validate() error { if req.token == "" { return apiutil.ErrBearerToken } - if req.page.Limit > api.DefLimit { + if req.page.Limit > httpapi.DefLimit { return apiutil.ErrLimitSize } - if req.page.Direction != "" && req.page.Direction != api.AscDir && req.page.Direction != api.DescDir { + if req.page.Direction != "" && req.page.Direction != httpapi.AscDir && req.page.Direction != httpapi.DescDir { return apiutil.ErrInvalidDirection } if req.page.EntityID == "" { diff --git a/journal/api/requests_test.go b/journal/api/requests_test.go index e88c52c9f01..be83b959558 100644 --- a/journal/api/requests_test.go +++ b/journal/api/requests_test.go @@ -6,7 +6,7 @@ package api import ( "testing" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/journal" "github.com/stretchr/testify/assert" @@ -51,7 +51,7 @@ func TestRetrieveJournalsReqValidate(t *testing.T) { req: retrieveJournalsReq{ token: token, page: journal.Page{ - Limit: api.DefLimit + 1, + Limit: httpapi.DefLimit + 1, EntityID: "id", EntityType: journal.UserEntity, }, diff --git a/journal/api/transport.go b/journal/api/transport.go index 7c7f5bc83c0..2b6427183c0 100644 --- a/journal/api/transport.go +++ b/journal/api/transport.go @@ -12,7 +12,7 @@ import ( "time" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/journal" smqauthn "github.com/absmach/supermq/pkg/authn" @@ -36,22 +36,22 @@ const ( // MakeHandler returns a HTTP API handler with health check and metrics. func MakeHandler(svc journal.Service, authn smqauthn.Authentication, logger *slog.Logger, svcName, instanceID string) http.Handler { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } mux := chi.NewRouter() - mux.With(api.AuthenticateMiddleware(authn, false)).Get("/journal/user/{userID}", otelhttp.NewHandler(kithttp.NewServer( + mux.With(httpapi.AuthenticateMiddleware(authn, false)).Get("/journal/user/{userID}", otelhttp.NewHandler(kithttp.NewServer( retrieveJournalsEndpoint(svc), decodeRetrieveUserJournalReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_user_journals").ServeHTTP) - mux.With(api.AuthenticateMiddleware(authn, true)).Get("/{domainID}/journal/{entityType}/{entityID}", otelhttp.NewHandler(kithttp.NewServer( + mux.With(httpapi.AuthenticateMiddleware(authn, true)).Get("/{domainID}/journal/{entityType}/{entityID}", otelhttp.NewHandler(kithttp.NewServer( retrieveJournalsEndpoint(svc), decodeRetrieveEntityJournalReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list__entity_journals").ServeHTTP) @@ -102,11 +102,11 @@ func decodeRetrieveUserJournalReq(_ context.Context, r *http.Request) (interface } func decodePageQuery(r *http.Request) (journal.Page, error) { - offset, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + offset, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return journal.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - limit, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + limit, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return journal.Page{}, errors.Wrap(apiutil.ErrValidation, err) } @@ -144,7 +144,7 @@ func decodePageQuery(r *http.Request) (journal.Page, error) { if err != nil { return journal.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - dir, err := apiutil.ReadStringQuery(r, api.DirKey, api.DescDir) + dir, err := apiutil.ReadStringQuery(r, httpapi.DirKey, httpapi.DescDir) if err != nil { return journal.Page{}, errors.Wrap(apiutil.ErrValidation, err) } diff --git a/pkg/roles/rolemanager/api/decoders.go b/pkg/roles/rolemanager/api/decoders.go index d8c6e23d21f..47173484eba 100644 --- a/pkg/roles/rolemanager/api/decoders.go +++ b/pkg/roles/rolemanager/api/decoders.go @@ -9,7 +9,7 @@ import ( "net/http" "strings" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/pkg/errors" "github.com/go-chi/chi/v5" @@ -24,7 +24,7 @@ func NewDecoder(entityIDTemplate string) Decoder { } func (d Decoder) DecodeCreateRole(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := createRoleReq{ @@ -38,11 +38,11 @@ func (d Decoder) DecodeCreateRole(_ context.Context, r *http.Request) (interface } func (d Decoder) DecodeListRoles(_ context.Context, r *http.Request) (interface{}, error) { - o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + o, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + l, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } @@ -65,7 +65,7 @@ func (d Decoder) DecodeViewRole(_ context.Context, r *http.Request) (interface{} } func (d Decoder) DecodeUpdateRole(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := updateRoleReq{ @@ -96,7 +96,7 @@ func (d Decoder) DecodeListAvailableActions(_ context.Context, r *http.Request) } func (d Decoder) DecodeAddRoleActions(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := addRoleActionsReq{ @@ -120,7 +120,7 @@ func (d Decoder) DecodeListRoleActions(_ context.Context, r *http.Request) (inte } func (d Decoder) DecodeDeleteRoleActions(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := deleteRoleActionsReq{ @@ -144,7 +144,7 @@ func (d Decoder) DecodeDeleteAllRoleActions(_ context.Context, r *http.Request) } func (d Decoder) DecodeAddRoleMembers(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := addRoleMembersReq{ @@ -159,11 +159,11 @@ func (d Decoder) DecodeAddRoleMembers(_ context.Context, r *http.Request) (inter } func (d Decoder) DecodeListRoleMembers(_ context.Context, r *http.Request) (interface{}, error) { - o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + o, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + l, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } @@ -178,7 +178,7 @@ func (d Decoder) DecodeListRoleMembers(_ context.Context, r *http.Request) (inte } func (d Decoder) DecodeDeleteRoleMembers(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := deleteRoleMembersReq{ diff --git a/pkg/roles/rolemanager/api/endpoints.go b/pkg/roles/rolemanager/api/endpoints.go index 5e35d68826b..035c145711d 100644 --- a/pkg/roles/rolemanager/api/endpoints.go +++ b/pkg/roles/rolemanager/api/endpoints.go @@ -6,7 +6,7 @@ package http import ( "context" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/pkg/authn" "github.com/absmach/supermq/pkg/errors" @@ -22,7 +22,7 @@ func CreateRoleEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -42,7 +42,7 @@ func ListRolesEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -62,7 +62,7 @@ func ViewRoleEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -82,7 +82,7 @@ func UpdateRoleEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -102,7 +102,7 @@ func DeleteRoleEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -121,7 +121,7 @@ func ListAvailableActionsEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -141,7 +141,7 @@ func AddRoleActionsEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -161,7 +161,7 @@ func ListRoleActionsEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -181,7 +181,7 @@ func DeleteRoleActionsEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -200,7 +200,7 @@ func DeleteAllRoleActionsEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -219,7 +219,7 @@ func AddRoleMembersEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -239,7 +239,7 @@ func ListRoleMembersEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -259,7 +259,7 @@ func DeleteRoleMembersEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -278,7 +278,7 @@ func DeleteAllRoleMembersEndpoint(svc roles.RoleManager) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } diff --git a/pkg/roles/rolemanager/api/requests.go b/pkg/roles/rolemanager/api/requests.go index a607ad5e89f..04271f99185 100644 --- a/pkg/roles/rolemanager/api/requests.go +++ b/pkg/roles/rolemanager/api/requests.go @@ -4,7 +4,7 @@ package http import ( - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" ) @@ -20,7 +20,7 @@ func (req createRoleReq) validate() error { if req.token == "" { return apiutil.ErrBearerToken } - if err := api.ValidateUUID(req.entityID); err != nil { + if err := httpapi.ValidateUUID(req.entityID); err != nil { return err } if len(req.RoleName) == 0 { @@ -47,7 +47,7 @@ func (req listRolesReq) validate() error { if req.entityID == "" { return apiutil.ErrMissingID } - if req.limit > api.MaxLimitSize || req.limit < 1 { + if req.limit > httpapi.MaxLimitSize || req.limit < 1 { return apiutil.ErrLimitSize } return nil @@ -249,7 +249,7 @@ func (req listRoleMembersReq) validate() error { if req.roleName == "" { return apiutil.ErrMissingRoleName } - if req.limit > api.MaxLimitSize || req.limit < 1 { + if req.limit > httpapi.MaxLimitSize || req.limit < 1 { return apiutil.ErrLimitSize } return nil diff --git a/pkg/roles/rolemanager/api/router.go b/pkg/roles/rolemanager/api/router.go index d47b7b3692c..21e4b00f747 100644 --- a/pkg/roles/rolemanager/api/router.go +++ b/pkg/roles/rolemanager/api/router.go @@ -4,7 +4,7 @@ package http import ( - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" "github.com/absmach/supermq/pkg/roles" "github.com/go-chi/chi/v5" kithttp "github.com/go-kit/kit/transport/http" @@ -16,14 +16,14 @@ func EntityRoleMangerRouter(svc roles.RoleManager, d Decoder, r chi.Router, opts r.Post("/", otelhttp.NewHandler(kithttp.NewServer( CreateRoleEndpoint(svc), d.DecodeCreateRole, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "create_role").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( ListRolesEndpoint(svc), d.DecodeListRoles, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_roles").ServeHTTP) @@ -31,21 +31,21 @@ func EntityRoleMangerRouter(svc roles.RoleManager, d Decoder, r chi.Router, opts r.Get("/", otelhttp.NewHandler(kithttp.NewServer( ViewRoleEndpoint(svc), d.DecodeViewRole, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view_role").ServeHTTP) r.Put("/", otelhttp.NewHandler(kithttp.NewServer( UpdateRoleEndpoint(svc), d.DecodeUpdateRole, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_role").ServeHTTP) r.Delete("/", otelhttp.NewHandler(kithttp.NewServer( DeleteRoleEndpoint(svc), d.DecodeDeleteRole, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_role").ServeHTTP) @@ -53,28 +53,28 @@ func EntityRoleMangerRouter(svc roles.RoleManager, d Decoder, r chi.Router, opts r.Post("/", otelhttp.NewHandler(kithttp.NewServer( AddRoleActionsEndpoint(svc), d.DecodeAddRoleActions, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "add_role_actions").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( ListRoleActionsEndpoint(svc), d.DecodeListRoleActions, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_role_actions").ServeHTTP) r.Post("/delete", otelhttp.NewHandler(kithttp.NewServer( DeleteRoleActionsEndpoint(svc), d.DecodeDeleteRoleActions, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_role_actions").ServeHTTP) r.Post("/delete-all", otelhttp.NewHandler(kithttp.NewServer( DeleteAllRoleActionsEndpoint(svc), d.DecodeDeleteAllRoleActions, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_all_role_actions").ServeHTTP) }) @@ -83,28 +83,28 @@ func EntityRoleMangerRouter(svc roles.RoleManager, d Decoder, r chi.Router, opts r.Post("/", otelhttp.NewHandler(kithttp.NewServer( AddRoleMembersEndpoint(svc), d.DecodeAddRoleMembers, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "add_role_members").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( ListRoleMembersEndpoint(svc), d.DecodeListRoleMembers, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_role_members").ServeHTTP) r.Post("/delete", otelhttp.NewHandler(kithttp.NewServer( DeleteRoleMembersEndpoint(svc), d.DecodeDeleteRoleMembers, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_role_members").ServeHTTP) r.Post("/delete-all", otelhttp.NewHandler(kithttp.NewServer( DeleteAllRoleMembersEndpoint(svc), d.DecodeDeleteAllRoleMembers, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_all_role_members").ServeHTTP) }) @@ -118,7 +118,7 @@ func EntityAvailableActionsRouter(svc roles.RoleManager, d Decoder, r chi.Router r.Get("/roles/available-actions", otelhttp.NewHandler(kithttp.NewServer( ListAvailableActionsEndpoint(svc), d.DecodeListAvailableActions, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_available_actions").ServeHTTP) diff --git a/provision/api/transport.go b/provision/api/transport.go index 911cf68e9a4..9625143b5d3 100644 --- a/provision/api/transport.go +++ b/provision/api/transport.go @@ -10,7 +10,7 @@ import ( "net/http" "github.com/absmach/supermq" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/pkg/errors" "github.com/absmach/supermq/provision" @@ -26,7 +26,7 @@ const ( // MakeHandler returns a HTTP handler for API endpoints. func MakeHandler(svc provision.Service, logger *slog.Logger, instanceID string) http.Handler { opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } r := chi.NewRouter() @@ -36,13 +36,13 @@ func MakeHandler(svc provision.Service, logger *slog.Logger, instanceID string) r.Post("/", kithttp.NewServer( doProvision(svc), decodeProvisionRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) r.Get("/", kithttp.NewServer( getMapping(svc), decodeMappingRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ).ServeHTTP) }) diff --git a/users/api/endpoint_test.go b/users/api/endpoint_test.go index 31b45d68d62..7cb5458ae6d 100644 --- a/users/api/endpoint_test.go +++ b/users/api/endpoint_test.go @@ -13,7 +13,7 @@ import ( "strings" "testing" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" authmocks "github.com/absmach/supermq/auth/mocks" grpcTokenV1 "github.com/absmach/supermq/internal/grpc/token/v1" @@ -506,7 +506,7 @@ func TestListUsers(t *testing.T) { { desc: "list users with limit greater than max", token: validToken, - query: fmt.Sprintf("limit=%d", api.MaxLimitSize+1), + query: fmt.Sprintf("limit=%d", httpapi.MaxLimitSize+1), status: http.StatusBadRequest, authnRes: smqauthn.Session{UserID: validID, DomainID: domainID}, err: apiutil.ErrValidation, @@ -2673,7 +2673,7 @@ func TestListUsersByUserGroupId(t *testing.T) { desc: "list users with limit greater than max", token: validToken, groupID: validID, - query: fmt.Sprintf("limit=%d", api.MaxLimitSize+1), + query: fmt.Sprintf("limit=%d", httpapi.MaxLimitSize+1), status: http.StatusBadRequest, authnRes: smqauthn.Session{UserID: validID, DomainID: domainID}, err: apiutil.ErrValidation, @@ -2990,7 +2990,7 @@ func TestListUsersByChannelID(t *testing.T) { desc: "list users with limit greater than max", token: validToken, channelID: validID, - query: fmt.Sprintf("limit=%d", api.MaxLimitSize+1), + query: fmt.Sprintf("limit=%d", httpapi.MaxLimitSize+1), status: http.StatusBadRequest, authnRes: smqauthn.Session{UserID: validID, DomainID: domainID}, err: apiutil.ErrValidation, @@ -3327,7 +3327,7 @@ func TestListUsersByDomainID(t *testing.T) { desc: "list users with limit greater than max", token: validToken, domainID: validID, - query: fmt.Sprintf("limit=%d", api.MaxLimitSize+1), + query: fmt.Sprintf("limit=%d", httpapi.MaxLimitSize+1), status: http.StatusBadRequest, authnRes: smqauthn.Session{UserID: validID, DomainID: domainID}, err: apiutil.ErrValidation, @@ -3673,7 +3673,7 @@ func TestListUsersByClientID(t *testing.T) { desc: "list users with limit greater than max", token: validToken, clientID: validID, - query: fmt.Sprintf("limit=%d", api.MaxLimitSize+1), + query: fmt.Sprintf("limit=%d", httpapi.MaxLimitSize+1), status: http.StatusBadRequest, authnRes: smqauthn.Session{UserID: validID, DomainID: domainID}, err: apiutil.ErrValidation, diff --git a/users/api/endpoints.go b/users/api/endpoints.go index 3641dddb875..4bf57a5164a 100644 --- a/users/api/endpoints.go +++ b/users/api/endpoints.go @@ -6,7 +6,7 @@ package api import ( "context" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/pkg/authn" "github.com/absmach/supermq/pkg/errors" @@ -25,7 +25,7 @@ func registrationEndpoint(svc users.Service, selfRegister bool) endpoint.Endpoin var ok bool if !selfRegister { - session, ok = ctx.Value(api.SessionKey).(authn.Session) + session, ok = ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -50,7 +50,7 @@ func viewEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -65,7 +65,7 @@ func viewEndpoint(svc users.Service) endpoint.Endpoint { func viewProfileEndpoint(svc users.Service) endpoint.Endpoint { return func(ctx context.Context, request interface{}) (interface{}, error) { - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -85,7 +85,7 @@ func listUsersEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -172,7 +172,7 @@ func listMembersByGroupEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -195,7 +195,7 @@ func listMembersByChannelEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -217,7 +217,7 @@ func listMembersByClientEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -239,7 +239,7 @@ func listMembersByDomainEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -260,7 +260,7 @@ func updateEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -288,7 +288,7 @@ func updateTagsEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -314,7 +314,7 @@ func updateEmailEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -363,7 +363,7 @@ func passwordResetEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -382,7 +382,7 @@ func updateSecretEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -402,7 +402,7 @@ func updateUsernameEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -428,7 +428,7 @@ func updateProfilePictureEndpoint(svc users.Service) endpoint.Endpoint { ProfilePicture: req.ProfilePicture, } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthorization } @@ -454,7 +454,7 @@ func updateRoleEndpoint(svc users.Service) endpoint.Endpoint { Role: req.role, } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -495,7 +495,7 @@ func refreshTokenEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -520,7 +520,7 @@ func enableEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -541,7 +541,7 @@ func disableEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } @@ -562,7 +562,7 @@ func deleteEndpoint(svc users.Service) endpoint.Endpoint { return nil, errors.Wrap(apiutil.ErrValidation, err) } - session, ok := ctx.Value(api.SessionKey).(authn.Session) + session, ok := ctx.Value(httpapi.SessionKey).(authn.Session) if !ok { return nil, svcerr.ErrAuthentication } diff --git a/users/api/requests.go b/users/api/requests.go index db71551ea8c..e2e4ef2066d 100644 --- a/users/api/requests.go +++ b/users/api/requests.go @@ -7,7 +7,7 @@ import ( "net/mail" "net/url" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" svcerr "github.com/absmach/supermq/pkg/errors/service" "github.com/absmach/supermq/users" @@ -20,10 +20,10 @@ type createUserReq struct { } func (req createUserReq) validate() error { - if len(req.User.FirstName) > api.MaxNameSize { + if len(req.User.FirstName) > httpapi.MaxNameSize { return apiutil.ErrNameSize } - if len(req.User.LastName) > api.MaxNameSize { + if len(req.User.LastName) > httpapi.MaxNameSize { return apiutil.ErrNameSize } if req.User.FirstName == "" { @@ -95,7 +95,7 @@ func (req listUsersReq) validate() error { if req.limit > maxLimitSize || req.limit < 1 { return apiutil.ErrLimitSize } - if req.dir != "" && (req.dir != api.AscDir && req.dir != api.DescDir) { + if req.dir != "" && (req.dir != httpapi.AscDir && req.dir != httpapi.DescDir) { return apiutil.ErrInvalidDirection } @@ -221,7 +221,7 @@ func (req updateUsernameReq) validate() error { if req.id == "" { return apiutil.ErrMissingID } - if len(req.Username) > api.MaxNameSize { + if len(req.Username) > httpapi.MaxNameSize { return apiutil.ErrNameSize } if req.Username == "" { diff --git a/users/api/requests_test.go b/users/api/requests_test.go index a437373c8fa..104ce64b220 100644 --- a/users/api/requests_test.go +++ b/users/api/requests_test.go @@ -8,7 +8,7 @@ import ( "strings" "testing" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" "github.com/absmach/supermq/internal/testsutil" "github.com/absmach/supermq/users" @@ -54,7 +54,7 @@ func TestCreateUserReqValidate(t *testing.T) { req: createUserReq{ User: users.User{ ID: validID, - FirstName: strings.Repeat("a", api.MaxNameSize+1), + FirstName: strings.Repeat("a", httpapi.MaxNameSize+1), LastName: valid, }, }, @@ -156,7 +156,7 @@ func TestListUsersReqValidate(t *testing.T) { { desc: "limit too big", req: listUsersReq{ - limit: api.MaxLimitSize + 1, + limit: httpapi.MaxLimitSize + 1, }, err: apiutil.ErrLimitSize, }, @@ -326,7 +326,7 @@ func TestUpdateUsernameReqValidate(t *testing.T) { desc: "name too long", req: updateUsernameReq{ id: validID, - Username: strings.Repeat("a", api.MaxNameSize+1), + Username: strings.Repeat("a", httpapi.MaxNameSize+1), }, err: apiutil.ErrNameSize, }, diff --git a/users/api/users.go b/users/api/users.go index cdf099ab390..87c64eefc08 100644 --- a/users/api/users.go +++ b/users/api/users.go @@ -11,7 +11,7 @@ import ( "regexp" "strings" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" apiutil "github.com/absmach/supermq/api/http/util" smqauth "github.com/absmach/supermq/auth" grpcTokenV1 "github.com/absmach/supermq/internal/grpc/token/v1" @@ -32,7 +32,7 @@ func usersHandler(svc users.Service, authn smqauthn.Authentication, tokenClient passRegex = pr opts := []kithttp.ServerOption{ - kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)), + kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, httpapi.EncodeError)), } r.Route("/users", func(r chi.Router) { @@ -41,140 +41,140 @@ func usersHandler(svc users.Service, authn smqauthn.Authentication, tokenClient r.Post("/", otelhttp.NewHandler(kithttp.NewServer( registrationEndpoint(svc, selfRegister), decodeCreateUserReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "register_user").ServeHTTP) default: - r.With(api.AuthenticateMiddleware(authn, false)).Post("/", otelhttp.NewHandler(kithttp.NewServer( + r.With(httpapi.AuthenticateMiddleware(authn, false)).Post("/", otelhttp.NewHandler(kithttp.NewServer( registrationEndpoint(svc, selfRegister), decodeCreateUserReq, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "register_user").ServeHTTP) } r.Group(func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, false)) + r.Use(httpapi.AuthenticateMiddleware(authn, false)) r.Get("/profile", otelhttp.NewHandler(kithttp.NewServer( viewProfileEndpoint(svc), decodeViewProfile, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view_profile").ServeHTTP) r.Get("/{id}", otelhttp.NewHandler(kithttp.NewServer( viewEndpoint(svc), decodeViewUser, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "view_user").ServeHTTP) r.Get("/", otelhttp.NewHandler(kithttp.NewServer( listUsersEndpoint(svc), decodeListUsers, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_users").ServeHTTP) r.Get("/search", otelhttp.NewHandler(kithttp.NewServer( searchUsersEndpoint(svc), decodeSearchUsers, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "search_users").ServeHTTP) r.Patch("/secret", otelhttp.NewHandler(kithttp.NewServer( updateSecretEndpoint(svc), decodeUpdateUserSecret, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_user_secret").ServeHTTP) r.Patch("/{id}", otelhttp.NewHandler(kithttp.NewServer( updateEndpoint(svc), decodeUpdateUser, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_user").ServeHTTP) r.Patch("/{id}/username", otelhttp.NewHandler(kithttp.NewServer( updateUsernameEndpoint(svc), decodeUpdateUsername, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_username").ServeHTTP) r.Patch("/{id}/picture", otelhttp.NewHandler(kithttp.NewServer( updateProfilePictureEndpoint(svc), decodeUpdateUserProfilePicture, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_profile_picture").ServeHTTP) r.Patch("/{id}/tags", otelhttp.NewHandler(kithttp.NewServer( updateTagsEndpoint(svc), decodeUpdateUserTags, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_user_tags").ServeHTTP) r.Patch("/{id}/email", otelhttp.NewHandler(kithttp.NewServer( updateEmailEndpoint(svc), decodeUpdateUserEmail, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_user_email").ServeHTTP) r.Patch("/{id}/role", otelhttp.NewHandler(kithttp.NewServer( updateRoleEndpoint(svc), decodeUpdateUserRole, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "update_user_role").ServeHTTP) r.Post("/{id}/enable", otelhttp.NewHandler(kithttp.NewServer( enableEndpoint(svc), decodeChangeUserStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "enable_user").ServeHTTP) r.Post("/{id}/disable", otelhttp.NewHandler(kithttp.NewServer( disableEndpoint(svc), decodeChangeUserStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "disable_user").ServeHTTP) r.Delete("/{id}", otelhttp.NewHandler(kithttp.NewServer( deleteEndpoint(svc), decodeChangeUserStatus, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "delete_user").ServeHTTP) r.Post("/tokens/refresh", otelhttp.NewHandler(kithttp.NewServer( refreshTokenEndpoint(svc), decodeRefreshToken, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "refresh_token").ServeHTTP) }) }) r.Group(func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, false)) + r.Use(httpapi.AuthenticateMiddleware(authn, false)) r.Put("/password/reset", otelhttp.NewHandler(kithttp.NewServer( passwordResetEndpoint(svc), decodePasswordReset, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "password_reset").ServeHTTP) }) r.Group(func(r chi.Router) { - r.Use(api.AuthenticateMiddleware(authn, true)) + r.Use(httpapi.AuthenticateMiddleware(authn, true)) // Ideal location: users service, groups endpoint. // Reason for placing here : @@ -184,7 +184,7 @@ func usersHandler(svc users.Service, authn smqauthn.Authentication, tokenClient r.Get("/{domainID}/groups/{groupID}/users", otelhttp.NewHandler(kithttp.NewServer( listMembersByGroupEndpoint(svc), decodeListMembersByGroup, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_users_by_user_group_id").ServeHTTP) @@ -196,21 +196,21 @@ func usersHandler(svc users.Service, authn smqauthn.Authentication, tokenClient r.Get("/{domainID}/channels/{channelID}/users", otelhttp.NewHandler(kithttp.NewServer( listMembersByChannelEndpoint(svc), decodeListMembersByChannel, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_users_by_channel_id").ServeHTTP) r.Get("/{domainID}/clients/{clientID}/users", otelhttp.NewHandler(kithttp.NewServer( listMembersByClientEndpoint(svc), decodeListMembersByClient, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_users_by_client_id").ServeHTTP) r.Get("/{domainID}/users", otelhttp.NewHandler(kithttp.NewServer( listMembersByDomainEndpoint(svc), decodeListMembersByDomain, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "list_users_by_domain_id").ServeHTTP) }) @@ -218,14 +218,14 @@ func usersHandler(svc users.Service, authn smqauthn.Authentication, tokenClient r.Post("/users/tokens/issue", otelhttp.NewHandler(kithttp.NewServer( issueTokenEndpoint(svc), decodeCredentials, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "issue_token").ServeHTTP) r.Post("/password/reset-request", otelhttp.NewHandler(kithttp.NewServer( passwordResetRequestEndpoint(svc), decodePasswordResetRequest, - api.EncodeResponse, + httpapi.EncodeResponse, opts..., ), "password_reset_req").ServeHTTP) @@ -249,51 +249,51 @@ func decodeViewProfile(_ context.Context, r *http.Request) (interface{}, error) } func decodeListUsers(_ context.Context, r *http.Request) (interface{}, error) { - s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefUserStatus) + s, err := apiutil.ReadStringQuery(r, httpapi.StatusKey, httpapi.DefUserStatus) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + o, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + l, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - m, err := apiutil.ReadMetadataQuery(r, api.MetadataKey, nil) + m, err := apiutil.ReadMetadataQuery(r, httpapi.MetadataKey, nil) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - n, err := apiutil.ReadStringQuery(r, api.UsernameKey, "") + n, err := apiutil.ReadStringQuery(r, httpapi.UsernameKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - d, err := apiutil.ReadStringQuery(r, api.EmailKey, "") + d, err := apiutil.ReadStringQuery(r, httpapi.EmailKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - i, err := apiutil.ReadStringQuery(r, api.FirstNameKey, "") + i, err := apiutil.ReadStringQuery(r, httpapi.FirstNameKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - f, err := apiutil.ReadStringQuery(r, api.LastNameKey, "") + f, err := apiutil.ReadStringQuery(r, httpapi.LastNameKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - t, err := apiutil.ReadStringQuery(r, api.TagKey, "") + t, err := apiutil.ReadStringQuery(r, httpapi.TagKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - order, err := apiutil.ReadStringQuery(r, api.OrderKey, api.DefOrder) + order, err := apiutil.ReadStringQuery(r, httpapi.OrderKey, httpapi.DefOrder) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - dir, err := apiutil.ReadStringQuery(r, api.DirKey, api.DefDir) + dir, err := apiutil.ReadStringQuery(r, httpapi.DirKey, httpapi.DefDir) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - id, err := apiutil.ReadStringQuery(r, api.IDOrder, "") + id, err := apiutil.ReadStringQuery(r, httpapi.IDOrder, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } @@ -321,35 +321,35 @@ func decodeListUsers(_ context.Context, r *http.Request) (interface{}, error) { } func decodeSearchUsers(_ context.Context, r *http.Request) (interface{}, error) { - o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + o, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + l, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - n, err := apiutil.ReadStringQuery(r, api.UsernameKey, "") + n, err := apiutil.ReadStringQuery(r, httpapi.UsernameKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - f, err := apiutil.ReadStringQuery(r, api.FirstNameKey, "") + f, err := apiutil.ReadStringQuery(r, httpapi.FirstNameKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - e, err := apiutil.ReadStringQuery(r, api.LastNameKey, "") + e, err := apiutil.ReadStringQuery(r, httpapi.LastNameKey, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - id, err := apiutil.ReadStringQuery(r, api.IDOrder, "") + id, err := apiutil.ReadStringQuery(r, httpapi.IDOrder, "") if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - order, err := apiutil.ReadStringQuery(r, api.OrderKey, api.DefOrder) + order, err := apiutil.ReadStringQuery(r, httpapi.OrderKey, httpapi.DefOrder) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } - dir, err := apiutil.ReadStringQuery(r, api.DirKey, api.DefDir) + dir, err := apiutil.ReadStringQuery(r, httpapi.DirKey, httpapi.DefDir) if err != nil { return nil, errors.Wrap(apiutil.ErrValidation, err) } @@ -376,7 +376,7 @@ func decodeSearchUsers(_ context.Context, r *http.Request) (interface{}, error) } func decodeUpdateUser(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -391,7 +391,7 @@ func decodeUpdateUser(_ context.Context, r *http.Request) (interface{}, error) { } func decodeUpdateUserTags(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -406,7 +406,7 @@ func decodeUpdateUserTags(_ context.Context, r *http.Request) (interface{}, erro } func decodeUpdateUserEmail(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -421,7 +421,7 @@ func decodeUpdateUserEmail(_ context.Context, r *http.Request) (interface{}, err } func decodeUpdateUserSecret(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -434,7 +434,7 @@ func decodeUpdateUserSecret(_ context.Context, r *http.Request) (interface{}, er } func decodeUpdateUsername(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -449,7 +449,7 @@ func decodeUpdateUsername(_ context.Context, r *http.Request) (interface{}, erro } func decodeUpdateUserProfilePicture(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -465,7 +465,7 @@ func decodeUpdateUserProfilePicture(_ context.Context, r *http.Request) (interfa } func decodePasswordResetRequest(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, apiutil.ErrUnsupportedContentType } @@ -479,7 +479,7 @@ func decodePasswordResetRequest(_ context.Context, r *http.Request) (interface{} } func decodePasswordReset(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -492,7 +492,7 @@ func decodePasswordReset(_ context.Context, r *http.Request) (interface{}, error } func decodeUpdateUserRole(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -508,7 +508,7 @@ func decodeUpdateUserRole(_ context.Context, r *http.Request) (interface{}, erro } func decodeCredentials(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -521,7 +521,7 @@ func decodeCredentials(_ context.Context, r *http.Request) (interface{}, error) } func decodeRefreshToken(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } req := tokenReq{RefreshToken: apiutil.ExtractBearerToken(r)} @@ -530,7 +530,7 @@ func decodeRefreshToken(_ context.Context, r *http.Request) (interface{}, error) } func decodeCreateUserReq(_ context.Context, r *http.Request) (interface{}, error) { - if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) { + if !strings.Contains(r.Header.Get("Content-Type"), httpapi.ContentType) { return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType) } @@ -551,7 +551,7 @@ func decodeChangeUserStatus(_ context.Context, r *http.Request) (interface{}, er } func decodeListMembersByGroup(_ context.Context, r *http.Request) (interface{}, error) { - page, err := queryPageParams(r, api.DefPermission) + page, err := queryPageParams(r, httpapi.DefPermission) if err != nil { return nil, err } @@ -564,7 +564,7 @@ func decodeListMembersByGroup(_ context.Context, r *http.Request) (interface{}, } func decodeListMembersByChannel(_ context.Context, r *http.Request) (interface{}, error) { - page, err := queryPageParams(r, api.DefPermission) + page, err := queryPageParams(r, httpapi.DefPermission) if err != nil { return nil, err } @@ -577,7 +577,7 @@ func decodeListMembersByChannel(_ context.Context, r *http.Request) (interface{} } func decodeListMembersByClient(_ context.Context, r *http.Request) (interface{}, error) { - page, err := queryPageParams(r, api.DefPermission) + page, err := queryPageParams(r, httpapi.DefPermission) if err != nil { return nil, err } @@ -604,39 +604,39 @@ func decodeListMembersByDomain(_ context.Context, r *http.Request) (interface{}, } func queryPageParams(r *http.Request, defPermission string) (users.Page, error) { - s, err := apiutil.ReadStringQuery(r, api.StatusKey, api.DefClientStatus) + s, err := apiutil.ReadStringQuery(r, httpapi.StatusKey, httpapi.DefClientStatus) if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - o, err := apiutil.ReadNumQuery[uint64](r, api.OffsetKey, api.DefOffset) + o, err := apiutil.ReadNumQuery[uint64](r, httpapi.OffsetKey, httpapi.DefOffset) if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - l, err := apiutil.ReadNumQuery[uint64](r, api.LimitKey, api.DefLimit) + l, err := apiutil.ReadNumQuery[uint64](r, httpapi.LimitKey, httpapi.DefLimit) if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - m, err := apiutil.ReadMetadataQuery(r, api.MetadataKey, nil) + m, err := apiutil.ReadMetadataQuery(r, httpapi.MetadataKey, nil) if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - n, err := apiutil.ReadStringQuery(r, api.UsernameKey, "") + n, err := apiutil.ReadStringQuery(r, httpapi.UsernameKey, "") if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - f, err := apiutil.ReadStringQuery(r, api.FirstNameKey, "") + f, err := apiutil.ReadStringQuery(r, httpapi.FirstNameKey, "") if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - a, err := apiutil.ReadStringQuery(r, api.LastNameKey, "") + a, err := apiutil.ReadStringQuery(r, httpapi.LastNameKey, "") if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - i, err := apiutil.ReadStringQuery(r, api.EmailKey, "") + i, err := apiutil.ReadStringQuery(r, httpapi.EmailKey, "") if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - t, err := apiutil.ReadStringQuery(r, api.TagKey, "") + t, err := apiutil.ReadStringQuery(r, httpapi.TagKey, "") if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } @@ -644,11 +644,11 @@ func queryPageParams(r *http.Request, defPermission string) (users.Page, error) if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - p, err := apiutil.ReadStringQuery(r, api.PermissionKey, defPermission) + p, err := apiutil.ReadStringQuery(r, httpapi.PermissionKey, defPermission) if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } - lp, err := apiutil.ReadBoolQuery(r, api.ListPerms, api.DefListPerms) + lp, err := apiutil.ReadBoolQuery(r, httpapi.ListPerms, httpapi.DefListPerms) if err != nil { return users.Page{}, errors.Wrap(apiutil.ErrValidation, err) } diff --git a/users/postgres/users.go b/users/postgres/users.go index 54c88820b47..53575d296d5 100644 --- a/users/postgres/users.go +++ b/users/postgres/users.go @@ -11,7 +11,7 @@ import ( "strings" "time" - api "github.com/absmach/supermq/api/http" + httpapi "github.com/absmach/supermq/api/http" "github.com/absmach/supermq/groups" "github.com/absmach/supermq/pkg/errors" repoerr "github.com/absmach/supermq/pkg/errors/repository" @@ -652,7 +652,7 @@ func applyOrdering(emq string, pm users.Page) string { switch pm.Order { case "username", "first_name", "email", "last_name", "created_at", "updated_at": emq = fmt.Sprintf("%s ORDER BY %s", emq, pm.Order) - if pm.Dir == api.AscDir || pm.Dir == api.DescDir { + if pm.Dir == httpapi.AscDir || pm.Dir == httpapi.DescDir { emq = fmt.Sprintf("%s %s", emq, pm.Dir) } }