utils

package
v0.0.0-...-247d2f1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AdminName                    = os.Getenv("ADMIN_USERNAME")
	AdminPassword                = os.Getenv("ADMIN_PASSWORD")
	DBUrl                        = os.Getenv("DB_SERVER")
	DBUser                       = os.Getenv("DB_USER")
	DBPassword                   = os.Getenv("DB_PASSWORD")
	JWTExpiryDuration            = getEnvAsInt("JWT_EXPIRY_MINS", 1440)
	OAuthJWTExpDuration          = getEnvAsInt("OAUTH_JWT_EXP_MINS", 5)
	OAuthJwtSecret               = os.Getenv("OAUTH_SECRET")
	DexEnabled                   = getEnvAsBool("DEX_ENABLED", false)
	DexCallBackURL               = os.Getenv("DEX_OAUTH_CALLBACK_URL")
	DexClientID                  = os.Getenv("DEX_OAUTH_CLIENT_ID")
	DexClientSecret              = os.Getenv("DEX_OAUTH_CLIENT_SECRET")
	DexOIDCIssuer                = os.Getenv("OIDC_ISSUER")
	EnableInternalTls            = getEnvAsBool("ENABLE_INTERNAL_TLS", false)
	TlsCertPath                  = os.Getenv("TLS_CERT_PATH")
	TlSKeyPath                   = os.Getenv("TLS_KEY_PATH")
	CaCertPath                   = os.Getenv("CA_CERT_TLS_PATH")
	RestPort                     = os.Getenv("REST_PORT")
	GrpcPort                     = os.Getenv("GRPC_PORT")
	DBName                       = "auth"
	UserCollection               = "users"
	ProjectCollection            = "project"
	AuthConfigCollection         = "auth-config"
	RevokedTokenCollection       = "revoked-token"
	ApiTokenCollection           = "api-token"
	UsernameField                = "username"
	ExpiresAtField               = "expires_at"
	PasswordEncryptionCost       = 8
	DefaultLitmusGqlGrpcEndpoint = "localhost"
	DefaultLitmusGqlGrpcPort     = ":8000"
)
View Source
var ErrorDescriptions = map[AppError]string{
	ErrServerError:                   "The authorization server encountered an unexpected condition that prevented it from fulfilling the request",
	ErrInvalidCredentials:            "Invalid Credentials",
	ErrInvalidRequest:                "The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed",
	ErrUnauthorized:                  "The user does not have requested authorization to access this resource",
	ErrUserExists:                    "This username is already assigned to another user",
	ErrStrictPasswordPolicyViolation: "Please ensure the password is atleast 8 characters long and atmost 16 characters long and has atleast 1 digit, 1 lowercase alphabet, 1 uppercase alphabet and 1 special character",
	ErrStrictUsernamePolicyViolation: "The username should be atleast 3 characters long and atmost 16 characters long.",
	ErrEmptyProjectName:              "Project name can't be empty",
	ErrInvalidRole:                   "Role is invalid",
	ErrProjectNotFound:               "This project does not exist",
	ErrInvalidEmail:                  "Email address is invalid",
	ErrPasswordNotUpdated:            "Please update your default password",
	ErrOldPassword:                   "old and new passwords can't be same",
}

ErrorDescriptions holds detailed error description for every AppError

ErrorStatusCodes holds the http status codes for every AppError

Functions

func CreateCollection

func CreateCollection(collectionName string, db *mongo.Database) error

CreateCollection creates a new mongo collection if it does not exist

func CreateIndex

func CreateIndex(collectionName string, field string, db *mongo.Database) error

CreateIndex creates a unique index for the given field in the collectionName

func CreateTTLIndex

func CreateTTLIndex(collectionName string, db *mongo.Database) error

CreateTTLIndex creates a TTL index for the given field in the collectionName

func GenerateOAuthJWT

func GenerateOAuthJWT() (string, error)

func GetProjectGRPCSvcClient

func GetProjectGRPCSvcClient(conn *grpc.ClientConn) (grpc2.ProjectClient, *grpc.ClientConn)

GetProjectGRPCSvcClient returns an RPC client for Project service

func GetTlsConfig

func GetTlsConfig() *tls.Config

func MongoConnection

func MongoConnection() (*mongo.Client, error)

MongoConnection creates a connection to the mongo

func ProjectInitializer

func ProjectInitializer(context context.Context, client grpc2.ProjectClient, projectID string, role string) error

ProjectInitializer initializes a new project with default hub and image registry

func RandomString

func RandomString(n int) (string, error)

RandomString generates random strings, can be used to create ids

func SanitizeString

func SanitizeString(input string) string

SanitizeString trims the string input

func ValidateOAuthJWT

func ValidateOAuthJWT(tokenString string) (bool, error)

func ValidateStrictPassword

func ValidateStrictPassword(input string) error

ValidateStrictPassword represents and checks for the following patterns: - Input is at least 8 characters long and at most 16 characters long - Input contains at least one special character of these @$!%*?_&# - Input contains at least one digit - Input contains at least one uppercase alphabet - Input contains at least one lowercase alphabet

func ValidateStrictUsername

func ValidateStrictUsername(username string) error

Types

type AppError

type AppError error

AppError defines general error's throughout the system

var (
	ErrInvalidCredentials            AppError = errors.New("invalid_credentials")
	ErrServerError                   AppError = errors.New("server_error")
	ErrInvalidRequest                AppError = errors.New("invalid_request")
	ErrStrictPasswordPolicyViolation AppError = errors.New("password_policy_violation")
	ErrStrictUsernamePolicyViolation AppError = errors.New("username_policy_violation")
	ErrUnauthorized                  AppError = errors.New("unauthorized")
	ErrUserExists                    AppError = errors.New("user_exists")
	ErrUserNotFound                  AppError = errors.New("user does not exist")
	ErrProjectNotFound               AppError = errors.New("project does not exist")
	ErrWrongPassword                 AppError = errors.New("password doesn't match")
	ErrUpdatingAdmin                 AppError = errors.New("cannot remove admin")
	ErrUserDeactivated               AppError = errors.New("your account has been deactivated")
	ErrUserAlreadyDeactivated        AppError = errors.New("user already deactivated")
	ErrEmptyProjectName              AppError = errors.New("invalid project name")
	ErrInvalidRole                   AppError = errors.New("invalid role")
	ErrInvalidEmail                  AppError = errors.New("invalid email")
	ErrPasswordNotUpdated            AppError = errors.New("default password not updated")
	ErrOldPassword                   AppError = errors.New("old and new passwords can't be same")
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL