2020-11-14 14:46:04 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-04-12 17:40:43 +00:00
|
|
|
"strings"
|
2020-11-14 14:46:04 +00:00
|
|
|
|
|
|
|
"github.com/adrg/xdg"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
|
|
|
)
|
|
|
|
|
2021-01-09 20:49:48 +00:00
|
|
|
// ReadUsersFromFilesystem reads all user information from filesystem and stores it internally. Call it during initialization.
|
|
|
|
func ReadUsersFromFilesystem() {
|
2021-04-26 16:29:41 +00:00
|
|
|
if util.UseFixedAuth {
|
|
|
|
rememberUsers(usersFromFixedCredentials())
|
|
|
|
}
|
|
|
|
if util.UseRegistration {
|
|
|
|
rememberUsers(usersFromRegistrationCredentials())
|
|
|
|
}
|
2021-01-09 20:49:48 +00:00
|
|
|
readTokensToUsers()
|
|
|
|
}
|
|
|
|
|
2021-04-26 16:29:41 +00:00
|
|
|
func usersFromFile(path string, source UserSource) (users []*User) {
|
|
|
|
contents, err := ioutil.ReadFile(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
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-04-26 16:29:41 +00:00
|
|
|
u.Source = source
|
2021-04-12 17:40:43 +00:00
|
|
|
}
|
2021-04-26 16:29:41 +00:00
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
func usersFromFixedCredentials() (users []*User) {
|
|
|
|
users = usersFromFile(util.FixedCredentialsPath, SourceFixed)
|
2021-01-09 20:49:48 +00:00
|
|
|
log.Println("Found", len(users), "fixed users")
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2021-04-26 16:29:41 +00:00
|
|
|
func usersFromRegistrationCredentials() (users []*User) {
|
|
|
|
users = usersFromFile(registrationCredentialsPath(), SourceRegistration)
|
|
|
|
log.Println("Found", len(users), "registered users")
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2021-01-09 20:49:48 +00:00
|
|
|
func rememberUsers(uu []*User) {
|
|
|
|
// uu is used to not shadow the `users` in `users.go`.
|
|
|
|
for _, user := range uu {
|
|
|
|
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() {
|
|
|
|
contents, err := ioutil.ReadFile(tokenStoragePath())
|
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-01-09 20:49:48 +00:00
|
|
|
commenceSession(username, token)
|
2020-11-14 14:46:04 +00:00
|
|
|
}
|
|
|
|
log.Println("Found", len(tmp), "active sessions")
|
|
|
|
}
|
|
|
|
|
2021-01-09 20:49:48 +00:00
|
|
|
// Return path to tokens.json. Creates folders if needed.
|
2020-11-14 14:46:04 +00:00
|
|
|
func tokenStoragePath() string {
|
|
|
|
dir, err := xdg.DataFile("mycorrhiza/tokens.json")
|
|
|
|
if err != nil {
|
2021-03-05 20:40:41 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(dir, util.WikiDir) {
|
|
|
|
log.Fatal("Error: Wiki storage directory includes private config files")
|
2020-11-14 14:46:04 +00:00
|
|
|
}
|
2021-04-26 16:29:41 +00:00
|
|
|
log.Println("Path for saving tokens:", dir)
|
2020-11-14 14:46:04 +00:00
|
|
|
return dir
|
|
|
|
}
|
2021-01-09 20:49:48 +00:00
|
|
|
|
2021-04-26 16:29:41 +00:00
|
|
|
func registrationCredentialsPath() string {
|
|
|
|
path := util.RegistrationCredentialsPath
|
|
|
|
if path == "" {
|
|
|
|
dir, err := xdg.DataFile("mycorrhiza/registration.json")
|
|
|
|
if err != nil {
|
|
|
|
// No error handling, because the program will fail later anyway when trying to read file ""
|
|
|
|
log.Println("Error: cannot get a file to registration credentials, so no registered users will be saved.")
|
|
|
|
} else {
|
|
|
|
path = dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpRegistrationCredentials() error {
|
|
|
|
tmp := []*User{}
|
|
|
|
|
|
|
|
for u := range YieldUsers() {
|
|
|
|
if u.Source != SourceRegistration {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
copiedUser := u
|
|
|
|
copiedUser.Password = ""
|
|
|
|
tmp = append(tmp, copiedUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
blob, err := json.Marshal(tmp)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(registrationCredentialsPath(), blob, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
blob, err := json.Marshal(tmp)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
} else {
|
|
|
|
ioutil.WriteFile(tokenStoragePath(), blob, 0644)
|
|
|
|
}
|
|
|
|
}
|