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/claims.go
2015-10-05 10:33:49 -07:00

44 lines
842 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 *jwt.Middleware
Config *jwt.Config = &jwt.Config{
Secret: os.Getenv("SECRET"),
Auth: models.DbAuthenticate,
Claims: claimsFunc,
}
)
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).Unix(),
"ref": "",
}, nil
}
func init() {
var err error
Middleware, err = jwt.New(Config)
if err != nil {
panic(err)
}
}