oauth2 package

repository·master·Indexed 26 days ago

https://github.com/golang/oauth2

A Go implementation of the OAuth 2.0 client specification. The package provides tools to authenticate and authorize applications via OAuth 2.0 providers, including support for 3-legged flows, Device Authorization Grant (RFC 8628), PKCE, and token management via TokenSource and Transport.

Tokens
2.5K
Snippets
3
Records
27
Agent score
91%

What's inside golang-oauth2

  1. Use the OAuth2 for Go package

    master

    The golang.org/x/oauth2 package provides a client implementation for the OAuth 2.0 specification. It is used to handle OAuth 2.0 flows in Go applications. For detailed API documentation and usage examples, refer to the official Go package documentation.

    https://pkg.go.dev/golang.org/x/oauth2
  2. Add a new OAuth2 endpoint

    master
    If you need to add a new provider-specific endpoint, do not create a new provider-specific package if it only adds a single endpoint variable. Instead, add the endpoint to the golang.org/x/oauth2/endpoints package.
  3. Configure a 3-legged OAuth2 flow with Config

    master

    The Config struct is used to manage a typical 3-legged OAuth2 flow. It requires client credentials and the provider's endpoint information.

    Key fields:

    • ClientID: The application's ID.
    • ClientSecret: The application's secret.
    • Endpoint: An Endpoint struct containing the provider's URLs.
    • RedirectURL: The URL to redirect users to after authorization.
    • Scopes: A slice of strings specifying requested permissions.
  4. Check if a Token is valid

    master

    Use the Valid() method to determine if a token is usable. A token is considered valid if:

    1. It is not nil.
    2. It has a non-empty AccessToken.
    3. It has not expired (accounting for a small safety buffer to prevent late expirations due to clock skew).
  5. Retrieve extra metadata from a Token

    master
    The Extra(key string) method allows you to access additional key-value pairs returned by the OAuth2 server that are not part of the standard Token fields. The method automatically attempts to parse values into appropriate Go types (like int64 or float64) if they are formatted as strings in the response.
  6. Manage token caching with ReuseTokenSource

    master

    ReuseTokenSource is used to wrap a TokenSource to provide caching behavior. It returns the same token as long as it is valid, and only uses the underlying src to refresh it when it expires.

    This is useful for reusing tokens from a cache (like a file on disk) between program runs.

    Variants:

    • ReuseTokenSource(t *Token, src TokenSource): Uses the provided token t as the initial cached token.
    • ReuseTokenSourceWithExpiry(t *Token, src TokenSource, earlyExpiry time.Duration): Allows configuring a buffer (earlyExpiry) before the token actually expires to trigger a refresh.
  7. Initiate Device Authorization Grant

    master
    Use the DeviceAuth method on a *Config instance to request a device code and user code. This is used for devices that lack a browser or have limited input capabilities (RFC 8628). The method returns a *DeviceAuthResponse containing the necessary information for the user to authorize the device on a separate device.
  8. Use PasswordCredentialsToken for resource owner credentials

    master
    Config.PasswordCredentialsToken converts a username and password pair directly into a token. This should only be used in highly trusted scenarios (e.g., a device OS) where other grant types are unavailable.
  9. Generate an authorization URL with AuthCodeURL

    master

    Use Config.AuthCodeURL to generate the URL for the provider's consent page. This is the first step in the authorization code flow.

    Parameters:

    • state: An opaque value used to maintain state and protect against CSRF. The server will return this value in the callback.
    • opts: Optional AuthCodeOption values to modify the request (e.g., AccessTypeOffline, ApprovalForce).
  10. Create an authorized HTTP client with Config.Client

    master
    The easiest way to make authorized requests is to use Config.Client. It returns an *http.Client that automatically handles token refreshing using the provided token.
  11. Use Transport to automatically inject OAuth2 tokens into HTTP requests

    master
    The Transport type implements the http.RoundTripper interface. It wraps a base http.RoundTripper (defaulting to http.DefaultTransport if Base is nil) and automatically adds an Authorization header to outgoing requests using a token retrieved from the provided TokenSource.