pulled out sqlite3 for json
This commit is contained in:
parent
2bb6ac5e2f
commit
2fee9546f8
@ -37,6 +37,6 @@ Dependencies
|
|||||||
Bootstrap DB
|
Bootstrap DB
|
||||||
============
|
============
|
||||||
|
|
||||||
$ sqlite3 db.sqlite < init_db.sql
|
$ echo "[]" > passwds.json
|
||||||
|
|
||||||
.. _bootstrap: http://twitter.github.com/bootstrap/assets/bootstrap.zip
|
.. _bootstrap: http://twitter.github.com/bootstrap/assets/bootstrap.zip
|
||||||
|
46
db.go
46
db.go
@ -2,26 +2,44 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/go.crypto/bcrypt"
|
"code.google.com/p/go.crypto/bcrypt"
|
||||||
"github.com/kuroneko/gosqlite3"
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbMutex sync.Mutex
|
func get_passes(filename string) (cur_passes []string, err error) {
|
||||||
|
b, err := ioutil.ReadFile(filename)
|
||||||
func check_password(attempt string) (result bool) {
|
if err != nil {
|
||||||
db, err := sqlite3.Open(*db_file)
|
log.Fatal(err)
|
||||||
defer db.Close()
|
}
|
||||||
|
err = json.Unmarshal(b, &cur_passes)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func add_password(filename, new_pw string) (err error) {
|
||||||
|
cur_passes, err := get_passes(filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
hpass, err := bcrypt.GenerateFromPassword(
|
||||||
|
[]byte(*add_pw), bcrypt.DefaultCost)
|
||||||
|
cur_passes = append(cur_passes, string(hpass))
|
||||||
|
b, err := json.Marshal(cur_passes)
|
||||||
|
err = ioutil.WriteFile(filename, b, 0644)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func check_password(filename, attempt string) (result bool) {
|
||||||
|
hashes, err := get_passes(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
// this feels ultra hokey ... I guess I could take it from 2N to N by |= ...
|
|
||||||
hashes := []string{}
|
|
||||||
cmd := "SELECT hash FROM passes;"
|
|
||||||
db.Execute(cmd, func(s *sqlite3.Statement, values ...interface{}) {
|
|
||||||
cur_hash := values[0].(string)
|
|
||||||
hashes = append(hashes, cur_hash)
|
|
||||||
})
|
|
||||||
for _, hash := range hashes {
|
for _, hash := range hashes {
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(attempt))
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(attempt))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
12
handlers.go
12
handlers.go
@ -15,12 +15,12 @@ func homeHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
func loginHandler(w http.ResponseWriter, req *http.Request) {
|
func loginHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
pwAttempt := req.FormValue("passwd")
|
pwAttempt := req.FormValue("passwd")
|
||||||
if check_password(pwAttempt) {
|
if check_password(*passes_file, pwAttempt) {
|
||||||
session, _ := store.Get(req, "creds")
|
session, _ := store.Get(req, "creds")
|
||||||
session.Values["logged in"] = true
|
session.Values["logged in"] = true
|
||||||
session.Save(req, w)
|
session.Save(req, w)
|
||||||
http.Redirect(w, req, "/", http.StatusSeeOther)
|
http.Redirect(w, req, "/", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
T("login.html").Execute(w, map[string]interface{}{})
|
T("login.html").Execute(w, map[string]interface{}{})
|
||||||
}
|
}
|
||||||
|
@ -1 +0,0 @@
|
|||||||
CREATE TABLE passes (id INTEGER PRIMARY KEY AUTOINCREMENT, hash STRING);
|
|
24
main.go
24
main.go
@ -1,11 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/go.crypto/bcrypt"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
"github.com/kuroneko/gosqlite3"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -13,35 +11,21 @@ import (
|
|||||||
|
|
||||||
var addr = flag.String("addr", ":8000", "address I'll listen on.")
|
var addr = flag.String("addr", ":8000", "address I'll listen on.")
|
||||||
var static_files = flag.String("static", "./static", "location of static files")
|
var static_files = flag.String("static", "./static", "location of static files")
|
||||||
var db_file = flag.String("db", "./db.sqlite", "the database")
|
var passes_file = flag.String("passes", "passwds.json", "the password database")
|
||||||
var template_dir = flag.String("templates", "templates", "template dir")
|
var template_dir = flag.String("templates", "templates", "template dir")
|
||||||
var add_pw = flag.String("passwd", "", "add this pass to the db")
|
var add_pw = flag.String("passwd", "", "add this pass to the db")
|
||||||
var check_pw = flag.String("checkpw", "", "check if this pw is in db")
|
var check_pw = flag.String("checkpw", "", "check if this pw is in db")
|
||||||
|
|
||||||
var store = sessions.NewCookieStore([]byte("hello world"))
|
var store = sessions.NewCookieStore([]byte("hello world"))
|
||||||
var templates *template.Template
|
var templates *template.Template
|
||||||
var db *sqlite3.Database
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *add_pw != "" {
|
if *add_pw != "" {
|
||||||
hpass, err := bcrypt.GenerateFromPassword([]byte(*add_pw), bcrypt.DefaultCost)
|
add_password(*passes_file, *add_pw)
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
db, err = sqlite3.Open(*db_file)
|
|
||||||
defer db.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
cmd := fmt.Sprintf("INSERT INTO passes ('id', 'hash') VALUES (null, '%v')",
|
|
||||||
string(hpass))
|
|
||||||
_, err = db.Execute(cmd)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
} else if *check_pw != "" {
|
} else if *check_pw != "" {
|
||||||
fmt.Printf("valid password: %v\n", check_password(*check_pw))
|
fmt.Printf("valid password: %v\n",
|
||||||
|
check_password(*passes_file, *check_pw))
|
||||||
} else {
|
} else {
|
||||||
http.HandleFunc("/", homeHandler)
|
http.HandleFunc("/", homeHandler)
|
||||||
http.HandleFunc("/login", loginHandler)
|
http.HandleFunc("/login", loginHandler)
|
||||||
|
Loading…
Reference in New Issue
Block a user