Commit 56054464 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

refactor: remove superfluous boolean return parameter from myirmaserver database

The two implementation did not return the same values for this parameter under the same
circumstances, and at the invocation false is treated as an error anyway.
parent 20cc3fe5
......@@ -11,7 +11,7 @@ type MyirmaDB interface {
AddEmailLoginToken(email, token string) error
LoginTokenCandidates(token string) ([]LoginCandidate, error)
TryUserLoginToken(token, username string) (int64, bool, error)
TryUserLoginToken(token, username string) (int64, error)
UserInformation(id int64) (UserInformation, error)
Logs(id int64, offset int, amount int) ([]LogEntry, error)
......
......@@ -112,27 +112,27 @@ func (db *myirmaMemoryDB) LoginTokenCandidates(token string) ([]LoginCandidate,
return result, nil
}
func (db *myirmaMemoryDB) TryUserLoginToken(token, username string) (int64, bool, error) {
func (db *myirmaMemoryDB) TryUserLoginToken(token, username string) (int64, error) {
db.lock.Lock()
defer db.lock.Unlock()
email, ok := db.loginEmailTokens[token]
if !ok {
return 0, false, nil
return 0, keyshare.ErrUserNotFound
}
user, ok := db.userData[username]
if !ok {
return 0, false, keyshare.ErrUserNotFound
return 0, keyshare.ErrUserNotFound
}
for _, userEmail := range user.email {
if userEmail == email {
delete(db.loginEmailTokens, token)
return user.id, true, nil
return user.id, nil
}
}
return 0, false, nil
return 0, keyshare.ErrUserNotFound
}
func (db *myirmaMemoryDB) UserInformation(id int64) (UserInformation, error) {
......
......@@ -82,23 +82,20 @@ func TestMemoryDBLoginToken(t *testing.T) {
_, err = db.LoginTokenCandidates("DNE")
assert.Error(t, err)
_, _, err = db.TryUserLoginToken("testtoken", "DNE")
_, err = db.TryUserLoginToken("testtoken", "DNE")
assert.Error(t, err)
id, ok, err := db.TryUserLoginToken("testtoken", "noemail")
id, err := db.TryUserLoginToken("testtoken", "noemail")
assert.Equal(t, int64(0), id)
assert.NoError(t, err)
assert.False(t, ok)
assert.Error(t, err)
id, ok, err = db.TryUserLoginToken("testtoken", "testuser")
id, err = db.TryUserLoginToken("testtoken", "testuser")
assert.Equal(t, int64(15), id)
assert.NoError(t, err)
assert.True(t, ok)
id, ok, err = db.TryUserLoginToken("testtoken", "testuser")
id, err = db.TryUserLoginToken("testtoken", "testuser")
assert.Equal(t, int64(0), id)
assert.NoError(t, err)
assert.False(t, ok)
assert.Error(t, err)
}
func TestMemoryDBUserInfo(t *testing.T) {
......
......@@ -125,7 +125,7 @@ func (db *myirmaPostgresDB) LoginTokenCandidates(token string) ([]LoginCandidate
return candidates, nil
}
func (db *myirmaPostgresDB) TryUserLoginToken(token, username string) (int64, bool, error) {
func (db *myirmaPostgresDB) TryUserLoginToken(token, username string) (int64, error) {
var id int64
err := db.db.QueryUser(
`SELECT users.id FROM irma.users INNER JOIN irma.emails ON users.id = emails.user_id WHERE
......@@ -133,18 +133,17 @@ func (db *myirmaPostgresDB) TryUserLoginToken(token, username string) (int64, bo
email = (SELECT email FROM irma.email_login_tokens WHERE token = $2 AND expiry >= $3)`,
[]interface{}{&id}, username, token, time.Now().Unix())
if err != nil {
return 0, false, err
return 0, err
}
// Successfull deletion of the token can only occur once, so we use that to signal ok to login
aff, err := db.db.ExecCount("DELETE FROM irma.email_login_tokens WHERE token = $1", token)
if err != nil {
return 0, false, err
return 0, err
}
if aff != 1 {
return 0, false, nil
return 0, errors.Errorf("Unexpected number of affected rows %d for token removal", aff)
}
return id, true, nil
return id, nil
}
func (db *myirmaPostgresDB) UserInformation(id int64) (UserInformation, error) {
......
......@@ -93,18 +93,17 @@ func TestPostgresDBLoginToken(t *testing.T) {
_, err = db.LoginTokenCandidates("DNE")
assert.Error(t, err)
_, _, err = db.TryUserLoginToken("testtoken", "DNE")
_, err = db.TryUserLoginToken("testtoken", "DNE")
assert.Error(t, err)
_, _, err = db.TryUserLoginToken("testtoken", "noemail")
_, err = db.TryUserLoginToken("testtoken", "noemail")
assert.Error(t, err)
id, ok, err := db.TryUserLoginToken("testtoken", "testuser")
id, err := db.TryUserLoginToken("testtoken", "testuser")
assert.NoError(t, err)
assert.Equal(t, int64(15), id)
assert.True(t, ok)
_, _, err = db.TryUserLoginToken("testtoken", "testuser")
_, err = db.TryUserLoginToken("testtoken", "testuser")
assert.Error(t, err)
assert.NoError(t, db.AddEmail(17, "test@test.com"))
......
......@@ -279,13 +279,13 @@ type TokenLoginRequest struct {
}
func (s *Server) processTokenLogin(request TokenLoginRequest) (string, error) {
id, ok, err := s.db.TryUserLoginToken(request.Token, request.Username)
if err != nil && err != keyshare.ErrUserNotFound {
s.conf.Logger.WithField("error", err).Error("Could not login user using token")
id, err := s.db.TryUserLoginToken(request.Token, request.Username)
if err == keyshare.ErrUserNotFound {
return "", err
}
if !ok || err == keyshare.ErrUserNotFound {
return "", keyshare.ErrUserNotFound
if err != nil {
s.conf.Logger.WithField("error", err).Error("Could not login user using token")
return "", err
}
session := s.store.create()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment