aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2018-12-27 10:13:29 -0500
committerThibaut Horel <thibaut.horel@gmail.com>2018-12-27 10:13:29 -0500
commit0adcee59a57178dc7706a43a15aa5b90fe1cb1ee (patch)
tree565a730e9952c431452f0f83de88f7ba2608242b
parentb953a2c81fa8a66a58a9c0ec185da48ce853e8d9 (diff)
downloadid-0adcee59a57178dc7706a43a15aa5b90fe1cb1ee.tar.gz
WIP: add login template
-rw-r--r--main.go27
-rw-r--r--store.go35
-rw-r--r--templates/login.tmpl49
3 files changed, 91 insertions, 20 deletions
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 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title></title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
+<style>
+body{width:400px; margin: 0 auto; font-family: "Source Sans Pro"; font-size: 15px}
+form{margin-top: 10em}
+form hr{border: none; border-top: 1px solid #e6e6e6; margin: 1.5em 0}
+form h4{font-weight: 300; font-size: 19px}
+label {font-weight: bold}
+input {border: 1px solid #cccccc; border-radius: 0; height:1em; padding: 1em;
+ transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
+ -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s; }
+input:focus { border-color: #66afe9; outline: 0;
+ -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,0.075),0 0 8px rgba(102,175,233,0.6);
+ box-shadow: inset 0 1px 1px rgba(0,0,0,0.075),0 0 8px rgba(102,175,233,0.6);}
+button { height: 1em; padding: 1em; border: 1px solid #2780e3; border-radius:
+ 0; vertical-align: middle; text-align: center; background-color: #2780e3;
+line-height: 1em; color: white; font-size: inherit; font-family: inherit;}
+</style>
+ </head>
+ <body>
+
+<form action="/login" role="form" method="post">
+ <h4>Connexion</h4>
+ <hr>
+ <div>
+ <label for="name">Nom :</label>
+ <input type="text" id="name" name="username" placeholder="Prénom"/>
+ </div>
+ <div>
+ <label for="password">Mot de passe :</label>
+ <input type="password" id="password" name="password" placeholder="Mot de passe"/>
+ </div>
+ <div>
+ <button type="submit" name="login">Se Connecter</button>
+ </div>
+ <hr>
+</form>
+
+<div class="alert alert-danger" role="alert">
+ <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true">
+ </span>
+</div>
+
+ </body>
+</html>