bearer

package
v1.65.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

bearer

Middleware that authenticates requests with Authorization: Bearer <token>, with the token supplied by a TokenSource.

TokenSource is the same one-method interface as golang.org/x/oauth2.TokenSource (Token() (*oauth2.Token, error)), so GCP credentials from golang.org/x/oauth2/google, the azuread package in this repository, or any other oauth2 source plug in directly.

Behaviour:

  • Tokens are cached and refreshed shortly before expiry (oauth2.ReuseTokenSource semantics, safe for concurrent use). A source that returns a token without an expiry is cached forever.
  • If the source returns an error, or an empty access token, the request fails and is never sent unauthenticated.
  • If the request already has an Authorization header the request fails instead of silently overwriting it, so this middleware cannot be combined with Client.Auth or Client.OAuth on the same client.
  • Client.BearerToken installs the middleware outside the HAR middlewares, so HAR captures the header (redacted by the HAR redaction policy) and trace redaction of Authorization keeps working.

Example

package main

import (
	"context"
	"fmt"

	"github.com/flanksource/commons/http"
	"golang.org/x/oauth2"
)

type staticToken string

func (s staticToken) Token() (*oauth2.Token, error) {
	return &oauth2.Token{AccessToken: string(s)}, nil
}

func main() {
	client := http.NewClient().BearerToken(staticToken("my-token"))

	resp, err := client.R(context.Background()).Get("https://api.example.com/v1/things")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	fmt.Println(resp.StatusCode)
}

With Google application default credentials:

creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
	return err
}
client := http.NewClient().BearerToken(creds.TokenSource)

Documentation

Overview

Package bearer provides a middleware that authenticates requests with an "Authorization: Bearer <token>" header obtained from a TokenSource.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

New returns a middleware that sets "Authorization: Bearer <token>" on every request. Tokens are cached and refreshed shortly before expiry (oauth2.ReuseTokenSource semantics, safe for concurrent use).

The middleware fails fast: if the token source errors or returns an empty token the request is not sent, and if the request already carries an Authorization header it is rejected rather than silently overwritten.

Types

type TokenSource

type TokenSource interface {
	Token() (*oauth2.Token, error)
}

TokenSource is compatible with golang.org/x/oauth2.TokenSource, so credentials from golang.org/x/oauth2/google (or any oauth2 source) can be passed in directly.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL