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

refactor: rename some myirmaserver db methods for clarity

parent 51d9654a
......@@ -8,12 +8,13 @@ type db interface {
user(id int64) (user, error)
userIDByUsername(username string) (int64, error)
userIDByEmailToken(token string) (int64, error)
userIDByLoginToken(token, username string) (int64, error)
verifyEmailToken(token string) (int64, error)
verifyLoginToken(token, username string) (int64, error)
scheduleUserRemoval(id int64, delay time.Duration) error
addEmailLoginToken(email, token string) error
addLoginToken(email, token string) error
loginUserCandidates(token string) ([]loginCandidate, error)
logs(id int64, offset int, amount int) ([]logEntry, error)
......
......@@ -52,7 +52,7 @@ func (db *memoryDB) scheduleUserRemoval(id int64, _ time.Duration) error {
return keyshare.ErrUserNotFound
}
func (db *memoryDB) userIDByEmailToken(token string) (int64, error) {
func (db *memoryDB) verifyEmailToken(token string) (int64, error) {
db.Lock()
defer db.Unlock()
......@@ -66,7 +66,7 @@ func (db *memoryDB) userIDByEmailToken(token string) (int64, error) {
return userID, nil
}
func (db *memoryDB) addEmailLoginToken(email, token string) error {
func (db *memoryDB) addLoginToken(email, token string) error {
db.Lock()
defer db.Unlock()
......@@ -112,7 +112,7 @@ func (db *memoryDB) loginUserCandidates(token string) ([]loginCandidate, error)
return result, nil
}
func (db *memoryDB) userIDByLoginToken(token, username string) (int64, error) {
func (db *memoryDB) verifyLoginToken(token, username string) (int64, error) {
db.Lock()
defer db.Unlock()
......
......@@ -25,11 +25,11 @@ func TestMemoryDBUserManagement(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, int64(15), id)
id, err = db.userIDByEmailToken("testtoken")
id, err = db.verifyEmailToken("testtoken")
assert.NoError(t, err)
assert.Equal(t, int64(15), id)
_, err = db.userIDByEmailToken("testtoken")
_, err = db.verifyEmailToken("testtoken")
assert.Error(t, err)
_, err = db.userIDByUsername("DNE")
......@@ -69,10 +69,10 @@ func TestMemoryDBLoginToken(t *testing.T) {
loginEmailTokens: map[string]string{},
}
err := db.addEmailLoginToken("test2@test.com", "test2token")
err := db.addLoginToken("test2@test.com", "test2token")
assert.Error(t, err)
err = db.addEmailLoginToken("test@test.com", "testtoken")
err = db.addLoginToken("test@test.com", "testtoken")
require.NoError(t, err)
cand, err := db.loginUserCandidates("testtoken")
......@@ -82,18 +82,18 @@ func TestMemoryDBLoginToken(t *testing.T) {
_, err = db.loginUserCandidates("DNE")
assert.Error(t, err)
_, err = db.userIDByLoginToken("testtoken", "DNE")
_, err = db.verifyLoginToken("testtoken", "DNE")
assert.Error(t, err)
id, err := db.userIDByLoginToken("testtoken", "noemail")
id, err := db.verifyLoginToken("testtoken", "noemail")
assert.Equal(t, int64(0), id)
assert.Error(t, err)
id, err = db.userIDByLoginToken("testtoken", "testuser")
id, err = db.verifyLoginToken("testtoken", "testuser")
assert.Equal(t, int64(15), id)
assert.NoError(t, err)
id, err = db.userIDByLoginToken("testtoken", "testuser")
id, err = db.verifyLoginToken("testtoken", "testuser")
assert.Equal(t, int64(0), id)
assert.Error(t, err)
}
......
......@@ -39,7 +39,7 @@ func (db *postgresDB) userIDByUsername(username string) (int64, error) {
return id, db.db.QueryUser("SELECT id FROM irma.users WHERE username = $1", []interface{}{&id}, username)
}
func (db *postgresDB) userIDByEmailToken(token string) (int64, error) {
func (db *postgresDB) verifyEmailToken(token string) (int64, error) {
var email string
var id int64
err := db.db.QueryScan(
......@@ -77,7 +77,7 @@ func (db *postgresDB) scheduleUserRemoval(id int64, delay time.Duration) error {
time.Now().Add(delay).Unix())
}
func (db *postgresDB) addEmailLoginToken(email, token string) error {
func (db *postgresDB) addLoginToken(email, token string) error {
// Check if email address exists in database
err := db.db.QueryScan("SELECT 1 FROM irma.emails WHERE email = $1 AND (delete_on >= $2 OR delete_on IS NULL) LIMIT 1",
nil, email, time.Now().Unix())
......@@ -125,7 +125,7 @@ func (db *postgresDB) loginUserCandidates(token string) ([]loginCandidate, error
return candidates, nil
}
func (db *postgresDB) userIDByLoginToken(token, username string) (int64, error) {
func (db *postgresDB) verifyLoginToken(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
......
......@@ -32,7 +32,7 @@ func TestPostgresDBUserManagement(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, []userEmail(nil), user.Emails)
id, err = db.userIDByEmailToken("testtoken")
id, err = db.verifyEmailToken("testtoken")
assert.NoError(t, err)
assert.Equal(t, int64(15), id)
......@@ -40,7 +40,7 @@ func TestPostgresDBUserManagement(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, []userEmail{{Email: "test@test.com", DeleteInProgress: false}}, user.Emails)
_, err = db.userIDByEmailToken("testtoken")
_, err = db.verifyEmailToken("testtoken")
assert.Error(t, err)
_, err = db.userIDByUsername("DNE")
......@@ -78,10 +78,10 @@ func TestPostgresDBLoginToken(t *testing.T) {
_, err = pdb.db.Exec("INSERT INTO irma.emails (user_id, email) VALUES (15, 'test@test.com')")
require.NoError(t, err)
err = db.addEmailLoginToken("test2@test.com", "test2token")
err = db.addLoginToken("test2@test.com", "test2token")
assert.Error(t, err)
err = db.addEmailLoginToken("test@test.com", "testtoken")
err = db.addLoginToken("test@test.com", "testtoken")
require.NoError(t, err)
cand, err := db.loginUserCandidates("testtoken")
......@@ -97,21 +97,21 @@ func TestPostgresDBLoginToken(t *testing.T) {
_, err = db.loginUserCandidates("DNE")
assert.Error(t, err)
_, err = db.userIDByLoginToken("testtoken", "DNE")
_, err = db.verifyLoginToken("testtoken", "DNE")
assert.Error(t, err)
_, err = db.userIDByLoginToken("testtoken", "noemail")
_, err = db.verifyLoginToken("testtoken", "noemail")
assert.Error(t, err)
id, err := db.userIDByLoginToken("testtoken", "testuser")
id, err := db.verifyLoginToken("testtoken", "testuser")
assert.NoError(t, err)
assert.Equal(t, int64(15), id)
_, err = db.userIDByLoginToken("testtoken", "testuser")
_, err = db.verifyLoginToken("testtoken", "testuser")
assert.Error(t, err)
assert.NoError(t, db.addEmail(17, "test@test.com"))
assert.NoError(t, db.addEmailLoginToken("test@test.com", "testtoken"))
assert.NoError(t, db.addLoginToken("test@test.com", "testtoken"))
cand, err = db.loginUserCandidates("testtoken")
assert.NoError(t, err)
assert.Equal(t, []loginCandidate{
......
......@@ -209,7 +209,7 @@ type emailLoginRequest struct {
func (s *Server) sendLoginEmail(request emailLoginRequest) error {
token := common.NewSessionToken()
err := s.db.addEmailLoginToken(request.Email, token)
err := s.db.addLoginToken(request.Email, token)
if err == errEmailNotFound {
return err
} else if err != nil {
......@@ -279,7 +279,7 @@ type tokenLoginRequest struct {
}
func (s *Server) processTokenLogin(request tokenLoginRequest) (string, error) {
id, err := s.db.userIDByLoginToken(request.Token, request.Username)
id, err := s.db.verifyLoginToken(request.Token, request.Username)
if err == keyshare.ErrUserNotFound {
return "", err
}
......@@ -391,7 +391,7 @@ func (s *Server) handleVerifyEmail(w http.ResponseWriter, r *http.Request) {
return
}
id, err := s.db.userIDByEmailToken(token)
id, err := s.db.verifyEmailToken(token)
if err == errTokenNotFound {
s.conf.Logger.Info("Unknown email verification token")
server.WriteError(w, server.ErrorInvalidRequest, "Unknown email verification token")
......
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