iris_jose

package
v0.0.0-...-69b838a Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2016 License: MIT Imports: 5 Imported by: 0

README

Jose Middleware for Iris web framework

GoDoc

Golang jose repository

Middleware information

This middleware has been converted to work with iris

Description

Middleware for Javascript Object Signing and Encryption. The implementation follows the JSON Web Encryption standard (RFC 7516) and JSON Web Signature standard (RFC 7515).

Underlying library is go-jose from Square

Install

$ go get -u github.com/Heirko/iris-contrib/middleware/jose

How to use

Read the jwt middleware section here

Documentation

Overview

Example
var (
	privateKey, _    = rsa.GenerateKey(rand.Reader, 2048)
	api              = iris.New()
	myJoseMiddleware = New(Config{
		KeysGetter: func() (interface{}, interface{}, error) {
			return privateKey, &privateKey.PublicKey, nil
		},
	})
)

securedPingHandler := func(ctx *iris.Context) {
	claimString := myJoseMiddleware.Get(ctx)

	response := Response{Text: "Iauthenticated " + string(claimString)}
	claim := Claim{}
	json.Unmarshal(claimString, &claim)
	fmt.Println("claim :", claim.Name)

	ctx.JSON(iris.StatusOK, response)
}

// assign middleware and callback
api.Get("/secured/ping", myJoseMiddleware.Serve, securedPingHandler)

e := api.Tester(nil)

// Create a new token
claim := Claim{Name: " with Jose"}
token := myJoseMiddleware.NewTokenWithClaim(claim)
e.GET("/secured/ping").WithHeader("Authorization", "Bearer "+token).
	Expect().Status(iris.StatusOK).Body().
	Contains("Iauthenticated").Contains("with Jose")
Output:

claim :  with Jose

Index

Examples

Constants

View Source
const (
	//Default context key
	DefaultContextKey = "jose"
)

Variables

This section is empty.

Functions

func FromAuthHeader

func FromAuthHeader(ctx *iris.Context) (string, error)

------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------

TOKEN EXTRACTORS 						  //

------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------ FromAuthHeader is a TokenExtractor that takes a given context and extracts the JWS or JWE from the Authorization header

func OnError

func OnError(ctx *iris.Context, err string)

Default error Handler

Types

type Config

type Config struct {
	// The function that will return the private ans public key to decrypt and validate the JOSE
	// Default : nil
	KeysGetter KeyFunc

	// String representing the key management algorithm
	//default value: RSA_OAEP_256
	KeyAlgorithm jose.KeyAlgorithm

	// the name of the property in the request where the user information
	// from the JOSE will be stored
	//default value: jose
	ContextKey string

	//The function that will be called when there's an error validation the token
	ErrorHandler errorHandler

	//A boolean indicating if the credentials are required or not
	//default value: false
	CredentialsOptional bool

	//A function that extract the jose from request
	// Default: FromAuthHeader ( i.e from Authorization header as bearer token )
	Extractor TokenExtractor

	// Debug flag turns on debugging output
	// Default : false
	Debug bool

	//When set, all requests with the OPTIONS method will use authentication
	// if you enable this options, you should register youre route with iris.Options(...) also
	// Default : false
	EnableAuthOnOptions bool

	// Define the signature algorithm.
	//default : RS512
	SignatureAlgorithm jose.SignatureAlgorithm

	// specify the encryption algorythm
	// Default : A256CBC_HS512
	EncryptionAlgorithm jose.ContentEncryption
}

Config is a struct for specifying configurations options for the iris_jose middleware

type KeyFunc

type KeyFunc func() (interface{}, interface{}, error)

Callback function to supply the key for verification. Used by the Parse methodbut unverified Token. This allows one to use properties in the Header of the token to identify which key to use

type Middleware

type Middleware struct {
	Config Config
}

middleware

func New

func New(cfg ...Config) *Middleware

Middleware Constructor

func (*Middleware) CheckToken

func (m *Middleware) CheckToken(ctx *iris.Context) error

CheckToken main functionnality, checks for token

func (*Middleware) Get

func (m *Middleware) Get(ctx *iris.Context) []byte

Get returns the user information for this client/request

func (*Middleware) NewTokenWithClaim

func (m *Middleware) NewTokenWithClaim(claim interface{}) string

create a new token with the given claim and with given encrypt and signing algs

func (*Middleware) Serve

func (m *Middleware) Serve(ctx *iris.Context)

Serve the middelware's action

type TokenExtractor

type TokenExtractor func(ctx *iris.Context) (string, error)

Token Extractor is a function that takes a context as input and returns either a token or an error. An error should only be returned if an attemps to specify a token was found, but the information was somehow incorrectly formed. In the case where a token is simply not present, this should not be treated as an error

func FromFirst

func FromFirst(extractors ...TokenExtractor) TokenExtractor

FromFirst returns a function that runs multiple token extractors and takes the first token it finds

func FromParameter

func FromParameter(param string) TokenExtractor

FromParameter returns a function that extracts the token from the specified query string parameter

Jump to

Keyboard shortcuts

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