首頁  >  文章  >  後端開發  >  密碼重設功能:在 Golang 中傳送電子郵件

密碼重設功能:在 Golang 中傳送電子郵件

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-10-01 06:12:30997瀏覽

Password Reset Feature: Sending Email in Golang

在撰寫本文時,我正在我的應用程式 Task-inator 3000 中實作一項為使用者重設密碼的功能。只是記錄我的思考過程和採取的步驟


規劃

我正在考慮這樣的流程:

  1. 使用者點選「忘記密碼?」按鈕
  2. 向要求電子郵件的使用者顯示模式
  3. 檢查電子郵件是否存在,並將 10 個字元長的 OTP 發送到電子郵件
  4. Modal 現在要求輸入 OTP 和新密碼
  5. 密碼已為使用者進行雜湊處理與更新

關注點分離

前端

  • 建立一個輸入電子郵件的模式
  • 相同的模式然後接受 OTP 和新密碼

後端

  • 建立用於發送電子郵件的 API
  • 建立重設密碼的API

我將從後端開始

後端

如上所述,我們需要兩個 API

1. 發送郵件

API只需要接收使用者的郵件,成功後不回傳任何內容。因此,建立控制器如下:

// controllers/passwordReset.go
func SendPasswordResetEmail(c *fiber.Ctx) error {
    type Input struct {
        Email string `json:"email"`
    }

    var input Input

    err := c.BodyParser(&input)
    if err != nil {
        return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
            "error": "invalid data",
        })
    }

    // TODO: send email with otp to user

    return c.SendStatus(fiber.StatusNoContent)
}

現在為其增加一條路線:

// routes/routes.go

// password reset
api.Post("/send-otp", controllers.SendPasswordResetEmail)

我將使用 Golang 標準庫中的 net/smtp。

閱讀文件後,我認為最好在專案初始化時建立 SMTPClient。因此,我會在 /config 目錄中建立一個檔案 smtpConnection.go。

在此之前,我會將以下環境變數加入我的 .env 或生產伺服器。

SMTP_HOST="smtp.zoho.in"
SMTP_PORT="587"
SMTP_EMAIL="<myemail>"
SMTP_PASSWORD="<mypassword>"

我使用的是 zohomail,因此其 smtp 主機和連接埠(用於 TLS)如此處所述。

// config/smtpConnection.go
package config

import (
    "crypto/tls"
    "fmt"
    "net/smtp"
    "os"
)

var SMTPClient *smtp.Client

func SMTPConnect() {
    host := os.Getenv("SMTP_HOST")
    port := os.Getenv("SMTP_PORT")
    email := os.Getenv("SMTP_EMAIL")
    password := os.Getenv("SMTP_PASSWORD")

    smtpAuth := smtp.PlainAuth("", email, password, host)

    // connect to smtp server
    client, err := smtp.Dial(host + ":" + port)
    if err != nil {
        panic(err)
    }

    SMTPClient = client
    client = nil

    // initiate TLS handshake
    if ok, _ := SMTPClient.Extension("STARTTLS"); ok {
        config := &tls.Config{ServerName: host}
        if err = SMTPClient.StartTLS(config); err != nil {
            panic(err)
        }
    }

    // authenticate
    err = SMTPClient.Auth(smtpAuth)
    if err != nil {
        panic(err)
    }

    fmt.Println("SMTP Connected")
}

為了抽象,我將在/utils 中建立一個passwordReset.go 檔案。該檔案目前具有以下功能:

  • 產生 OTP:產生一個唯一的字母數字 10 位 OTP 以在電子郵件中發送
  • AddOTPtoRedis:以鍵值格式將 OTP 新增至 Redis,其中
key -> password-reset:<email>
value -> hashed otp
expiry -> 10 mins

出於安全原因,我儲存 OTP 的雜湊值而不是 OTP 本身

  • SendOTP:將產生的 OTP 傳送到使用者的電子郵件

在寫程式碼時,我發現我們需要 5 個常數:

  • OTP 的 redis 金鑰前綴
  • OTP 過期時間
  • 用於 OTP 產生的字元集
  • 電子郵件範本
  • OTP 長度

我會立即將它們加入 /utils/constants.go

// utils/constants.go
package utils

import "time"

