utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BackOffWaitTime is the base value used for exponential backoff i.e 3**i where i is
	// number of failed login counts
	BackOffWaitTime = 3

	// OrganisationContextKey is used to add/retrieve the organisation ID in the context
	OrganisationContextKey = firebasetools.ContextKey("OrganisationID")

	// ProgramContextKey is used to add/retrieve the program ID that is passed around in the context
	ProgramContextKey = firebasetools.ContextKey("ProgramID")
)
View Source
const (
	// DefaultSaltLen is the length of generated salt for the user is 256
	DefaultSaltLen = 256

	// DefaultKeyLen is the length of encoded key in PBKDF2 function is 512
	DefaultKeyLen = 512
)
View Source
const FeedbackNotificationEmail = `` /* 3068-byte string literal not displayed */

FeedbackNotificationEmail if the supports feedback email template for feedback

Variables

View Source
var DefaultHashFunction = sha512.New

DefaultHashFunction ...

Functions

func CalculateAge

func CalculateAge(birthday time.Time) int

CalculateAge calculates the age of a community member

func CalculateDistance

func CalculateDistance(startPoint geodist.Coord, destination geodist.Coord) (float64, error)

CalculateDistance is used to calculate the distance between two points on the earth give the starting point coordinates and destination point coordinates

func CalculateNextAllowedLoginTime

func CalculateNextAllowedLoginTime(hour, minute, second time.Duration) time.Time

CalculateNextAllowedLoginTime will be used to calculate the next allowed login time in cases where a login failure occurs. If the failed login count is 1, then the next allowed login time will be after 3 seconds due to exponential backoff.

func CheckIfCurrentDBIsLocal

func CheckIfCurrentDBIsLocal() bool

CheckIfCurrentDBIsLocal checks whether the database used to run the test is a test/local database. If not, the setup exits

func ComparePIN

func ComparePIN(rawPwd string, salt string, encodedPwd string, options *Options) bool

ComparePIN takes four arguments, the raw password, its generated salt, the encoded password, and a pointer to the Options struct, and returns a boolean value determining whether the password is the correct one or not. Passing `nil` as the last argument resorts to default options.

func ConvertJSONStringToMap

func ConvertJSONStringToMap(jsonString string) (map[string]interface{}, error)

ConvertJSONStringToMap converts a json string to a map of string interface{}

func ConvertTimeToScalarDate

func ConvertTimeToScalarDate(t time.Time) (scalarutils.Date, error)

ConvertTimeToScalarDate converts a time to a scalar date

func EncryptPIN

func EncryptPIN(rawPwd string, options *Options) (string, string)

EncryptPIN takes two arguments, a raw pin, and a pointer to an Options struct. In order to use default options, pass `nil` as the second argument. It returns the generated salt and encoded key for the user.

func FormatFilterParamsHelper

func FormatFilterParamsHelper(a map[string]interface{}) map[string]interface{}

FormatFilterParamsHelper is a helper function that formats the filter params from getstream

func GenerateOTP

func GenerateOTP() (string, error)

GenerateOTP is used to generate a one time password

func GenerateTempPIN

func GenerateTempPIN(ctx context.Context) (string, error)

GenerateTempPIN generates a temporary One Time PIN for a user The PIN will have 4 digits formatted as a string

func GetLoggedInUserID

func GetLoggedInUserID(ctx context.Context) *string

func GetValueFromContext

func GetValueFromContext(ctx context.Context, key firebasetools.ContextKey) (string, error)

GetValueFromContext retrieves a value from the supplied context

func InterfaceToFloat64

func InterfaceToFloat64(n interface{}) float64

InterfaceToFloat64 converts an interface to float64

func InterfaceToInt

func InterfaceToInt(n interface{}) int

InterfaceToInt converts an interface to an int

func InterfaceToString

func InterfaceToString(n interface{}) string

InterfaceToString converts an interface to a string

func LoadScreeningTools

func LoadScreeningTools() ([]*dto.ScreeningToolInput, error)

LoadScreeningTools is a helper function that loads the given screening tools when a program is created TODO: temporary solution. this should be removed after implementing screening tool creation in the frontend

func NextAllowedLoginTime

func NextAllowedLoginTime(loginAttempts int) time.Time

NextAllowedLoginTime calculates the user's next allowed login time. This implements an exponential backoff that will help to mitigate brute force attacks. Each incorrect PIN entry will add a delay, hence increasing the time to the next allowed login

func ParseChoice

func ParseChoice[T any](choices []T, choice string) (*T, error)

ParseChoice validates a choice is in the range of a choices

func ParseFacilitiesFromCSV

func ParseFacilitiesFromCSV(path string) ([]*dto.FacilityInput, error)

ParseFacilitiesFromCSV maps the values of the csv file to the Facilities object

func ReadCSVFile

func ReadCSVFile(path string) (*csv.Reader, error)

ReadCSVFile reads the content of a csv file

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile reads the content of a file and return a slice of bytes

func ShuffleSecurityQuestionResponses

func ShuffleSecurityQuestionResponses(slice []*domain.SecurityQuestionResponse) ([]*domain.SecurityQuestionResponse, error)

ShuffleSecurityQuestionResponses is used to randomize users security questions. It uses crypto/rand package which makes use of a shuffling algorithm that provides higher level of randomness and security.

func TruncateMatrixUserID

func TruncateMatrixUserID(userID string) string

TruncateMatrixUserID truncates the Matrix's user ID and returns the name

func ValidatePIN

func ValidatePIN(pin string) error

ValidatePIN is used to check for the validity of the PIN provided.

func ValidatePINDigits

func ValidatePINDigits(pin string) error

ValidatePINDigits validates user pin to ensure a PIN only contains digits

func ValidatePINLength

func ValidatePINLength(pin string) error

ValidatePINLength ...

Types

type Options

type Options struct {
	SaltLen      int
	Iterations   int
	KeyLen       int
	HashFunction func() hash.Hash
}

Options is a struct for custom values of salt length, number of iterations, the encoded key's length, and the hash function being used. If set to `nil`, default options are used: &Options{ 256, 10000, 512, "sha512" }

Jump to

Keyboard shortcuts

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