2021-12-30 04:31:51 +00:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-22 19:54:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/cookie"
|
2022-02-28 15:56:49 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/crypto"
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2021-12-30 04:31:51 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// AdminLoginResolver is a resolver for admin login mutation
|
2022-01-09 13:10:30 +00:00
|
|
|
func AdminLoginResolver(ctx context.Context, params model.AdminLoginInput) (*model.Response, error) {
|
2021-12-30 04:31:51 +00:00
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
2022-01-09 13:10:30 +00:00
|
|
|
var res *model.Response
|
2021-12-31 08:58:00 +00:00
|
|
|
|
2021-12-30 04:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2021-12-31 08:58:00 +00:00
|
|
|
|
2022-02-28 02:25:01 +00:00
|
|
|
adminSecret := envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret)
|
2022-01-17 06:02:13 +00:00
|
|
|
if params.AdminSecret != adminSecret {
|
2021-12-31 08:58:00 +00:00
|
|
|
return res, fmt.Errorf(`invalid admin secret`)
|
2021-12-30 04:31:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 15:56:49 +00:00
|
|
|
hashedKey, err := crypto.EncryptPassword(adminSecret)
|
2021-12-31 08:58:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2022-01-22 19:54:41 +00:00
|
|
|
cookie.SetAdminCookie(gc, hashedKey)
|
2021-12-30 04:31:51 +00:00
|
|
|
|
2022-01-09 13:10:30 +00:00
|
|
|
res = &model.Response{
|
2022-01-09 12:32:16 +00:00
|
|
|
Message: "admin logged in successfully",
|
2021-12-30 04:31:51 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|