This commit adds the ability to provide a config file in different ways: The following options are given in the order in which they are checked: 1. A config file path passed by the -c parameter on the command line 2. A config file path passed by the HS_CONFIGFILE environment variable 3. A config.json file in the application root Should these fail we try to get the parameters for the config struct directly from environment variables or secret files as used in a container environment. As example the JwtKey can be supplied by either passing the value through: * An environment variable HS_CONFIG_JWTKEY which contains the value directly * An environment variable HS_CONFIG_JWTKEY_FILE which contains a path pointing to the Secret File
22 lines
490 B
Go
22 lines
490 B
Go
package auth
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.beisel.it/florian/hostname-service/config"
|
|
"github.com/dgrijalva/jwt-go"
|
|
)
|
|
|
|
func GenerateToken(username string) (string, error) {
|
|
expirationTime := time.Now().Add(1 * time.Hour)
|
|
claims := &jwt.StandardClaims{
|
|
Subject: username,
|
|
ExpiresAt: expirationTime.Unix(),
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
tokenString, err := token.SignedString(config.GlobalConfig.JwtKey)
|
|
|
|
return tokenString, err
|
|
}
|