Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "fmt" "net/http" "github.com/shaj13/go-guardian/auth" "github.com/shaj13/go-guardian/auth/strategies/basic" "github.com/shaj13/go-guardian/auth/strategies/twofactor" "github.com/shaj13/go-guardian/tfa" ) type OTPManager struct{} func (OTPManager) Enabled(_ auth.Info) bool { return true } func (OTPManager) Load(_ auth.Info) (twofactor.OTP, error) { // user otp configuration must be loaded from persistent storage cfg := tfa.OTPConfig{ OTPType: tfa.HOTP, Label: "LABEL", Counter: 0, Secret: "GXNRHI2MFRFWXQGJHWZJFOSYI6E7MEVA", } _, otp, err := tfa.NewOTP(&cfg) return otp, err } func (OTPManager) Store(_ auth.Info, otp twofactor.OTP) error { // persist user otp after verification fmt.Println("Failed: ", otp.(tfa.OTP).Failed()) return nil } func main() { strategy := twofactor.Strategy{ Parser: twofactor.XHeaderParser("X-Example-OTP"), Manager: OTPManager{}, Primary: basic.AuthenticateFunc( func(ctx context.Context, r *http.Request, userName, password string) (auth.Info, error) { return auth.NewDefaultUser("example", "1", nil, nil), nil }, ), } r, _ := http.NewRequest("GET", "/", nil) r.SetBasicAuth("example", "example") r.Header.Set("X-Example-OTP", "345515") info, err := strategy.Authenticate(r.Context(), r) fmt.Println(info.UserName(), err) }
Output: Failed: 0 example <nil>
Index ¶
Examples ¶
Constants ¶
const StrategyKey = auth.StrategyKey("2FA.Strategy")
StrategyKey export identifier for the two factor strategy, commonly used when enable/add strategy to go-guardian authenticator.
Variables ¶
var ErrInvalidPin = errors.New("strategies/twofactor: Invalid one time password")
ErrInvalidPin is returned by strategy, When the user-supplied an invalid one time password and verification process failed.
var ErrMissingPin = errors.New("strategies/twofactor: One-time password missing or empty")
ErrMissingPin is returned by Parser, When one-time password missing or empty in HTTP request.
Functions ¶
This section is empty.
Types ¶
type OTPManager ¶
type OTPManager interface { // Enabled check if two factor for user. Enabled(user auth.Info) bool // Load return user OTP or error. Load(user auth.Info) (OTP, error) // Store user OTP. Store(user auth.Info, otp OTP) error }
OTPManager load and store user OTP.
type Parser ¶
Parser parse and extract one-time password from incoming HTTP request.
func CookieParser ¶
CookieParser return a one-time password parser, where pin extracted form HTTP Cookie.
func JSONBodyParser ¶
JSONBodyParser return a one-time password parser, where pin extracted form request body.
func QueryParser ¶
QueryParser return a one-time password parser, where pin extracted form HTTP query string.
func XHeaderParser ¶
XHeaderParser return a one-time password parser, where pin extracted form "X-" header.