moves claims into models
This commit is contained in:
parent
38722c9da5
commit
f1c2249744
6 changed files with 57 additions and 35 deletions
|
|
@ -144,7 +144,12 @@ func (rs *Resource) token(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
access, refresh := rs.Token.GenTokenPair(acc, token)
|
access, refresh, err := rs.Token.GenTokenPair(acc.Claims(), token.Claims())
|
||||||
|
if err != nil {
|
||||||
|
log(r).Error(err)
|
||||||
|
render.Render(w, r, ErrInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
acc.LastLogin = time.Now()
|
acc.LastLogin = time.Now()
|
||||||
if err := rs.store.UpdateAccount(acc); err != nil {
|
if err := rs.store.UpdateAccount(acc); err != nil {
|
||||||
|
|
@ -183,7 +188,13 @@ func (rs *Resource) refresh(w http.ResponseWriter, r *http.Request) {
|
||||||
token.Expiry = time.Now().Add(time.Minute * rs.Token.jwtRefreshExpiry)
|
token.Expiry = time.Now().Add(time.Minute * rs.Token.jwtRefreshExpiry)
|
||||||
token.UpdatedAt = time.Now()
|
token.UpdatedAt = time.Now()
|
||||||
|
|
||||||
access, refresh := rs.Token.GenTokenPair(acc, token)
|
access, refresh, err := rs.Token.GenTokenPair(acc.Claims(), token.Claims())
|
||||||
|
if err != nil {
|
||||||
|
log(r).Error(err)
|
||||||
|
render.Render(w, r, ErrInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err := rs.store.SaveRefreshToken(token); err != nil {
|
if err := rs.store.SaveRefreshToken(token); err != nil {
|
||||||
log(r).Error(err)
|
log(r).Error(err)
|
||||||
render.Render(w, r, ErrInternalServerError)
|
render.Render(w, r, ErrInternalServerError)
|
||||||
|
|
|
||||||
52
auth/jwt.go
52
auth/jwt.go
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dhax/go-base/models"
|
|
||||||
"github.com/go-chi/jwtauth"
|
"github.com/go-chi/jwtauth"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
@ -44,38 +43,33 @@ func (a *TokenAuth) Verifier() func(http.Handler) http.Handler {
|
||||||
return jwtauth.Verifier(a.JwtAuth)
|
return jwtauth.Verifier(a.JwtAuth)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenTokenPair returns both an access token and a refresh token for provided account.
|
// GenTokenPair returns both an access token and a refresh token.
|
||||||
func (a *TokenAuth) GenTokenPair(u *models.Account, tok *models.Token) (string, string) {
|
func (a *TokenAuth) GenTokenPair(ca jwtauth.Claims, cr jwtauth.Claims) (string, string, error) {
|
||||||
access := a.CreateJWT(u)
|
access, err := a.CreateJWT(ca)
|
||||||
refresh := a.CreateRefreshJWT(tok)
|
if err != nil {
|
||||||
return access, refresh
|
return "", "", err
|
||||||
|
}
|
||||||
|
refresh, err := a.CreateRefreshJWT(cr)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
return access, refresh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateJWT returns an access token for provided account.
|
// CreateJWT returns an access token for provided account claims.
|
||||||
func (a *TokenAuth) CreateJWT(acc *models.Account) string {
|
func (a *TokenAuth) CreateJWT(c jwtauth.Claims) (string, error) {
|
||||||
claims := jwtauth.Claims{
|
c.SetIssuedNow()
|
||||||
"id": acc.ID,
|
c.SetExpiryIn(a.jwtExpiry * time.Minute)
|
||||||
"sub": acc.Name,
|
_, tokenString, err := a.JwtAuth.Encode(c)
|
||||||
"roles": acc.Roles,
|
return tokenString, err
|
||||||
}
|
|
||||||
claims.SetIssuedNow()
|
|
||||||
claims.SetExpiryIn(a.jwtExpiry * time.Minute)
|
|
||||||
|
|
||||||
_, tokenString, _ := a.JwtAuth.Encode(claims)
|
|
||||||
return tokenString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateRefreshJWT returns a refresh token for provided account.
|
// CreateRefreshJWT returns a refresh token for provided token Claims.
|
||||||
func (a *TokenAuth) CreateRefreshJWT(tok *models.Token) string {
|
func (a *TokenAuth) CreateRefreshJWT(c jwtauth.Claims) (string, error) {
|
||||||
claims := jwtauth.Claims{
|
c.SetIssuedNow()
|
||||||
"id": tok.ID,
|
c.SetExpiryIn(time.Minute * a.jwtRefreshExpiry)
|
||||||
"token": tok.Token,
|
_, tokenString, err := a.JwtAuth.Encode(c)
|
||||||
}
|
return tokenString, err
|
||||||
claims.SetIssuedNow()
|
|
||||||
claims.SetExpiryIn(time.Minute * a.jwtRefreshExpiry)
|
|
||||||
|
|
||||||
_, tokenString, _ := a.JwtAuth.Encode(claims)
|
|
||||||
return tokenString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseClaims(c jwtauth.Claims) (AppClaims, bool) {
|
func parseClaims(c jwtauth.Claims) (AppClaims, bool) {
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ type ContentLoginToken struct {
|
||||||
|
|
||||||
// LoginToken creates and sends a login token email with provided template content.
|
// LoginToken creates and sends a login token email with provided template content.
|
||||||
func (m *Mailer) LoginToken(name, address string, content ContentLoginToken) error {
|
func (m *Mailer) LoginToken(name, address string, content ContentLoginToken) error {
|
||||||
msg := &Mail{
|
msg := &message{
|
||||||
from: NewEmail(m.fromName, m.from),
|
from: NewEmail(m.fromName, m.from),
|
||||||
to: NewEmail(name, address),
|
to: NewEmail(name, address),
|
||||||
subject: "Login Token",
|
subject: "Login Token",
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ func NewMailer() (*Mailer, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send parses the corrsponding template and sends the mail via smtp.
|
// Send parses the corrsponding template and sends the mail via smtp.
|
||||||
func (m *Mailer) Send(mail *Mail) error {
|
func (m *Mailer) Send(mail *message) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
if err := m.templates.ExecuteTemplate(buf, mail.template, mail.content); err != nil {
|
if err := m.templates.ExecuteTemplate(buf, mail.template, mail.content); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -95,8 +95,8 @@ func (m *Mailer) Send(mail *Mail) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mail struct holds all parts of a specific email.
|
// message struct holds all parts of a specific email message.
|
||||||
type Mail struct {
|
type message struct {
|
||||||
from *Email
|
from *Email
|
||||||
to *Email
|
to *Email
|
||||||
subject string
|
subject string
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/jwtauth"
|
||||||
validation "github.com/go-ozzo/ozzo-validation"
|
validation "github.com/go-ozzo/ozzo-validation"
|
||||||
"github.com/go-ozzo/ozzo-validation/is"
|
"github.com/go-ozzo/ozzo-validation/is"
|
||||||
"github.com/go-pg/pg/orm"
|
"github.com/go-pg/pg/orm"
|
||||||
|
|
@ -70,6 +71,14 @@ func (a *Account) CanLogin() bool {
|
||||||
return a.Active
|
return a.Active
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Account) Claims() jwtauth.Claims {
|
||||||
|
return jwtauth.Claims{
|
||||||
|
"id": a.ID,
|
||||||
|
"sub": a.Name,
|
||||||
|
"roles": a.Roles,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// AccountFilter provides pagination and filtering options on accounts.
|
// AccountFilter provides pagination and filtering options on accounts.
|
||||||
type AccountFilter struct {
|
type AccountFilter struct {
|
||||||
orm.Pager
|
orm.Pager
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package models
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/jwtauth"
|
||||||
"github.com/go-pg/pg/orm"
|
"github.com/go-pg/pg/orm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -34,3 +35,10 @@ func (t *Token) BeforeUpdate(db orm.DB) error {
|
||||||
t.UpdatedAt = time.Now()
|
t.UpdatedAt = time.Now()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Token) Claims() jwtauth.Claims {
|
||||||
|
return jwtauth.Claims{
|
||||||
|
"id": t.ID,
|
||||||
|
"token": t.Token,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue