first commit
This commit is contained in:
45
servers/grpcapi/server/authentication.go
Normal file
45
servers/grpcapi/server/authentication.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) AuthLogin(ctx context.Context, req *grpcproto.AuthLoginRequest) (res *grpcproto.AuthLoginResponse, err error) {
|
||||
log.Info().
|
||||
Str("username", req.Username).
|
||||
Msg("AuthLogin")
|
||||
|
||||
jwt, err := s.Handler.Login(ctx, req.Username, req.Password)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("username", req.Username).
|
||||
Msg("user not found")
|
||||
|
||||
return nil, errors.New("user/password not found")
|
||||
}
|
||||
|
||||
return &grpcproto.AuthLoginResponse{
|
||||
Token: jwt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) AuthRegister(ctx context.Context, req *grpcproto.AuthRegisterRequest) (res *grpcproto.AuthRegisterResponse, err error) {
|
||||
log.Info().
|
||||
Str("username", req.Email).
|
||||
Msg("AuthRegister")
|
||||
|
||||
jwt, err := s.Handler.Register(ctx, req.Email, req.Password, req.Email, req.PhoneNumber, req.FirstName, req.LastName)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("AuthRegister failed")
|
||||
return nil, errors.New("could not register user")
|
||||
}
|
||||
|
||||
return &grpcproto.AuthRegisterResponse{
|
||||
Token: jwt,
|
||||
}, nil
|
||||
}
|
||||
27
servers/grpcapi/server/forget_password.go
Normal file
27
servers/grpcapi/server/forget_password.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) ForgetAccount(ctx context.Context, req *grpcproto.ForgetAccountRequest) (res *grpcproto.ForgetAccountResponse, err error) {
|
||||
log.Info().
|
||||
Str("username", req.Username).
|
||||
Str("namespace", req.Namespace).
|
||||
Msg("ForgetAccount")
|
||||
response, access_code := s.Handler.ForgetAccount(ctx, req.Username, req.Namespace)
|
||||
if response == true {
|
||||
return &grpcproto.ForgetAccountResponse{
|
||||
Response: true,
|
||||
AccessCode: access_code,
|
||||
}, nil
|
||||
} else {
|
||||
return &grpcproto.ForgetAccountResponse{
|
||||
Response: false,
|
||||
AccessCode: "",
|
||||
}, nil
|
||||
}
|
||||
|
||||
}
|
||||
55
servers/grpcapi/server/onboarding_validation.go
Normal file
55
servers/grpcapi/server/onboarding_validation.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) SetPhoneNumber(ctx context.Context, req *grpcproto.SetPhoneNumberRequest) (res *grpcproto.SetPhoneNumberResponse, err error) {
|
||||
if err = s.Handler.UpdatePhoneNumber(
|
||||
context.Background(),
|
||||
req.Email,
|
||||
req.PhoneNumber,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &grpcproto.SetPhoneNumberResponse{Ok: true}, nil
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) VerifyPhoneNumber(ctx context.Context, req *grpcproto.VerifyPhoneNumberRequest) (res *grpcproto.VerifyPhoneNumberResponse, err error) {
|
||||
|
||||
if err = s.Handler.VerifyPhoneNumber(
|
||||
context.Background(),
|
||||
req.Email,
|
||||
req.PhoneNumber,
|
||||
req.VerificationCode,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &grpcproto.VerifyPhoneNumberResponse{Ok: true}, nil
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) SetBirthDate(ctx context.Context, req *grpcproto.BirthDateRequest) (res *grpcproto.BirthDateResponse, err error) {
|
||||
birthdate := time.Unix(req.Birthdate.Seconds, int64(req.Birthdate.Nanos)).UTC()
|
||||
birthdateString := birthdate.Format("2006-01-02T15:04:05Z")
|
||||
if err = s.Handler.UpdateBirthDate(ctx, req.Email, birthdateString); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &grpcproto.BirthDateResponse{
|
||||
Ok: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s SilvermobiGRPCService) GetValidation(ctx context.Context, req *grpcproto.ValidationRequest) (res *grpcproto.ValidationResponse, err error) {
|
||||
phone_validation, birth_validation, err := s.Handler.CheckValidation(ctx, req.Email)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &grpcproto.ValidationResponse{
|
||||
Phone: phone_validation,
|
||||
Birthdate: birth_validation,
|
||||
}, nil
|
||||
}
|
||||
125
servers/grpcapi/server/silvermobi-grpc-server.go
Normal file
125
servers/grpcapi/server/silvermobi-grpc-server.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/silvermobi/handler"
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
||||
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
|
||||
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
var (
|
||||
contextKeyUser = contextKey("user")
|
||||
)
|
||||
|
||||
type SilvermobiGRPCService struct {
|
||||
Config *viper.Viper
|
||||
Handler *handler.SilvermobiHandler
|
||||
|
||||
grpcproto.UnimplementedSilvermobiGRPCServer
|
||||
}
|
||||
|
||||
func NewSilvermobiGRPCService(cfg *viper.Viper, handler *handler.SilvermobiHandler) SilvermobiGRPCService {
|
||||
return SilvermobiGRPCService{
|
||||
Config: cfg,
|
||||
Handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func Run(done chan error, cfg *viper.Viper, handler *handler.SilvermobiHandler) {
|
||||
var (
|
||||
address = "127.0.0.1:" + cfg.GetString("services.external.grpc.port")
|
||||
jwt_secret = cfg.GetString("identification.local.jwt_secret")
|
||||
)
|
||||
|
||||
log.Info().Msg("GRPC server on " + address)
|
||||
|
||||
server := grpc.NewServer(
|
||||
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
|
||||
grpc_ctxtags.StreamServerInterceptor(),
|
||||
StreamAuthServerInterceptor(GRPCAuthFunc(jwt_secret)),
|
||||
)),
|
||||
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
|
||||
grpc_ctxtags.UnaryServerInterceptor(),
|
||||
UnaryAuthServerInterceptor(GRPCAuthFunc(jwt_secret)),
|
||||
)),
|
||||
)
|
||||
|
||||
grpcproto.RegisterSilvermobiGRPCServer(server, NewSilvermobiGRPCService(cfg, handler))
|
||||
l, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
|
||||
if err := server.Serve(l); err != nil {
|
||||
log.Error().Err(err).Msg("gRPC service ended")
|
||||
done <- err
|
||||
}
|
||||
}
|
||||
|
||||
func UnaryAuthServerInterceptor(authFunc grpc_auth.AuthFunc) grpc.UnaryServerInterceptor {
|
||||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||
if NoAuth(info.FullMethod) {
|
||||
return handler(ctx, req)
|
||||
}
|
||||
var newCtx context.Context
|
||||
var err error
|
||||
newCtx, err = authFunc(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler(newCtx, req)
|
||||
}
|
||||
}
|
||||
|
||||
func StreamAuthServerInterceptor(authFunc grpc_auth.AuthFunc) grpc.StreamServerInterceptor {
|
||||
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||
if NoAuth(info.FullMethod) {
|
||||
wrapped := grpc_middleware.WrapServerStream(stream)
|
||||
wrapped.WrappedContext = stream.Context()
|
||||
return handler(srv, wrapped)
|
||||
}
|
||||
var newCtx context.Context
|
||||
var err error
|
||||
newCtx, err = authFunc(stream.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wrapped := grpc_middleware.WrapServerStream(stream)
|
||||
wrapped.WrappedContext = newCtx
|
||||
return handler(srv, wrapped)
|
||||
}
|
||||
}
|
||||
|
||||
func GRPCAuthFunc(jwtKey string) grpc_auth.AuthFunc {
|
||||
return func(ctx context.Context) (context.Context, error) {
|
||||
tokenString, err := grpc_auth.AuthFromMD(ctx, "bearer")
|
||||
if err != nil {
|
||||
log.Error().Err(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
claims := jwt.MapClaims{}
|
||||
jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(jwtKey), nil
|
||||
})
|
||||
|
||||
ctx = context.WithValue(ctx, contextKeyUser, claims["sub"].(string))
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
}
|
||||
|
||||
func NoAuth(method string) bool {
|
||||
return strings.Contains(method, "Auth")
|
||||
}
|
||||
23
servers/grpcapi/server/update_password.go
Normal file
23
servers/grpcapi/server/update_password.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (s SilvermobiGRPCService) UpdatePassword(ctx context.Context, req *grpcproto.UpdatePasswordRequest) (res *grpcproto.UpdatePasswordResponse, err error) {
|
||||
log.Info().
|
||||
Str("username", req.Email).
|
||||
Msg("Update Password")
|
||||
result := s.Handler.UpdatePassword(ctx, req.Email, req.Password)
|
||||
if result == true {
|
||||
return &grpcproto.UpdatePasswordResponse{
|
||||
Response: true,
|
||||
}, nil
|
||||
} else {
|
||||
return &grpcproto.UpdatePasswordResponse{
|
||||
Response: false,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user