mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-14 19:29:11 +08:00

* Add authorization HTTP API to things service Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add new tests and update existing ones Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update swagger documentation Update swagger documentation for auth endpoints. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update README docs for things service Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update docker-compose and fix endpoint typo Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove commented code Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
122 lines
2.5 KiB
Go
122 lines
2.5 KiB
Go
//
|
|
// Copyright (c) 2019
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package http
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
"github.com/go-zoo/bone"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/things"
|
|
)
|
|
|
|
const contentType = "application/json"
|
|
|
|
var errUnsupportedContentType = errors.New("unsupported content type")
|
|
|
|
// MakeHandler returns a HTTP handler for auth API endpoints.
|
|
func MakeHandler(svc things.Service) http.Handler {
|
|
opts := []kithttp.ServerOption{
|
|
kithttp.ServerErrorEncoder(encodeError),
|
|
}
|
|
|
|
r := bone.New()
|
|
|
|
r.Post("/identify", kithttp.NewServer(
|
|
identifyEndpoint(svc),
|
|
decodeIdentify,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Post("/channels/:chanId/access", kithttp.NewServer(
|
|
canAccessEndpoint(svc),
|
|
decodeCanAccess,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
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
|
|
}
|
|
|
|
func decodeCanAccess(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
|
|
return nil, errUnsupportedContentType
|
|
}
|
|
|
|
req := canAccessReq{
|
|
chanID: bone.GetValue(r, "chanId"),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|