github.com/cs3org/reva/v2@v2.27.7/internal/grpc/interceptors/token/token.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package token
    20  
    21  import (
    22  	"context"
    23  
    24  	ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
    25  	"google.golang.org/grpc"
    26  	"google.golang.org/grpc/metadata"
    27  )
    28  
    29  // NewUnary returns a new unary interceptor that adds
    30  // the token to the context.
    31  func NewUnary() grpc.UnaryServerInterceptor {
    32  	interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    33  		md, ok := metadata.FromIncomingContext(ctx)
    34  		if ok && md != nil {
    35  			if val, ok := md[ctxpkg.TokenHeader]; ok {
    36  				if len(val) > 0 && val[0] != "" {
    37  					tkn := val[0]
    38  					ctx = ctxpkg.ContextSetToken(ctx, tkn)
    39  					ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, tkn)
    40  				}
    41  			}
    42  
    43  			if val, ok := md[ctxpkg.InitiatorHeader]; ok {
    44  				if len(val) > 0 && val[0] != "" {
    45  					initiatorID := val[0]
    46  					ctx = ctxpkg.ContextSetInitiator(ctx, initiatorID)
    47  					ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.InitiatorHeader, initiatorID)
    48  				}
    49  			}
    50  		}
    51  
    52  		return handler(ctx, req)
    53  	}
    54  	return interceptor
    55  }
    56  
    57  // NewStream returns a new server stream interceptor
    58  // that adds trace information to the request.
    59  func NewStream() grpc.StreamServerInterceptor {
    60  	interceptor := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    61  		ctx := ss.Context()
    62  
    63  		md, ok := metadata.FromIncomingContext(ss.Context())
    64  		if ok && md != nil {
    65  			if val, ok := md[ctxpkg.TokenHeader]; ok {
    66  				if len(val) > 0 && val[0] != "" {
    67  					tkn := val[0]
    68  					ctx = ctxpkg.ContextSetToken(ctx, tkn)
    69  					ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, tkn)
    70  				}
    71  			}
    72  
    73  			if val, ok := md[ctxpkg.InitiatorHeader]; ok {
    74  				if len(val) > 0 && val[0] != "" {
    75  					initiatorID := val[0]
    76  					ctx = ctxpkg.ContextSetInitiator(ctx, initiatorID)
    77  					ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.InitiatorHeader, initiatorID)
    78  				}
    79  			}
    80  		}
    81  
    82  		wrapped := newWrappedServerStream(ctx, ss)
    83  		return handler(srv, wrapped)
    84  	}
    85  	return interceptor
    86  }
    87  
    88  func newWrappedServerStream(ctx context.Context, ss grpc.ServerStream) *wrappedServerStream {
    89  	return &wrappedServerStream{ServerStream: ss, newCtx: ctx}
    90  }
    91  
    92  type wrappedServerStream struct {
    93  	grpc.ServerStream
    94  	newCtx context.Context
    95  }
    96  
    97  func (ss *wrappedServerStream) Context() context.Context {
    98  	return ss.newCtx
    99  }