2020-11-14 14:46:04 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-06-27 14:52:55 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-04-28 10:12:05 +00:00
|
|
|
|
2021-06-27 14:52:55 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
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() {
|
2021-05-09 09:36:39 +00:00
|
|
|
AuthUsed = cfg.UseFixedAuth || cfg.UseRegistration
|
2021-04-28 10:12:05 +00:00
|
|
|
|
2021-06-19 04:51:10 +00:00
|
|
|
if AuthUsed {
|
2021-04-28 10:12:05 +00:00
|
|
|
ReadUsersFromFilesystem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadUsersFromFilesystem reads all user information from filesystem and stores it internally.
|
2021-01-09 20:49:48 +00:00
|
|
|
func ReadUsersFromFilesystem() {
|
2021-05-09 09:36:39 +00:00
|
|
|
if cfg.UseFixedAuth {
|
2021-06-27 14:52:55 +00:00
|
|
|
// This one will be removed.
|
2021-04-26 16:29:41 +00:00
|
|
|
rememberUsers(usersFromFixedCredentials())
|
|
|
|
}
|
2021-06-27 14:52:55 +00:00
|
|
|
|
|
|
|
// And this one will be renamed to just "users" in the future.
|
|
|
|
rememberUsers(usersFromRegistrationCredentials())
|
|
|
|
|
|
|
|
// Migrate fixed users to registered
|
|
|
|
tryToMigrate()
|
|
|
|
|
2021-01-09 20:49:48 +00:00
|
|
|
readTokensToUsers()
|
|
|
|
}
|
|
|
|
|
2021-06-27 14:52:55 +00:00
|
|
|
func tryToMigrate() {
|
|
|
|
// Fixed authorization should be removed by the next release (1.13).
|
|
|
|
// So let's try to help fixed users and migrate them over!
|
|
|
|
|
|
|
|
migrated := 0
|
|
|
|
|
|
|
|
for user := range YieldUsers() {
|
|
|
|
if user.Source == SourceFixed {
|
|
|
|
hashedPasswd, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to migrate fixed users:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = ""
|
|
|
|
user.HashedPassword = string(hashedPasswd)
|
|
|
|
user.Source = SourceRegistration
|
|
|
|
migrated++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if migrated > 0 {
|
|
|
|
if err := dumpRegistrationCredentials(); err != nil {
|
|
|
|
log.Fatal("Failed to migrate fixed users:", err)
|
|
|
|
}
|
|
|
|
log.Printf("Migrated %d users", migrated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-12 17:40:43 +00:00
|
|
|
}
|
2021-04-26 16:29:41 +00:00
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2021-06-27 14:52:55 +00:00
|
|
|
func usersFromFixedCredentials() []*User {
|
|
|
|
users := usersFromFile(files.FixedCredentialsJSON(), SourceFixed)
|
2021-01-09 20:49:48 +00:00
|
|
|
log.Println("Found", len(users), "fixed users")
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2021-06-27 14:52:55 +00:00
|
|
|
func usersFromRegistrationCredentials() []*User {
|
|
|
|
users := usersFromFile(files.RegistrationCredentialsJSON(), SourceRegistration)
|
2021-04-26 16:29:41 +00:00
|
|
|
log.Println("Found", len(users), "registered users")
|
|
|
|
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-04-26 17:25:47 +00:00
|
|
|
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)
|
|
|
|
}
|
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-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)
|
|
|
|
}
|
|
|
|
|
2021-05-27 12:04:10 +00:00
|
|
|
blob, err := json.MarshalIndent(tmp, "", "\t")
|
2021-04-26 16:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2021-04-26 17:25:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
} else {
|
2021-04-26 17:25:47 +00:00
|
|
|
ioutil.WriteFile(files.TokensJSON(), blob, 0644)
|
2021-01-09 20:49:48 +00:00
|
|
|
}
|
|
|
|
}
|