googleverifier

package
v0.0.0-...-8210b9c Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ActionLogin    = "login"
	ActionSignup   = "signup"
	ActionResetPwd = "password_reset"
)

ReCaptcha 相关常量

Variables

This section is empty.

Functions

func Setup

func Setup(cfg *Config, httpClient HTTPClient)

Setup 初始化所有服务

func SetupReCaptcha

func SetupReCaptcha(url, secret string, httpClient HTTPClient)

SetupReCaptcha 初始化 reCAPTCHA 服务

func SetupTwoFactor

func SetupTwoFactor(timeStep int64, codeLength uint32, windowSize int)

SetupTwoFactor 初始化双因素认证服务

Types

type Config

type Config struct {
	ReCaptcha struct {
		URL    string
		Secret string
	}
	TwoFactor struct {
		TimeStep   int64
		CodeLength uint32
		WindowSize int
	}
}

Config 配置结构

type HTTPClient

type HTTPClient interface {
	Post(url string, data []byte) ([]byte, error)
}

HTTPClient 接口用于HTTP请求,方便测试时mock

type ReCaptchaRequest

type ReCaptchaRequest struct {
	Event RecapEvent `json:"event"`
}

ReCaptcha 请求结构

type ReCaptchaResponse

type ReCaptchaResponse struct {
	Name            string                `json:"name"`
	RiskAnalysis    ReCaptchaRiskAnalysis `json:"riskAnalysis"`
	TokenProperties struct {
		Valid              bool      `json:"valid"`
		InvalidReason      string    `json:"invalidReason"`
		Hostname           string    `json:"hostname"`
		AndroidPackageName string    `json:"androidPackageName"`
		IosBundleId        string    `json:"iosBundleId"`
		Action             string    `json:"action"`
		CreateTime         time.Time `json:"createTime"`
	} `json:"tokenProperties"`
	AccountDefenderAssessment struct {
		Labels []interface{} `json:"labels"`
	} `json:"accountDefenderAssessment"`
	Error *struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
		Status  string `json:"status"`
	} `json:"error"`
}

ReCaptcha 响应结构

type ReCaptchaRiskAnalysis

type ReCaptchaRiskAnalysis struct {
	Score                  float64       `json:"score"`
	Reasons                []interface{} `json:"reasons"`
	ExtendedVerdictReasons []interface{} `json:"extendedVerdictReasons"`
}

type ReCaptchaService

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

ReCaptchaService 处理 Google reCAPTCHA 验证

func GetReCaptcha

func GetReCaptcha() *ReCaptchaService

GetReCaptcha 获取 reCAPTCHA 服务实例

func NewReCaptchaService

func NewReCaptchaService(url, secret string, client HTTPClient) *ReCaptchaService

NewReCaptchaService 创建新的ReCaptchaService实例

func (*ReCaptchaService) Verify

func (s *ReCaptchaService) Verify(action, recToken string, minScore float64) (bool, error)

Verify 验证reCAPTCHA token

type RecapEvent

type RecapEvent struct {
	Token          string `json:"token"`
	ExpectedAction string `json:"expectedAction"`
	SiteKey        string `json:"siteKey"`
}

type TwoFactorAuth

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

TwoFactorAuth 处理双因素认证

func GetTwoFactor

func GetTwoFactor() *TwoFactorAuth

GetTwoFactor 获取双因素认证服务实例

func NewTwoFactorAuth

func NewTwoFactorAuth(opts ...TwoFactorOption) *TwoFactorAuth

NewTwoFactorAuth 创建新的TwoFactorAuth实例

func (*TwoFactorAuth) GenerateQRCodeURL

func (a *TwoFactorAuth) GenerateQRCodeURL(issuer, accountName, secret string) string

GenerateQRCodeURL 生成用于扫描的二维码URL

func (*TwoFactorAuth) GenerateSecret

func (a *TwoFactorAuth) GenerateSecret() (string, error)

GenerateSecret 生成随机密钥

func (*TwoFactorAuth) VerifyCode

func (a *TwoFactorAuth) VerifyCode(secret string, code int32) bool

VerifyCode 验证TOTP码

type TwoFactorOption

type TwoFactorOption func(*TwoFactorAuth)

TwoFactorOption 定义TwoFactorAuth的可选配置

func WithCodeLength

func WithCodeLength(length uint32) TwoFactorOption

WithCodeLength 设置验证码长度

func WithTimeStep

func WithTimeStep(seconds int64) TwoFactorOption

WithTimeStep 设置TOTP时间步长

func WithWindowSize

func WithWindowSize(size int) TwoFactorOption

WithWindowSize 设置时间窗口大小

Jump to

Keyboard shortcuts

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