From 0adcee59a57178dc7706a43a15aa5b90fe1cb1ee Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Thu, 27 Dec 2018 10:13:29 -0500 Subject: WIP: add login template --- main.go | 27 ++++++++++++++++++--------- store.go | 35 ++++++++++++++++++++++++----------- templates/login.tmpl | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 templates/login.tmpl diff --git a/main.go b/main.go index 2c79316..5317ffa 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,16 @@ package main import ( + "html/template" "log" "net/http" + "strconv" "time" ) type App struct { Store + Template *template.Template } func logMux(handler http.Handler) http.Handler { @@ -18,15 +21,15 @@ func logMux(handler http.Handler) http.Handler { }) } -func (app *App) rootHandler(w http.ResponseWriter, r *http.Request) { +func (app *App) validateHandler(w http.ResponseWriter, r *http.Request) { c, err := r.Cookie("id") //log.Println(r.Header.Get("X-Original-URI")) //log.Println(r.Host) if err != nil { w.WriteHeader(http.StatusUnauthorized) } else { - if s, ok := app.Get(c.Value); ok { - w.Header().Set("X-Remote-User", s.UserId) + if s, ok := app.GetSession(c.Value); ok { + w.Header().Set("X-Remote-User", strconv.FormatInt(s.UserId, 10)) w.WriteHeader(http.StatusOK) } else { log.Println("Session does not exist:", c.Value) @@ -42,20 +45,26 @@ func (app *App) loginHandler(w http.ResponseWriter, r *http.Request) { } else { username := r.Form.Get("username") password := r.Form.Get("password") - row := app.Query("SELECT id FROM users WHERE user_name = ?", username) - var id int64 - if err := row.rowScan(&id); err != nil { - panic(err) + u, ok := app.GetUser(username) + if ok && (u.Password == password) { + + } else { + } } + } else if r.Method == http.MethodGet { + app.Template.ExecuteTemplate(w, "login.tmpl", nil) } + } func main() { //log.SetFlags(log.LstdFlags) store := NewPgStore() - app := &App{store} - http.HandleFunc("/", app.rootHandler) + template := template.Must(template.New("").ParseGlob("templates/*.tmpl")) + app := &App{store, template} + http.HandleFunc("/validate", app.validateHandler) + http.HandleFunc("/login", app.loginHandler) if err := http.ListenAndServe(":8080", logMux(http.DefaultServeMux)); err != nil { panic(err) } diff --git a/store.go b/store.go index 55d808b..1db6411 100644 --- a/store.go +++ b/store.go @@ -15,32 +15,45 @@ type Session struct { type User struct { Id int64 UserName string + Password string } type Store interface { GetSession(id string) (*Session, bool) - GetUser(id int64) (*User, bool) + GetUser(name string) (*User, bool) } type PgStore struct { *sql.DB - cache map[string]*Session + sessionCache map[string]*Session } -func NewPgStore() *PgSessionStore { +func NewPgStore() *PgStore { db, err := sql.Open("postgres", "postgres://auth_master:pass@localhost/authdb") if err != nil { log.Panic(err) } - return &PgSessionStore{db, make(map[string]*Session)} + return &PgStore{db, make(map[string]*Session)} } -func (store *PgSessionStore) Get(id string) (*Session, bool) { - s, ok := store.cache[id] - if !ok { - row := store.QueryRow("SELECT id, user_id FROM session WHERE id = ?", id) - var s Session - row.Scan(&s. +func (store *PgStore) GetSession(id string) (*Session, bool) { + s, ok := store.sessionCache[id] + if ok { + return s, true } - return s, ok + s = new(Session) + row := store.QueryRow("SELECT id, user_id FROM session WHERE id = $1", id) + if err := row.Scan(s.Id, s.UserId); err != nil { + return nil, false + } + return s, true +} + +func (store *PgStore) GetUser(name string) (*User, bool) { + u := new(User) + row := store.QueryRow("SELECT id, user_name, password FROM user WHERE user_name = $1", name) + if err := row.Scan(u.Id, u.UserName, u.Password); err != nil { + return nil, false + } + return u, true } diff --git a/templates/login.tmpl b/templates/login.tmpl new file mode 100644 index 0000000..bb97d2e --- /dev/null +++ b/templates/login.tmpl @@ -0,0 +1,49 @@ + + + + + + + + + + +
+

Connexion

+
+
+ + +
+
+ + +
+
+ +
+
+
+ + + + + -- cgit v1.2.3-70-g09d2