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

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

View File

@ -20,6 +20,9 @@ const (
deleteTokenQuery = `DELETE FROM tokens WHERE user_id=$1 AND value=$2;` 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 // User represents an user
type User struct { type User struct {
sqly.BaseModel sqly.BaseModel
@ -38,6 +41,9 @@ func Get(q sqlx.Queryer, name string) (*User, error) {
u := &User{} u := &User{}
err := q.QueryRowx(getUserQuery, name).StructScan(u) err := q.QueryRowx(getUserQuery, name).StructScan(u)
if err != nil { if err != nil {
if err.Error() == "sql: no rows in result set" {
return nil, ErrUnknownUser
}
return nil, err return nil, err
} }
return u, nil return u, nil
@ -125,3 +131,8 @@ func (u *User) DeleteToken(ex *sqlx.DB, value string) error {
} }
return nil 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 { if err == nil {
t.Fatal("We expect an error here, the user didn't exist anymore") 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) t.Fatalf("Unexpected error: %q", err)
} }
if u != nil { if u != nil {