jwt

package
v0.0.0-...-ecdd364 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2022 License: MIT Imports: 7 Imported by: 0

README

jwt

Package jwt is an Ambient plugin that enables jwt.

Import: github.com/ambientkit/plugin/middleware/jwt

Version: 1.0.0

Plugin Type

The plugin can be used as the following core types:

  • Logger: false
  • Storage System: false
  • Router: false
  • Template Engine: false
  • Session Manager: false

Grants

The plugin request the following grants (1):

  • Name: router.middleware:write
    • Description: Access to force authentication on routes using JWTs.

Settings

The plugin does not have any settings.

Routes

The plugin does not have any routes.

Middleware

The plugin has middleware (1).

FuncMap

The plugin does not have a FuncMap.

Assets

The plugin does not inject any assets.

Embedded Files

The plugin does not have any embedded files.

Example Usage

package main

import (
	"log"
	"time"

	"github.com/ambientkit/ambient"
	"github.com/ambientkit/ambient/pkg/ambientapp"
	"github.com/ambientkit/plugin/logger/zaplogger"
	"github.com/ambientkit/plugin/middleware/jwt"
	"github.com/ambientkit/plugin/pkg/uuid"
	"github.com/ambientkit/plugin/storage/memorystorage"
)

func main() {
	plugins := &ambient.PluginLoader{
		// Core plugins are implicitly trusted.
		Router:         nil,
		TemplateEngine: nil,
		SessionManager: nil,
		// Trusted plugins are those that are typically needed to boot so they
		// will be enabled and given full access.
		TrustedPlugins: map[string]bool{},
		Plugins:        []ambient.Plugin{},
		Middleware: []ambient.MiddlewarePlugin{
			// Middleware - executes top to bottom.
			jwt.New([]byte(uuid.EncodedString(32)), time.Hour*1, []string{}),
		},
	}
	_, _, err := ambientapp.NewApp("myapp", "1.0",
		zaplogger.New(),
		ambient.StoragePluginGroup{
			Storage: memorystorage.New(),
		},
		plugins)
	if err != nil {
		log.Fatalln(err.Error())
	}
}

Docgen by Ambient

Documentation

Overview

Package jwt is an Ambient plugin that enables jwt.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsWhitelisted

func IsWhitelisted(method string, path string, arr []string) (found bool)

IsWhitelisted returns true if the request is in the whitelist. If only an asterisk is found in the whitelist, allow all routes. If an asterisk is found in the page string, then whitelist only the matching paths.

Types

type Config

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

Config contains the dependencies for the handler.

func NewJWT

func NewJWT(whitelist []string, webtoken IToken, ctx IContext) *Config

NewJWT returns a new loq request middleware.

func (*Config) Handler

func (c *Config) Handler(next http.Handler) http.Handler

Handler will require a JWT.

type GenericResponse

type GenericResponse struct {
	// in: body
	Body struct {
		// Status contains the string of the HTTP status.
		//
		// Required: true
		Status string `json:"status"`
		// Message can contain a user friendly message.
		Message string `json:"message,omitempty"`
	}
}

GenericResponse returns any status code.

type IContext

type IContext interface {
	UserLogin(r *http.Request, username string) error
}

IContext provides handlers for type request context.

type IToken

type IToken interface {
	Verify(s string) (string, error)
}

IToken provides outputs for the JWT.

type InternalServerErrorResponse

type InternalServerErrorResponse struct {
	GenericResponse
}

InternalServerErrorResponse returns 500. swagger:response InternalServerErrorResponse

type Plugin

type Plugin struct {
	*ambient.PluginBase
	// contains filtered or unexported fields
}

Plugin represents an Ambient plugin.

func New

func New(secret []byte, sessionTimeout time.Duration, whitelist []string) *Plugin

New an Ambient plugin that provides request logging middleware.

Example
package main

import (
	"log"
	"time"

	"github.com/ambientkit/ambient"
	"github.com/ambientkit/ambient/pkg/ambientapp"
	"github.com/ambientkit/plugin/logger/zaplogger"
	"github.com/ambientkit/plugin/middleware/jwt"
	"github.com/ambientkit/plugin/pkg/uuid"
	"github.com/ambientkit/plugin/storage/memorystorage"
)

func main() {
	plugins := &ambient.PluginLoader{
		// Core plugins are implicitly trusted.
		Router:         nil,
		TemplateEngine: nil,
		SessionManager: nil,
		// Trusted plugins are those that are typically needed to boot so they
		// will be enabled and given full access.
		TrustedPlugins: map[string]bool{},
		Plugins:        []ambient.Plugin{},
		Middleware: []ambient.MiddlewarePlugin{
			// Middleware - executes top to bottom.
			jwt.New([]byte(uuid.EncodedString(32)), time.Hour*1, []string{}),
		},
	}
	_, _, err := ambientapp.NewApp("myapp", "1.0",
		zaplogger.New(),
		ambient.StoragePluginGroup{
			Storage: memorystorage.New(),
		},
		plugins)
	if err != nil {
		log.Fatalln(err.Error())
	}
}
Output:

func (*Plugin) GrantRequests

func (p *Plugin) GrantRequests() []ambient.GrantRequest

GrantRequests returns a list of grants requested by the plugin.

func (*Plugin) Middleware

func (p *Plugin) Middleware() []func(next http.Handler) http.Handler

Middleware returns router middleware.

func (*Plugin) PluginName

func (p *Plugin) PluginName() string

PluginName returns the plugin name.

func (*Plugin) PluginVersion

func (p *Plugin) PluginVersion() string

PluginVersion returns the plugin version.

type UnauthorizedResponse

type UnauthorizedResponse struct {
	GenericResponse
}

UnauthorizedResponse returns 401. swagger:response UnauthorizedResponse

Jump to

Keyboard shortcuts

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