-
Notifications
You must be signed in to change notification settings - Fork 33
/
utils.go
192 lines (166 loc) · 7.28 KB
/
utils.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package passwordless
import (
"reflect"
"regexp"
"github.com/nyaruka/phonenumbers"
"github.com/supertokens/supertokens-golang/ingredients/emaildelivery"
"github.com/supertokens/supertokens-golang/ingredients/smsdelivery"
"github.com/supertokens/supertokens-golang/recipe/passwordless/emaildelivery/backwardCompatibilityService"
"github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels"
smsBackwardCompatibilityService "github.com/supertokens/supertokens-golang/recipe/passwordless/smsdelivery/backwardCompatibilityService"
"github.com/supertokens/supertokens-golang/supertokens"
)
func validateAndNormaliseUserInput(appInfo supertokens.NormalisedAppinfo, config plessmodels.TypeInput) plessmodels.TypeNormalisedInput {
if config.FlowType != "USER_INPUT_CODE" && config.FlowType != "MAGIC_LINK" && config.FlowType != "USER_INPUT_CODE_AND_MAGIC_LINK" {
panic("FlowType config must be provided and must be one of \"USER_INPUT_CODE\", \"MAGIC_LINK\" or \"USER_INPUT_CODE_AND_MAGIC_LINK\"")
}
contactMethodEnabledCounter := 0
if config.ContactMethodEmail.Enabled {
contactMethodEnabledCounter++
}
if config.ContactMethodPhone.Enabled {
contactMethodEnabledCounter++
}
if config.ContactMethodEmailOrPhone.Enabled {
contactMethodEnabledCounter++
}
if contactMethodEnabledCounter != 1 {
panic("Please enable only one of ContactMethodEmail, ContactMethodPhone or ContactMethodEmailOrPhone")
}
typeNormalisedInput := makeTypeNormalisedInput(appInfo, config)
if config.ContactMethodPhone.Enabled {
typeNormalisedInput.ContactMethodPhone.Enabled = true
if config.ContactMethodPhone.ValidatePhoneNumber != nil {
typeNormalisedInput.ContactMethodPhone.ValidatePhoneNumber = config.ContactMethodPhone.ValidatePhoneNumber
}
}
if config.ContactMethodEmail.Enabled {
typeNormalisedInput.ContactMethodEmail.Enabled = true
if config.ContactMethodEmail.ValidateEmailAddress != nil {
typeNormalisedInput.ContactMethodEmail.ValidateEmailAddress = config.ContactMethodEmail.ValidateEmailAddress
}
}
if config.ContactMethodEmailOrPhone.Enabled {
typeNormalisedInput.ContactMethodEmailOrPhone.Enabled = true
if config.ContactMethodEmailOrPhone.ValidateEmailAddress != nil {
typeNormalisedInput.ContactMethodEmailOrPhone.ValidateEmailAddress = config.ContactMethodEmailOrPhone.ValidateEmailAddress
}
if config.ContactMethodEmailOrPhone.ValidatePhoneNumber != nil {
typeNormalisedInput.ContactMethodEmailOrPhone.ValidatePhoneNumber = config.ContactMethodEmailOrPhone.ValidatePhoneNumber
}
}
// FlowType is initialized correctly in makeTypeNormalisedInput
// GetCustomUserInputCode is initialized correctly in makeTypeNormalisedInput
typeNormalisedInput.GetEmailDeliveryConfig = func() emaildelivery.TypeInputWithService {
createAndSendCustomEmail := DefaultCreateAndSendCustomEmail(appInfo)
emailService := backwardCompatibilityService.MakeBackwardCompatibilityService(appInfo, createAndSendCustomEmail)
if config.EmailDelivery != nil && config.EmailDelivery.Service != nil {
emailService = *config.EmailDelivery.Service
}
result := emaildelivery.TypeInputWithService{
Service: emailService,
}
if config.EmailDelivery != nil && config.EmailDelivery.Override != nil {
result.Override = config.EmailDelivery.Override
}
return result
}
typeNormalisedInput.GetSmsDeliveryConfig = func() smsdelivery.TypeInputWithService {
createAndSendCustomSms := DefaultCreateAndSendCustomTextMessage(appInfo)
smsService := smsBackwardCompatibilityService.MakeBackwardCompatibilityService(createAndSendCustomSms)
if config.SmsDelivery != nil && config.SmsDelivery.Service != nil {
smsService = *config.SmsDelivery.Service
}
result := smsdelivery.TypeInputWithService{
Service: smsService,
}
if config.SmsDelivery != nil && config.SmsDelivery.Override != nil {
result.Override = config.SmsDelivery.Override
}
return result
}
if config.Override != nil {
if config.Override.Functions != nil {
typeNormalisedInput.Override.Functions = config.Override.Functions
}
if config.Override.APIs != nil {
typeNormalisedInput.Override.APIs = config.Override.APIs
}
}
return typeNormalisedInput
}
func makeTypeNormalisedInput(appInfo supertokens.NormalisedAppinfo, inputConfig plessmodels.TypeInput) plessmodels.TypeNormalisedInput {
return plessmodels.TypeNormalisedInput{
FlowType: inputConfig.FlowType,
ContactMethodEmailOrPhone: plessmodels.ContactMethodEmailOrPhoneConfig{
Enabled: false,
ValidateEmailAddress: DefaultValidateEmailAddress,
ValidatePhoneNumber: DefaultValidatePhoneNumber,
},
ContactMethodPhone: plessmodels.ContactMethodPhoneConfig{
Enabled: false,
ValidatePhoneNumber: DefaultValidatePhoneNumber,
},
ContactMethodEmail: plessmodels.ContactMethodEmailConfig{
Enabled: false,
ValidateEmailAddress: DefaultValidateEmailAddress,
},
GetCustomUserInputCode: inputConfig.GetCustomUserInputCode,
Override: plessmodels.OverrideStruct{
Functions: func(originalImplementation plessmodels.RecipeInterface) plessmodels.RecipeInterface {
return originalImplementation
},
APIs: func(originalImplementation plessmodels.APIInterface) plessmodels.APIInterface {
return originalImplementation
},
},
}
}
func DefaultValidateEmailAddress(value interface{}, tenantId string) *string {
if reflect.TypeOf(value).Kind() != reflect.String {
msg := "Development bug: Please make sure the email field yields a string"
return &msg
}
check, err := regexp.Match(`^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`, []byte(value.(string)))
if err != nil || !check {
msg := "Email is invalid"
return &msg
}
return nil
}
func DefaultValidatePhoneNumber(value interface{}, tenantId string) *string {
if reflect.TypeOf(value).Kind() != reflect.String {
msg := "Development bug: Please make sure the email field yields a string"
return &msg
}
parsedPhoneNumber, err := phonenumbers.Parse(value.(string), "")
if err != nil {
msg := "Phone number is invalid"
return &msg
}
if !phonenumbers.IsValidNumber(parsedPhoneNumber) {
msg := "Phone number is invalid"
return &msg
}
return nil
}
// func defaultCreateAndSendCustomEmail(email string, userInputCode *string, urlWithLinkCode *string, codeLifetime uint64, preAuthSessionId string, userContext supertokens.UserContext) {
// // TODO:
// }
// func defaultCreateAndSendCustomTextMessage(phoneNumber string, userInputCode *string, urlWithLinkCode *string, codeLifetime uint64, preAuthSessionId string, userContext supertokens.UserContext) {
// // TODO:
// }