Dealer/pkg/internal/grpc/auth.go

46 lines
1.4 KiB
Go
Raw Normal View History

2024-07-14 12:25:30 +00:00
package grpc
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/directory"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"time"
)
func (v *Server) Authenticate(ctx context.Context, request *proto.AuthRequest) (*proto.AuthReply, error) {
instance := directory.GetServiceInstanceByType(directory.ServiceTypeAuthProvider)
if instance == nil {
return &proto.AuthReply{}, fmt.Errorf("no available service %s found", directory.ServiceTypeAuthProvider)
}
conn, err := instance.GetGrpcConn()
if err != nil {
return nil, fmt.Errorf("service is down: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
out, err := proto.NewAuthClient(conn).Authenticate(ctx, request)
return out, err
}
func (v *Server) EnsurePermGranted(ctx context.Context, request *proto.CheckPermRequest) (*proto.CheckPermReply, error) {
instance := directory.GetServiceInstanceByType(directory.ServiceTypeAuthProvider)
if instance == nil {
return &proto.CheckPermReply{}, fmt.Errorf("no available service %s found", directory.ServiceTypeAuthProvider)
}
conn, err := instance.GetGrpcConn()
if err != nil {
return nil, fmt.Errorf("service is down: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
out, err := proto.NewAuthClient(conn).EnsurePermGranted(ctx, request)
return out, err
}