47 lines
1.5 KiB
Go
47 lines
1.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/tls"
|
|
"encoding/base64"
|
|
gomail "gopkg.in/mail.v2"
|
|
)
|
|
|
|
func generateRandomPassword(length int) (string, error) {
|
|
buffer := make([]byte, length)
|
|
_, err := rand.Read(buffer)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
password := base64.StdEncoding.EncodeToString(buffer)[:length]
|
|
return password, nil
|
|
}
|
|
|
|
func (h *SilvermobiHandler) ForgetAccount(ctx context.Context, username string, namespace string) (response bool, access_Code string) {
|
|
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, username, namespace)
|
|
if err == nil {
|
|
message := []byte("Hello dear,\nYour new Silvermobi password is: ")
|
|
password, _ := generateRandomPassword(10)
|
|
update := h.Services.MobilityAccounts.UpdatePassword(ctx, account.ID, password)
|
|
if update == false {
|
|
return false, ""
|
|
}
|
|
m := gomail.NewMessage()
|
|
m.SetHeader("From", h.Config.GetString("emailing.smtp.username"))
|
|
m.SetHeader("To", username)
|
|
m.SetHeader("Subject", "Silvermobi Password Recovery")
|
|
message = append(message, []byte(password)...)
|
|
m.SetBody("text/plain", string(message))
|
|
d := gomail.NewDialer(h.Config.GetString("emailing.smtp.host"), h.Config.GetInt("emailing.smtp.port"), h.Config.GetString("emailing.smtp.username"),
|
|
h.Config.GetString("emailing.smtp.password"))
|
|
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
|
if err := d.DialAndSend(m); err != nil {
|
|
return false, ""
|
|
}
|
|
return true, password
|
|
} else {
|
|
return false, ""
|
|
}
|
|
}
|