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