1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 05:20:26 +00:00
mycorrhiza/user/files.go

136 lines
2.9 KiB
Go
Raw Normal View History

2020-11-14 14:46:04 +00:00
package user
import (
"encoding/json"
"github.com/bouncepaw/mycorrhiza/cfg"
2021-06-03 13:16:15 +00:00
"github.com/bouncepaw/mycorrhiza/util"
2020-11-14 14:46:04 +00:00
"io/ioutil"
"log"
"os"
2021-04-28 10:12:05 +00:00
"github.com/bouncepaw/mycorrhiza/files"
2020-11-14 14:46:04 +00:00
)
2021-04-28 10:12:05 +00:00
// InitUserDatabase checks the configuration for auth methods and loads users
// if necessary. Call it during initialization.
func InitUserDatabase() {
AuthUsed = cfg.UseFixedAuth || cfg.UseRegistration
2021-04-28 10:12:05 +00:00
if AuthUsed && (cfg.FixedAuthCredentialsPath != "" || cfg.RegistrationCredentialsPath != "") {
2021-04-28 10:12:05 +00:00
ReadUsersFromFilesystem()
}
}
// ReadUsersFromFilesystem reads all user information from filesystem and stores it internally.
func ReadUsersFromFilesystem() {
if cfg.UseFixedAuth {
2021-04-26 16:29:41 +00:00
rememberUsers(usersFromFixedCredentials())
}
if cfg.UseRegistration {
2021-04-26 16:29:41 +00:00
rememberUsers(usersFromRegistrationCredentials())
}
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)
}
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)
2021-04-26 16:29:41 +00:00
u.Source = source
}
2021-04-26 16:29:41 +00:00
return users
}
func usersFromFixedCredentials() (users []*User) {
users = usersFromFile(files.FixedCredentialsJSON(), SourceFixed)
log.Println("Found", len(users), "fixed users")
return users
}
2021-04-26 16:29:41 +00:00
func usersFromRegistrationCredentials() (users []*User) {
users = usersFromFile(files.RegistrationCredentialsJSON(), SourceRegistration)
2021-04-26 16:29:41 +00:00
log.Println("Found", len(users), "registered users")
return users
}
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
}
}
2020-11-14 14:46:04 +00:00
func readTokensToUsers() {
contents, err := ioutil.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 {
commenceSession(username, token)
2020-11-14 14:46:04 +00:00
}
log.Println("Found", len(tmp), "active sessions")
}
2021-04-26 16:29:41 +00:00
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.MarshalIndent(tmp, "", "\t")
2021-04-26 16:29:41 +00:00
if err != nil {
log.Println(err)
return err
}
err = ioutil.WriteFile(files.RegistrationCredentialsJSON(), blob, 0644)
2021-04-26 16:29:41 +00:00
if err != nil {
log.Println(err)
return err
}
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)
} else {
ioutil.WriteFile(files.TokensJSON(), blob, 0644)
}
}