2021-09-20 05:06:26 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-01-21 06:48:07 +00:00
|
|
|
"log"
|
2022-03-24 08:01:56 +00:00
|
|
|
"reflect"
|
2022-01-21 06:48:07 +00:00
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
2022-01-21 06:48:07 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
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
|
|
|
|
|
|
|
// SaveSessionInDB saves sessions generated for a given user with meta information
|
2022-01-21 06:48:07 +00:00
|
|
|
// Do not store token here as that could be security breach
|
2022-03-02 12:12:31 +00:00
|
|
|
func SaveSessionInDB(c *gin.Context, userId string) {
|
2022-01-21 06:48:07 +00:00
|
|
|
sessionData := models.Session{
|
2022-01-17 06:02:13 +00:00
|
|
|
UserID: userId,
|
|
|
|
UserAgent: GetUserAgent(c.Request),
|
|
|
|
IP: GetIP(c.Request),
|
|
|
|
}
|
|
|
|
|
2022-01-21 08:04:04 +00:00
|
|
|
err := db.Provider.AddSession(sessionData)
|
2022-01-21 06:48:07 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("=> error saving session in db:", err)
|
|
|
|
} else {
|
|
|
|
log.Println("=> session saved in db:", sessionData)
|
|
|
|
}
|
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
|
|
|
|
}
|