Files
authorizer/server/resolvers/env.go

176 lines
6.2 KiB
Go
Raw Normal View History

package resolvers
import (
"context"
"fmt"
2022-05-31 13:11:54 +05:30
"strings"
2022-05-24 12:42:29 +05:30
log "github.com/sirupsen/logrus"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/graph/model"
2022-05-30 09:19:55 +05:30
"github.com/authorizerdev/authorizer/server/memorystore"
2022-07-15 22:11:08 +05:30
"github.com/authorizerdev/authorizer/server/refs"
"github.com/authorizerdev/authorizer/server/token"
"github.com/authorizerdev/authorizer/server/utils"
)
2022-01-17 13:12:46 +05:30
// EnvResolver is a resolver for config query
2022-01-17 11:32:13 +05:30
// This is admin only query
2022-01-17 13:12:46 +05:30
func EnvResolver(ctx context.Context) (*model.Env, error) {
2022-05-31 13:11:54 +05:30
res := &model.Env{}
2022-05-24 12:42:29 +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)
return res, err
}
if !token.IsSuperAdmin(gc) {
2022-05-24 12:42:29 +05:30
log.Debug("Not logged in as super admin.")
return res, fmt.Errorf("unauthorized")
}
2022-01-17 11:32:13 +05:30
// get clone of store
2022-05-30 09:19:55 +05:30
store, err := memorystore.Provider.GetEnvStore()
if err != nil {
log.Debug("Failed to get env store: ", err)
return res, err
}
2022-05-31 13:11:54 +05:30
if val, ok := store[constants.EnvKeyAccessTokenExpiryTime]; ok {
2022-07-15 22:11:08 +05:30
res.AccessTokenExpiryTime = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyAdminSecret]; ok {
2022-07-15 22:11:08 +05:30
res.AdminSecret = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyClientID]; ok {
res.ClientID = val.(string)
}
if val, ok := store[constants.EnvKeyClientSecret]; ok {
res.ClientSecret = val.(string)
}
if val, ok := store[constants.EnvKeyDatabaseURL]; ok {
2022-07-15 22:11:08 +05:30
res.DatabaseURL = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyDatabaseName]; ok {
2022-07-15 22:11:08 +05:30
res.DatabaseName = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyDatabaseType]; ok {
2022-07-15 22:11:08 +05:30
res.DatabaseType = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyDatabaseUsername]; ok {
2022-07-15 22:11:08 +05:30
res.DatabaseUsername = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyDatabasePassword]; ok {
2022-07-15 22:11:08 +05:30
res.DatabasePassword = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyDatabaseHost]; ok {
2022-07-15 22:11:08 +05:30
res.DatabaseHost = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyDatabasePort]; ok {
2022-07-15 22:11:08 +05:30
res.DatabasePort = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyCustomAccessTokenScript]; ok {
2022-07-15 22:11:08 +05:30
res.CustomAccessTokenScript = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeySmtpHost]; ok {
2022-07-15 22:11:08 +05:30
res.SMTPHost = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeySmtpPort]; ok {
2022-07-15 22:11:08 +05:30
res.SMTPPort = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeySmtpUsername]; ok {
2022-07-15 22:11:08 +05:30
res.SMTPUsername = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeySmtpPassword]; ok {
2022-07-15 22:11:08 +05:30
res.SMTPPassword = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeySenderEmail]; ok {
2022-07-15 22:11:08 +05:30
res.SenderEmail = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyJwtType]; ok {
2022-07-15 22:11:08 +05:30
res.JwtType = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyJwtSecret]; ok {
2022-07-15 22:11:08 +05:30
res.JwtSecret = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyJwtRoleClaim]; ok {
2022-07-15 22:11:08 +05:30
res.JwtRoleClaim = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyJwtPublicKey]; ok {
2022-07-15 22:11:08 +05:30
res.JwtPublicKey = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyJwtPrivateKey]; ok {
2022-07-15 22:11:08 +05:30
res.JwtPrivateKey = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyAppURL]; ok {
2022-07-15 22:11:08 +05:30
res.AppURL = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyRedisURL]; ok {
2022-07-15 22:11:08 +05:30
res.RedisURL = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyResetPasswordURL]; ok {
2022-07-15 22:11:08 +05:30
res.ResetPasswordURL = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyGoogleClientID]; ok {
2022-07-15 22:11:08 +05:30
res.GoogleClientID = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyGoogleClientSecret]; ok {
2022-07-15 22:11:08 +05:30
res.GoogleClientSecret = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyFacebookClientID]; ok {
2022-07-15 22:11:08 +05:30
res.FacebookClientID = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyFacebookClientSecret]; ok {
2022-07-15 22:11:08 +05:30
res.FacebookClientSecret = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyGithubClientID]; ok {
2022-07-15 22:11:08 +05:30
res.GithubClientID = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyGithubClientSecret]; ok {
2022-07-15 22:11:08 +05:30
res.GithubClientSecret = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
2022-06-06 22:08:32 +05:30
if val, ok := store[constants.EnvKeyLinkedInClientID]; ok {
2022-07-15 22:11:08 +05:30
res.LinkedinClientID = refs.NewStringRef(val.(string))
2022-06-06 22:08:32 +05:30
}
if val, ok := store[constants.EnvKeyLinkedInClientSecret]; ok {
2022-07-15 22:11:08 +05:30
res.LinkedinClientSecret = refs.NewStringRef(val.(string))
2022-06-06 22:08:32 +05:30
}
2022-06-12 14:49:48 +05:30
if val, ok := store[constants.EnvKeyAppleClientID]; ok {
2022-07-15 22:11:08 +05:30
res.AppleClientID = refs.NewStringRef(val.(string))
2022-06-12 14:49:48 +05:30
}
if val, ok := store[constants.EnvKeyAppleClientSecret]; ok {
2022-07-15 22:11:08 +05:30
res.AppleClientSecret = refs.NewStringRef(val.(string))
2022-06-12 14:49:48 +05:30
}
2022-05-31 13:11:54 +05:30
if val, ok := store[constants.EnvKeyOrganizationName]; ok {
2022-07-15 22:11:08 +05:30
res.OrganizationName = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
if val, ok := store[constants.EnvKeyOrganizationLogo]; ok {
2022-07-15 22:11:08 +05:30
res.OrganizationLogo = refs.NewStringRef(val.(string))
2022-05-31 13:11:54 +05:30
}
2022-05-30 09:19:55 +05:30
// string slice vars
2022-05-31 13:11:54 +05:30
res.AllowedOrigins = strings.Split(store[constants.EnvKeyAllowedOrigins].(string), ",")
res.Roles = strings.Split(store[constants.EnvKeyRoles].(string), ",")
res.DefaultRoles = strings.Split(store[constants.EnvKeyDefaultRoles].(string), ",")
2022-06-07 07:30:01 +05:30
// since protected role is optional default split gives array with empty string
protectedRoles := strings.Split(store[constants.EnvKeyProtectedRoles].(string), ",")
res.ProtectedRoles = []string{}
for _, role := range protectedRoles {
if strings.Trim(role, " ") != "" {
res.ProtectedRoles = append(res.ProtectedRoles, strings.Trim(role, " "))
}
}
2022-05-30 09:19:55 +05:30
// bool vars
2022-05-31 13:11:54 +05:30
res.DisableEmailVerification = store[constants.EnvKeyDisableEmailVerification].(bool)
res.DisableBasicAuthentication = store[constants.EnvKeyDisableBasicAuthentication].(bool)
res.DisableMagicLinkLogin = store[constants.EnvKeyDisableMagicLinkLogin].(bool)
res.DisableLoginPage = store[constants.EnvKeyDisableLoginPage].(bool)
res.DisableSignUp = store[constants.EnvKeyDisableSignUp].(bool)
res.DisableStrongPassword = store[constants.EnvKeyDisableStrongPassword].(bool)
2022-03-25 20:29:00 +05:30
return res, nil
}