首页 > 后端开发 > Golang > 正文

密码重置功能:在 Golang 中发送电子邮件

Mary-Kate Olsen
发布: 2024-10-01 06:12:30
原创
996 人浏览过

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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!