2020-11-14 14:46:04 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-07-02 08:20:03 +00:00
|
|
|
"errors"
|
2020-11-14 14:46:04 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2021-04-28 10:12:05 +00:00
|
|
|
|
2021-06-27 14:52:55 +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
|
|
|
)
|
|
|
|
|
2021-07-02 08:20:03 +00:00
|
|
|
// InitUserDatabase loads users, if necessary. Call it during initialization.
|
2021-04-28 10:12:05 +00:00
|
|
|
func InitUserDatabase() {
|
2021-07-02 08:20:03 +00:00
|
|
|
ReadUsersFromFilesystem()
|
2021-04-28 10:12:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:20:03 +00:00
|
|
|
// ReadUsersFromFilesystem reads all user information from filesystem and
|
|
|
|
// stores it internally.
|
2021-01-09 20:49:48 +00:00
|
|
|
func ReadUsersFromFilesystem() {
|
2021-07-02 08:20:03 +00:00
|
|
|
if cfg.UseAuth {
|
|
|
|
rememberUsers(usersFromFile())
|
|
|
|
readTokensToUsers()
|
2021-04-26 16:29:41 +00:00
|
|
|
}
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:20:03 +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)
|
|
|
|
}
|
2021-01-09 20:49:48 +00:00
|
|
|
err = json.Unmarshal(contents, &users)
|
2020-11-14 14:46:04 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-04-12 17:40:43 +00:00
|
|
|
for _, u := range users {
|
2021-06-03 13:16:15 +00:00
|
|
|
u.Name = util.CanonicalName(u.Name)
|
2021-07-14 21:00:35 +00:00
|
|
|
if u.Source == "" {
|
|
|
|
u.Source = "local"
|
|
|
|
}
|
2021-04-12 17:40:43 +00:00
|
|
|
}
|
2021-07-02 08:20:03 +00:00
|
|
|
log.Println("Found", len(users), "users")
|
2021-04-26 16:29:41 +00:00
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2021-06-27 14:52:55 +00:00
|
|
|
func rememberUsers(userList []*User) {
|
|
|
|
for _, user := range userList {
|
2021-01-09 20:49:48 +00:00
|
|
|
users.Store(user.Name, user)
|
2020-11-14 14:46:04 +00:00
|
|
|
}
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|
2020-11-14 14:46:04 +00:00
|
|
|
|
2021-01-09 20:49:48 +00:00
|
|
|
func readTokensToUsers() {
|
2021-07-02 08:20:03 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-01-09 20:49:48 +00:00
|
|
|
|
2020-11-14 14:46:04 +00:00
|
|
|
var tmp map[string]string
|
|
|
|
err = json.Unmarshal(contents, &tmp)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-01-09 20:49:48 +00:00
|
|
|
|
2020-11-14 14:46:04 +00:00
|
|
|
for token, username := range tmp {
|
2021-07-02 08:20:03 +00:00
|
|
|
tokens.Store(token, username)
|
|
|
|
// commenceSession(username, token)
|
2020-11-14 14:46:04 +00:00
|
|
|
}
|
|
|
|
log.Println("Found", len(tmp), "active sessions")
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:12:16 +00:00
|
|
|
// SaveUserDatabase stores current user credentials into JSON file by configured path.
|
2021-06-29 15:10:48 +00:00
|
|
|
func SaveUserDatabase() error {
|
2021-07-02 08:20:03 +00:00
|
|
|
return dumpUserCredentials()
|
2021-06-29 15:10:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:20:03 +00:00
|
|
|
func dumpUserCredentials() error {
|
2021-10-29 09:00:19 +00:00
|
|
|
var userList []*User
|
2021-04-26 16:29:41 +00:00
|
|
|
|
2021-07-02 08:20:03 +00:00
|
|
|
// TODO: lock the map during saving to prevent corruption
|
2021-04-26 16:29:41 +00:00
|
|
|
for u := range YieldUsers() {
|
2021-07-02 08:20:03 +00:00
|
|
|
userList = append(userList, u)
|
2021-04-26 16:29:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:20:03 +00:00
|
|
|
blob, err := json.MarshalIndent(userList, "", "\t")
|
2021-04-26 16:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-07-02 08:20:03 +00:00
|
|
|
|
|
|
|
err = os.WriteFile(files.UserCredentialsJSON(), blob, 0666)
|
2021-04-26 16:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-07-02 08:20:03 +00:00
|
|
|
|
2021-04-26 16:29:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-09 20:49:48 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2021-05-27 12:04:10 +00:00
|
|
|
blob, err := json.MarshalIndent(tmp, "", "\t")
|
2021-01-09 20:49:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2021-07-02 08:20:03 +00:00
|
|
|
return
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|
2021-10-29 09:00:19 +00:00
|
|
|
err = os.WriteFile(files.TokensJSON(), blob, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("an error occurred in dumpTokens function:", err)
|
|
|
|
}
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|