2020-03-24 14:15:01 +01:00
|
|
|
package middleware
|
2020-03-23 07:01:59 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-08 13:56:37 +02:00
|
|
|
|
2020-03-23 07:01:59 +01:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
grpc_util "github.com/zitadel/zitadel/internal/api/grpc"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/http"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2020-03-23 07:01:59 +01:00
|
|
|
)
|
|
|
|
|
2023-10-25 17:10:45 +02:00
|
|
|
func AuthorizationInterceptor(verifier authz.APITokenVerifier, authConfig authz.Config) grpc.UnaryServerInterceptor {
|
2020-03-23 07:01:59 +01:00
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
2020-07-08 13:56:37 +02:00
|
|
|
return authorize(ctx, req, info, handler, verifier, authConfig)
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 07:01:59 +01:00
|
|
|
|
2023-10-25 17:10:45 +02:00
|
|
|
func authorize(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, verifier authz.APITokenVerifier, authConfig authz.Config) (_ interface{}, err error) {
|
2020-07-08 13:56:37 +02:00
|
|
|
authOpt, needsToken := verifier.CheckAuthMethod(info.FullMethod)
|
|
|
|
if !needsToken {
|
2020-03-23 07:01:59 +01:00
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
2020-07-08 13:56:37 +02:00
|
|
|
|
2020-12-14 13:34:05 +01:00
|
|
|
authCtx, span := tracing.NewServerInterceptorSpan(ctx)
|
2020-11-20 07:57:39 +01:00
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2020-12-14 13:34:05 +01:00
|
|
|
authToken := grpc_util.GetAuthorizationHeader(authCtx)
|
2020-07-08 13:56:37 +02:00
|
|
|
if authToken == "" {
|
|
|
|
return nil, status.Error(codes.Unauthenticated, "auth header missing")
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:37:35 +03:00
|
|
|
orgID, orgDomain := orgIDAndDomainFromRequest(authCtx, req)
|
2023-05-05 17:34:53 +02:00
|
|
|
ctxSetter, err := authz.CheckUserAuthorization(authCtx, req, authToken, orgID, orgDomain, verifier, authConfig, authOpt, info.FullMethod)
|
2020-07-08 13:56:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-21 10:18:34 +02:00
|
|
|
span.End()
|
2020-12-14 13:34:05 +01:00
|
|
|
return handler(ctxSetter(ctx), req)
|
2020-03-23 07:01:59 +01:00
|
|
|
}
|
2023-04-26 07:47:57 +02:00
|
|
|
|
2023-10-13 15:37:35 +03:00
|
|
|
func orgIDAndDomainFromRequest(ctx context.Context, req interface{}) (id, domain string) {
|
|
|
|
orgID := grpc_util.GetHeader(ctx, http.ZitadelOrgID)
|
2024-03-21 09:07:00 +01:00
|
|
|
oz, ok := req.(OrganizationFromRequest)
|
|
|
|
if ok {
|
|
|
|
id = oz.OrganizationFromRequest().ID
|
|
|
|
domain = oz.OrganizationFromRequest().Domain
|
|
|
|
if id != "" || domain != "" {
|
|
|
|
return id, domain
|
|
|
|
}
|
2023-10-13 15:37:35 +03:00
|
|
|
}
|
|
|
|
return orgID, domain
|
|
|
|
}
|
|
|
|
|
|
|
|
type Organization struct {
|
2023-09-13 14:43:01 +02:00
|
|
|
ID string
|
|
|
|
Domain string
|
2023-04-26 07:47:57 +02:00
|
|
|
}
|
2023-10-13 15:37:35 +03:00
|
|
|
|
|
|
|
type OrganizationFromRequest interface {
|
|
|
|
OrganizationFromRequest() *Organization
|
|
|
|
}
|