@@ -6,21 +6,17 @@ import (
66 "os"
77 "sort"
88 "strings"
9- "sync"
109
1110 "github.com/numbleroot/pluto/config"
1211)
1312
1413// Structs
1514
16- // FileAuthenticator contains file based authentication
15+ // File contains file based authentication
1716// information including the in-memory map of username to
1817// password mapping.
19- type FileAuthenticator struct {
20- lock * sync.RWMutex
21- File string
22- Separator string
23- Users []User
18+ type File struct {
19+ Users []User
2420}
2521
2622// User holds name and password from one line from users file.
@@ -30,49 +26,37 @@ type User struct {
3026 Password string
3127}
3228
33- // UsersByName defines a list type of users to search efficiently.
34- type UsersByName []User
35-
3629// Functions
3730
38- // Make list of users searchable efficiently.
39- func (u UsersByName ) Len () int { return len (u ) }
40- func (u UsersByName ) Swap (i , j int ) { u [i ], u [j ] = u [j ], u [i ] }
41- func (u UsersByName ) Less (i , j int ) bool { return u [i ].Name < u [j ].Name }
42-
43- // NewFileAuthenticator takes in a file name and a separator,
31+ // NewFile takes in a file name and a separator,
4432// reads in specified file and parses it line by line as
4533// username - password elements separated by the separator.
4634// At the end, the returned struct contains the information
4735// and an in-memory map of username mapped to password.
48- func NewFileAuthenticator (file string , sep string ) (* FileAuthenticator , error ) {
49-
50- i := 1
51- var err error
52- var handle * os.File
53- var nextUser User
36+ func NewFile (file string , sep string ) (* File , error ) {
5437
5538 // Reserve space for the ordered users list in memory.
5639 users := make ([]User , 0 , 50 )
5740
5841 // Open file with authentication information.
59- handle , err = os .Open (file )
42+ handle , err : = os .Open (file )
6043 if err != nil {
61- return nil , fmt .Errorf ("[auth.NewFileAuthenticator ] Could not open supplied authentication file: %s \n " , err . Error () )
44+ return nil , fmt .Errorf ("[auth.NewFile ] Could not open supplied authentication file: %v " , err )
6245 }
6346 defer handle .Close ()
6447
6548 // Create a new scanner on top of file handle.
6649 scanner := bufio .NewScanner (handle )
6750
51+ i := 1
6852 // As long as there are lines left, scan them into memory.
6953 for scanner .Scan () {
7054
7155 // Split read line based on separator defined in config file.
7256 userData := strings .Split (scanner .Text (), sep )
7357
7458 // Create new user struct.
75- nextUser = User {
59+ nextUser : = User {
7660 ID : i ,
7761 Name : userData [0 ],
7862 Password : userData [1 ],
@@ -87,23 +71,22 @@ func NewFileAuthenticator(file string, sep string) (*FileAuthenticator, error) {
8771
8872 // If the scanner ended with an error, report it.
8973 if err := scanner .Err (); err != nil {
90- return nil , fmt .Errorf ("[auth.NewFileAuthenticator ] Experienced error while scanning authentication file: %s \n " , err . Error () )
74+ return nil , fmt .Errorf ("[auth.NewFile ] Experienced error while scanning authentication file: %v " , err )
9175 }
9276
9377 // Sort users list to search it efficiently later on.
94- sort .Sort (UsersByName (users ))
78+ sort .Slice (users , func (i , j int ) bool {
79+ return users [i ].Name < users [j ].Name
80+ })
9581
96- return & FileAuthenticator {
97- lock : new (sync.RWMutex ),
98- File : file ,
99- Separator : sep ,
100- Users : users ,
82+ return & File {
83+ Users : users ,
10184 }, nil
10285}
10386
10487// GetWorkerForUser returns the name of the worker node
10588// that is responsible for handling the user's mailbox.
106- func (f * FileAuthenticator ) GetWorkerForUser (workers map [string ]config.Worker , id int ) (string , error ) {
89+ func (f * File ) GetWorkerForUser (workers map [string ]config.Worker , id int ) (string , error ) {
10790
10891 for name , worker := range workers {
10992
@@ -122,12 +105,7 @@ func (f *FileAuthenticator) GetWorkerForUser(workers map[string]config.Worker, i
122105// process by taking supplied credentials and attempting
123106// to find a matching entry the in-memory list taken from
124107// the authentication file.
125- func (f * FileAuthenticator ) AuthenticatePlain (username string , password string , clientAddr string ) (int , string , error ) {
126-
127- // This routine has to be safe for concurrent usage,
128- // therefore lock the struct on entry.
129- f .lock .RLock ()
130- defer f .lock .RUnlock ()
108+ func (f * File ) AuthenticatePlain (username string , password string , clientAddr string ) (int , string , error ) {
131109
132110 // Search in user list for user matching supplied name.
133111 i := sort .Search (len (f .Users ), func (i int ) bool {
0 commit comments