oauth2

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2020 License: MIT Imports: 7 Imported by: 1

README

Equinix oAuth2 Go client

Go implementation of oAuth2 enabbled HTTP client for interactions with Equinix APIs. Module implementes Equinix specific client credentials grant type with custom TokenSource from standard Go oauth2 module.

Requirements

  • Go 1.14+ (to build provider plugin)

Usage

  1. Import

    import "github.com/equinix/oauth2-go"
    
  2. Prepare configuration and get http client

    authConfig := oauth2.Config{
    	ClientID:     "myClientId",
    	ClientSecret: "myClientSecret"
    	BaseURL:      "https://api.equinix.com"}
    
    //*http.Client is returned
    hc := authConfig.New(context.Background())
    
  3. Use client

    *http.Client created by oAuth2 library will deal with token acquisition, refreshment and population of Authorization headers in subsequent requests.

    Below example shows how to use oAuth2 client with Resty REST client library

    rc := resty.NewWithClient(hc)
    resp, err := rc.R().Get("https://api.equinix.com/ecx/v3/port/userport")
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("Status Code:", resp.StatusCode())
        fmt.Println("Body:\n", resp)
    }
    

Documentation

Overview

Package oauth2 provides support for making oAuth2 authorized and authenticated HTTP requests for interactions with Equinix APIs, in particular Equinix specific client credencials grant type

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// ClientID is the application's ID.
	ClientID string
	// ClientSecret is the application's secret.
	ClientSecret string
	// BaseURL is the base endpoint of a server that  token endpoint
	BaseURL string
}

Config describes oauth2 client credentials flow

func (*Config) New

func (c *Config) New(ctx context.Context) *http.Client

New creates *http.Client with Equinix oAuth2 tokensource. The returned client is not valid beyond the lifetime of the context.

func (*Config) NewWithClient

func (c *Config) NewWithClient(ctx context.Context, hc *http.Client) *http.Client

NewWithClient creates *http.Client with Equinix oAuth2 tokensource and custom *http.Client. The returned client is not valid beyond the lifetime of the context.

func (*Config) TokenSource

func (c *Config) TokenSource(ctx context.Context, hc *http.Client) oauth2.TokenSource

TokenSource returns a TokenSource that returns t until t expires, automatically refreshing it as necessary using the provided context and the client ID and client secret.

type Error

type Error struct {
	Code    string
	Message string
}

Error describes oauth2 err

func (Error) Error

func (e Error) Error() string

Jump to

Keyboard shortcuts

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