1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-03-10 13:38:20 +00:00

136 lines
2.9 KiB
Go
Raw Permalink Normal View History

2020-11-14 19:46:04 +05:00
package user
import (
"encoding/json"
"errors"
"log/slog"
2020-11-14 19:46:04 +05:00
"os"
2021-04-28 17:12:05 +07:00
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files"
2021-06-29 17:34:36 +07:00
"github.com/bouncepaw/mycorrhiza/util"
2020-11-14 19:46:04 +05:00
)
// InitUserDatabase loads users, if necessary. Call it during initialization.
2021-04-28 17:12:05 +07:00
func InitUserDatabase() {
ReadUsersFromFilesystem()
2021-04-28 17:12:05 +07:00
}
// ReadUsersFromFilesystem reads all user information from filesystem and
// stores it internally.
func ReadUsersFromFilesystem() {
if cfg.UseAuth {
rememberUsers(usersFromFile())
readTokensToUsers()
2021-04-26 21:29:41 +05:00
}
}
func usersFromFile() []*User {
var users []*User
contents, err := os.ReadFile(files.UserCredentialsJSON())
if errors.Is(err, os.ErrNotExist) {
return users
2021-04-26 21:29:41 +05:00
}
2020-11-14 19:46:04 +05:00
if err != nil {
slog.Error("Failed to read users.json", "err", err)
os.Exit(1)
2020-11-14 19:46:04 +05:00
}
err = json.Unmarshal(contents, &users)
2020-11-14 19:46:04 +05:00
if err != nil {
slog.Error("Failed to unmarshal users.json contents", "err", err)
os.Exit(1)
2020-11-14 19:46:04 +05:00
}
for _, u := range users {
2021-06-03 18:16:15 +05:00
u.Name = util.CanonicalName(u.Name)
2021-07-14 21:00:35 +00:00
if u.Source == "" {
u.Source = "local"
}
}
slog.Info("Indexed users", "n", len(users))
2021-04-26 21:29:41 +05:00
return users
}
func rememberUsers(userList []*User) {
for _, user := range userList {
if !IsValidUsername(user.Name) {
continue
}
users.Store(user.Name, user)
2020-11-14 19:46:04 +05:00
}
}
2020-11-14 19:46:04 +05:00
func readTokensToUsers() {
contents, err := os.ReadFile(files.TokensJSON())
2020-11-14 19:46:04 +05:00
if os.IsNotExist(err) {
return
}
if err != nil {
slog.Error("Failed to read tokens.json", "err", err)
os.Exit(1)
2020-11-14 19:46:04 +05:00
}
2020-11-14 19:46:04 +05:00
var tmp map[string]string
err = json.Unmarshal(contents, &tmp)
if err != nil {
slog.Error("Failed to unmarshal tokens.json contents", "err", err)
os.Exit(1)
2020-11-14 19:46:04 +05:00
}
2020-11-14 19:46:04 +05:00
for token, username := range tmp {
tokens.Store(token, username)
// commenceSession(username, token)
2020-11-14 19:46:04 +05:00
}
slog.Info("Indexed active sessions", "n", len(tmp))
2020-11-14 19:46:04 +05:00
}
// SaveUserDatabase stores current user credentials into JSON file by configured path.
func SaveUserDatabase() error {
return dumpUserCredentials()
}
func dumpUserCredentials() error {
var userList []*User
2021-04-26 21:29:41 +05:00
// TODO: lock the map during saving to prevent corruption
2021-04-26 21:29:41 +05:00
for u := range YieldUsers() {
userList = append(userList, u)
2021-04-26 21:29:41 +05:00
}
blob, err := json.MarshalIndent(userList, "", "\t")
2021-04-26 21:29:41 +05:00
if err != nil {
slog.Error("Failed to marshal users.json", "err", err)
2021-04-26 21:29:41 +05:00
return err
}
err = os.WriteFile(files.UserCredentialsJSON(), blob, 0666)
2021-04-26 21:29:41 +05:00
if err != nil {
slog.Error("Failed to write users.json", "err", err)
2021-04-26 21:29:41 +05:00
return err
}
2021-04-26 21:29:41 +05: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 {
slog.Error("Failed to marshal tokens.json", "err", err)
return
}
err = os.WriteFile(files.TokensJSON(), blob, 0666)
if err != nil {
slog.Error("Failed to write tokens.json", "err", err)
}
}