github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/web/interceptor/token_auth_client_interceptor.go (about)

     1  package interceptor
     2  
     3  import (
     4  	"context"
     5  
     6  	"connectrpc.com/connect"
     7  )
     8  
     9  // NewTokenAuthClientInterceptor returns a client interceptor that will add the given token in the Authorization header.
    10  func NewTokenAuthClientInterceptor(token string) connect.UnaryInterceptorFunc {
    11  	interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
    12  		return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
    13  			if req.Spec().IsClient {
    14  				// Send a token with client requests.
    15  				req.Header().Set(tokenHeader, token)
    16  			}
    17  			return next(ctx, req)
    18  		})
    19  	}
    20  	return connect.UnaryInterceptorFunc(interceptor)
    21  }