Documentation ¶
Overview ¶
A helper utilities that I use in my work in Go and want to share with the community. It has helpers to work with `dotEnv` in a basic and simple use, and `JWT` to generate, and validate tokens and refresh tokens.
Index ¶
- func CastJwtClaimsToCustomClaims(mapClaims, claims interface{}) error
- func FlattenMap(in map[string]interface{}, want map[string]interface{})
- func GenerateJwtToken(secretKey []byte, customClaims ...jwt.Claims) (string, error)
- func GenerateSecretKey(secretKeyEnvName string, envfile ...bool) ([]byte, error)
- func GetClaims(tokenString string, secretKey []byte) (interface{}, error)
- func GetEnvKey(keyName string) string
- func HashPassword(password string) (string, error)
- func InSlice(in interface{}, list interface{}) bool
- func LoadDotEnvToOsEnv(envfile ...string) error
- func RandomMd5String(input string) string
- func RemoveFieldFromStruct(in any, field string) (map[string]interface{}, error)
- func SliceStringToMapString(slice []string) map[string]string
- func SliceToMap(slice interface{}) (map[string]interface{}, error)
- func StructToMap(in any) map[string]any
- func VerifyHashedPassword(plain, hashed string) bool
- func VerifyJwtToken(tokenString string, secretKey []byte) (bool, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CastJwtClaimsToCustomClaims ¶ added in v1.0.0
func CastJwtClaimsToCustomClaims(mapClaims, claims interface{}) error
Cast jwt Claims to custom interfaces
func FlattenMap ¶
Flatten a map
func GenerateJwtToken ¶ added in v0.2.0
Generate JWT token, this func will generate an access token or a refresh token, based on the claims. If no custom claims sent as second arg, it will go with the `jwtCustomClaims` struct that contains: `username`, `uuid`, and `jwt.RegisteredClaims`
func GenerateSecretKey ¶ added in v1.0.0
This is a method that return a []byte secret key to use it in JWT. Passing a second arg as `true`, will make the func look for the `secretKeyEnvName` in the OS env vars. If that key is a custom one exists in a '.env` file, you need to load env vars to the OS first: `gohelpers.LoadDotEnvToOsEnv()`. Using the func without a second arg, will take the `secretKeyEnvName` as input to produce a slice of bytes: []byte(secretKeyEnvName)
func GetClaims ¶ added in v0.2.0
Get claims from the token, and the used secret key to generate the token.
func GetEnvKey ¶
Get environment variable from the system. Looking for a custom key in `.env` file, requires to load the file to the OS env vars first.
func HashPassword ¶
Hash password with the bcrypt
func InSlice ¶
func InSlice(in interface{}, list interface{}) bool
Check if a value of any type exist in a slice of any type
func LoadDotEnvToOsEnv ¶
Load all the vars in environment file to the OS environment. It will skip the already exists env variable in the OS, thus done, by checking the var in the file if it is exists in the OS vars or not. If We didn't provide a file, it will load the default one ".env" file in the root. This func SHOULD be called in the main func.
NOTE: this is a very simple and basic usage of loading env file data to the OS env vars. For advanced use, it's better to go with https://github.com/joho/godotenv
func RandomMd5String ¶
Generate random string in md5 format A good use of this func is to rename the uploaded files.
func RemoveFieldFromStruct ¶
Remove a field from struct of any type, and return a new map of keys of strings and values of any type.
func SliceStringToMapString ¶
Convert slice of strings to map of strings. It will take the first item in the slice as a map key, and the next item as the map value.
func SliceToMap ¶
Convert slice of these types: `[]string, []int, []float32, []float64, []byte, []bool, []rune` To a `map[string]interface{}`. It will return an error if the type is not supported, or the map if there's no error.
func StructToMap ¶
Convert a struct of any type to a map of keys of strings and values of any type.
func VerifyHashedPassword ¶
Verify if two passwords matched with bcrypt
func VerifyJwtToken ¶ added in v0.2.0
Verify the issued tokens, access and refresh. You can use the return error and check if the `access_token` is expired. Therefore, generate new one based on the refresh token validity. Intended to be used in middlewares.
Types ¶
This section is empty.