auth

package
v0.21.9 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package auth provides an API to use GraphJin serv auth handles with your own application. Works with routers like chi and http mux. For detailed documentation visit https://graphjin.com

Example usage:

package main

import (
	"net/http"
	"path/filepath"
	"github.com/go-chi/chi"
	"github.com/dosco/graphjin/serv"
	"github.com/dosco/graphjin/serv/auth"
)

func main() {
	conf, err := serv.ReadInConfig(filepath.Join("./config", serv.GetConfigName()))
	if err != nil {
		panic(err)
	}

	useAuth, err := auth.NewAuth(conf.Auth, log, auth.Options{AuthFailBlock: true})
	if err != nil {
		panic(err)
	}

	r := chi.NewRouter()
	r.Use(useAuth)
	r.Get("/user", userInfo)

	http.ListenAndServe(":8080", r)
}

Index

Constants

This section is empty.

Variables

View Source
var Err401 = errors.New("401 unauthorized")
View Source
var ErrNoAuthDefined = errors.New("no auth defined")

Functions

func IsAuth

func IsAuth(c context.Context) bool

func NewAuth

func NewAuth(ac Auth, log *zap.Logger, opt Options, hFn ...HandlerFunc) (
	func(next http.Handler) http.Handler, error)

NewAuth returns a new auth handler. It will create a HandlerFunc based on the provided config.

Optionally an existing HandlerFunc can be provided. This is required to support auth in WS subscriptions.

func UserID

func UserID(c context.Context) interface{}

func UserIDInt

func UserIDInt(c context.Context) int

Types

type Auth

type Auth struct {
	// Enable development mode used to set credentials in the header and vars for testing
	Development bool `jsonschema:"title=Development Mode,default=false"`

	// Name is a friendly name for this auth config
	Name string

	// Type can be one of rails, jwt or header
	Type string `jsonschema:"title=Type,enum=jwt,enum=rails,enum=header"`

	// The name of the cookie that holds the authentication token
	Cookie string `jsonschema:"title=Cookie Name"`

	// Ruby on Rails cookie authentication
	Rails struct {
		// Rails version is needed to decode the cookie correctly.
		// Can be 5.2 or 6
		Version string `jsonschema:"enum=5.2,enum=6"`

		// SecretKeyBase is the cookie encryption key used in your Rails config
		SecretKeyBase string `mapstructure:"secret_key_base"`

		// URL is used for Rails cookie store based auth.
		// Example: redis://redis-host:6379 or memcache://memcache-host
		URL string `jsonschema:"title=Cookie Store URL,example=redis://redis-host:6379"`

		// Password is set if needed by the cookie store (Redis, Memcache, etc)
		Password string

		// Maximum idle time for the connection
		MaxIdle int `mapstructure:"max_idle" jsonschema:"title=Cookie Store Maximum Idle Time"`

		// MaxActive maximum active time for the connection
		MaxActive int `mapstructure:"max_active" jsonschema:"title=Cookie Store Maximum Active Time"`

		// Salt value is from your Rails 5.2 and below auth config
		Salt string

		// SignSalt value is from your Rails 5.2 and below auth config
		SignSalt string `mapstructure:"sign_salt" jsonschema:"title=Siging Salt (Rails 5.2)"`

		// AuthSalt value is from your Rails 5.2 and below auth config
		AuthSalt string `mapstructure:"auth_salt" jsonschema:"title=Authentication Salt (Rails 5.2)"`
	}

	// JWT authentication
	JWT JWTConfig

	// Header authentication
	Header struct {
		// Name of the HTTP header
		Name string

		// Value if set must match expected value (optional)
		Value string

		// Exists if set to true then the header must exist
		// this is an alternative to using value
		Exists bool
	}
}

Auth struct contains authentication related config values used by the GraphJin service

type HandlerFunc added in v0.20.24

type HandlerFunc func(w http.ResponseWriter, r *http.Request) (context.Context, error)

func HeaderHandler

func HeaderHandler(ac Auth) (HandlerFunc, error)

func JwtHandler

func JwtHandler(ac Auth) (HandlerFunc, error)

func NewAuthHandlerFunc added in v0.20.24

func NewAuthHandlerFunc(ac Auth) (HandlerFunc, error)

NewAuthHandlerFunc returns a HandlerFunc based on the provided config. Usually you don't need to use this function, because is called by NewAuth if no HandlerFunc is provided.

func RailsCookieHandler

func RailsCookieHandler(ac Auth) (HandlerFunc, error)

func RailsHandler

func RailsHandler(ac Auth) (HandlerFunc, error)

func RailsMemcacheHandler

func RailsMemcacheHandler(ac Auth) (HandlerFunc, error)

func RailsRedisHandler

func RailsRedisHandler(ac Auth) (HandlerFunc, error)

func SimpleHandler

func SimpleHandler(ac Auth) (HandlerFunc, error)

type JWTConfig

type JWTConfig = provider.JWTConfig

type Options

type Options struct {
	// Return a HTTP '401 Unauthoized' when auth fails
	AuthFailBlock bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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