auth to middleware
This commit is contained in:
+3
-2
@@ -46,8 +46,9 @@ func main() {
|
||||
|
||||
sessionStore := sessions.NewFilesystemStore(opts.SessionStore, []byte(opts.StoreKey))
|
||||
srv := rest.Server{
|
||||
Version: revision,
|
||||
Store: dataStore,
|
||||
Version: revision,
|
||||
Store: dataStore,
|
||||
SessionStore: sessionStore,
|
||||
AuthGoogle: auth.NewGoogle(auth.Params{
|
||||
Cid: opts.GoogleCID,
|
||||
Csecret: opts.GoogleCSEC,
|
||||
|
||||
+1
-13
@@ -5,9 +5,9 @@ import (
|
||||
"crypto/sha1"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
|
||||
"github.com/umputun/remark/app/store"
|
||||
)
|
||||
|
||||
@@ -18,18 +18,6 @@ type Params struct {
|
||||
Admins []string
|
||||
}
|
||||
|
||||
type SessionStore struct {
|
||||
StorePath string
|
||||
StoreKey string
|
||||
|
||||
store *sessions.FilesystemStore
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (s *SessionStore) GetSession(name string) {
|
||||
|
||||
}
|
||||
|
||||
func randToken() string {
|
||||
b := make([]byte, 32)
|
||||
rand.Read(b)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -9,6 +10,9 @@ import (
|
||||
|
||||
"github.com/didip/tollbooth"
|
||||
"github.com/go-chi/render"
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/umputun/remark/app/store"
|
||||
)
|
||||
|
||||
var org = "Umputun"
|
||||
@@ -102,3 +106,48 @@ func Recoverer(next http.Handler) http.Handler {
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
|
||||
type contextKey string
|
||||
|
||||
// Auth adds auth from session and populate user info
|
||||
func Auth(sessionStore *sessions.FilesystemStore) func(http.Handler) http.Handler {
|
||||
f := func(h http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
session, err := sessionStore.Get(r, "remark")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
uinfoData, ok := session.Values["uinfo"]
|
||||
if !ok {
|
||||
http.Error(w, "login required", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
ctx = context.WithValue(ctx, contextKey("user"), uinfoData.(store.User))
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
h.ServeHTTP(w, r)
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// GetUserInfo extracts user, or and token from request's context
|
||||
func GetUserInfo(r *http.Request) (user store.User, err error) {
|
||||
|
||||
ctx := r.Context()
|
||||
if ctx == nil {
|
||||
return store.User{}, errors.New("user not defined")
|
||||
}
|
||||
|
||||
if u, ok := ctx.Value(contextKey("user")).(store.User); ok {
|
||||
return u, nil
|
||||
}
|
||||
|
||||
return store.User{}, errors.New("user can't be parsed")
|
||||
}
|
||||
|
||||
+9
-13
@@ -11,16 +11,18 @@ import (
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/render"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/umputun/remark/app/rest/auth"
|
||||
"github.com/umputun/remark/app/store"
|
||||
)
|
||||
|
||||
// Server is a rest access server
|
||||
type Server struct {
|
||||
Version string
|
||||
Store store.Interface
|
||||
AuthGoogle *auth.Google
|
||||
AuthGithub *auth.Github
|
||||
Version string
|
||||
Store store.Interface
|
||||
AuthGoogle *auth.Google
|
||||
AuthGithub *auth.Github
|
||||
SessionStore *sessions.FilesystemStore
|
||||
}
|
||||
|
||||
// Run the lister and request's router, activate rest server
|
||||
@@ -40,7 +42,7 @@ func (s *Server) Run() {
|
||||
router.Post("/comment", s.createCommentCtrl)
|
||||
router.Delete("/comment/{id}", s.deleteCommentCtrl)
|
||||
router.Get("/find", s.getURLComments)
|
||||
router.Get("/last/{max}", s.getLastComments)
|
||||
router.With(Auth(s.SessionStore)).Get("/last/{max}", s.getLastComments)
|
||||
router.Get("/id/{id}", s.getByID)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", router))
|
||||
@@ -119,18 +121,12 @@ func (s *Server) getLastComments(w http.ResponseWriter, r *http.Request) {
|
||||
max = 0
|
||||
}
|
||||
|
||||
session, err := s.AuthGoogle.Get(r, "remark")
|
||||
uinfoData, err := GetUserInfo(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
uinfoData, ok := session.Values["uinfo"]
|
||||
if !ok {
|
||||
http.Error(w, "login required", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
log.Printf("[DEBUG] user: %+v", uinfoData.(store.User))
|
||||
log.Printf("[DEBUG] user: %+v", uinfoData)
|
||||
|
||||
comments, err := s.Store.Last(store.Locator{}, max)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user