2021-12-31 14:28:00 +05:30
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-23 01:24:41 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/cookie"
|
2022-02-28 21:26:49 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/crypto"
|
2022-01-17 11:32:13 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2021-12-31 14:28:00 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-01-23 01:24:41 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
2021-12-31 14:28:00 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
|
|
|
)
|
|
|
|
|
2022-01-17 11:32:13 +05:30
|
|
|
// AdminSessionResolver is a resolver for admin session query
|
|
|
|
func AdminSessionResolver(ctx context.Context) (*model.Response, error) {
|
2021-12-31 14:28:00 +05:30
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
2022-01-09 18:40:30 +05:30
|
|
|
var res *model.Response
|
2021-12-31 14:28:00 +05:30
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-01-23 01:24:41 +05:30
|
|
|
if !token.IsSuperAdmin(gc) {
|
2021-12-31 14:28:00 +05:30
|
|
|
return res, fmt.Errorf("unauthorized")
|
|
|
|
}
|
|
|
|
|
2022-02-28 21:26:49 +05:30
|
|
|
hashedKey, err := crypto.EncryptPassword(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyAdminSecret))
|
2021-12-31 14:28:00 +05:30
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2022-01-23 01:24:41 +05:30
|
|
|
cookie.SetAdminCookie(gc, hashedKey)
|
2021-12-31 14:28:00 +05:30
|
|
|
|
2022-01-09 18:40:30 +05:30
|
|
|
res = &model.Response{
|
2022-01-09 18:02:16 +05:30
|
|
|
Message: "admin logged in successfully",
|
2021-12-31 14:28:00 +05:30
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|