refactor auth pkg into libraries

This commit is contained in:
dhax 2017-10-31 19:10:09 +01:00
parent 521f081ba0
commit aaf0a0928d
26 changed files with 592 additions and 504 deletions

View file

@ -6,9 +6,9 @@ import (
"net/http"
"strconv"
"github.com/dhax/go-base/auth/pwdless"
"github.com/go-ozzo/ozzo-validation"
"github.com/dhax/go-base/auth"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
@ -20,11 +20,11 @@ var (
// AccountStore defines database operations for account management.
type AccountStore interface {
List(f auth.AccountFilter) ([]auth.Account, int, error)
Create(*auth.Account) error
Get(id int) (*auth.Account, error)
Update(*auth.Account) error
Delete(*auth.Account) error
List(f pwdless.AccountFilter) ([]pwdless.Account, int, error)
Create(*pwdless.Account) error
Get(id int) (*pwdless.Account, error)
Update(*pwdless.Account) error
Delete(*pwdless.Account) error
}
// AccountResource implements account management handler.
@ -70,7 +70,7 @@ func (rs *AccountResource) accountCtx(next http.Handler) http.Handler {
}
type accountRequest struct {
*auth.Account
*pwdless.Account
}
func (d *accountRequest) Bind(r *http.Request) error {
@ -78,20 +78,20 @@ func (d *accountRequest) Bind(r *http.Request) error {
}
type accountResponse struct {
*auth.Account
*pwdless.Account
}
func newAccountResponse(a *auth.Account) *accountResponse {
func newAccountResponse(a *pwdless.Account) *accountResponse {
resp := &accountResponse{Account: a}
return resp
}
type accountListResponse struct {
Accounts []auth.Account `json:"accounts"`
Count int `json:"count"`
Accounts []pwdless.Account `json:"accounts"`
Count int `json:"count"`
}
func newAccountListResponse(a []auth.Account, count int) *accountListResponse {
func newAccountListResponse(a []pwdless.Account, count int) *accountListResponse {
resp := &accountListResponse{
Accounts: a,
Count: count,
@ -100,7 +100,7 @@ func newAccountListResponse(a []auth.Account, count int) *accountListResponse {
}
func (rs *AccountResource) list(w http.ResponseWriter, r *http.Request) {
f := auth.NewAccountFilter(r.URL.Query())
f := pwdless.NewAccountFilter(r.URL.Query())
al, count, err := rs.Store.List(f)
if err != nil {
render.Render(w, r, ErrRender(err))
@ -129,12 +129,12 @@ func (rs *AccountResource) create(w http.ResponseWriter, r *http.Request) {
}
func (rs *AccountResource) get(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*auth.Account)
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
render.Respond(w, r, newAccountResponse(acc))
}
func (rs *AccountResource) update(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*auth.Account)
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
data := &accountRequest{Account: acc}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, ErrInvalidRequest(err))
@ -155,7 +155,7 @@ func (rs *AccountResource) update(w http.ResponseWriter, r *http.Request) {
}
func (rs *AccountResource) delete(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*auth.Account)
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
if err := rs.Store.Delete(acc); err != nil {
render.Render(w, r, ErrInvalidRequest(err))
return

View file

@ -9,7 +9,7 @@ import (
"github.com/go-chi/chi"
"github.com/go-pg/pg"
"github.com/dhax/go-base/auth"
"github.com/dhax/go-base/auth/authorize"
"github.com/dhax/go-base/database"
"github.com/dhax/go-base/logging"
)
@ -44,7 +44,7 @@ func NewAPI(db *pg.DB) (*API, error) {
// Router provides admin application routes.
func (a *API) Router() *chi.Mux {
r := chi.NewRouter()
r.Use(auth.RequiresRole(roleAdmin))
r.Use(authorize.RequiresRole(roleAdmin))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello Admin"))

View file

@ -10,7 +10,8 @@ import (
"github.com/dhax/go-base/api/admin"
"github.com/dhax/go-base/api/app"
"github.com/dhax/go-base/auth"
"github.com/dhax/go-base/auth/jwt"
"github.com/dhax/go-base/auth/pwdless"
"github.com/dhax/go-base/database"
"github.com/dhax/go-base/email"
"github.com/dhax/go-base/logging"
@ -37,7 +38,7 @@ func New() (*chi.Mux, error) {
}
authStore := database.NewAuthStore(db)
authResource, err := auth.NewResource(authStore, mailer)
authResource, err := pwdless.NewResource(authStore, mailer)
if err != nil {
logger.WithField("module", "auth").Error(err)
return nil, err
@ -70,8 +71,8 @@ func New() (*chi.Mux, error) {
r.Mount("/auth", authResource.Router())
r.Group(func(r chi.Router) {
r.Use(authResource.Token.Verifier())
r.Use(auth.Authenticator)
r.Use(authResource.TokenAuth.Verifier())
r.Use(jwt.Authenticator)
r.Mount("/admin", adminAPI.Router())
r.Mount("/api", appAPI.Router())
})

View file

@ -10,16 +10,17 @@ import (
"github.com/go-chi/render"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/dhax/go-base/auth"
"github.com/dhax/go-base/auth/jwt"
"github.com/dhax/go-base/auth/pwdless"
)
// AccountStore defines database operations for account.
type AccountStore interface {
Get(id int) (*auth.Account, error)
Update(*auth.Account) error
Delete(*auth.Account) error
UpdateToken(*auth.Token) error
DeleteToken(*auth.Token) error
Get(id int) (*pwdless.Account, error)
Update(*pwdless.Account) error
Delete(*pwdless.Account) error
UpdateToken(*jwt.Token) error
DeleteToken(*jwt.Token) error
}
// AccountResource implements account management handler.
@ -49,7 +50,7 @@ func (rs *AccountResource) router() *chi.Mux {
func (rs *AccountResource) accountCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims := auth.ClaimsFromCtx(r.Context())
claims := jwt.ClaimsFromCtx(r.Context())
log(r).WithField("account_id", claims.ID)
account, err := rs.Store.Get(claims.ID)
if err != nil {
@ -63,7 +64,7 @@ func (rs *AccountResource) accountCtx(next http.Handler) http.Handler {
}
type accountRequest struct {
*auth.Account
*pwdless.Account
// override protected data here, although not really necessary here
// as we limit updated database columns in store as well
ProtectedID int `json:"id"`
@ -78,21 +79,21 @@ func (d *accountRequest) Bind(r *http.Request) error {
}
type accountResponse struct {
*auth.Account
*pwdless.Account
}
func newAccountResponse(a *auth.Account) *accountResponse {
func newAccountResponse(a *pwdless.Account) *accountResponse {
resp := &accountResponse{Account: a}
return resp
}
func (rs *AccountResource) get(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*auth.Account)
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
render.Respond(w, r, newAccountResponse(acc))
}
func (rs *AccountResource) update(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*auth.Account)
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
data := &accountRequest{Account: acc}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, ErrInvalidRequest(err))
@ -113,7 +114,7 @@ func (rs *AccountResource) update(w http.ResponseWriter, r *http.Request) {
}
func (rs *AccountResource) delete(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*auth.Account)
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
if err := rs.Store.Delete(acc); err != nil {
render.Render(w, r, ErrRender(err))
return
@ -142,10 +143,10 @@ func (rs *AccountResource) updateToken(w http.ResponseWriter, r *http.Request) {
render.Render(w, r, ErrInvalidRequest(err))
return
}
acc := r.Context().Value(ctxAccount).(*auth.Account)
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
for _, t := range acc.Token {
if t.ID == id {
if err := rs.Store.UpdateToken(&auth.Token{
if err := rs.Store.UpdateToken(&jwt.Token{
ID: t.ID,
Identifier: data.Identifier,
}); err != nil {
@ -163,10 +164,10 @@ func (rs *AccountResource) deleteToken(w http.ResponseWriter, r *http.Request) {
render.Render(w, r, ErrBadRequest)
return
}
acc := r.Context().Value(ctxAccount).(*auth.Account)
acc := r.Context().Value(ctxAccount).(*pwdless.Account)
for _, t := range acc.Token {
if t.ID == id {
rs.Store.DeleteToken(&auth.Token{ID: t.ID})
rs.Store.DeleteToken(&jwt.Token{ID: t.ID})
}
}
render.Respond(w, r, http.NoBody)

View file

@ -4,7 +4,7 @@ import (
"context"
"net/http"
"github.com/dhax/go-base/auth"
"github.com/dhax/go-base/auth/jwt"
"github.com/dhax/go-base/models"
"github.com/go-chi/chi"
"github.com/go-chi/render"
@ -39,7 +39,7 @@ func (rs *ProfileResource) router() *chi.Mux {
func (rs *ProfileResource) profileCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims := auth.ClaimsFromCtx(r.Context())
claims := jwt.ClaimsFromCtx(r.Context())
p, err := rs.Store.Get(claims.ID)
if err != nil {
log(r).WithField("profileCtx", claims.Sub).Error(err)