Adding encryption package to encrypt email
This commit is contained in:
@ -81,7 +81,7 @@ func main() {
|
||||
flag.IntVar(&cfg.smtp.port, "smtpport", 1025, "smtp host")
|
||||
flag.StringVar(&cfg.smtp.username, "smtpuser", "user", "smtp user")
|
||||
flag.StringVar(&cfg.smtp.password, "smtppwd", "password", "smtp password")
|
||||
flag.StringVar(&cfg.secretkey, "secret", "secRetKeY", "secret key")
|
||||
flag.StringVar(&cfg.secretkey, "secretkey", "b47df3d8380241c1177f13bdd69c6a60", "secret key")
|
||||
flag.StringVar(&cfg.frontend, "frontend", "http://localhost:4000", "frontend address")
|
||||
|
||||
flag.Parse()
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"myapp/internal/cards"
|
||||
"myapp/internal/cards/encryption"
|
||||
"myapp/internal/models"
|
||||
"myapp/internal/urlsigner"
|
||||
"net/http"
|
||||
@ -480,7 +481,18 @@ func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := app.DB.GetUserByEmail(payload.Email)
|
||||
encryptor := encryption.Encryption{
|
||||
Key: []byte(app.config.secretkey),
|
||||
}
|
||||
|
||||
realEmail, err := encryptor.Decrypt(payload.Email)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := app.DB.GetUserByEmail(realEmail)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
|
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"myapp/internal/cards"
|
||||
"myapp/internal/cards/encryption"
|
||||
"myapp/internal/models"
|
||||
"myapp/internal/urlsigner"
|
||||
"net/http"
|
||||
@ -328,6 +329,7 @@ func (app *application) ForgotPassword(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (app *application) ShowResetPassword(w http.ResponseWriter, r *http.Request) {
|
||||
email := r.URL.Query().Get("email")
|
||||
theURL := r.RequestURI
|
||||
testURL := fmt.Sprintf("%s%s", app.config.frontend, theURL)
|
||||
|
||||
@ -347,8 +349,18 @@ func (app *application) ShowResetPassword(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
|
||||
encryptor := encryption.Encryption{
|
||||
Key: []byte(app.config.secretkey),
|
||||
}
|
||||
|
||||
encryptedEmail, err := encryptor.Encrypt(email)
|
||||
if err != nil {
|
||||
app.errorLog.Println("Encryption failed", err)
|
||||
return
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
data["email"] = r.URL.Query().Get("email")
|
||||
data["email"] = encryptedEmail
|
||||
if err := app.renderTemplate(w, r, "reset-password", &templateData{
|
||||
Data: data,
|
||||
}); err != nil {
|
||||
|
@ -85,7 +85,7 @@ func main() {
|
||||
"DSN",
|
||||
)
|
||||
flag.StringVar(&cfg.api, "api", "http://localhost:4001", "URL to api")
|
||||
flag.StringVar(&cfg.secretkey, "secret", "secRetKeY", "secret key")
|
||||
flag.StringVar(&cfg.secretkey, "secretkey", "b47df3d8380241c1177f13bdd69c6a60", "secret key")
|
||||
flag.StringVar(&cfg.frontend, "frontend", "http://localhost:4000", "frontend address")
|
||||
|
||||
flag.Parse()
|
||||
|
@ -34,8 +34,15 @@ Forgot Password
|
||||
{{define "js"}}
|
||||
<script type="module">
|
||||
import {forgot} from "/static/js/login.js"
|
||||
document.getElementById("reset-btn").addEventListener("click", () => {
|
||||
document.getElementById("reset-btn").addEventListener("click", (event) => {
|
||||
event.preventDefault();
|
||||
forgot({{.API}});
|
||||
})
|
||||
document.getElementById("email").addEventListener("keypress", (event) => {
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
forgot({{.API}});
|
||||
}
|
||||
})
|
||||
</script>
|
||||
{{end}}
|
||||
|
Reference in New Issue
Block a user