pulled out sqlite3 for json

This commit is contained in:
Stephen McQuay 2013-02-26 22:30:23 -08:00
parent 2bb6ac5e2f
commit 2fee9546f8
5 changed files with 43 additions and 42 deletions

View File

@ -37,6 +37,6 @@ Dependencies
Bootstrap DB
============
$ sqlite3 db.sqlite < init_db.sql
$ echo "[]" > passwds.json
.. _bootstrap: http://twitter.github.com/bootstrap/assets/bootstrap.zip

46
db.go
View File

@ -2,26 +2,44 @@ package main
import (
"code.google.com/p/go.crypto/bcrypt"
"github.com/kuroneko/gosqlite3"
"encoding/json"
"io/ioutil"
"log"
"sync"
)
var dbMutex sync.Mutex
func check_password(attempt string) (result bool) {
db, err := sqlite3.Open(*db_file)
defer db.Close()
func get_passes(filename string) (cur_passes []string, err error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
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 {
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 {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(attempt))
if err == nil {

View File

@ -15,12 +15,12 @@ func homeHandler(w http.ResponseWriter, req *http.Request) {
func loginHandler(w http.ResponseWriter, req *http.Request) {
pwAttempt := req.FormValue("passwd")
if check_password(pwAttempt) {
session, _ := store.Get(req, "creds")
session.Values["logged in"] = true
session.Save(req, w)
if check_password(*passes_file, pwAttempt) {
session, _ := store.Get(req, "creds")
session.Values["logged in"] = true
session.Save(req, w)
http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
return
}
T("login.html").Execute(w, map[string]interface{}{})
}

View File

@ -1 +0,0 @@
CREATE TABLE passes (id INTEGER PRIMARY KEY AUTOINCREMENT, hash STRING);

24
main.go
View File

@ -1,11 +1,9 @@
package main
import (
"code.google.com/p/go.crypto/bcrypt"
"flag"
"fmt"
"github.com/gorilla/sessions"
"github.com/kuroneko/gosqlite3"
"html/template"
"log"
"net/http"
@ -13,35 +11,21 @@ import (
var addr = flag.String("addr", ":8000", "address I'll listen on.")
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 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 store = sessions.NewCookieStore([]byte("hello world"))
var templates *template.Template
var db *sqlite3.Database
func main() {
flag.Parse()
if *add_pw != "" {
hpass, err := bcrypt.GenerateFromPassword([]byte(*add_pw), bcrypt.DefaultCost)
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)
}
add_password(*passes_file, *add_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 {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/login", loginHandler)