gohelpers

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 11 Imported by: 0

README

GoHelpers

العربية

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.

The Motif

I first come in touch with Go back in 2019, but for some work reason, I stopped working with it. Lately, I did a freshen up, and I see that a lot of funcs are repetitive in my code base. Thus, I decided to move them to a helpers package for reusability.

Installation
go get https://github.com/mbougarne/gohelpers
Usage

It's straightforward and easy to use, import the gohelpers package, and use its funcs. To work with dotenv you'll need to call the LoadDotEnvToOsEnv func on the main func, the default use case for this assumes that you have .env file in the root directory, if not you can pass the file name as an arg to that func. For more advanced and highly trustable dotenv in Go It's better to use godotenv.

Contributing

Contributions are more than welcome. As in The Motif, I'm new in the Go, if you see that you can help, by improving tests, code, implement new functionalities, the readme file. You're welcome.

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

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

func FlattenMap(in map[string]interface{}, want map[string]interface{})

Flatten a map

func GenerateJwtToken added in v0.2.0

func GenerateJwtToken(secretKey []byte, customClaims ...jwt.Claims) (string, error)

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

func GenerateSecretKey(secretKeyEnvName string, envfile ...bool) ([]byte, error)

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

func GetClaims(tokenString string, secretKey []byte) (interface{}, error)

Get claims from the token, and the used secret key to generate the token.

func GetEnvKey

func GetEnvKey(keyName string) string

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

func HashPassword(password string) (string, error)

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

func LoadDotEnvToOsEnv(envfile ...string) error

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

func RandomMd5String(input string) string

Generate random string in md5 format A good use of this func is to rename the uploaded files.

func RemoveFieldFromStruct

func RemoveFieldFromStruct(in any, field string) (map[string]interface{}, error)

Remove a field from struct of any type, and return a new map of keys of strings and values of any type.

func SliceStringToMapString

func SliceStringToMapString(slice []string) map[string]string

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

func SliceToMap(slice interface{}) (map[string]interface{}, error)

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

func StructToMap(in any) map[string]any

Convert a struct of any type to a map of keys of strings and values of any type.

func VerifyHashedPassword

func VerifyHashedPassword(plain, hashed string) bool

Verify if two passwords matched with bcrypt

func VerifyJwtToken added in v0.2.0

func VerifyJwtToken(tokenString string, secretKey []byte) (bool, error)

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.

Jump to

Keyboard shortcuts

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