-
Notifications
You must be signed in to change notification settings - Fork 27
/
form.go
71 lines (62 loc) · 1.58 KB
/
form.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
60
61
62
63
64
65
66
67
68
69
70
71
package otp
import (
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/util/secretcode"
)
type Form string
const (
FormCode Form = "code"
FormLink Form = "link"
)
func (f Form) AllowLookupByCode() bool {
return f == FormLink
}
func (f Form) codeType() secretCode {
switch f {
case FormCode:
return secretcode.OOBOTPSecretCode
case FormLink:
return secretcode.LinkOTPSecretCode
default:
panic("unexpected form: " + f)
}
}
func (f Form) CodeLength() int {
return f.codeType().Length()
}
func (f Form) GenerateCode(cfg *config.TestModeConfig, featureCfg *config.TestModeFeatureConfig, target string, userID string) string {
codeType := f.codeType()
switch c := codeType.(type) {
case secretcode.OOBOTPSecretCodeType:
if cfg.FixedOOBOTP.Enabled {
if r, ok := cfg.FixedOOBOTP.MatchTarget(target); ok {
fixedOTP := r.FixedCode
if fixedOTP == "" && featureCfg.FixedOOBOTP.Enabled {
fixedOTP = featureCfg.FixedOOBOTP.Code
}
if fixedOTP == "" {
fixedOTP = c.Generate()
}
return c.GenerateFixed(fixedOTP)
}
}
if featureCfg.FixedOOBOTP.Enabled {
return c.GenerateFixed(featureCfg.FixedOOBOTP.Code)
}
return c.Generate()
case secretcode.LinkOTPSecretCodeType:
if featureCfg.DeterministicLinkOTP.Enabled {
return c.GenerateDeterministic(userID)
} else {
return c.Generate()
}
}
panic("unknown otp form")
}
func (f Form) VerifyCode(input string, expected string) bool {
return f.codeType().Compare(input, expected)
}
type secretCode interface {
Length() int
Compare(string, string) bool
}