1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00

Implement registration

This commit is contained in:
Timur Ismagilov 2021-04-26 21:29:41 +05:00
parent 49d072a9ed
commit bcdb38bd68
8 changed files with 96 additions and 13 deletions

View File

@ -4,6 +4,9 @@ run: build
config_run: build
./mycorrhiza -config-path "assets/config.ini" metarrhiza
devconfig_run: build
./mycorrhiza -config-path "assets/devconfig.ini" metarrhiza
build:
go generate
go build .

View File

@ -339,7 +339,6 @@ table { background-color: #eee; }
/* Other stuff */
html { background-color: #ddd;
background-image: url("data:image/svg+xml,%3Csvg width='42' height='44' viewBox='0 0 42 44' xmlns='http://www.w3.org/2000/svg'%3E%3Cg id='Page-1' fill='none' fill-rule='evenodd'%3E%3Cg id='brick-wall' fill='%23bbbbbb' fill-opacity='0.4'%3E%3Cpath d='M0 0h42v44H0V0zm1 1h40v20H1V1zM0 23h20v20H0V23zm22 0h20v20H22V23z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='199' viewBox='0 0 100 199'%3E%3Cg fill='%23bbbbbb' %3E%3Cpath d='M0 199V0h1v1.99L100 199h-1.12L1 4.22V199H0zM100 2h-.12l-1-2H100v2z'%3E%3C/path%3E%3C/g%3E%3C/svg%3E");
} /* heropatterns.com */
header { background-color: #bbb; }
@ -468,8 +467,8 @@ function selectionWrapper(cursorPosition, prefix, postfix = null, el = editTexta
if (postfix == null) {
postfix = prefix
}
text = getSelectedText(el)
result = prefix + text + postfix
let text = getSelectedText(el)
let result = prefix + text + postfix
el.setRangeText(result, start, end, 'select')
el.focus()
placeCursor(end + cursorPosition)

19
assets/devconfig.ini Normal file
View File

@ -0,0 +1,19 @@
WikiName = Mycorrhiza (dev)
NaviTitleIcon = 🧑‍💻
[Hyphae]
HomeHypha = home
UserHypha = u
HeaderLinksHypha = header-links
[Network]
HTTPPort = 1737
URL = http://localhost:1737
[Authorization]
UseFixedAuth = true
FixedAuthCredentialsPath = mycocredentials.json
UseRegistration = true
RegistrationCredentialsPath = mycoregistration.json
LimitRegistration = 3

View File

@ -30,8 +30,8 @@ function selectionWrapper(cursorPosition, prefix, postfix = null, el = editTexta
if (postfix == null) {
postfix = prefix
}
text = getSelectedText(el)
result = prefix + text + postfix
let text = getSelectedText(el)
let result = prefix + text + postfix
el.setRangeText(result, start, end, 'select')
el.focus()
placeCursor(end + cursorPosition)

View File

@ -59,7 +59,4 @@ func parseCliArgs() {
util.UserHypha = util.CanonicalName(util.UserHypha)
util.HeaderLinksHypha = util.CanonicalName(util.HeaderLinksHypha)
user.AuthUsed = util.UseFixedAuth || util.UseRegistration
if user.AuthUsed && (util.FixedCredentialsPath != "" || util.RegistrationCredentialsPath != "") {
user.ReadUsersFromFilesystem()
}
}

View File

@ -186,6 +186,9 @@ func main() {
hyphae.Index(WikiDir)
log.Println("Indexed", hyphae.Count(), "hyphae")
if user.AuthUsed && (util.FixedCredentialsPath != "" || util.RegistrationCredentialsPath != "") {
user.ReadUsersFromFilesystem()
}
history.Start(WikiDir)
shroom.SetHeaderLinks()

View File

@ -13,13 +13,20 @@ import (
// ReadUsersFromFilesystem reads all user information from filesystem and stores it internally. Call it during initialization.
func ReadUsersFromFilesystem() {
if util.UseFixedAuth {
rememberUsers(usersFromFixedCredentials())
}
if util.UseRegistration {
rememberUsers(usersFromRegistrationCredentials())
}
readTokensToUsers()
}
func usersFromFixedCredentials() []*User {
var users []*User
contents, err := ioutil.ReadFile(util.FixedCredentialsPath)
func usersFromFile(path string, source UserSource) (users []*User) {
contents, err := ioutil.ReadFile(path)
if os.IsNotExist(err) {
return
}
if err != nil {
log.Fatal(err)
}
@ -28,12 +35,23 @@ func usersFromFixedCredentials() []*User {
log.Fatal(err)
}
for _, u := range users {
u.Source = SourceFixed
u.Source = source
}
return users
}
func usersFromFixedCredentials() (users []*User) {
users = usersFromFile(util.FixedCredentialsPath, SourceFixed)
log.Println("Found", len(users), "fixed users")
return users
}
func usersFromRegistrationCredentials() (users []*User) {
users = usersFromFile(registrationCredentialsPath(), SourceRegistration)
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 {
@ -71,9 +89,49 @@ func tokenStoragePath() string {
if strings.HasPrefix(dir, util.WikiDir) {
log.Fatal("Error: Wiki storage directory includes private config files")
}
log.Println("Path for saving tokens:", dir)
return dir
}
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
}
func dumpTokens() {
tmp := make(map[string]string)

View File

@ -60,6 +60,10 @@ func Register(username, password string) error {
Source: SourceRegistration,
}
users.Store(username, &u)
err = dumpRegistrationCredentials()
if err != nil {
return err
}
return nil
}