auth

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

Auth

Golang library for exchanging IAM principal for a Twisp OIDC token.

Usage

Twisp provides an http.RoundTripper implementation that handles:

  • Exchanging IAM principal for a Twisp OIDC token
  • Auto-refreshing those tokens on expiration
  • Automatically setting the Authorization and X-Twisp-Account-Id headers on HTTP requests
package main

import (
    "fmt"
    "io"
    "log"
    "net/http"
    "strings"
    "time"

    "github.com/twisp/auth-go/v1"
)

func main() {
    var (
        accountID = "TwispAuthExample"
        region    = "us-west-2"
        api       = fmt.Sprintf("https://api.%s.cloud.twisp.com/financial/graphql/v1", region)
    )

    client := &http.Client{
        Transport: auth.NewRoundTripper(accountID, region, http.DefaultTransport),
        Timeout:   time.Second * 5,
    }

    query := `{"query": "{ journal { code } }"}`

    req, err := http.NewRequest(http.MethodPost, api, strings.NewReader(query))
    if err != nil {
        log.Fatal(err)
    }

    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()

    out, err := io.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%s", string(out))
}

Documentation

Index

Constants

View Source
const (
	DefaultEnv          = "cloud"
	HeaderAuthorization = "Authorization"
	HeaderAccountID     = "X-Twisp-Account-Id"
)

Variables

View Source
var (
	DefaultClock         = time.Now
	DefaultMaxTokenAge   = time.Minute
	DefaultMaxRefreshAge = 5 * time.Minute
)

Functions

func NewEnvironmentRoundTripper

func NewEnvironmentRoundTripper(accountID, env, region string, transport http.RoundTripper) http.RoundTripper

func NewRoundTripper

func NewRoundTripper(accountID string, region string, transport http.RoundTripper) http.RoundTripper

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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