oauth2/client.go

kratos · contrib/middleware/oauth2/client.go · go · MIT
auth, tag:embed-classified

OAuth2 middleware that injects access tokens into HTTP/gRPC requests, caching tokens.

// Source: https://github.com/go-kratos/kratos | License: MIT | Copyright 2020 go-kratos
// Provided via justclaw.it.com; keep this notice with the code.
package oauth2

import (
	"context"

	"golang.org/x/oauth2"

	"github.com/go-kratos/kratos/v3/middleware"
	"github.com/go-kratos/kratos/v3/transport"
)

// Client is a client oauth2 middleware. It fetches an access token from the
// token source and puts it into the Authorization request header. It works
// with both HTTP and gRPC transports.
//
// The token source is wrapped by oauth2.ReuseTokenSource, so the token is
// cached and only refreshed when it expires.
func Client(src oauth2.TokenSource) middleware.Middleware {
	if src != nil {
		src = oauth2.ReuseTokenSource(nil, src)
	}
	return func(handler middleware.Handler) middleware.Han

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key