Browse Source

Removed Name from user, only use ID

master
Gisle Aune 7 years ago
parent
commit
d8e1c2453b
  1. 5
      auth/authenticator.go
  2. 43
      auth/authenticator_test.go
  3. 2
      auth/session_test.go
  4. 5
      auth/user.go

5
auth/authenticator.go

@ -3,8 +3,7 @@ package auth
type Authenticator interface {
ID() string
Name() string
Exists(username string) bool
Find(userid string) *User
Login(username, password string) (*User, error)
Register(username, password string, data map[string]string) (*User, error)
Login(userid, password string) (*User, error)
Register(userid, password string, data map[string]string) (*User, error)
}

43
auth/authenticator_test.go

@ -4,8 +4,6 @@ import (
"errors"
"strings"
"testing"
"git.aiterp.net/gisle/wrouter/generate"
)
var ErrExists = errors.New("auth: user exists")
@ -24,17 +22,6 @@ func (ta *testAuther) ID() string {
func (ta *testAuther) Name() string {
return ta.FullName
}
func (ta *testAuther) Exists(username string) bool {
for _, user := range ta.users {
if user.Name == username {
return true
}
}
return false
}
func (ta *testAuther) Find(userid string) *User {
for _, user := range ta.users {
if user.ID == userid {
@ -45,9 +32,9 @@ func (ta *testAuther) Find(userid string) *User {
return nil
}
func (ta *testAuther) Login(username, password string) (*User, error) {
func (ta *testAuther) Login(userid, password string) (*User, error) {
for _, user := range ta.users {
if user.Name == username && password == ta.passwords[user.ID] {
if user.ID == userid && password == ta.passwords[user.ID] {
return user, nil
}
}
@ -55,8 +42,8 @@ func (ta *testAuther) Login(username, password string) (*User, error) {
return nil, ErrLogin
}
func (ta *testAuther) Register(username, password string, data map[string]string) (*User, error) {
if ta.Exists(username) {
func (ta *testAuther) Register(userid, password string, data map[string]string) (*User, error) {
if ta.Find(userid) != nil {
return nil, ErrExists
}
@ -64,15 +51,13 @@ func (ta *testAuther) Register(username, password string, data map[string]string
ta.passwords = make(map[string]string)
}
id := generate.ID()
ta.passwords[id] = password
user := NewUser(ta, id, username, "member", data)
user := NewUser(ta, userid, "member", data)
ta.users = append(ta.users, user)
ta.passwords[userid] = password
return user, nil
}
func TestList(t *testing.T) {
func TestAuthenticator(t *testing.T) {
ta1 := testAuther{FullName: "Auth1"}
ta2 := testAuther{FullName: "Auth2"}
Register(&ta1)
@ -105,33 +90,33 @@ func TestList(t *testing.T) {
t.Run("Register", func(t *testing.T) {
user, err := ta1.Register("Test", "CakesAndStuff", nil)
if err != nil || user.Name != "Test" {
t.Logf("err = %v; name = \"%s\"", err, user.Name)
if err != nil || user.ID != "Test" {
t.Logf("err = %v; name = \"%s\"", err, user.ID)
t.Fail()
}
if !ta1.Exists("Test") {
if ta1.Find("Test") == nil {
t.Log("Registered user does not exist")
t.Fail()
}
user2, err := ta1.Register("Test", "CakesAndStuff", nil)
if err == nil || user2 != nil {
t.Logf("err = %s; name = \"%s\"", err, user2.Name)
t.Logf("err = %s; name = \"%s\"", err, user2.ID)
t.Fail()
}
})
t.Run("Login", func(t *testing.T) {
user, err := ta1.Login("Test", "CakesAndStuff")
if err != nil || user.Name != "Test" {
t.Logf("err = %v; name = \"%s\"", err, user.Name)
if err != nil || user.ID != "Test" {
t.Logf("err = %v; user = %+v", err, user)
t.Fail()
}
user2, err := ta1.Login("Test", "WrongPassword")
if err == nil || user2 != nil {
t.Logf("err = %v; name = \"%s\"", err, user.Name)
t.Logf("err = %v; user = %+v", err, user2)
t.Fail()
}
})

2
auth/session_test.go

@ -5,7 +5,7 @@ import "testing"
func TestSession(t *testing.T) {
auther := testAuther{FullName: "Test"}
user := NewUser(&auther, "Tester", "Tester", "member", nil)
user := NewUser(&auther, "Tester", "member", nil)
sessions := []*Session{OpenSession(user), OpenSession(user), OpenSession(user)}
ids := []string{sessions[0].ID, sessions[1].ID, sessions[2].ID}

5
auth/user.go

@ -2,7 +2,6 @@ package auth
type User struct {
ID string
Name string
Level string
Data map[string]string
@ -26,6 +25,6 @@ func (user *User) LoggedOut() bool {
}
// NewUser creates a new User object
func NewUser(method Authenticator, id, name, level string, data map[string]string) *User {
return &User{id, name, level, data, method, false}
func NewUser(method Authenticator, id, level string, data map[string]string) *User {
return &User{id, level, data, method, false}
}
Loading…
Cancel
Save