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"))