2021-09-20 05:06:26 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-03-24 08:01:56 +00:00
|
|
|
"reflect"
|
2022-08-08 20:13:37 +00:00
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2021-09-20 05:06:26 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// StringSliceContains checks if a string slice contains a particular string
|
2021-10-19 07:27:59 +00:00
|
|
|
func StringSliceContains(s []string, e string) bool {
|
2021-10-13 16:41:41 +00:00
|
|
|
for _, a := range s {
|
|
|
|
if a == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-01-17 06:02:13 +00:00
|
|
|
|
2022-01-31 06:05:24 +00:00
|
|
|
// RemoveDuplicateString removes duplicate strings from a string slice
|
|
|
|
func RemoveDuplicateString(strSlice []string) []string {
|
|
|
|
allKeys := make(map[string]bool)
|
|
|
|
list := []string{}
|
|
|
|
for _, item := range strSlice {
|
|
|
|
if _, value := allKeys[item]; !value {
|
|
|
|
allKeys[item] = true
|
|
|
|
list = append(list, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
2022-03-24 08:01:56 +00:00
|
|
|
|
|
|
|
// ConvertInterfaceToSlice to convert interface to slice interface
|
|
|
|
func ConvertInterfaceToSlice(slice interface{}) []interface{} {
|
|
|
|
s := reflect.ValueOf(slice)
|
|
|
|
if s.Kind() != reflect.Slice {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the distinction between nil and empty slice input
|
|
|
|
if s.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := make([]interface{}, s.Len())
|
|
|
|
|
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
|
|
ret[i] = s.Index(i).Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
2022-05-29 11:52:46 +00:00
|
|
|
|
|
|
|
// ConvertInterfaceToStringSlice to convert interface to string slice
|
|
|
|
func ConvertInterfaceToStringSlice(slice interface{}) []string {
|
|
|
|
data := slice.([]interface{})
|
|
|
|
var resSlice []string
|
|
|
|
|
|
|
|
for _, v := range data {
|
|
|
|
resSlice = append(resSlice, v.(string))
|
|
|
|
}
|
|
|
|
return resSlice
|
|
|
|
}
|
2022-08-08 20:13:37 +00:00
|
|
|
|
|
|
|
// GetOrganization to get organization object
|
|
|
|
func GetOrganization() map[string]interface{} {
|
|
|
|
orgLogo, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyOrganizationLogo)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
orgName, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyOrganizationName)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
organization := map[string]interface{}{
|
|
|
|
"name": orgName,
|
|
|
|
"logo": orgLogo,
|
|
|
|
}
|
|
|
|
|
|
|
|
return organization
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetForgotPasswordURL to get url for given token and hostname
|
2022-10-24 06:07:42 +00:00
|
|
|
func GetForgotPasswordURL(token, redirectURI string) string {
|
2023-05-12 11:09:02 +00:00
|
|
|
verificationURL := redirectURI + "?token=" + token
|
2022-08-08 20:13:37 +00:00
|
|
|
return verificationURL
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInviteVerificationURL to get url for invite email verification
|
|
|
|
func GetInviteVerificationURL(verificationURL, token, redirectURI string) string {
|
|
|
|
return verificationURL + "?token=" + token + "&redirect_uri=" + redirectURI
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEmailVerificationURL to get url for invite email verification
|
2023-05-02 13:09:10 +00:00
|
|
|
func GetEmailVerificationURL(token, hostname, redirectURI string) string {
|
|
|
|
return hostname + "/verify_email?token=" + token + "&redirect_uri=" + redirectURI
|
2022-08-08 20:13:37 +00:00
|
|
|
}
|
2023-12-07 14:03:59 +00:00
|
|
|
|
|
|
|
// FindDeletedValues find deleted values between original and updated one
|
|
|
|
func FindDeletedValues(original, updated []string) []string {
|
|
|
|
deletedValues := make([]string, 0)
|
|
|
|
|
|
|
|
// Create a map to store elements of the updated array for faster lookups
|
|
|
|
updatedMap := make(map[string]bool)
|
|
|
|
for _, value := range updated {
|
|
|
|
updatedMap[value] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for deleted values in the original array
|
|
|
|
for _, value := range original {
|
|
|
|
if _, found := updatedMap[value]; !found {
|
|
|
|
deletedValues = append(deletedValues, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return deletedValues
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFromArray will delete array from an array
|
|
|
|
func DeleteFromArray(original, valuesToDelete []string) []string {
|
|
|
|
result := make([]string, 0)
|
|
|
|
|
|
|
|
// Create a map to store values to delete for faster lookups
|
|
|
|
valuesToDeleteMap := make(map[string]bool)
|
|
|
|
for _, value := range valuesToDelete {
|
|
|
|
valuesToDeleteMap[value] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if each element in the original array should be deleted
|
|
|
|
for _, value := range original {
|
|
|
|
if _, found := valuesToDeleteMap[value]; !found {
|
|
|
|
result = append(result, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|