mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* Add both an ID and an Email to API key requests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Use return UserIdentity response Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Replace GetValue with GetEmail Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Refactor Mainflux Key Add `Subject` field and reorganize Key manipulation. **Remove backward compatibility** Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix service test Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix DB tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix API tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix JWT tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Uncomment and fix API tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix SQL statements alignment Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Issue method docs Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Retrieve API and API docs Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
92 lines
2.8 KiB
Go
92 lines
2.8 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package grpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
kitot "github.com/go-kit/kit/tracing/opentracing"
|
|
kitgrpc "github.com/go-kit/kit/transport/grpc"
|
|
mainflux "github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/authn"
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
var _ mainflux.AuthNServiceServer = (*grpcServer)(nil)
|
|
|
|
type grpcServer struct {
|
|
issue kitgrpc.Handler
|
|
identify kitgrpc.Handler
|
|
}
|
|
|
|
// NewServer returns new AuthnServiceServer instance.
|
|
func NewServer(tracer opentracing.Tracer, svc authn.Service) mainflux.AuthNServiceServer {
|
|
return &grpcServer{
|
|
issue: kitgrpc.NewServer(
|
|
kitot.TraceServer(tracer, "issue")(issueEndpoint(svc)),
|
|
decodeIssueRequest,
|
|
encodeIssueResponse,
|
|
),
|
|
identify: kitgrpc.NewServer(
|
|
kitot.TraceServer(tracer, "identify")(identifyEndpoint(svc)),
|
|
decodeIdentifyRequest,
|
|
encodeIdentifyResponse,
|
|
),
|
|
}
|
|
}
|
|
|
|
func (s *grpcServer) Issue(ctx context.Context, req *mainflux.IssueReq) (*mainflux.Token, error) {
|
|
_, res, err := s.issue.ServeGRPC(ctx, req)
|
|
if err != nil {
|
|
return nil, encodeError(err)
|
|
}
|
|
return res.(*mainflux.Token), nil
|
|
}
|
|
|
|
func (s *grpcServer) Identify(ctx context.Context, token *mainflux.Token) (*mainflux.UserIdentity, error) {
|
|
_, res, err := s.identify.ServeGRPC(ctx, token)
|
|
if err != nil {
|
|
return nil, encodeError(err)
|
|
}
|
|
return res.(*mainflux.UserIdentity), nil
|
|
}
|
|
|
|
func decodeIssueRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
|
|
req := grpcReq.(*mainflux.IssueReq)
|
|
return issueReq{id: req.GetId(), email: req.GetEmail(), keyType: req.GetType()}, nil
|
|
}
|
|
|
|
func encodeIssueResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
|
|
res := grpcRes.(issueRes)
|
|
return &mainflux.Token{Value: res.value}, encodeError(res.err)
|
|
}
|
|
|
|
func decodeIdentifyRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
|
|
req := grpcReq.(*mainflux.Token)
|
|
return identityReq{token: req.GetValue()}, nil
|
|
}
|
|
|
|
func encodeIdentifyResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
|
|
res := grpcRes.(identityRes)
|
|
return &mainflux.UserIdentity{Id: res.id, Email: res.email}, encodeError(res.err)
|
|
}
|
|
|
|
func encodeError(err error) error {
|
|
switch {
|
|
case errors.Contains(err, nil):
|
|
return nil
|
|
case errors.Contains(err, authn.ErrMalformedEntity):
|
|
return status.Error(codes.InvalidArgument, "received invalid token request")
|
|
case errors.Contains(err, authn.ErrUnauthorizedAccess):
|
|
return status.Error(codes.Unauthenticated, err.Error())
|
|
case errors.Contains(err, authn.ErrKeyExpired):
|
|
return status.Error(codes.Unauthenticated, err.Error())
|
|
default:
|
|
return status.Error(codes.Internal, "internal server error")
|
|
}
|
|
}
|