mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-06-10 14:44:06 +00:00
Automatically migrate fixed users to registered
Now registered users are loaded unconditionally, ignoring the config field.
This commit is contained in:
parent
ea2c94980e
commit
16861bd3d4
@ -2,12 +2,13 @@ package user
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
|
||||||
"github.com/bouncepaw/mycorrhiza/util"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||||
|
"github.com/bouncepaw/mycorrhiza/util"
|
||||||
"github.com/bouncepaw/mycorrhiza/files"
|
"github.com/bouncepaw/mycorrhiza/files"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,14 +25,47 @@ func InitUserDatabase() {
|
|||||||
// ReadUsersFromFilesystem reads all user information from filesystem and stores it internally.
|
// ReadUsersFromFilesystem reads all user information from filesystem and stores it internally.
|
||||||
func ReadUsersFromFilesystem() {
|
func ReadUsersFromFilesystem() {
|
||||||
if cfg.UseFixedAuth {
|
if cfg.UseFixedAuth {
|
||||||
|
// This one will be removed.
|
||||||
rememberUsers(usersFromFixedCredentials())
|
rememberUsers(usersFromFixedCredentials())
|
||||||
}
|
}
|
||||||
if cfg.UseRegistration {
|
|
||||||
|
// And this one will be renamed to just "users" in the future.
|
||||||
rememberUsers(usersFromRegistrationCredentials())
|
rememberUsers(usersFromRegistrationCredentials())
|
||||||
}
|
|
||||||
|
// Migrate fixed users to registered
|
||||||
|
tryToMigrate()
|
||||||
|
|
||||||
readTokensToUsers()
|
readTokensToUsers()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func usersFromFile(path string, source UserSource) (users []*User) {
|
func usersFromFile(path string, source UserSource) (users []*User) {
|
||||||
contents, err := ioutil.ReadFile(path)
|
contents, err := ioutil.ReadFile(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@ -51,21 +85,20 @@ func usersFromFile(path string, source UserSource) (users []*User) {
|
|||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
func usersFromFixedCredentials() (users []*User) {
|
func usersFromFixedCredentials() []*User {
|
||||||
users = usersFromFile(files.FixedCredentialsJSON(), SourceFixed)
|
users := usersFromFile(files.FixedCredentialsJSON(), SourceFixed)
|
||||||
log.Println("Found", len(users), "fixed users")
|
log.Println("Found", len(users), "fixed users")
|
||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
func usersFromRegistrationCredentials() (users []*User) {
|
func usersFromRegistrationCredentials() []*User {
|
||||||
users = usersFromFile(files.RegistrationCredentialsJSON(), SourceRegistration)
|
users := usersFromFile(files.RegistrationCredentialsJSON(), SourceRegistration)
|
||||||
log.Println("Found", len(users), "registered users")
|
log.Println("Found", len(users), "registered users")
|
||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
func rememberUsers(uu []*User) {
|
func rememberUsers(userList []*User) {
|
||||||
// uu is used to not shadow the `users` in `users.go`.
|
for _, user := range userList {
|
||||||
for _, user := range uu {
|
|
||||||
users.Store(user.Name, user)
|
users.Store(user.Name, user)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user