/
settings.go
172 lines (146 loc) · 4.79 KB
/
settings.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package viewmodels
import (
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/authenticator"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/lib/config"
)
type SettingsViewModel struct {
Authenticators []*authenticator.Info
HasDeviceTokens bool
ListRecoveryCodesAllowed bool
ShowBiometric bool
HasSecondaryTOTP bool
HasSecondaryOOBOTPEmail bool
HasSecondaryOOBOTPSMS bool
SecondaryPassword *authenticator.Info
HasMFA bool
ShowSecondaryTOTP bool
ShowSecondaryOOBOTPEmail bool
ShowSecondaryOOBOTPSMS bool
ShowSecondaryPassword bool
ShowMFA bool
LatestPrimaryPasskey *authenticator.Info
ShowPrimaryPasskey bool
}
type SettingsIdentityService interface {
ListByUser(userID string) ([]*identity.Info, error)
}
type SettingsAuthenticatorService interface {
List(userID string, filters ...authenticator.Filter) ([]*authenticator.Info, error)
}
type SettingsMFAService interface {
HasDeviceTokens(userID string) (bool, error)
}
type SettingsViewModeler struct {
Authenticators SettingsAuthenticatorService
MFA SettingsMFAService
Authentication *config.AuthenticationConfig
Biometric *config.BiometricConfig
}
// nolint: gocognit
func (m *SettingsViewModeler) ViewModel(userID string) (*SettingsViewModel, error) {
authenticators, err := m.Authenticators.List(userID)
if err != nil {
return nil, err
}
somePrimaryAuthenticatorCanHaveMFA := len(authenticator.ApplyFilters(
authenticators,
authenticator.KeepPrimaryAuthenticatorCanHaveMFA,
)) > 0
hasDeviceTokens, err := m.MFA.HasDeviceTokens(userID)
if err != nil {
return nil, err
}
hasSecondaryTOTP := false
hasSecondaryOOBOTPEmail := false
hasSecondaryOOBOTPSMS := false
var secondaryPassword *authenticator.Info
totpAllowed := false
oobotpEmailAllowed := false
oobotpSMSAllowed := false
passwordAllowed := false
passkeyAllowed := false
var latestPrimaryPasskey *authenticator.Info
if somePrimaryAuthenticatorCanHaveMFA {
for _, typ := range *m.Authentication.SecondaryAuthenticators {
switch typ {
case model.AuthenticatorTypeTOTP:
totpAllowed = true
case model.AuthenticatorTypeOOBEmail:
oobotpEmailAllowed = true
case model.AuthenticatorTypeOOBSMS:
oobotpSMSAllowed = true
case model.AuthenticatorTypePassword:
passwordAllowed = true
}
}
}
for _, typ := range *m.Authentication.PrimaryAuthenticators {
switch typ {
case model.AuthenticatorTypePasskey:
passkeyAllowed = true
}
}
for _, a := range authenticators {
switch a.Kind {
case authenticator.KindPrimary:
switch a.Type {
case model.AuthenticatorTypePasskey:
aa := a
latestPrimaryPasskey = aa
}
case authenticator.KindSecondary:
switch a.Type {
case model.AuthenticatorTypeTOTP:
hasSecondaryTOTP = true
case model.AuthenticatorTypeOOBEmail:
hasSecondaryOOBOTPEmail = true
case model.AuthenticatorTypeOOBSMS:
hasSecondaryOOBOTPSMS = true
case model.AuthenticatorTypePassword:
aa := a
secondaryPassword = aa
}
}
}
showBiometric := false
for _, typ := range m.Authentication.Identities {
if typ == model.IdentityTypeBiometric && *m.Biometric.ListEnabled {
showBiometric = true
}
}
hasMFA := (hasSecondaryTOTP ||
hasSecondaryOOBOTPEmail ||
hasSecondaryOOBOTPSMS ||
secondaryPassword != nil)
showSecondaryTOTP := hasSecondaryTOTP || totpAllowed
showSecondaryOOBOTPEmail := hasSecondaryOOBOTPEmail || oobotpEmailAllowed
showSecondaryOOBOTPSMS := hasSecondaryOOBOTPSMS || oobotpSMSAllowed
showSecondaryPassword := secondaryPassword != nil || passwordAllowed
showPrimaryPasskey := latestPrimaryPasskey != nil || passkeyAllowed
showMFA := !m.Authentication.SecondaryAuthenticationMode.IsDisabled() &&
(showSecondaryTOTP ||
showSecondaryOOBOTPEmail ||
showSecondaryOOBOTPSMS ||
showSecondaryPassword)
viewModel := &SettingsViewModel{
Authenticators: authenticators,
HasDeviceTokens: hasDeviceTokens,
ListRecoveryCodesAllowed: !*m.Authentication.RecoveryCode.Disabled && m.Authentication.RecoveryCode.ListEnabled,
ShowBiometric: showBiometric,
HasSecondaryTOTP: hasSecondaryTOTP,
HasSecondaryOOBOTPEmail: hasSecondaryOOBOTPEmail,
HasSecondaryOOBOTPSMS: hasSecondaryOOBOTPSMS,
SecondaryPassword: secondaryPassword,
HasMFA: hasMFA,
ShowSecondaryTOTP: showSecondaryTOTP,
ShowSecondaryOOBOTPEmail: showSecondaryOOBOTPEmail,
ShowSecondaryOOBOTPSMS: showSecondaryOOBOTPSMS,
ShowSecondaryPassword: showSecondaryPassword,
ShowMFA: showMFA,
LatestPrimaryPasskey: latestPrimaryPasskey,
ShowPrimaryPasskey: showPrimaryPasskey,
}
return viewModel, nil
}