auth provider
This commit is contained in:
parent
08d8450dac
commit
a66873d12d
6 changed files with 432 additions and 0 deletions
40
pkg/authCheck/handler.go
Normal file
40
pkg/authCheck/handler.go
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
package authCheck
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
|
verifyV1 "merch-api/pkg/verify/v1"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const pkgLogHeader string = "Remote session checker |"
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
client verifyV1.AuthServiceClient
|
||||||
|
*service
|
||||||
|
}
|
||||||
|
|
||||||
|
type Deps struct {
|
||||||
|
Addr string
|
||||||
|
Timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(deps Deps) *Handler {
|
||||||
|
var opts []grpc.DialOption
|
||||||
|
insec := grpc.WithTransportCredentials(insecure.NewCredentials())
|
||||||
|
opts = append(opts, insec)
|
||||||
|
|
||||||
|
conn, err := grpc.NewClient(deps.Addr, opts...)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Fatalf("%v grpc connection failed", pkgLogHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := verifyV1.NewAuthServiceClient(conn)
|
||||||
|
log.WithField("address", deps.Addr).Debugf("%v client", pkgLogHeader)
|
||||||
|
|
||||||
|
return &Handler{
|
||||||
|
client: client,
|
||||||
|
service: newService(client, deps.Timeout),
|
||||||
|
}
|
||||||
|
}
|
||||||
7
pkg/authCheck/interface.go
Normal file
7
pkg/authCheck/interface.go
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package authCheck
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type AuthChecker interface {
|
||||||
|
VerifySession(ctx context.Context, sessionUuid string, serviceId int32) (string, error)
|
||||||
|
}
|
||||||
43
pkg/authCheck/service.go
Normal file
43
pkg/authCheck/service.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
package authCheck
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
verifyV1 "merch-api/pkg/verify/v1"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type service struct {
|
||||||
|
client verifyV1.AuthServiceClient
|
||||||
|
timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func newService(c verifyV1.AuthServiceClient, timeout time.Duration) *service {
|
||||||
|
return &service{
|
||||||
|
client: c,
|
||||||
|
timeout: timeout,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) VerifySession(ctx context.Context, sessionUuid string, serviceId int32) (string, error) {
|
||||||
|
runCtx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
response, err := s.client.VerifyToken(runCtx, &verifyV1.VerifyTokenRequest{
|
||||||
|
SessionToken: sessionUuid,
|
||||||
|
ServiceId: serviceId,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if response == nil {
|
||||||
|
return "", errors.New("no token")
|
||||||
|
}
|
||||||
|
|
||||||
|
if response.IsValid != true {
|
||||||
|
return "", errors.New("invalid token")
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.UserUuid, nil
|
||||||
|
}
|
||||||
202
pkg/verify/v1/verify.pb.go
Normal file
202
pkg/verify/v1/verify.pb.go
Normal file
|
|
@ -0,0 +1,202 @@
|
||||||
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// protoc-gen-go v1.36.8
|
||||||
|
// protoc v6.33.1
|
||||||
|
// source: proto/verify.proto
|
||||||
|
|
||||||
|
package verifyV1
|
||||||
|
|
||||||
|
import (
|
||||||
|
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||||
|
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||||
|
reflect "reflect"
|
||||||
|
sync "sync"
|
||||||
|
unsafe "unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Verify that this generated code is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||||
|
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||||
|
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||||
|
)
|
||||||
|
|
||||||
|
type VerifyTokenRequest struct {
|
||||||
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
|
SessionToken string `protobuf:"bytes,1,opt,name=sessionToken,proto3" json:"sessionToken,omitempty"`
|
||||||
|
ServiceId int32 `protobuf:"varint,2,opt,name=serviceId,proto3" json:"serviceId,omitempty"`
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *VerifyTokenRequest) Reset() {
|
||||||
|
*x = VerifyTokenRequest{}
|
||||||
|
mi := &file_proto_verify_proto_msgTypes[0]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *VerifyTokenRequest) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*VerifyTokenRequest) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *VerifyTokenRequest) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_proto_verify_proto_msgTypes[0]
|
||||||
|
if x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use VerifyTokenRequest.ProtoReflect.Descriptor instead.
|
||||||
|
func (*VerifyTokenRequest) Descriptor() ([]byte, []int) {
|
||||||
|
return file_proto_verify_proto_rawDescGZIP(), []int{0}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *VerifyTokenRequest) GetSessionToken() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.SessionToken
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *VerifyTokenRequest) GetServiceId() int32 {
|
||||||
|
if x != nil {
|
||||||
|
return x.ServiceId
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type VerifyTokenResponse struct {
|
||||||
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
|
UserUuid string `protobuf:"bytes,1,opt,name=userUuid,proto3" json:"userUuid,omitempty"`
|
||||||
|
IsValid bool `protobuf:"varint,2,opt,name=isValid,proto3" json:"isValid,omitempty"`
|
||||||
|
AccessCode int32 `protobuf:"varint,3,opt,name=accessCode,proto3" json:"accessCode,omitempty"`
|
||||||
|
unknownFields protoimpl.UnknownFields
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *VerifyTokenResponse) Reset() {
|
||||||
|
*x = VerifyTokenResponse{}
|
||||||
|
mi := &file_proto_verify_proto_msgTypes[1]
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *VerifyTokenResponse) String() string {
|
||||||
|
return protoimpl.X.MessageStringOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*VerifyTokenResponse) ProtoMessage() {}
|
||||||
|
|
||||||
|
func (x *VerifyTokenResponse) ProtoReflect() protoreflect.Message {
|
||||||
|
mi := &file_proto_verify_proto_msgTypes[1]
|
||||||
|
if x != nil {
|
||||||
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
|
if ms.LoadMessageInfo() == nil {
|
||||||
|
ms.StoreMessageInfo(mi)
|
||||||
|
}
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
return mi.MessageOf(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: Use VerifyTokenResponse.ProtoReflect.Descriptor instead.
|
||||||
|
func (*VerifyTokenResponse) Descriptor() ([]byte, []int) {
|
||||||
|
return file_proto_verify_proto_rawDescGZIP(), []int{1}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *VerifyTokenResponse) GetUserUuid() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.UserUuid
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *VerifyTokenResponse) GetIsValid() bool {
|
||||||
|
if x != nil {
|
||||||
|
return x.IsValid
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *VerifyTokenResponse) GetAccessCode() int32 {
|
||||||
|
if x != nil {
|
||||||
|
return x.AccessCode
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var File_proto_verify_proto protoreflect.FileDescriptor
|
||||||
|
|
||||||
|
const file_proto_verify_proto_rawDesc = "" +
|
||||||
|
"\n" +
|
||||||
|
"\x12proto/verify.proto\x12\rverifyAuth.v1\"V\n" +
|
||||||
|
"\x12VerifyTokenRequest\x12\"\n" +
|
||||||
|
"\fsessionToken\x18\x01 \x01(\tR\fsessionToken\x12\x1c\n" +
|
||||||
|
"\tserviceId\x18\x02 \x01(\x05R\tserviceId\"k\n" +
|
||||||
|
"\x13VerifyTokenResponse\x12\x1a\n" +
|
||||||
|
"\buserUuid\x18\x01 \x01(\tR\buserUuid\x12\x18\n" +
|
||||||
|
"\aisValid\x18\x02 \x01(\bR\aisValid\x12\x1e\n" +
|
||||||
|
"\n" +
|
||||||
|
"accessCode\x18\x03 \x01(\x05R\n" +
|
||||||
|
"accessCode2c\n" +
|
||||||
|
"\vAuthService\x12T\n" +
|
||||||
|
"\vVerifyToken\x12!.verifyAuth.v1.VerifyTokenRequest\x1a\".verifyAuth.v1.VerifyTokenResponseB\x19Z\x17/pkg/verify/v1;verifyV1b\x06proto3"
|
||||||
|
|
||||||
|
var (
|
||||||
|
file_proto_verify_proto_rawDescOnce sync.Once
|
||||||
|
file_proto_verify_proto_rawDescData []byte
|
||||||
|
)
|
||||||
|
|
||||||
|
func file_proto_verify_proto_rawDescGZIP() []byte {
|
||||||
|
file_proto_verify_proto_rawDescOnce.Do(func() {
|
||||||
|
file_proto_verify_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_proto_verify_proto_rawDesc), len(file_proto_verify_proto_rawDesc)))
|
||||||
|
})
|
||||||
|
return file_proto_verify_proto_rawDescData
|
||||||
|
}
|
||||||
|
|
||||||
|
var file_proto_verify_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||||
|
var file_proto_verify_proto_goTypes = []any{
|
||||||
|
(*VerifyTokenRequest)(nil), // 0: verifyAuth.v1.VerifyTokenRequest
|
||||||
|
(*VerifyTokenResponse)(nil), // 1: verifyAuth.v1.VerifyTokenResponse
|
||||||
|
}
|
||||||
|
var file_proto_verify_proto_depIdxs = []int32{
|
||||||
|
0, // 0: verifyAuth.v1.AuthService.VerifyToken:input_type -> verifyAuth.v1.VerifyTokenRequest
|
||||||
|
1, // 1: verifyAuth.v1.AuthService.VerifyToken:output_type -> verifyAuth.v1.VerifyTokenResponse
|
||||||
|
1, // [1:2] is the sub-list for method output_type
|
||||||
|
0, // [0:1] is the sub-list for method input_type
|
||||||
|
0, // [0:0] is the sub-list for extension type_name
|
||||||
|
0, // [0:0] is the sub-list for extension extendee
|
||||||
|
0, // [0:0] is the sub-list for field type_name
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { file_proto_verify_proto_init() }
|
||||||
|
func file_proto_verify_proto_init() {
|
||||||
|
if File_proto_verify_proto != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
type x struct{}
|
||||||
|
out := protoimpl.TypeBuilder{
|
||||||
|
File: protoimpl.DescBuilder{
|
||||||
|
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||||
|
RawDescriptor: unsafe.Slice(unsafe.StringData(file_proto_verify_proto_rawDesc), len(file_proto_verify_proto_rawDesc)),
|
||||||
|
NumEnums: 0,
|
||||||
|
NumMessages: 2,
|
||||||
|
NumExtensions: 0,
|
||||||
|
NumServices: 1,
|
||||||
|
},
|
||||||
|
GoTypes: file_proto_verify_proto_goTypes,
|
||||||
|
DependencyIndexes: file_proto_verify_proto_depIdxs,
|
||||||
|
MessageInfos: file_proto_verify_proto_msgTypes,
|
||||||
|
}.Build()
|
||||||
|
File_proto_verify_proto = out.File
|
||||||
|
file_proto_verify_proto_goTypes = nil
|
||||||
|
file_proto_verify_proto_depIdxs = nil
|
||||||
|
}
|
||||||
121
pkg/verify/v1/verify_grpc.pb.go
Normal file
121
pkg/verify/v1/verify_grpc.pb.go
Normal file
|
|
@ -0,0 +1,121 @@
|
||||||
|
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// - protoc-gen-go-grpc v1.5.1
|
||||||
|
// - protoc v6.33.1
|
||||||
|
// source: proto/verify.proto
|
||||||
|
|
||||||
|
package verifyV1
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
grpc "google.golang.org/grpc"
|
||||||
|
codes "google.golang.org/grpc/codes"
|
||||||
|
status "google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the grpc package it is being compiled against.
|
||||||
|
// Requires gRPC-Go v1.64.0 or later.
|
||||||
|
const _ = grpc.SupportPackageIsVersion9
|
||||||
|
|
||||||
|
const (
|
||||||
|
AuthService_VerifyToken_FullMethodName = "/verifyAuth.v1.AuthService/VerifyToken"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AuthServiceClient is the client API for AuthService service.
|
||||||
|
//
|
||||||
|
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||||
|
type AuthServiceClient interface {
|
||||||
|
VerifyToken(ctx context.Context, in *VerifyTokenRequest, opts ...grpc.CallOption) (*VerifyTokenResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type authServiceClient struct {
|
||||||
|
cc grpc.ClientConnInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAuthServiceClient(cc grpc.ClientConnInterface) AuthServiceClient {
|
||||||
|
return &authServiceClient{cc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *authServiceClient) VerifyToken(ctx context.Context, in *VerifyTokenRequest, opts ...grpc.CallOption) (*VerifyTokenResponse, error) {
|
||||||
|
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||||
|
out := new(VerifyTokenResponse)
|
||||||
|
err := c.cc.Invoke(ctx, AuthService_VerifyToken_FullMethodName, in, out, cOpts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AuthServiceServer is the server API for AuthService service.
|
||||||
|
// All implementations must embed UnimplementedAuthServiceServer
|
||||||
|
// for forward compatibility.
|
||||||
|
type AuthServiceServer interface {
|
||||||
|
VerifyToken(context.Context, *VerifyTokenRequest) (*VerifyTokenResponse, error)
|
||||||
|
mustEmbedUnimplementedAuthServiceServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnimplementedAuthServiceServer must be embedded to have
|
||||||
|
// forward compatible implementations.
|
||||||
|
//
|
||||||
|
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||||
|
// pointer dereference when methods are called.
|
||||||
|
type UnimplementedAuthServiceServer struct{}
|
||||||
|
|
||||||
|
func (UnimplementedAuthServiceServer) VerifyToken(context.Context, *VerifyTokenRequest) (*VerifyTokenResponse, error) {
|
||||||
|
return nil, status.Errorf(codes.Unimplemented, "method VerifyToken not implemented")
|
||||||
|
}
|
||||||
|
func (UnimplementedAuthServiceServer) mustEmbedUnimplementedAuthServiceServer() {}
|
||||||
|
func (UnimplementedAuthServiceServer) testEmbeddedByValue() {}
|
||||||
|
|
||||||
|
// UnsafeAuthServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||||
|
// Use of this interface is not recommended, as added methods to AuthServiceServer will
|
||||||
|
// result in compilation errors.
|
||||||
|
type UnsafeAuthServiceServer interface {
|
||||||
|
mustEmbedUnimplementedAuthServiceServer()
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterAuthServiceServer(s grpc.ServiceRegistrar, srv AuthServiceServer) {
|
||||||
|
// If the following call pancis, it indicates UnimplementedAuthServiceServer was
|
||||||
|
// embedded by pointer and is nil. This will cause panics if an
|
||||||
|
// unimplemented method is ever invoked, so we test this at initialization
|
||||||
|
// time to prevent it from happening at runtime later due to I/O.
|
||||||
|
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||||
|
t.testEmbeddedByValue()
|
||||||
|
}
|
||||||
|
s.RegisterService(&AuthService_ServiceDesc, srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _AuthService_VerifyToken_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(VerifyTokenRequest)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(AuthServiceServer).VerifyToken(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: AuthService_VerifyToken_FullMethodName,
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(AuthServiceServer).VerifyToken(ctx, req.(*VerifyTokenRequest))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AuthService_ServiceDesc is the grpc.ServiceDesc for AuthService service.
|
||||||
|
// It's only intended for direct use with grpc.RegisterService,
|
||||||
|
// and not to be introspected or modified (even as a copy)
|
||||||
|
var AuthService_ServiceDesc = grpc.ServiceDesc{
|
||||||
|
ServiceName: "verifyAuth.v1.AuthService",
|
||||||
|
HandlerType: (*AuthServiceServer)(nil),
|
||||||
|
Methods: []grpc.MethodDesc{
|
||||||
|
{
|
||||||
|
MethodName: "VerifyToken",
|
||||||
|
Handler: _AuthService_VerifyToken_Handler,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Streams: []grpc.StreamDesc{},
|
||||||
|
Metadata: "proto/verify.proto",
|
||||||
|
}
|
||||||
19
proto/verify.proto
Normal file
19
proto/verify.proto
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package verifyAuth.v1;
|
||||||
|
option go_package = "/pkg/verify/v1;verifyV1";
|
||||||
|
|
||||||
|
message VerifyTokenRequest {
|
||||||
|
string sessionToken = 1;
|
||||||
|
int32 serviceId = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message VerifyTokenResponse {
|
||||||
|
string userUuid = 1;
|
||||||
|
bool isValid = 2;
|
||||||
|
int32 accessCode = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
service AuthService {
|
||||||
|
rpc VerifyToken(VerifyTokenRequest) returns (VerifyTokenResponse);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue