Merge branch 'master' of ssh://gitlab.quimbo.fr:5022/odwrtw/canape-sql

Conflicts:
	sql/0001_initial.up.sql
This commit is contained in:
Nicolas Duhamel 2016-02-27 21:41:36 +01:00
commit ea6d40746b
3 changed files with 19 additions and 8 deletions

View File

@ -13,10 +13,10 @@ import (
)
const (
peeper = "polp"
key = "plop"
cookie = "auth"
cost = 10
peeper = "polp"
key = "plop"
cookieName = "auth"
cost = 10
username = "plop"
password = "ploppwd"
@ -55,7 +55,7 @@ func getBackend() *Backend {
}
func login(w http.ResponseWriter, r *http.Request) {
a := New(getBackend(), peeper, cookie, key, cost)
a := New(getBackend(), peeper, cookieName, key, cost)
err := a.Login(w, r, username, password)
if err != nil {
fmt.Fprintf(w, "%s", err)
@ -66,7 +66,7 @@ func login(w http.ResponseWriter, r *http.Request) {
}
func logout(w http.ResponseWriter, r *http.Request) {
a := New(getBackend(), peeper, cookie, key, cost)
a := New(getBackend(), peeper, cookieName, key, cost)
err := a.Logout(w, r)
if err != nil {
fmt.Fprintf(w, "%s", err)
@ -76,7 +76,7 @@ func logout(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func check(w http.ResponseWriter, r *http.Request) {
a := New(getBackend(), peeper, cookie, key, cost)
a := New(getBackend(), peeper, cookieName, key, cost)
u, err := a.CurrentUser(w, r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)

View File

@ -20,6 +20,9 @@ const (
deleteTokenQuery = `DELETE FROM tokens WHERE user_id=$1 AND value=$2;`
)
// ErrUnknownUser returned web a user does'nt exist
var ErrUnknownUser = fmt.Errorf("users: user does'nt exist")
// User represents an user
type User struct {
sqly.BaseModel
@ -38,6 +41,9 @@ func Get(q sqlx.Queryer, name string) (*User, error) {
u := &User{}
err := q.QueryRowx(getUserQuery, name).StructScan(u)
if err != nil {
if err.Error() == "sql: no rows in result set" {
return nil, ErrUnknownUser
}
return nil, err
}
return u, nil
@ -125,3 +131,8 @@ func (u *User) DeleteToken(ex *sqlx.DB, value string) error {
}
return nil
}
// GetHash implements auth.User interface
func (u *User) GetHash() string {
return u.Hash
}

View File

@ -86,7 +86,7 @@ func TestUser(t *testing.T) {
if err == nil {
t.Fatal("We expect an error here, the user didn't exist anymore")
}
if err.Error() != "sql: no rows in result set" {
if err != ErrUnknownUser {
t.Fatalf("Unexpected error: %q", err)
}
if u != nil {