github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/utils/delegator.go (about)

     1  /*
     2  Copyright 2020 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package utils
    18  
    19  import "context"
    20  
    21  type contextKey string
    22  
    23  const (
    24  	// ContextDelegator is a delegator for access requests set in the context
    25  	// of the request
    26  	ContextDelegator contextKey = "delegator"
    27  )
    28  
    29  // GetDelegator attempts to load the context value AccessRequestDelegator,
    30  // returning the empty string if no value was found.
    31  func GetDelegator(ctx context.Context) string {
    32  	delegator, ok := ctx.Value(ContextDelegator).(string)
    33  	if !ok {
    34  		return ""
    35  	}
    36  	return delegator
    37  }
    38  
    39  // WithDelegator creates a child context with the AccessRequestDelegator
    40  // value set.  Optionally used by AuthServer.SetAccessRequestState to log
    41  // a delegating identity.
    42  func WithDelegator(ctx context.Context, delegator string) context.Context {
    43  	return context.WithValue(ctx, ContextDelegator, delegator)
    44  }