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
|
|
|
)
|
|
|
|
|
2020-07-08 13:56:37 +02:00
|
|
|
func AuthorizationInterceptor(verifier *authz.TokenVerifier, 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
|
|
|
|
2020-10-21 10:18:34 +02:00
|
|
|
func authorize(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, verifier *authz.TokenVerifier, 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-05-05 17:34:53 +02:00
|
|
|
var orgDomain string
|
2020-12-14 13:34:05 +01:00
|
|
|
orgID := grpc_util.GetHeader(authCtx, http.ZitadelOrgID)
|
2023-05-04 10:50:19 +02:00
|
|
|
if o, ok := req.(OrganisationFromRequest); ok {
|
2023-09-13 14:43:01 +02:00
|
|
|
orgID = o.OrganisationFromRequest().ID
|
|
|
|
orgDomain = o.OrganisationFromRequest().Domain
|
2023-04-26 07:47:57 +02:00
|
|
|
}
|
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-05-04 10:50:19 +02:00
|
|
|
type OrganisationFromRequest interface {
|
2023-09-13 14:43:01 +02:00
|
|
|
OrganisationFromRequest() *Organisation
|
|
|
|
}
|
|
|
|
|
|
|
|
type Organisation struct {
|
|
|
|
ID string
|
|
|
|
Domain string
|
2023-04-26 07:47:57 +02:00
|
|
|
}
|