From 91a72f25fd17d49a098421bc5e1c6d1cd0bb84a9 Mon Sep 17 00:00:00 2001 From: timka Date: Wed, 17 Dec 2025 17:42:16 +0300 Subject: [PATCH] setup tasks auth middleware and compose --- internal/competition/middleware/auth.go | 67 ------------------------- internal/competition/server/server.go | 4 +- internal/task/config/config.go | 4 +- internal/task/server/server.go | 8 ++- 4 files changed, 12 insertions(+), 71 deletions(-) delete mode 100644 internal/competition/middleware/auth.go diff --git a/internal/competition/middleware/auth.go b/internal/competition/middleware/auth.go deleted file mode 100644 index 50856cc..0000000 --- a/internal/competition/middleware/auth.go +++ /dev/null @@ -1,67 +0,0 @@ -package middleware - -import ( - "context" - "strings" - - authpb "datarush/pkg/api/auth" - - "google.golang.org/grpc" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/metadata" - "google.golang.org/grpc/status" -) - -type contextKey string - -const ( - UserIDKey contextKey = "user_id" - authHeader = "authorization" - bearerScheme = "bearer" -) - -type AuthInterceptor struct { - authClient authpb.AuthServiceClient -} - -func NewAuthInterceptor(authClient authpb.AuthServiceClient) *AuthInterceptor { - return &AuthInterceptor{authClient: authClient} -} - -func (i *AuthInterceptor) Unary() grpc.UnaryServerInterceptor { - return func( - ctx context.Context, - req interface{}, - info *grpc.UnaryServerInfo, - handler grpc.UnaryHandler, - ) (interface{}, error) { - md, ok := metadata.FromIncomingContext(ctx) - if !ok { - return nil, status.Error(codes.Unauthenticated, "metadata is not provided") - } - - authHeaders := md.Get(authHeader) - if len(authHeaders) == 0 { - return nil, status.Error(codes.Unauthenticated, "authorization token is not provided") - } - - header := authHeaders[0] - parts := strings.Split(header, " ") - if len(parts) != 2 || !strings.EqualFold(parts[0], bearerScheme) { - return nil, status.Errorf(codes.Unauthenticated, "invalid authorization header format") - } - - token := parts[1] - - validateResp, err := i.authClient.ValidateToken(ctx, &authpb.ValidateTokenRequest{ - Token: token, - }) - if err != nil { - return nil, status.Errorf(codes.Unauthenticated, "failed to validate token: %v", err) - } - - newCtx := context.WithValue(ctx, UserIDKey, validateResp.GetUserId()) - - return handler(newCtx, req) - } -} diff --git a/internal/competition/server/server.go b/internal/competition/server/server.go index 528ab3a..a0d821a 100644 --- a/internal/competition/server/server.go +++ b/internal/competition/server/server.go @@ -8,11 +8,11 @@ import ( "datarush/internal/competition/config" grpcHandlers "datarush/internal/competition/handler/grpc" - "datarush/internal/competition/middleware" "datarush/internal/competition/repository/postgres" "datarush/internal/competition/service" authpb "datarush/pkg/api/auth" pb "datarush/pkg/api/competition" + "datarush/pkg/interceptor" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" @@ -82,7 +82,7 @@ func (s *Server) Start() error { func (s *Server) registerGRPCServices() error { authClient := authpb.NewAuthServiceClient(s.authConn) - authInterceptor := middleware.NewAuthInterceptor(authClient) + authInterceptor := interceptor.NewAuthInterceptor(authClient) s.grpcServer = grpc.NewServer( grpc.UnaryInterceptor(authInterceptor.Unary()), diff --git a/internal/task/config/config.go b/internal/task/config/config.go index 983d00e..df5206f 100644 --- a/internal/task/config/config.go +++ b/internal/task/config/config.go @@ -20,6 +20,7 @@ type Config struct { DBUser string DBPassword string DBName string + AuthSvcAddr string } func Load() (*Config, error) { @@ -35,6 +36,7 @@ func Load() (*Config, error) { DBUser: getEnv("POSTGRES_USERNAME", "postgres"), DBPassword: getEnv("POSTGRES_PASSWORD", "postgres"), DBName: getEnv("POSTGRES_DATABASE", "postgres"), + AuthSvcAddr: getEnv("AUTH_SVC_ADDR", "localhost:50051"), }, nil } @@ -71,4 +73,4 @@ func (c Config) BuildPostgresConnStr() string { func (c Config) BuildPostgresDSN() string { return fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable", c.DBUser, c.DBPassword, net.JoinHostPort(c.DBHost, strconv.Itoa(c.DBPort)), c.DBName) -} \ No newline at end of file +} diff --git a/internal/task/server/server.go b/internal/task/server/server.go index 5926882..b2f94bf 100644 --- a/internal/task/server/server.go +++ b/internal/task/server/server.go @@ -10,7 +10,9 @@ import ( grpcHandlers "datarush/internal/task/handler/grpc" taskPostgresRepo "datarush/internal/task/repository/postgres" "datarush/internal/task/service" + authpb "datarush/pkg/api/auth" pb "datarush/pkg/api/task" + "datarush/pkg/interceptor" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" @@ -28,6 +30,7 @@ type Server struct { grpcServer *grpc.Server config *config.Config db *sqlx.DB + authConn *grpc.ClientConn } func New(cfg *config.Config) *Server { @@ -63,6 +66,9 @@ func (s *Server) Start() error { } func (s *Server) registerGRPCServices() error { + authClient := authpb.NewAuthServiceClient(s.authConn) + authInterceptor := interceptor.NewAuthInterceptor(authClient) + s.grpcServer = grpc.NewServer() taskRepo := taskPostgresRepo.NewTaskRepository(s.db) @@ -93,4 +99,4 @@ func (s *Server) Stop() { } log.Println("task server stopped") -} \ No newline at end of file +}