2021-07-21 13:36:26 +05:30
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-08-04 15:55:13 +05:30
|
|
|
"strings"
|
2022-01-08 23:01:06 +05:30
|
|
|
"time"
|
2021-07-21 13:36:26 +05:30
|
|
|
|
2022-05-24 12:42:29 +05:30
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2021-07-28 15:22:11 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-02-28 21:26:49 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/crypto"
|
2021-07-23 21:57:44 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-05-30 09:19:55 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2022-05-30 11:54:16 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/parsers"
|
2022-01-23 01:24:41 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
2022-03-02 17:42:31 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2022-05-30 11:54:16 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/validators"
|
2021-07-21 13:36:26 +05:30
|
|
|
)
|
|
|
|
|
2022-01-17 11:32:13 +05:30
|
|
|
// ResetPasswordResolver is a resolver for reset password mutation
|
|
|
|
func ResetPasswordResolver(ctx context.Context, params model.ResetPasswordInput) (*model.Response, error) {
|
2021-07-21 13:36:26 +05:30
|
|
|
var res *model.Response
|
2022-05-24 12:42:29 +05:30
|
|
|
|
2022-03-02 17:42:31 +05:30
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to get GinContext: ", err)
|
2022-03-02 17:42:31 +05:30
|
|
|
return res, err
|
|
|
|
}
|
2022-05-30 09:19:55 +05:30
|
|
|
|
|
|
|
isBasicAuthDisabled, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisableBasicAuthentication)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("Error getting basic auth disabled: ", err)
|
|
|
|
isBasicAuthDisabled = true
|
|
|
|
}
|
|
|
|
if isBasicAuthDisabled {
|
2022-05-24 12:42:29 +05:30
|
|
|
log.Debug("Basic authentication is disabled")
|
2021-07-28 15:22:11 +05:30
|
|
|
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
|
|
|
|
}
|
2021-07-21 13:36:26 +05:30
|
|
|
|
2022-07-10 21:49:33 +05:30
|
|
|
verificationRequest, err := db.Provider.GetVerificationRequestByToken(ctx, params.Token)
|
2021-07-21 13:36:26 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to get verification request: ", err)
|
2021-07-21 13:36:26 +05:30
|
|
|
return res, fmt.Errorf(`invalid token`)
|
|
|
|
}
|
|
|
|
|
2021-08-10 22:20:24 +05:30
|
|
|
if params.Password != params.ConfirmPassword {
|
2022-05-24 12:42:29 +05:30
|
|
|
log.Debug("Passwords do not match")
|
2021-08-10 22:20:24 +05:30
|
|
|
return res, fmt.Errorf(`passwords don't match`)
|
|
|
|
}
|
|
|
|
|
2022-06-18 15:31:57 +05:30
|
|
|
if err := validators.IsValidPassword(params.Password); err != nil {
|
2022-05-24 12:42:29 +05:30
|
|
|
log.Debug("Invalid password")
|
2022-06-18 15:31:57 +05:30
|
|
|
return res, err
|
2022-03-17 15:35:07 +05:30
|
|
|
}
|
|
|
|
|
2021-07-21 13:36:26 +05:30
|
|
|
// verify if token exists in db
|
2022-05-30 11:54:16 +05:30
|
|
|
hostname := parsers.GetHost(gc)
|
2022-06-11 19:10:39 +05:30
|
|
|
claim, err := token.ParseJWTToken(params.Token)
|
2021-07-21 13:36:26 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to parse token: ", err)
|
2021-07-21 13:36:26 +05:30
|
|
|
return res, fmt.Errorf(`invalid token`)
|
|
|
|
}
|
|
|
|
|
2022-06-11 19:10:39 +05:30
|
|
|
if ok, err := token.ValidateJWTClaims(claim, hostname, verificationRequest.Nonce, verificationRequest.Email); !ok || err != nil {
|
|
|
|
log.Debug("Failed to validate jwt claims: ", err)
|
|
|
|
return res, fmt.Errorf(`invalid token`)
|
|
|
|
}
|
|
|
|
|
2022-05-24 12:42:29 +05:30
|
|
|
email := claim["sub"].(string)
|
|
|
|
log := log.WithFields(log.Fields{
|
|
|
|
"email": email,
|
|
|
|
})
|
2022-07-10 21:49:33 +05:30
|
|
|
user, err := db.Provider.GetUserByEmail(ctx, email)
|
2021-07-21 13:36:26 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to get user: ", err)
|
2021-07-21 13:36:26 +05:30
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-02-28 21:26:49 +05:30
|
|
|
password, _ := crypto.EncryptPassword(params.Password)
|
2021-12-22 15:31:45 +05:30
|
|
|
user.Password = &password
|
2021-07-21 13:36:26 +05:30
|
|
|
|
2021-12-22 10:51:12 +05:30
|
|
|
signupMethod := user.SignupMethods
|
2022-06-29 22:24:00 +05:30
|
|
|
if !strings.Contains(signupMethod, constants.AuthRecipeMethodBasicAuth) {
|
|
|
|
signupMethod = signupMethod + "," + constants.AuthRecipeMethodBasicAuth
|
2021-08-04 15:55:13 +05:30
|
|
|
}
|
2021-12-22 10:51:12 +05:30
|
|
|
user.SignupMethods = signupMethod
|
2021-08-04 15:55:13 +05:30
|
|
|
|
2022-01-08 23:01:06 +05:30
|
|
|
// helpful if user has not signed up with basic auth
|
|
|
|
if user.EmailVerifiedAt == nil {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
user.EmailVerifiedAt = &now
|
|
|
|
}
|
|
|
|
|
2021-07-21 13:36:26 +05:30
|
|
|
// delete from verification table
|
2022-07-10 21:49:33 +05:30
|
|
|
err = db.Provider.DeleteVerificationRequest(ctx, verificationRequest)
|
2022-05-24 12:42:29 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to delete verification request: ", err)
|
2022-05-24 12:42:29 +05:30
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-07-10 21:49:33 +05:30
|
|
|
_, err = db.Provider.UpdateUser(ctx, user)
|
2022-05-24 12:42:29 +05:30
|
|
|
if err != nil {
|
2022-05-25 12:30:22 +05:30
|
|
|
log.Debug("Failed to update user: ", err)
|
2022-05-24 12:42:29 +05:30
|
|
|
return res, err
|
|
|
|
}
|
2021-07-21 13:36:26 +05:30
|
|
|
|
|
|
|
res = &model.Response{
|
|
|
|
Message: `Password updated successfully.`,
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|