jwttest

package
v0.169.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenRSKeys

func GenRSKeys(keysize int) (*jose.JSONWebKey, *jose.JSONWebKey, error)

GenRSKeys generates a public/private key pair for signing using RS256 algorithm.

Types

type Issuer

type Issuer struct {
	jose.Signer
	PubKey *jose.JSONWebKey
	Name   string
}

Issuer is a test issuer to issue jwts with claims on demand

Intended for easy testing. Use Issue to create a new jwt that can be authenticated by the public key

ServeHTTP serves the public key as a jwks payload.

func NewIssuer

func NewIssuer(name string, keysize int) (Issuer, error)

NewIssuer creates a new jwt token issuer with a RS256 key of given size.

func (Issuer) Authenticate

func (i Issuer) Authenticate(ctx context.Context, token string) (jwtauth.Claims, error)

Authenticate implements jwtauth.Authenticator

Checks the issuer on the inbound jwt matches the name of the issuer.

func (Issuer) Authenticator

func (i Issuer) Authenticator() jwtauth.Authenticator

Authenticator produces a standard authenticator with only this issuer as a trusted issuer.

func (Issuer) HTTPAuth

func (i Issuer) HTTPAuth() *jwthttp.Auth

HTTPAuth returns an httpauth middleware struct with the issuer set as the authenticator.

func (Issuer) Issue

func (i Issuer) Issue(claims jwtauth.Claims) (string, error)

Issue issues a new jwt with the given claims.

Example

Shows how to use the issuer to issue a token with the desired claims.

package main

import (
	"context"
	"fmt"

	"github.com/anz-bank/sysl-go/jwtauth"
	"github.com/anz-bank/sysl-go/jwtauth/jwttest"
)

func main() {
	ctx := context.Background()
	issuer, _ := jwttest.NewIssuer("test", 1024)
	token, _ := issuer.Issue(jwtauth.Claims{
		"sub":   "me",
		"aud":   []string{"target"},
		"scope": "MY.SCOPE ANOTHER.SCOPE",
	})

	claims, _ := issuer.Authenticate(ctx, token)
	fmt.Println("iss:", claims["iss"])
	fmt.Println("sub:", claims["sub"])
	fmt.Println("aud:", claims["aud"])
	fmt.Println("scope:", claims["scope"])

}
Output:

iss: test
sub: me
aud: [target]
scope: MY.SCOPE ANOTHER.SCOPE

func (Issuer) IssueFromMap

func (i Issuer) IssueFromMap(claims map[string]interface{}) (string, error)

IssueFromMap issues a new jwt with the given claims.

func (Issuer) ServeHTTP

func (i Issuer) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (Issuer) Verify

func (i Issuer) Verify(token *jwt.JSONWebToken, claims ...interface{}) error

Verify implements jwtauth.Verifier.

Jump to

Keyboard shortcuts

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