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/v3"
"github.com/dosco/graphjin/auth/v3"
)
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 ¶
var Err401 = errors.New("401 unauthorized")
Functions ¶
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.
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 jwt or header
Type string `jsonschema:"title=Type,enum=jwt,enum=header"`
// The name of the cookie that holds the authentication token
Cookie string `jsonschema:"title=Cookie Name"`
// 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 ¶
func HeaderHandler ¶
func HeaderHandler(ac Auth) (HandlerFunc, error)
HeaderHandler is a middleware that checks for a header value
func JwtHandler ¶
func JwtHandler(ac Auth) (HandlerFunc, error)
JwtHandler is a middleware that checks for a JWT token in the cookie or the authorization header. If the token is found, it is validated and the claims
func NewAuthHandlerFunc ¶
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 NoAuth ¶
func NoAuth() (HandlerFunc, error)
NoAuth returns a handler that does not perform any authentication.
func SimpleHandler ¶
func SimpleHandler(ac Auth) (HandlerFunc, error)
SimpleHandler is a simple auth handler that sets the user ID, provider and role