-
Notifications
You must be signed in to change notification settings - Fork 33
/
recipe.go
212 lines (191 loc) · 8.07 KB
/
recipe.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* 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 emailpassword
import (
defaultErrors "errors"
"net/http"
"github.com/supertokens/supertokens-golang/ingredients/emaildelivery"
"github.com/supertokens/supertokens-golang/recipe/emailpassword/api"
"github.com/supertokens/supertokens-golang/recipe/emailpassword/constants"
"github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels"
"github.com/supertokens/supertokens-golang/recipe/emailpassword/errors"
"github.com/supertokens/supertokens-golang/recipe/emailverification"
"github.com/supertokens/supertokens-golang/supertokens"
)
const RECIPE_ID = "emailpassword"
type Recipe struct {
RecipeModule supertokens.RecipeModule
Config epmodels.TypeNormalisedInput
RecipeImpl epmodels.RecipeInterface
APIImpl epmodels.APIInterface
EmailVerificationRecipe emailverification.Recipe
EmailDelivery emaildelivery.Ingredient
}
var singletonInstance *Recipe
func MakeRecipe(recipeId string, appInfo supertokens.NormalisedAppinfo, config *epmodels.TypeInput, emailVerificationInstance *emailverification.Recipe, emailDeliveryIngredient *emaildelivery.Ingredient, onGeneralError func(err error, req *http.Request, res http.ResponseWriter)) (Recipe, error) {
r := &Recipe{}
r.RecipeModule = supertokens.MakeRecipeModule(recipeId, appInfo, r.handleAPIRequest, r.getAllCORSHeaders, r.getAPIsHandled, r.handleError, onGeneralError)
querierInstance, err := supertokens.GetNewQuerierInstanceOrThrowError(recipeId)
if err != nil {
return Recipe{}, err
}
verifiedConfig := validateAndNormaliseUserInput(r, appInfo, config)
r.Config = verifiedConfig
r.APIImpl = verifiedConfig.Override.APIs(api.MakeAPIImplementation())
r.RecipeImpl = verifiedConfig.Override.Functions(MakeRecipeImplementation(*querierInstance))
if emailDeliveryIngredient != nil {
r.EmailDelivery = *emailDeliveryIngredient
} else {
r.EmailDelivery = emaildelivery.MakeIngredient(verifiedConfig.GetEmailDeliveryConfig(r.RecipeImpl))
}
if emailVerificationInstance == nil {
emailVerificationRecipe, err := emailverification.MakeRecipe(recipeId, appInfo, verifiedConfig.EmailVerificationFeature, &r.EmailDelivery, onGeneralError)
if err != nil {
return Recipe{}, err
}
r.EmailVerificationRecipe = emailVerificationRecipe
} else {
r.EmailVerificationRecipe = *emailVerificationInstance
}
return *r, nil
}
func recipeInit(config *epmodels.TypeInput) supertokens.Recipe {
return func(appInfo supertokens.NormalisedAppinfo, onGeneralError func(err error, req *http.Request, res http.ResponseWriter)) (*supertokens.RecipeModule, error) {
if singletonInstance == nil {
recipe, err := MakeRecipe(RECIPE_ID, appInfo, config, nil, nil, onGeneralError)
if err != nil {
return nil, err
}
singletonInstance = &recipe
return &singletonInstance.RecipeModule, nil
}
return nil, defaultErrors.New("emailpassword recipe has already been initialised. Please check your code for bugs.")
}
}
func getRecipeInstanceOrThrowError() (*Recipe, error) {
if singletonInstance != nil {
return singletonInstance, nil
}
return nil, defaultErrors.New("initialisation not done. Did you forget to call the init function?")
}
// implement RecipeModule
func (r *Recipe) getAPIsHandled() ([]supertokens.APIHandled, error) {
signUpAPI, err := supertokens.NewNormalisedURLPath(constants.SignUpAPI)
if err != nil {
return nil, err
}
signInAPI, err := supertokens.NewNormalisedURLPath(constants.SignInAPI)
if err != nil {
return nil, err
}
generatePasswordResetTokenAPI, err := supertokens.NewNormalisedURLPath(constants.GeneratePasswordResetTokenAPI)
if err != nil {
return nil, err
}
passwordResetAPI, err := supertokens.NewNormalisedURLPath(constants.PasswordResetAPI)
if err != nil {
return nil, err
}
signupEmailExistsAPI, err := supertokens.NewNormalisedURLPath(constants.SignupEmailExistsAPI)
if err != nil {
return nil, err
}
emailverificationAPIhandled, err := r.EmailVerificationRecipe.RecipeModule.GetAPIsHandled()
if err != nil {
return nil, err
}
return append([]supertokens.APIHandled{{
Method: http.MethodPost,
PathWithoutAPIBasePath: signUpAPI,
ID: constants.SignUpAPI,
Disabled: r.APIImpl.SignUpPOST == nil,
}, {
Method: http.MethodPost,
PathWithoutAPIBasePath: signInAPI,
ID: constants.SignInAPI,
Disabled: r.APIImpl.SignInPOST == nil,
}, {
Method: http.MethodPost,
PathWithoutAPIBasePath: generatePasswordResetTokenAPI,
ID: constants.GeneratePasswordResetTokenAPI,
Disabled: r.APIImpl.GeneratePasswordResetTokenPOST == nil,
}, {
Method: http.MethodPost,
PathWithoutAPIBasePath: passwordResetAPI,
ID: constants.PasswordResetAPI,
Disabled: r.APIImpl.PasswordResetPOST == nil,
}, {
Method: http.MethodGet,
PathWithoutAPIBasePath: signupEmailExistsAPI,
ID: constants.SignupEmailExistsAPI,
Disabled: r.APIImpl.EmailExistsGET == nil,
}}, emailverificationAPIhandled...), nil
}
func (r *Recipe) handleAPIRequest(id string, req *http.Request, res http.ResponseWriter, theirHandler http.HandlerFunc, path supertokens.NormalisedURLPath, method string) error {
options := epmodels.APIOptions{
Config: r.Config,
OtherHandler: theirHandler,
RecipeID: r.RecipeModule.GetRecipeID(),
RecipeImplementation: r.RecipeImpl,
EmailVerificationRecipeImplementation: r.EmailVerificationRecipe.RecipeImpl,
Req: req,
Res: res,
EmailDelivery: r.EmailDelivery,
}
if id == constants.SignUpAPI {
return api.SignUpAPI(r.APIImpl, options)
} else if id == constants.SignInAPI {
return api.SignInAPI(r.APIImpl, options)
} else if id == constants.GeneratePasswordResetTokenAPI {
return api.GeneratePasswordResetToken(r.APIImpl, options)
} else if id == constants.PasswordResetAPI {
return api.PasswordReset(r.APIImpl, options)
} else if id == constants.SignupEmailExistsAPI {
return api.EmailExists(r.APIImpl, options)
}
return r.EmailVerificationRecipe.RecipeModule.HandleAPIRequest(id, req, res, theirHandler, path, method)
}
func (r *Recipe) getAllCORSHeaders() []string {
return r.EmailVerificationRecipe.RecipeModule.GetAllCORSHeaders()
}
func (r *Recipe) handleError(err error, req *http.Request, res http.ResponseWriter) (bool, error) {
if defaultErrors.As(err, &errors.FieldError{}) {
errs := err.(errors.FieldError)
return true, supertokens.Send200Response(res, map[string]interface{}{
"status": "FIELD_ERROR",
"formFields": errs.Payload,
})
}
return r.EmailVerificationRecipe.RecipeModule.HandleError(err, req, res)
}
func (r *Recipe) getEmailForUserId(userID string, userContext supertokens.UserContext) (string, error) {
userInfo, err := (*r.RecipeImpl.GetUserByID)(userID, userContext)
if err != nil {
return "", err
}
if userInfo == nil {
return "", defaultErrors.New("unknown User ID provided")
}
return userInfo.Email, nil
}
func ResetForTest() {
singletonInstance = nil
PasswordResetEmailSentForTest = false
PasswordResetDataForTest = struct {
User epmodels.User
PasswordResetURLWithToken string
UserContext supertokens.UserContext
}{}
}