validate

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2016 License: Apache-2.0 Imports: 11 Imported by: 1

README

基于golang实现的邮箱验证

GoDoc

支持令牌的安全存储及验证、自动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("xxx@gmail.com")
	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: xxx@gmail.com

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.

Documentation

Index

Constants

View Source
const (
	// DefaultExpire 默认过期时间(2个小时)
	DefaultExpire = time.Hour * 2
	// DefaultCodeLen 默认验证码的长度
	DefaultCodeLen = 6
	// DefaultGCInterval 默认验证信息的GC间隔
	DefaultGCInterval = time.Second * 60
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CodeValidate added in v1.1.0

type CodeValidate struct {
	// contains filtered or unexported fields
}

CodeValidate 提供验证码验证

func NewCodeValidate added in v1.1.0

func NewCodeValidate(store Store, cfg ...Config) *CodeValidate

NewCodeValidate 创建CodeValidate的实例 store 验证信息存储方式 cfg 配置参数(可使用默认参数)

func (*CodeValidate) Generate added in v1.1.0

func (this *CodeValidate) Generate(email string) (string, error)

Generate 根据邮箱生成验证码,同时返回生成的验证码; 如果获取失败,则返回错误

func (*CodeValidate) Validate added in v1.1.0

func (this *CodeValidate) Validate(email, code string) (isValid bool, err error)

Validate 检查验证信息是否有效 如果验证失败,则返回错误

type Config

type Config struct {
	Expire  time.Duration // 过期的持续时间
	CodeLen int           // 验证码的长度
}

Config 邮箱验证的配置参数

type DataItem added in v1.1.0

type DataItem struct {
	ID         int64         // 唯一标识
	Email      string        // 邮箱
	Code       string        // 验证码
	CreateTime time.Time     // 存储时间
	Expire     time.Duration // 过期的持续时间
}

DataItem 存储验证信息的数据项

type MemoryStore

type MemoryStore struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

MemoryStore 提供内存存储

func (*MemoryStore) Put

func (this *MemoryStore) Put(item DataItem) (int64, error)

Put Put item

func (*MemoryStore) TakeByEmailAndCode added in v1.1.0

func (this *MemoryStore) TakeByEmailAndCode(email, code string) (*DataItem, error)

Take Take item by email and code

func (*MemoryStore) TakeByID added in v1.1.0

func (this *MemoryStore) TakeByID(id int64) (*DataItem, error)

Take Take item by ID

type Store

type Store interface {
	// Put 将元素放入存储,返回存储的ID
	// 如果存储发生异常,则返回错误
	Put(item DataItem) (int64, error)

	// TakeByID 根据ID取出一个元素,返回取出的元素
	// 如果取出元素发生异常,则返回错误
	TakeByID(id int64) (*DataItem, error)

	// TakeByEmailAndCode 根据验证码和验证码取出一个元素,返回取出的元素
	// 如果取出元素发生异常,则返回错误
	TakeByEmailAndCode(email, code string) (*DataItem, error)
}

Store 提供线程安全的验证信息存储接口,支持自动GC过期的元素

func NewMemoryStore

func NewMemoryStore(gcInterval time.Duration) Store

NewMemoryStore 创建基于内存存储的存储实例

type TokenValidate added in v1.1.0

type TokenValidate struct {
	// contains filtered or unexported fields
}

TokenValidate 提供令牌验证

func NewTokenValidate added in v1.1.0

func NewTokenValidate(store Store, cfg ...Config) *TokenValidate

NewTokenValidate 创建TokenValidate的实例 store 验证信息存储方式 cfg 配置参数(可使用默认参数)

func (*TokenValidate) Generate added in v1.1.0

func (this *TokenValidate) Generate(email string) (string, error)

Generate 根据邮箱生成令牌,同时返回生成的令牌; 如果获取失败,则返回错误

func (*TokenValidate) Validate added in v1.1.0

func (this *TokenValidate) Validate(token string) (isValid bool, email string, err error)

Validate 验证令牌是否有效,同时返回有效的邮箱地址; 如果验证失败,则返回错误

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL