prosody-password/internal/services/prosody/account.go

55 lines
1.3 KiB
Go

package prosody
import (
"log"
"os"
"reflect"
"strings"
)
type account struct {
Salt string `prosody:"salt"`
StoredKey string `prosody:"stored_key"`
IterationCount string `prosody:"iteration_count"`
}
func (acc *account) unmarshal(data map[string]interface{}) {
valueOfPerson := reflect.ValueOf(acc).Elem()
typeOfPerson := valueOfPerson.Type()
for i := 0; i < valueOfPerson.NumField(); i++ {
field := valueOfPerson.Field(i)
tag := typeOfPerson.Field(i).Tag.Get("prosody")
if val, ok := data[tag]; ok {
field.Set(reflect.ValueOf(val))
}
}
}
// loadAccount read the user .dat file and retrieves the data store in it
func (p *Prosody) loadAccount(username string) (*account, error) {
if strings.HasSuffix(username, p.plainDomain) {
username = strings.Replace(username, p.plainDomain, "", -1)
}
data, err := os.ReadFile(p.accountsPath + username + ".dat")
if err != nil {
return nil, err
}
lines := strings.Split(string(data), "\n")
mapValues := make(map[string]interface{})
for _, line := range lines {
if strings.Contains(line, "=") {
parts := strings.Split(line, "=")
key := strings.Trim(strings.TrimSpace(parts[0]), "[]\"")
log.Println("key", key)
value := strings.TrimSpace(strings.Trim(parts[1], "\"; "))
mapValues[key] = value
}
}
acc := &account{}
acc.unmarshal(mapValues)
return acc, nil
}