const (
    authTokenExp       = time.Minute * 10
    refreshTokenExp    = time.Hour * 24 * 30 // 1 month
    blacklistKeyPrefix = "blacklisted:"
    otpKeyPrefix       = "password-reset:"
    otpExp             = time.Minute * 10
    otpCharSet         = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    emailTemplate      = "To: %s\r\n" +
        "Subject: Task-inator 3000 Password Reset\r\n" +
        "\r\n" +
        "Your OTP for password reset is %s\r\n"

    // public because needed for testing
    OTPLength = 10
)

(請注意,我們將從 crypto/rand 導入,而不是 math/rand,因為它將提供真正的隨機性)

// utils/passwordReset.go
package utils

import (
    "context"
    "crypto/rand"
    "fmt"
    "math/big"
    "os"
    "task-inator3000/config"

    "golang.org/x/crypto/bcrypt"
)

func GenerateOTP() string {
    result := make([]byte, OTPLength)
    charsetLength := big.NewInt(int64(len(otpCharSet)))

    for i := range result {
        // generate a secure random number in the range of the charset length
        num, _ := rand.Int(rand.Reader, charsetLength)
        result[i] = otpCharSet[num.Int64()]
    }

    return string(result)
}

func AddOTPtoRedis(otp string, email string, c context.Context) error {
    key := otpKeyPrefix + email

    // hashing the OTP
    data, _ := bcrypt.GenerateFromPassword([]byte(otp), 10)

    // storing otp with expiry
    err := config.RedisClient.Set(c, key, data, otpExp).Err()
    if err != nil {
        return err
    }

    return nil
}

func SendOTP(otp string, recipient string) error {
    sender := os.Getenv("SMTP_EMAIL")
    client := config.SMTPClient

    // setting the sender
    err := client.Mail(sender)
    if err != nil {
        return err
    }

    // set recipient
    err = client.Rcpt(recipient)
    if err != nil {
        return err
    }

    // start writing email
    writeCloser, err := client.Data()
    if err != nil {
        return err
    }

    // contents of the email
    msg := fmt.Sprintf(emailTemplate, recipient, otp)

    // write the email
    _, err = writeCloser.Write([]byte(msg))
    if err != nil {
        return err
    }

    // close writecloser and send email
    err = writeCloser.Close()
    if err != nil {
        return err
    }

    return nil
}

函數GenerateOTP()無需模擬即可測試(單元測試),因此為它編寫了一個簡單的測試

package utils_test

import (
    "task-inator3000/utils"
    "testing"
)

func TestGenerateOTP(t *testing.T) {
    result := utils.GenerateOTP()

    if len(result) != utils.OTPLength {
        t.Errorf("Length of OTP was not %v. OTP: %v", utils.OTPLength, result)
    }
}

現在我們需要將它們全部放在控制器內。在這之前,我們需要確保資料庫中存在提供的電子郵件地址。

控制器的完整程式碼如下:

func SendPasswordResetEmail(c *fiber.Ctx) error {
    type Input struct {
        Email string `json:"email"`
    }

    var input Input

    err := c.BodyParser(&input)
    if err != nil {
        return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
            "error": "invalid data",
        })
    }

    // check if user with email exists
    users := config.DB.Collection("users")
    filter := bson.M{"_id": input.Email}
    err = users.FindOne(c.Context(), filter).Err()
    if err != nil {
        if err == mongo.ErrNoDocuments {
            return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
                "error": "user with given email not found",
            })
        }

        return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
            "error": "error while finding in the database:\n" + err.Error(),
        })
    }

    // generate otp and add it to redis
    otp := utils.GenerateOTP()
    err = utils.AddOTPtoRedis(otp, input.Email, c.Context())
    if err != nil {
        return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
            "error": err.Error(),
        })
    }

    // send the otp to user through email
    err = utils.SendOTP(otp, input.Email)
    if err != nil {
        return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
            "error": err.Error(),
        })
    }

    return c.SendStatus(fiber.StatusNoContent)
}

我們可以透過向正確的 URL 發送 POST 請求來測試 API。 cURL 範例如下:

curl --location 'localhost:3000/api/send-otp' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email": "yashjaiswal.cse@gmail.com"
}'

我們將在本系列的下一部分中建立下一個 API - 用於重設密碼

以上是密碼重設功能:在 Golang 中傳送電子郵件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:將 HTMX 加入 GO下一篇:將 HTMX 加入 GO