# README
基于golang实现的邮箱验证
支持令牌的安全存储及验证、自动GC过期的验证信息
获取
$ go get -v github.com/go-mailer/validate
使用
package main
import (
"fmt"
"time"
"github.com/go-mailer/validate"
)
func main() {
// 创建验证信息存储,每10分钟执行一次GC
store := validate.NewMemoryStore(time.Second * 60 * 10)
// 创建验证信息管理器,验证信息的过期时间为1小时
tokenV := validate.NewTokenValidate(store, validate.Config{Expire: time.Second * 60 * 60})
// 使用邮箱生成验证令牌
token, err := tokenV.Generate("[email protected]")
if err != nil {
panic(err)
}
fmt.Println("Token:", token)
// 验证令牌
isValid, email, err := tokenV.Validate(token)
if err != nil {
panic(err)
}
fmt.Println("Valid:", isValid, ",Email:", email)
}
输出
Token: MS45OGZiNWI0ZTU2NmE2NDVhZjRiYmE2ODNmODY3YzllZQ
Valid: true ,Email: [email protected]
License
Copyright 2016.All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
# Functions
NewCodeValidate 创建CodeValidate的实例 store 验证信息存储方式 cfg 配置参数(可使用默认参数).
NewMemoryStore 创建基于内存存储的存储实例.
NewTokenValidate 创建TokenValidate的实例 store 验证信息存储方式 cfg 配置参数(可使用默认参数).
# Constants
DefaultCodeLen 默认验证码的长度.
DefaultExpire 默认过期时间(2个小时).
DefaultGCInterval 默认验证信息的GC间隔.
# Structs
CodeValidate 提供验证码验证.
Config 邮箱验证的配置参数.
DataItem 存储验证信息的数据项.
MemoryStore 提供内存存储.
TokenValidate 提供令牌验证.
# Interfaces
Store 提供线程安全的验证信息存储接口,支持自动GC过期的元素.