1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-15 22:40:27 +00:00
mycorrhiza/user/files.go

120 lines
2.2 KiB
Go
Raw Normal View History

2020-11-14 14:46:04 +00:00
package user
import (
"encoding/json"
"errors"
2020-11-14 14:46:04 +00:00
"log"
"os"
2021-04-28 10:12:05 +00:00
"github.com/bouncepaw/mycorrhiza/cfg"
2021-04-28 10:12:05 +00:00
"github.com/bouncepaw/mycorrhiza/files"
2021-06-29 10:34:36 +00:00
"github.com/bouncepaw/mycorrhiza/util"
2020-11-14 14:46:04 +00:00
)
// InitUserDatabase loads users, if necessary. Call it during initialization.
2021-04-28 10:12:05 +00:00
func InitUserDatabase() {
ReadUsersFromFilesystem()
2021-04-28 10:12:05 +00:00
}
// ReadUsersFromFilesystem reads all user information from filesystem and
// stores it internally.
func ReadUsersFromFilesystem() {
if cfg.UseAuth {
rememberUsers(usersFromFile())
readTokensToUsers()
2021-04-26 16:29:41 +00:00
}
}
func usersFromFile() []*User {
var users []*User
contents, err := os.ReadFile(files.UserCredentialsJSON())
if errors.Is(err, os.ErrNotExist) {
return users
2021-04-26 16:29:41 +00:00
}
2020-11-14 14:46:04 +00:00
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(contents, &users)
2020-11-14 14:46:04 +00:00
if err != nil {
log.Fatal(err)
}
for _, u := range users {
2021-06-03 13:16:15 +00:00
u.Name = util.CanonicalName(u.Name)
}
log.Println("Found", len(users), "users")
2021-04-26 16:29:41 +00:00
return users
}
func rememberUsers(userList []*User) {
for _, user := range userList {
users.Store(user.Name, user)
2020-11-14 14:46:04 +00:00
}
}
2020-11-14 14:46:04 +00:00
func readTokensToUsers() {
contents, err := os.ReadFile(files.TokensJSON())
2020-11-14 14:46:04 +00:00
if os.IsNotExist(err) {
return
}
if err != nil {
log.Fatal(err)
}
2020-11-14 14:46:04 +00:00
var tmp map[string]string
err = json.Unmarshal(contents, &tmp)
if err != nil {
log.Fatal(err)
}
2020-11-14 14:46:04 +00:00
for token, username := range tmp {
tokens.Store(token, username)
// commenceSession(username, token)
2020-11-14 14:46:04 +00:00
}
log.Println("Found", len(tmp), "active sessions")
}
func SaveUserDatabase() error {
return dumpUserCredentials()
}
func dumpUserCredentials() error {
userList := []*User{}
2021-04-26 16:29:41 +00:00
// TODO: lock the map during saving to prevent corruption
2021-04-26 16:29:41 +00:00
for u := range YieldUsers() {
userList = append(userList, u)
2021-04-26 16:29:41 +00:00
}
blob, err := json.MarshalIndent(userList, "", "\t")
2021-04-26 16:29:41 +00:00
if err != nil {
log.Println(err)
return err
}
err = os.WriteFile(files.UserCredentialsJSON(), blob, 0666)
2021-04-26 16:29:41 +00:00
if err != nil {
log.Println(err)
return err
}
2021-04-26 16:29:41 +00:00
return nil
}
func dumpTokens() {
tmp := make(map[string]string)
tokens.Range(func(k, v interface{}) bool {
token := k.(string)
username := v.(string)
tmp[token] = username
return true
})
blob, err := json.MarshalIndent(tmp, "", "\t")
if err != nil {
log.Println(err)
return
}
os.WriteFile(files.TokensJSON(), blob, 0666)
}