This repository has been archived on 2025-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
bactdb/auth/auth.go
Matthew Dillon 8e9d6380e7 24 hr token
2015-11-17 06:31:43 -07:00

48 lines
984 B
Go

package auth
import (
"os"
"time"
"github.com/thermokarst/bactdb/Godeps/_workspace/src/github.com/thermokarst/jwt"
"github.com/thermokarst/bactdb/models"
)
var (
// Middleware is for JWT
Middleware *jwt.Middleware
// Config handles JWT middleware configuration
Config = &jwt.Config{
Secret: os.Getenv("SECRET"),
Auth: models.DbAuthenticate,
Claims: claimsFunc,
IdentityField: "username",
VerifyField: "password",
}
)
func claimsFunc(email string) (map[string]interface{}, error) {
// TODO: use helper
currentTime := time.Now()
user, err := models.DbGetUserByEmail(email)
if err != nil {
return nil, err
}
return map[string]interface{}{
"name": user.Name,
"iss": "bactdb",
"sub": user.ID,
"role": user.Role,
"iat": currentTime.Unix(),
"exp": currentTime.Add(time.Minute * 60 * 24).Unix(),
"ref": "",
}, nil
}
func init() {
var err error
Middleware, err = jwt.New(Config)
if err != nil {
panic(err)
}
}