dynoidtest

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Overview

dynoidtest provides helper functions for testing code that uses DynoID

Index

Examples

Constants

View Source
const (
	DefaultHerokuHost = "heroku.local"                         // issuer host used when one is not provided
	DefaultSpaceID    = "test"                                 // space id used when one is not provided
	DefaultAppID      = "00000000-0000-0000-0000-000000000001" // app id used when one is not provided
	DefaultAppName    = "sushi"                                // app name used when one is not provided
	DefaultDyno       = "web.1"                                // dyno used when one is not provided
)

Variables

This section is empty.

Functions

func GenerateDefaultFS added in v0.4.0

func GenerateDefaultFS(iss *Issuer, audiences ...string) error

Configure dynoid.DefaultFS to use tokens generated by the provided issuer for the audiences listed.

func Issue added in v0.4.0

func Issue(iss *Issuer) http.Handler

The Issue http.Handler generates test tokens via a local Issuer using the provided opts.

A query param 'audience' is expected.

func LocalIssuer added in v0.4.0

func LocalIssuer(iss *Issuer) func(http.Handler) http.Handler

Configures the oidc client to use the issuer provided.

Types

type FS added in v0.3.0

type FS struct {
	// contains filtered or unexported fields
}

FS implements fs.ReadFileFS and is suitable for testing reading tokens.

func NewFS added in v0.3.0

func NewFS(tokens map[string]string) *FS

Create a new FS where the DynoID tokens have been populated.

The tokens map keys are the expected audience and the values are the token contents.

func (*FS) Open added in v0.3.0

func (f *FS) Open(name string) (fs.File, error)

func (*FS) ReadFile added in v0.3.0

func (f *FS) ReadFile(name string) ([]byte, error)

type Issuer

type Issuer struct {
	// contains filtered or unexported fields
}

Issuer generates test tokens and provides a client for verifying them.

Example
package main

import (
	"context"
	"fmt"

	"github.com/heroku/x/dynoid"
	"github.com/heroku/x/dynoid/dynoidtest"
)

const Audience = "testing"

func main() {
	ctx, iss, err := dynoidtest.NewWithContext(context.Background())
	if err != nil {
		panic(err)
	}

	if err := dynoidtest.GenerateDefaultFS(iss, Audience); err != nil {
		panic(err)
	}

	token, err := dynoid.ReadLocalToken(ctx, Audience)
	if err != nil {
		panic(err)
	}

	fmt.Println(token.Subject.AppID)
	fmt.Println(token.Subject.AppName)
	fmt.Println(token.Subject.Dyno)
}
Output:

00000000-0000-0000-0000-000000000001
sushi
web.1

func New

func New(opts ...IssuerOpt) (*Issuer, error)

Create a new Issuer with the supplied opts applied

func NewWithContext added in v0.2.0

func NewWithContext(ctx context.Context, opts ...IssuerOpt) (context.Context, *Issuer, error)

Create a new Issuer with the supplied opts applied inheriting from the provided context

func (*Issuer) GenerateIDToken

func (iss *Issuer) GenerateIDToken(audience string, opts ...TokenOpt) (string, error)

GenerateIDToken returns a new signed token as a string

func (*Issuer) HTTPClient

func (iss *Issuer) HTTPClient() *http.Client

HTTPClient returns a client that leverages the Issuer to validate tokens.

type IssuerOpt added in v0.3.0

type IssuerOpt interface {
	// contains filtered or unexported methods
}

IssuerOpt allows the behavior of the issuer to be modified.

func WithHerokuHost added in v0.4.0

func WithHerokuHost(herokuHost string) IssuerOpt

WithHerokuHost allows an issuer host to be supplied instead of using the default

func WithKey added in v0.4.0

func WithKey(key *rsa.PrivateKey) IssuerOpt

WithKey allows you to set the issuer's private key. Useful for leveraging test middleware.

func WithSpaceID added in v0.3.0

func WithSpaceID(spaceID string) IssuerOpt

WithSpaceID allows a spaceID to be supplied instead of using the default

func WithTokenOpts added in v0.3.0

func WithTokenOpts(opts ...TokenOpt) IssuerOpt

WithTokenOpts allows a default set of TokenOpt to be applied to every token generated by the issuer

type LocalConfiguration added in v0.4.0

type LocalConfiguration struct {
	// contains filtered or unexported fields
}

LocalConfiguration provides methods for working with a local issuer configured with ConfigureLocal

func ConfigureLocal added in v0.4.0

func ConfigureLocal(audiences []string, opts ...IssuerOpt) (*LocalConfiguration, error)

ConfigureLocal sets up the environment with a local DynoID issuer and generates tokens for the audiences provided.

The returned LocalConfiguration provides methods for working with the issuer.

func ConfigureLocalWithContext added in v0.4.0

func ConfigureLocalWithContext(ctx context.Context, audiences []string, opts ...IssuerOpt) (*LocalConfiguration, error)

func (*LocalConfiguration) Context added in v0.4.0

func (c *LocalConfiguration) Context() context.Context

func (*LocalConfiguration) GenerateToken added in v0.4.0

func (c *LocalConfiguration) GenerateToken(audience string) (string, error)

GenerateToken mints a token for the given audience using the configured issuer.

func (*LocalConfiguration) Handler added in v0.4.0

func (c *LocalConfiguration) Handler() http.Handler

Handler mints tokens for the configured issuer using for the audience specified by the audience query param.

func (*LocalConfiguration) Middleware added in v0.4.0

func (c *LocalConfiguration) Middleware() func(http.Handler) http.Handler

The Middleware should be inserted in the middleware stack before any functions that use dynoid are called.

type TokenOpt added in v0.3.0

type TokenOpt interface {
	// contains filtered or unexported methods
}

A TokenOpt modifies the way a token is minted

func WithSubject added in v0.3.0

func WithSubject(s *dynoid.Subject) TokenOpt

WithSubject allows the Subject to be different than the default

func WithSubjectFunc added in v0.4.0

func WithSubjectFunc(fn func(audience string, subject *dynoid.Subject) *dynoid.Subject) TokenOpt

WithSubjectFunc allows the Subject to be different than the default based on the audience being generated.

Jump to

Keyboard shortcuts

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