-
Notifications
You must be signed in to change notification settings - Fork 55
/
parser.go
59 lines (45 loc) · 1.55 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package twofactor
import (
"errors"
"net/http"
"github.com/shaj13/go-guardian/v2/auth/internal"
)
// ErrMissingOTP is returned by Parser,
// When one-time password missing or empty in HTTP request.
var ErrMissingOTP = errors.New("strategies/twofactor: One-time password missing or empty")
// Parser parse and extract one-time password from incoming HTTP request.
type Parser interface {
GetOTP(r *http.Request) (string, error)
}
type otpFn func(r *http.Request) (string, error)
func (fn otpFn) GetOTP(r *http.Request) (string, error) {
return fn(r)
}
// XHeaderParser return a one-time password parser, where otp extracted form "X-" header.
func XHeaderParser(header string) Parser {
fn := func(r *http.Request) (string, error) {
return internal.ParseHeader(header, r, ErrMissingOTP)
}
return otpFn(fn)
}
// JSONBodyParser return a one-time password parser, where otp extracted form request body.
func JSONBodyParser(key string) Parser {
fn := func(r *http.Request) (string, error) {
return internal.ParseJSONBody(key, r, ErrMissingOTP)
}
return otpFn(fn)
}
// QueryParser return a one-time password parser, where otp extracted form HTTP query string.
func QueryParser(key string) Parser {
fn := func(r *http.Request) (string, error) {
return internal.ParseQuery(key, r, ErrMissingOTP)
}
return otpFn(fn)
}
// CookieParser return a one-time password parser, where otp extracted form HTTP Cookie.
func CookieParser(key string) Parser {
fn := func(r *http.Request) (string, error) {
return internal.ParseCookie(key, r, ErrMissingOTP)
}
return otpFn(fn)
}