github.com/cs3org/reva/v2@v2.27.7/pkg/ctx/tokenctx.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 ctx
    20  
    21  import (
    22  	"context"
    23  )
    24  
    25  // TokenHeader is the header to be used across grpc and http services
    26  // to forward the access token.
    27  const TokenHeader = "x-access-token"
    28  
    29  // ContextGetToken returns the token if set in the given context.
    30  func ContextGetToken(ctx context.Context) (string, bool) {
    31  	u, ok := ctx.Value(tokenKey).(string)
    32  	return u, ok
    33  }
    34  
    35  // ContextMustGetToken panics if token is not in context.
    36  func ContextMustGetToken(ctx context.Context) string {
    37  	u, ok := ContextGetToken(ctx)
    38  	if !ok {
    39  		panic("token not found in context")
    40  	}
    41  	return u
    42  }
    43  
    44  // ContextSetToken stores the token in the context.
    45  func ContextSetToken(ctx context.Context, t string) context.Context {
    46  	return context.WithValue(ctx, tokenKey, t)
    47  }