mirror of
https://github.com/paralus/paralus.git
synced 2026-05-06 16:36:46 +00:00
Rework session handling to prevent cyclic import
imports github.com/RafayLabs/rcloud-base/internal/fixtures imports github.com/RafayLabs/rcloud-base/pkg/service imports github.com/RafayLabs/rcloud-base/pkg/auth/v3 imports github.com/RafayLabs/rcloud-base/pkg/service
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
context "context"
|
||||
"strings"
|
||||
|
||||
"github.com/RafayLabs/rcloud-base/pkg/common"
|
||||
"github.com/RafayLabs/rcloud-base/pkg/gateway"
|
||||
commonv3 "github.com/RafayLabs/rcloud-base/proto/types/commonpb/v3"
|
||||
grpc "google.golang.org/grpc"
|
||||
@@ -88,7 +89,7 @@ func (ac authContext) NewAuthUnaryInterceptor(opt Option) grpc.UnaryServerInterc
|
||||
s := res.GetStatus()
|
||||
switch s {
|
||||
case commonv3.RequestStatus_RequestAllowed:
|
||||
ctx := NewSessionContext(ctx, res.SessionData)
|
||||
ctx := context.WithValue(ctx, common.SessionDataKey, res.SessionData)
|
||||
return handler(ctx, req)
|
||||
case commonv3.RequestStatus_RequestMethodOrURLNotAllowed:
|
||||
return nil, status.Error(codes.PermissionDenied, res.GetReason())
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package authv3
|
||||
|
||||
import (
|
||||
context "context"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/RafayLabs/rcloud-base/internal/dao"
|
||||
"github.com/RafayLabs/rcloud-base/pkg/common"
|
||||
commonpbv3 "github.com/RafayLabs/rcloud-base/proto/types/commonpb/v3"
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
@@ -92,7 +94,7 @@ func (am *authMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, nex
|
||||
s := res.GetStatus()
|
||||
switch s {
|
||||
case commonpbv3.RequestStatus_RequestAllowed:
|
||||
ctx := NewSessionContext(r.Context(), res.SessionData)
|
||||
ctx := context.WithValue(r.Context(), common.SessionDataKey, res.SessionData)
|
||||
next(rw, r.WithContext(ctx))
|
||||
return
|
||||
case commonpbv3.RequestStatus_RequestMethodOrURLNotAllowed:
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package authv3
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/RafayLabs/rcloud-base/pkg/common"
|
||||
commonv3 "github.com/RafayLabs/rcloud-base/proto/types/commonpb/v3"
|
||||
)
|
||||
|
||||
func NewSessionContext(ctx context.Context, s *commonv3.SessionData) context.Context {
|
||||
return context.WithValue(ctx, common.SessionDataKey, s)
|
||||
}
|
||||
|
||||
func GetSession(ctx context.Context) (*commonv3.SessionData, bool) {
|
||||
s, ok := ctx.Value(common.SessionDataKey).(*commonv3.SessionData)
|
||||
return s, ok
|
||||
}
|
||||
@@ -47,6 +47,4 @@ const (
|
||||
RelayCommandsAuditType = "RelayCommands"
|
||||
)
|
||||
|
||||
type contextKey struct{}
|
||||
|
||||
var SessionDataKey contextKey
|
||||
|
||||
@@ -24,3 +24,5 @@ type CliConfigDownloadData struct {
|
||||
Organization string `json:"organization"`
|
||||
Partner string `json:"partner"`
|
||||
}
|
||||
|
||||
type contextKey struct{}
|
||||
|
||||
@@ -219,18 +219,12 @@ func (s *projectService) Delete(ctx context.Context, project *systemv3.Project)
|
||||
}
|
||||
|
||||
func (s *projectService) List(ctx context.Context, project *systemv3.Project) (*systemv3.ProjectList, error) {
|
||||
sessionData := ctx.Value(common.SessionDataKey)
|
||||
sd, ok := ctx.Value(common.SessionDataKey).(*commonv3.SessionData)
|
||||
username := ""
|
||||
if sessionData == nil {
|
||||
if !ok {
|
||||
return &systemv3.ProjectList{}, fmt.Errorf("cannot perform project listing without auth")
|
||||
} else {
|
||||
sd, ok := sessionData.(*commonv3.SessionData)
|
||||
if !ok {
|
||||
return &systemv3.ProjectList{}, fmt.Errorf("cannot perform project listing without auth")
|
||||
} else {
|
||||
username = sd.Username
|
||||
}
|
||||
}
|
||||
username = sd.Username
|
||||
|
||||
var projects []*systemv3.Project
|
||||
projectList := &systemv3.ProjectList{
|
||||
|
||||
@@ -376,18 +376,12 @@ func (s *userService) GetByName(ctx context.Context, user *userv3.User) (*userv3
|
||||
}
|
||||
|
||||
func (s *userService) GetUserInfo(ctx context.Context, user *userv3.User) (*userv3.UserInfo, error) {
|
||||
sessionData := ctx.Value(common.SessionDataKey)
|
||||
sd, ok := ctx.Value(common.SessionDataKey).(*commonv3.SessionData)
|
||||
username := ""
|
||||
if sessionData == nil {
|
||||
if !ok {
|
||||
return &userv3.UserInfo{}, fmt.Errorf("cannot perform project listing without auth")
|
||||
} else {
|
||||
sd, ok := sessionData.(*commonv3.SessionData)
|
||||
if !ok {
|
||||
return &userv3.UserInfo{}, fmt.Errorf("cannot perform project listing without auth")
|
||||
} else {
|
||||
username = sd.Username
|
||||
}
|
||||
}
|
||||
username = sd.Username
|
||||
|
||||
entity, err := dao.GetByTraits(ctx, s.db, username, &models.KratosIdentities{})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user