src.dualinventive.com/go/companies-service/internal/infra/auth/auth.go

50 lines
1.1 KiB
Go

package auth
import (
"context"
"fmt"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc"
auth "src.dualinventive.com/go/authentication-service/grpc"
)
//Service contains authentication related operations
type Service interface {
VerifyToken(token string) (bool, error)
}
//ServiceGrpc contains authentication related operations over GRPC
type ServiceGrpc struct {
client auth.AuthenticationServiceClient
}
//NewAuthService returns a new GRPC based AuthService
func NewAuthService(host, port string) (Service, error) {
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
return nil, err
}
client := auth.NewAuthenticationServiceClient(conn)
return &ServiceGrpc{client}, nil
}
//VerifyToken verifies the given token to see if its valid
func (as *ServiceGrpc) VerifyToken(token string) (bool, error) {
// Login
md := metadata.Pairs(
"token", token,
)
ctx := metadata.NewOutgoingContext(context.Background(), md)
in := &auth.Empty{}
resp, err := as.client.VerifyToken(ctx, in)
if err != nil {
return false, err
}
return resp.Valid, nil
}