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"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"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"
|
|
|
|
"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"
|
|
|
|
|
|
|
|
var errUnsupportedContentType = errors.New("unsupported content type")
|
|
|
|
|
|
|
|
// MakeHandler returns a HTTP handler for auth API endpoints.
|
2019-07-18 15:01:09 +02:00
|
|
|
func MakeHandler(tracer opentracing.Tracer, svc things.Service) http.Handler {
|
2019-07-04 17:06:56 +02:00
|
|
|
opts := []kithttp.ServerOption{
|
|
|
|
kithttp.ServerErrorEncoder(encodeError),
|
|
|
|
}
|
|
|
|
|
|
|
|
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...,
|
|
|
|
))
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
r.Post("/channels/:chanId/access-by-key", kithttp.NewServer(
|
|
|
|
kitot.TraceServer(tracer, "can_access_by_key")(canAccessByKeyEndpoint(svc)),
|
|
|
|
decodeCanAccessByKey,
|
2019-07-04 17:06:56 +02:00
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
))
|
|
|
|
|
2019-07-15 18:28:15 +02:00
|
|
|
r.Post("/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) {
|
|
|
|
return nil, errUnsupportedContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
req := identifyReq{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
return nil, errUnsupportedContentType
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return nil, errUnsupportedContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
req := canAccessByIDReq{
|
|
|
|
chanID: bone.GetValue(r, "chanId"),
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-04 17:06:56 +02:00
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
|
|
|
|
if ar, ok := response.(mainflux.Response); ok {
|
|
|
|
for k, v := range ar.Headers() {
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(ar.Code())
|
|
|
|
|
|
|
|
if ar.Empty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case things.ErrUnauthorizedAccess:
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
case errUnsupportedContentType:
|
|
|
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
|
|
|
case io.ErrUnexpectedEOF:
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
case io.EOF:
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
default:
|
|
|
|
switch err.(type) {
|
|
|
|
case *json.SyntaxError:
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
case *json.UnmarshalTypeError:
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|