tokenauth

package module
v0.0.0-...-88e9f6c Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2017 License: Apache-2.0 Imports: 1 Imported by: 0

README

go-tokenauth

Build Status Go Report Card

This package contains different useful Auth Sources that can return valid auth token for future use in a form of string.

It also contains tiny interface called tokenauth.Source that allows to create more generic (e.g HTTP, gRPC) Clients:

type Source interface {
	// Name of the auth source.
	Name() string

	// Token allows the source to return a valid token for specific authorization type in a form of string.
	//
	// Example usage:
	// - filling Authorization HTTP header with valid auth.
	// In that case it is up to caller to properly save it into specific http request header (usually called "Authorization")
	// and add "bearer" prefix if needed.
	Token(context.Context) (string, error)
}

Example usage:

The usefulness of this interface can be shown in this example, when we wrap http.RoundTripper to inject required auth:

package example

import (
    "fmt"
    "net/http"
    
    "github.com/Bplotka/go-tokenauth"
    "github.com/Bplotka/go-tokenauth/direct"
)

func newExampleTripper(parent http.RoundTripper /* , <any configuration here> */) http.RoundTripper {
    auth := directauth.New("direct", "token1")
      // or:
      // oidcauth.New(...)
      // oauth2auth.New(...)
      // k8sauth.New(...)

    return &exampleTripper{
        parent: parent,
        auth: auth,
    }
}

type exampleTripper struct {
    parent http.RoundTripper
    auth tokenauth.Source
}

func (t *exampleTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    token, err := t.auth.Token(req.Context())
    if err != nil {
        // handle err
    }
    
    req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
    return t.parent.RoundTrip(req)
}

See ready to use tripper

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Source

type Source interface {
	// Name of the auth source.
	Name() string

	// Token allows the source to return a valid token for specific authorization type in a form of string.
	//
	// Example usage:
	// - filling Authorization HTTP header with valid auth.
	// In that case it is up to caller to properly save it into specific http request header (usually called "Authorization")
	// and add "bearer" prefix if needed.
	Token(context.Context) (string, error)
}

Source represents a way to get client auth in a form of token.

Directories

Path Synopsis
sources
k8s

Jump to

Keyboard shortcuts

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