2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2017-09-23 01:55:29 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-02-14 22:49:23 +01:00
|
|
|
"encoding/json"
|
2023-06-14 12:40:37 +02:00
|
|
|
"io"
|
2017-09-23 01:55:29 +02:00
|
|
|
"net/http"
|
2019-03-15 18:38:07 +01:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-04-13 15:05:05 +02:00
|
|
|
"time"
|
2017-09-23 01:55:29 +02:00
|
|
|
|
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
|
|
"github.com/go-zoo/bone"
|
2017-09-23 15:52:39 +02:00
|
|
|
"github.com/mainflux/mainflux"
|
2020-04-01 21:22:13 +02:00
|
|
|
adapter "github.com/mainflux/mainflux/http"
|
2022-03-03 17:13:46 +01:00
|
|
|
"github.com/mainflux/mainflux/internal/apiutil"
|
2022-01-27 17:03:57 +01:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2020-06-03 15:16:19 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
2017-09-23 01:55:29 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2023-08-11 02:30:25 -07:00
|
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
2018-05-11 16:37:32 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2017-09-23 01:55:29 +02:00
|
|
|
)
|
|
|
|
|
2022-02-14 22:49:23 +01:00
|
|
|
const (
|
|
|
|
protocol = "http"
|
2022-06-13 12:04:48 +02:00
|
|
|
ctSenmlJSON = "application/senml+json"
|
|
|
|
ctSenmlCBOR = "application/senml+cbor"
|
2023-08-10 02:32:44 +05:30
|
|
|
contentType = "application/json"
|
2022-02-14 22:49:23 +01:00
|
|
|
)
|
2017-09-23 01:55:29 +02:00
|
|
|
|
2023-08-11 02:30:25 -07:00
|
|
|
var errMalformedSubtopic = errors.New("malformed subtopic")
|
2019-03-15 18:38:07 +01:00
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
var channelPartRegExp = regexp.MustCompile(`^/channels/([\w\-]+)/messages(/[^?]*)?(\?.*)?$`)
|
2017-09-23 01:55:29 +02:00
|
|
|
|
|
|
|
// MakeHandler returns a HTTP handler for API endpoints.
|
2023-07-07 12:14:55 +03:00
|
|
|
func MakeHandler(svc adapter.Service, instanceID string) http.Handler {
|
2017-09-23 01:55:29 +02:00
|
|
|
opts := []kithttp.ServerOption{
|
|
|
|
kithttp.ServerErrorEncoder(encodeError),
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
r := bone.New()
|
2023-07-31 20:20:04 +03:00
|
|
|
r.Post("/channels/:chanID/messages", otelhttp.NewHandler(kithttp.NewServer(
|
|
|
|
sendMessageEndpoint(svc),
|
2019-07-18 15:01:09 +02:00
|
|
|
decodeRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
2023-07-31 20:20:04 +03:00
|
|
|
), "publish"))
|
2019-07-18 15:01:09 +02:00
|
|
|
|
2023-07-31 20:20:04 +03:00
|
|
|
r.Post("/channels/:chanID/messages/*", otelhttp.NewHandler(kithttp.NewServer(
|
|
|
|
sendMessageEndpoint(svc),
|
2017-09-23 01:55:29 +02:00
|
|
|
decodeRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
2023-07-31 20:20:04 +03:00
|
|
|
), "publish"))
|
2019-07-18 15:01:09 +02:00
|
|
|
|
2023-07-07 12:14:55 +03:00
|
|
|
r.GetFunc("/health", mainflux.Health("http", instanceID))
|
2019-07-18 15:01:09 +02:00
|
|
|
r.Handle("/metrics", promhttp.Handler())
|
|
|
|
|
|
|
|
return r
|
2019-03-15 18:38:07 +01:00
|
|
|
}
|
2017-09-23 01:55:29 +02:00
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
func parseSubtopic(subtopic string) (string, error) {
|
|
|
|
if subtopic == "" {
|
|
|
|
return subtopic, nil
|
|
|
|
}
|
2017-09-23 01:55:29 +02:00
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
subtopic, err := url.QueryUnescape(subtopic)
|
2019-03-15 18:38:07 +01:00
|
|
|
if err != nil {
|
2023-08-10 02:32:44 +05:30
|
|
|
return "", errors.Wrap(apiutil.ErrValidation, errMalformedSubtopic)
|
2019-03-15 18:38:07 +01:00
|
|
|
}
|
2023-05-29 17:23:39 +03:00
|
|
|
subtopic = strings.ReplaceAll(subtopic, "/", ".")
|
2019-03-19 16:05:56 +01:00
|
|
|
|
|
|
|
elems := strings.Split(subtopic, ".")
|
|
|
|
filteredElems := []string{}
|
|
|
|
for _, elem := range elems {
|
|
|
|
if elem == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(elem) > 1 && (strings.Contains(elem, "*") || strings.Contains(elem, ">")) {
|
2023-08-10 02:32:44 +05:30
|
|
|
return "", errors.Wrap(apiutil.ErrValidation, errMalformedSubtopic)
|
2019-03-19 16:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
filteredElems = append(filteredElems, elem)
|
|
|
|
}
|
|
|
|
|
|
|
|
subtopic = strings.Join(filteredElems, ".")
|
2019-03-15 18:38:07 +01:00
|
|
|
return subtopic, nil
|
2017-09-23 01:55:29 +02:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
func decodeRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
2022-06-13 12:04:48 +02:00
|
|
|
ct := r.Header.Get("Content-Type")
|
2023-08-10 02:32:44 +05:30
|
|
|
if ct != ctSenmlJSON && ct != contentType && ct != ctSenmlCBOR {
|
|
|
|
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
|
2022-02-14 22:49:23 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
channelParts := channelPartRegExp.FindStringSubmatch(r.RequestURI)
|
|
|
|
if len(channelParts) < 2 {
|
2023-08-10 02:32:44 +05:30
|
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.ErrMalformedEntity)
|
2019-03-15 18:38:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
subtopic, err := parseSubtopic(channelParts[2])
|
2017-09-23 01:55:29 +02:00
|
|
|
if err != nil {
|
2023-08-10 02:32:44 +05:30
|
|
|
return nil, errors.Wrap(apiutil.ErrValidation, err)
|
2017-09-23 01:55:29 +02:00
|
|
|
}
|
2022-02-18 14:56:01 +01:00
|
|
|
|
|
|
|
var token string
|
|
|
|
_, pass, ok := r.BasicAuth()
|
|
|
|
switch {
|
|
|
|
case ok:
|
2021-07-22 15:47:55 +02:00
|
|
|
token = pass
|
2022-02-18 14:56:01 +01:00
|
|
|
case !ok:
|
2022-03-06 01:49:34 +01:00
|
|
|
token = apiutil.ExtractThingKey(r)
|
2021-07-22 15:47:55 +02:00
|
|
|
}
|
2017-09-23 01:55:29 +02:00
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
payload, err := io.ReadAll(r.Body)
|
2019-03-15 18:38:07 +01:00
|
|
|
if err != nil {
|
2023-08-10 02:32:44 +05:30
|
|
|
return nil, errors.Wrap(apiutil.ErrValidation, errors.ErrMalformedEntity)
|
2017-09-29 00:42:45 +02:00
|
|
|
}
|
2022-02-14 22:49:23 +01:00
|
|
|
defer r.Body.Close()
|
2017-09-29 00:42:45 +02:00
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
req := publishReq{
|
2023-02-02 20:28:32 +03:00
|
|
|
msg: &messaging.Message{
|
2022-02-14 22:49:23 +01:00
|
|
|
Protocol: protocol,
|
2023-04-20 22:53:02 +03:00
|
|
|
Channel: bone.GetValue(r, "chanID"),
|
2022-02-14 22:49:23 +01:00
|
|
|
Subtopic: subtopic,
|
|
|
|
Payload: payload,
|
|
|
|
Created: time.Now().UnixNano(),
|
|
|
|
},
|
2021-07-22 15:47:55 +02:00
|
|
|
token: token,
|
2018-01-07 14:42:38 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return req, nil
|
2017-09-23 01:55:29 +02:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
func encodeResponse(_ context.Context, w http.ResponseWriter, _ interface{}) error {
|
2017-09-23 01:55:29 +02:00
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
2023-08-10 02:32:44 +05:30
|
|
|
var wrapper error
|
|
|
|
if errors.Contains(err, apiutil.ErrValidation) {
|
|
|
|
wrapper, err = errors.Unwrap(err)
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:49:23 +01:00
|
|
|
switch {
|
2022-03-03 17:13:46 +01:00
|
|
|
case errors.Contains(err, errors.ErrAuthentication),
|
2023-08-10 02:32:44 +05:30
|
|
|
errors.Contains(err, apiutil.ErrBearerKey),
|
|
|
|
errors.Contains(err, apiutil.ErrBearerToken):
|
2022-02-01 17:33:23 +01:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2022-02-14 22:49:23 +01:00
|
|
|
case errors.Contains(err, errors.ErrAuthorization):
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2023-08-10 02:32:44 +05:30
|
|
|
case errors.Contains(err, apiutil.ErrUnsupportedContentType):
|
2022-02-14 22:49:23 +01:00
|
|
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
|
|
|
case errors.Contains(err, errMalformedSubtopic),
|
|
|
|
errors.Contains(err, errors.ErrMalformedEntity):
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
2017-09-23 01:55:29 +02:00
|
|
|
default:
|
2022-02-14 22:49:23 +01:00
|
|
|
switch e, ok := status.FromError(err); {
|
|
|
|
case ok:
|
2018-05-11 16:37:32 +02:00
|
|
|
switch e.Code() {
|
2022-02-01 17:33:23 +01:00
|
|
|
case codes.Unauthenticated:
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2022-02-14 22:49:23 +01:00
|
|
|
case codes.PermissionDenied:
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
case codes.Internal:
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-05-29 17:23:39 +03:00
|
|
|
case codes.NotFound:
|
|
|
|
err = errors.ErrNotFound
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2018-05-11 16:37:32 +02:00
|
|
|
default:
|
2022-02-14 22:49:23 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-05-11 16:37:32 +02:00
|
|
|
}
|
2022-02-14 22:49:23 +01:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 02:32:44 +05:30
|
|
|
if wrapper != nil {
|
|
|
|
err = errors.Wrap(wrapper, err)
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:49:23 +01:00
|
|
|
if errorVal, ok := err.(errors.Error); ok {
|
2023-08-10 02:32:44 +05:30
|
|
|
w.Header().Set("Content-Type", contentType)
|
2023-08-24 16:09:23 +03:00
|
|
|
if err := json.NewEncoder(w).Encode(errorVal); err != nil {
|
2022-02-14 22:49:23 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-05-11 16:37:32 +02:00
|
|
|
}
|
2017-09-23 01:55:29 +02:00
|
|
|
}
|
|
|
|
}
|