2020-03-24 14:15:01 +01:00
|
|
|
package middleware
|
2020-03-23 07:01:59 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2020-03-23 13:26:05 +01:00
|
|
|
"github.com/caos/zitadel/internal/api"
|
2020-03-23 07:01:59 +01:00
|
|
|
"github.com/caos/zitadel/internal/api/auth"
|
2020-03-24 14:15:01 +01:00
|
|
|
grpc_util "github.com/caos/zitadel/internal/api/grpc"
|
2020-03-23 07:01:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func AuthorizationInterceptor(verifier auth.TokenVerifier, authConfig *auth.Config, authMethods auth.MethodMapping) func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
authOpt, needsToken := authMethods[info.FullMethod]
|
|
|
|
if !needsToken {
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:23:04 +02:00
|
|
|
authToken := ""
|
|
|
|
//TODO: Remoce check internal as soon as authentification is implemented
|
|
|
|
if !auth.CheckInternal(ctx) {
|
|
|
|
authToken = grpc_util.GetAuthorizationHeader(ctx)
|
|
|
|
if authToken == "" {
|
|
|
|
return nil, status.Error(codes.Unauthenticated, "auth header missing")
|
|
|
|
}
|
2020-03-23 07:01:59 +01:00
|
|
|
}
|
2020-03-24 14:15:01 +01:00
|
|
|
orgID := grpc_util.GetHeader(ctx, api.ZitadelOrgID)
|
2020-03-23 07:01:59 +01:00
|
|
|
|
|
|
|
ctx, err := auth.CheckUserAuthorization(ctx, req, authToken, orgID, verifier, authConfig, authOpt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
}
|