basic auth management for GO
import "github.com/howkyle/authman"
secret - secret key used to sign token, authid - cookie name or header name where token is stored, issuer - issuer of auth eg localhost, session_exp - duration of the session (time.Duration)
a := authman.NewJWTAuthManager(secret, authid, issuer, session_exp)
principal - user id or username as a string, password - password input from user
cred:=authman.NewUserPassCredentials(principal, password)
pass, err:= cred.Hash()
- using an instance of authmanager
a := authman.NewJWTAuthManager(secret, authid, issuer, session_exp)
- retrieve the user and create new credentials
cred:=authman.NewUserPassCredentials(principal, password)
- take the credentials created from the retrieved user as authman.Credentials and takes takes the password to be validated. Returns an authman.Authentication instance or an error if authentication fails
auth, err:= a.Authenticate(cred authman.Credentials, password string)
- get auth as string type
s:=auth.AsString() or get created auth as a net/http cookie
c:=auth.AsCookie()
- using an instance of authmanager
a := authman.NewJWTAuthManager(secret, authid, issuer, session_exp)
- wrap handler func with auth filter
http.HandleFunc("/", a.Filter(func(w http.ResponseWriter, r *http.Request){}))
filter checks the http request for the cookie storing the access token, verifies the token, extracts the principal, passes it to the request context with the key 'sub' and serves the wrapped HandlerFunc
If the token is invalid or the cookie isnt present, Filter returns a 401