Skip to content
This repository was archived by the owner on Dec 21, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions fileBackend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package httpauth
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a docstring describing why this backend is useful

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Human readable, file based backend


import (
"encoding/json"
"errors"
"fmt"
"os"
)

var (
ErrMissingFileBackend = errors.New("fileauthbackend: missing backend")
)

type FileAuthBackend struct {
filepath string
users map[string]UserData
}

func NewFileAuthBackend(filepath string) (b FileAuthBackend, e error) {
b.filepath = filepath
if _, err := os.Stat(b.filepath); err == nil {
file, err := os.Open(b.filepath)
defer file.Close()
if err != nil {
return b, fmt.Errorf("fileauthbackend: %v", err.Error())
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&b.users)
if err != nil {
b.users = make(map[string]UserData)
}
} else {
return b, ErrMissingFileBackend
}
if b.users == nil {
b.users = make(map[string]UserData)
}
return b, nil
}

func (b FileAuthBackend) User(username string) (user UserData, e error) {
if user, ok := b.users[username]; ok {
return user, nil
}
return user, ErrMissingUser
}

func (b FileAuthBackend) Users() (us []UserData, e error) {
for _, user := range b.users {
us = append(us, user)
}
return
}

func (b FileAuthBackend) SaveUser(user UserData) error {
b.users[user.Username] = user
err := b.save()
return err
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just be return b.save()?

}

func (b FileAuthBackend) save() error {
file, err := os.OpenFile(b.filepath,os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
defer file.Close()
if err != nil {
fmt.Printf("==>%v",err)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove debugging comment

return errors.New("fileauthbackend: failed to edit auth file")
}
data, err := json.Marshal(b.users)
if err != nil {
return errors.New(fmt.Sprintf("fileauthbackend: save: %v", err))
}
_,err = file.Write(data)
if err != nil {
return errors.New(fmt.Sprintf("fileauthbackend: save: %v", err))
}
return nil
}

func (b FileAuthBackend) DeleteUser(username string) error {
_, err := b.User(username)
if err == ErrMissingUser {
return ErrDeleteNull
} else if err != nil {
return fmt.Errorf("filebauthbackend: %v", err)
}
delete(b.users, username)
return b.save()
}

func (b FileAuthBackend) Close() {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment explaining why this is a no-op


}
56 changes: 56 additions & 0 deletions fileBackend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package httpauth

import (
"encoding/json"
"os"
"testing"
"fmt"
)

var (
filedb = "test.json"
)

func TestInitFileAuthBackend(t *testing.T) {
os.Remove(filedb)
b, err := NewFileAuthBackend(filedb)
if err != ErrMissingFileBackend {
t.Fatal(err.Error())
}
// Create test file
file, err := os.OpenFile(filedb,os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
users:= map[string]UserData{}
data, err := json.Marshal(users)
if err != nil {
fmt.Println(err)
}
file.Write(data)
file.Close()
//
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless comment

b, err = NewFileAuthBackend(filedb)
if err != nil {
t.Fatal(err.Error())
}
if b.filepath != filedb {
t.Fatal("File path not saved.")
}
if len(b.users) != 0 {
t.Fatal("Users initialized with items.")
}

testBackend(t, b)
}

func TestFileReopen(t *testing.T) {
b, err := NewFileAuthBackend(filedb)
if err != nil {
t.Fatal(err.Error())
}
b.Close()
b, err = NewFileAuthBackend(filedb)
if err != nil {
t.Fatal(err.Error())
}

testBackend2(t, b)
}