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

57 lines
1.4 KiB
Go
Raw Normal View History

2023-07-05 22:07:10 +02:00
package prosody
import (
"os"
"reflect"
"strings"
)
type account struct {
2023-08-09 20:07:49 +02:00
User string
2023-07-07 23:36:05 +02:00
Salt string `prosody:"salt"`
StoredKey string `prosody:"stored_key"`
2023-07-07 23:40:57 +02:00
IterationCount string `prosody:"iteration_count"`
2023-07-05 22:07:10 +02:00
}
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)
2023-07-07 23:06:47 +02:00
tag := typeOfPerson.Field(i).Tag.Get("prosody")
2023-07-05 22:07:10 +02:00
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) {
2023-08-09 19:30:07 +02:00
if strings.Contains(username, "@") && strings.HasSuffix(username, p.plainDomain) {
2023-07-07 23:06:47 +02:00
username = strings.Replace(username, p.plainDomain, "", -1)
2023-08-09 19:34:51 +02:00
username = strings.Replace(username, "@", "", -1)
2023-07-07 23:06:47 +02:00
}
2023-07-05 22:07:10 +02:00
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]), "[]\"")
value := strings.TrimSpace(strings.Trim(parts[1], "\"; "))
mapValues[key] = value
}
}
2023-08-09 20:07:49 +02:00
acc := &account{
User: username,
}
2023-07-05 22:07:10 +02:00
acc.unmarshal(mapValues)
return acc, nil
}