2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2019-07-04 17:06:56 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
kitot "github.com/go-kit/kit/tracing/opentracing"
|
2019-07-04 17:06:56 +02:00
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
|
|
"github.com/go-zoo/bone"
|
|
|
|
"github.com/mainflux/mainflux"
|
2022-03-03 17:13:46 +01:00
|
|
|
"github.com/mainflux/mainflux/internal/apiutil"
|
|
|
|
"github.com/mainflux/mainflux/logger"
|
2021-03-23 11:48:05 +01:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2019-07-04 17:06:56 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2019-07-18 15:01:09 +02:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2019-07-04 17:06:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const contentType = "application/json"
|
|
|
|
|
|
|
|
// MakeHandler returns a HTTP handler for auth API endpoints.
|
2022-03-03 17:13:46 +01:00
|
|
|
func MakeHandler(tracer opentracing.Tracer, svc things.Service, logger logger.Logger) http.Handler {
|
2019-07-04 17:06:56 +02:00
|
|
|
opts := []kithttp.ServerOption{
|
2022-03-03 17:13:46 +01:00
|
|
|
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, encodeError)),
|
2019-07-04 17:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
r := bone.New()
|
|
|
|
|
|
|
|
r.Post("/identify", kithttp.NewServer(
|
2019-07-18 15:01:09 +02:00
|
|
|
kitot.TraceServer(tracer, "identify")(identifyEndpoint(svc)),
|
2019-07-04 17:06:56 +02:00
|
|
|
decodeIdentify,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
))
|
|
|
|
|
2021-03-10 11:52:33 +01:00
|
|
|
r.Post("/identify/channels/:chanId/access-by-key", kithttp.NewServer(
|
2019-10-21 15:24:45 -06:00
|
|
|
kitot.TraceServer(tracer, "can_access_by_key")(canAccessByKeyEndpoint(svc)),
|
|
|
|
decodeCanAccessByKey,
|
2019-07-04 17:06:56 +02:00
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
))
|
|
|
|
|
2021-03-10 11:52:33 +01:00
|
|
|
r.Post("/identify/channels/:chanId/access-by-id", kithttp.NewServer(
|
2019-07-18 15:01:09 +02:00
|
|
|
kitot.TraceServer(tracer, "can_access_by_id")(canAccessByIDEndpoint(svc)),
|
2019-07-15 18:28:15 +02:00
|
|
|
decodeCanAccessByID,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
))
|
|
|
|
|
2019-07-04 17:06:56 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeIdentify(_ context.Context, r *http.Request) (interface{}, error) {
|
|
|
|
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
|
2021-03-23 11:48:05 +01:00
|
|
|
return nil, errors.ErrUnsupportedContentType
|
2019-07-04 17:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
req := identifyReq{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2022-02-14 22:49:23 +01:00
|
|
|
return nil, errors.Wrap(errors.ErrMalformedEntity, err)
|
2019-07-04 17:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
func decodeCanAccessByKey(_ context.Context, r *http.Request) (interface{}, error) {
|
2019-07-04 17:06:56 +02:00
|
|
|
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
|
2021-03-23 11:48:05 +01:00
|
|
|
return nil, errors.ErrUnsupportedContentType
|
2019-07-04 17:06:56 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
req := canAccessByKeyReq{
|
2019-07-04 17:06:56 +02:00
|
|
|
chanID: bone.GetValue(r, "chanId"),
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2022-02-14 22:49:23 +01:00
|
|
|
return nil, errors.Wrap(errors.ErrMalformedEntity, err)
|
2019-07-04 17:06:56 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:28:15 +02:00
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeCanAccessByID(_ context.Context, r *http.Request) (interface{}, error) {
|
|
|
|
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
|
2021-03-23 11:48:05 +01:00
|
|
|
return nil, errors.ErrUnsupportedContentType
|
2019-07-15 18:28:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
req := canAccessByIDReq{
|
|
|
|
chanID: bone.GetValue(r, "chanId"),
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2022-02-14 22:49:23 +01:00
|
|
|
return nil, errors.Wrap(errors.ErrMalformedEntity, err)
|
2019-07-15 18:28:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-04 17:06:56 +02:00
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
|
|
|
|
if ar, ok := response.(mainflux.Response); ok {
|
|
|
|
for k, v := range ar.Headers() {
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
}
|
2020-03-30 15:22:18 +02:00
|
|
|
w.Header().Set("Content-Type", contentType)
|
2019-07-04 17:06:56 +02:00
|
|
|
w.WriteHeader(ar.Code())
|
|
|
|
|
|
|
|
if ar.Empty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
2022-02-14 22:49:23 +01:00
|
|
|
switch {
|
2022-12-15 07:24:19 -08:00
|
|
|
case errors.Contains(err, apiutil.ErrBearerToken),
|
|
|
|
errors.Contains(err, apiutil.ErrBearerKey),
|
2022-03-03 17:13:46 +01:00
|
|
|
errors.Contains(err, errors.ErrAuthentication):
|
2020-09-23 15:44:39 +02:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2022-02-14 22:49:23 +01:00
|
|
|
case errors.Contains(err, errors.ErrNotFound):
|
2020-09-23 15:44:39 +02:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2022-02-14 22:49:23 +01:00
|
|
|
case errors.Contains(err, errors.ErrAuthorization):
|
2019-07-04 17:06:56 +02:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2022-02-14 22:49:23 +01:00
|
|
|
case errors.Contains(err, errors.ErrUnsupportedContentType):
|
2019-07-04 17:06:56 +02:00
|
|
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
2022-03-03 17:13:46 +01:00
|
|
|
case errors.Contains(err, errors.ErrMalformedEntity),
|
2022-12-15 07:24:19 -08:00
|
|
|
errors.Contains(err, apiutil.ErrMissingID):
|
2019-07-04 17:06:56 +02:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
default:
|
2022-02-14 22:49:23 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errorVal, ok := err.(errors.Error); ok {
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
2022-03-03 17:13:46 +01:00
|
|
|
if err := json.NewEncoder(w).Encode(apiutil.ErrorRes{Err: errorVal.Msg()}); err != nil {
|
2019-07-04 17:06:56 +02:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|