2024-07-14 12:25:30 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-07-14 12:56:06 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
2024-07-14 12:25:30 +00:00
|
|
|
"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) {
|
2024-07-14 12:56:06 +00:00
|
|
|
instance := directory.GetServiceInstanceByType(hyper.ServiceTypeAuthProvider)
|
2024-07-14 12:25:30 +00:00
|
|
|
if instance == nil {
|
2024-07-14 12:56:06 +00:00
|
|
|
return &proto.AuthReply{}, fmt.Errorf("no available service %s found", hyper.ServiceTypeAuthProvider)
|
2024-07-14 12:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-07-14 12:56:06 +00:00
|
|
|
instance := directory.GetServiceInstanceByType(hyper.ServiceTypeAuthProvider)
|
2024-07-14 12:25:30 +00:00
|
|
|
if instance == nil {
|
2024-07-14 12:56:06 +00:00
|
|
|
return &proto.CheckPermReply{}, fmt.Errorf("no available service %s found", hyper.ServiceTypeAuthProvider)
|
2024-07-14 12:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|