/
authflow.go
103 lines (87 loc) · 2.8 KB
/
authflow.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package webapp
import (
"fmt"
"net/mail"
"regexp"
"strings"
authflow "github.com/authgear/authgear-server/pkg/lib/authenticationflow"
"github.com/authgear/authgear-server/pkg/lib/authenticationflow/declarative"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/util/phone"
)
var phoneRegexp = regexp.MustCompile(`^\+[0-9]*$`)
func GetIdentificationOptions(f *authflow.FlowResponse) []declarative.IdentificationOption {
var options []declarative.IdentificationOption
switch data := f.Action.Data.(type) {
case declarative.IdentificationData:
options = data.Options
default:
panic(fmt.Errorf("unexpected type of data: %T", f.Action.Data))
}
return options
}
func GetMostAppropriateIdentification(f *authflow.FlowResponse, loginID string, loginIDInputType string) config.AuthenticationFlowIdentification {
// If loginIDInputType already tell us the login id type, return the corresponding type
switch loginIDInputType {
case "email":
return config.AuthenticationFlowIdentificationEmail
case "phone":
return config.AuthenticationFlowIdentificationPhone
}
// Else, guess the type
lookLikeAPhoneNumber := func(loginID string) bool {
err := phone.LegalAndValidParser.CheckE164(loginID)
if err == nil {
return true
}
if phoneRegexp.MatchString(loginID) {
return true
}
return false
}
lookLikeAnEmailAddress := func(loginID string) bool {
_, err := mail.ParseAddress(loginID)
if err == nil {
return true
}
if strings.Contains(loginID, "@") {
return true
}
return false
}
isPhoneLike := lookLikeAPhoneNumber(loginID)
isEmailLike := lookLikeAnEmailAddress(loginID)
options := GetIdentificationOptions(f)
var iden config.AuthenticationFlowIdentification
for _, o := range options {
switch o.Identification {
case config.AuthenticationFlowIdentificationEmail:
// If it is a email like login id, and there is an email option, it must be email
if isEmailLike {
iden = config.AuthenticationFlowIdentificationEmail
break
}
case config.AuthenticationFlowIdentificationPhone:
// If it is a phone like login id, and there is an phone option, it must be phone
if isPhoneLike {
iden = config.AuthenticationFlowIdentificationPhone
break
}
case config.AuthenticationFlowIdentificationUsername:
// The login id is not phone or email, then it can only be username
if !isPhoneLike && !isEmailLike {
iden = config.AuthenticationFlowIdentificationUsername
break
}
// If it is like a email or phone, it can be username,
// but we should continue the loop to see if there are better options
if iden == "" {
iden = config.AuthenticationFlowIdentificationUsername
}
}
}
if iden == "" {
panic(fmt.Errorf("expected the authflow to allow login ID as identification"))
}
return iden
}