2021-12-31 11:33:37 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-01-17 07:50:32 +00:00
|
|
|
"errors"
|
2021-12-31 11:33:37 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-22 19:54:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/cookie"
|
2021-12-31 11:33:37 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2021-12-31 11:33:37 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-01-31 06:05:24 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/oauth"
|
|
|
|
"github.com/authorizerdev/authorizer/server/sessionstore"
|
2022-01-22 19:54:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
2021-12-31 11:33:37 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
|
|
|
)
|
|
|
|
|
2022-01-17 07:42:46 +00:00
|
|
|
// UpdateEnvResolver is a resolver for update config mutation
|
2022-01-17 06:02:13 +00:00
|
|
|
// This is admin only mutation
|
2022-01-17 07:42:46 +00:00
|
|
|
func UpdateEnvResolver(ctx context.Context, params model.UpdateEnvInput) (*model.Response, error) {
|
2021-12-31 11:33:37 +00:00
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
|
|
|
var res *model.Response
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-01-22 19:54:41 +00:00
|
|
|
if !token.IsSuperAdmin(gc) {
|
2021-12-31 11:33:37 +00:00
|
|
|
return res, fmt.Errorf("unauthorized")
|
|
|
|
}
|
|
|
|
|
|
|
|
var data map[string]interface{}
|
|
|
|
byteData, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf("error marshalling params: %t", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(byteData, &data)
|
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf("error un-marshalling params: %t", err)
|
|
|
|
}
|
|
|
|
|
2022-01-25 07:36:52 +00:00
|
|
|
// in case of admin secret change update the cookie with new hash
|
|
|
|
if params.AdminSecret != nil {
|
|
|
|
if params.OldAdminSecret == nil {
|
|
|
|
return res, errors.New("admin secret and old admin secret are required for secret change")
|
|
|
|
}
|
|
|
|
|
|
|
|
if *params.OldAdminSecret != envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret) {
|
|
|
|
return res, errors.New("old admin secret is not correct")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(*params.AdminSecret) < 6 {
|
|
|
|
err = fmt.Errorf("admin secret must be at least 6 characters")
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
updatedData := envstore.EnvInMemoryStoreObj.GetEnvStoreClone()
|
2021-12-31 11:33:37 +00:00
|
|
|
for key, value := range data {
|
|
|
|
if value != nil {
|
|
|
|
fieldType := reflect.TypeOf(value).String()
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
if fieldType == "string" {
|
|
|
|
updatedData.StringEnv[key] = value.(string)
|
2021-12-31 11:33:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
if fieldType == "bool" {
|
|
|
|
updatedData.BoolEnv[key] = value.(bool)
|
|
|
|
}
|
2021-12-31 11:33:37 +00:00
|
|
|
if fieldType == "[]interface {}" {
|
|
|
|
stringArr := []string{}
|
|
|
|
for _, v := range value.([]interface{}) {
|
|
|
|
stringArr = append(stringArr, v.(string))
|
|
|
|
}
|
2022-01-20 11:22:37 +00:00
|
|
|
updatedData.SliceEnv[key] = stringArr
|
2021-12-31 11:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle derivative cases like disabling email verification & magic login
|
|
|
|
// in case SMTP is off but env is set to true
|
2022-01-20 11:22:37 +00:00
|
|
|
if updatedData.StringEnv[constants.EnvKeySmtpHost] == "" || updatedData.StringEnv[constants.EnvKeySmtpUsername] == "" || updatedData.StringEnv[constants.EnvKeySmtpPassword] == "" || updatedData.StringEnv[constants.EnvKeySenderEmail] == "" && updatedData.StringEnv[constants.EnvKeySmtpPort] == "" {
|
|
|
|
if !updatedData.BoolEnv[constants.EnvKeyDisableEmailVerification] {
|
|
|
|
updatedData.BoolEnv[constants.EnvKeyDisableEmailVerification] = true
|
2021-12-31 11:33:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
if !updatedData.BoolEnv[constants.EnvKeyDisableMagicLinkLogin] {
|
|
|
|
updatedData.BoolEnv[constants.EnvKeyDisableMagicLinkLogin] = true
|
2021-12-31 11:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 05:59:03 +00:00
|
|
|
|
|
|
|
// check the roles change
|
|
|
|
if len(params.Roles) > 0 {
|
|
|
|
if len(params.DefaultRoles) > 0 {
|
|
|
|
// should be subset of roles
|
|
|
|
for _, role := range params.DefaultRoles {
|
|
|
|
if !utils.StringSliceContains(params.Roles, role) {
|
|
|
|
return res, fmt.Errorf("default role %s is not in roles", role)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(params.ProtectedRoles) > 0 {
|
|
|
|
for _, role := range params.ProtectedRoles {
|
|
|
|
if utils.StringSliceContains(params.Roles, role) || utils.StringSliceContains(params.DefaultRoles, role) {
|
|
|
|
return res, fmt.Errorf("protected role %s found roles or default roles", role)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
// Update local store
|
|
|
|
envstore.EnvInMemoryStoreObj.UpdateEnvStore(updatedData)
|
2022-01-31 06:05:24 +00:00
|
|
|
sessionstore.InitSession()
|
|
|
|
oauth.InitOAuth()
|
2021-12-31 11:33:37 +00:00
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
// Fetch the current db store and update it
|
2022-01-21 08:04:04 +00:00
|
|
|
env, err := db.Provider.GetEnv()
|
2021-12-31 11:33:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.AdminSecret != nil {
|
2022-01-20 11:22:37 +00:00
|
|
|
hashedKey, err := utils.EncryptPassword(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret))
|
2021-12-31 11:33:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2022-01-22 19:54:41 +00:00
|
|
|
cookie.SetAdminCookie(gc, hashedKey)
|
2021-12-31 11:33:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 07:36:52 +00:00
|
|
|
encryptedConfig, err := utils.EncryptEnvData(updatedData)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
env.EnvData = encryptedConfig
|
2022-01-21 08:04:04 +00:00
|
|
|
_, err = db.Provider.UpdateEnv(env)
|
2021-12-31 11:33:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error updating config:", err)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res = &model.Response{
|
|
|
|
Message: "configurations updated successfully",
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|