secure

package
v0.0.0-...-f28940e Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

ワンタイムパスワードを作成、検証します。

Example:

accountName := "example@example.com"
otp, _ := OnetimePasswordNew(accountName)

secret := GetSecret() // データベースに保存
public := GetPublic() // ユーザに提供

passcode := Send(public) // Public()は例: ユーザにpublicを提供しそれを使用してパスコードを生成してもらいそれをもらう
if ValidateOnetimePassword(passcode, secret) {
	fmt.Println("OK")
	SaveSecret(secret) // SaveSecret()は例: secretを保存する
} else {
	fmt.Println("NO")
}

Note: secretをデータベースに保存する前に一度、ユーザにパスコードを生成してもらい結果を検証してください

Google reCAPCHAの検証を行う

Based on: https://qiita.com/supertaihei02/items/fb15726fd603de7dcefb

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateOnetimePassword

func ValidateOnetimePassword(passcode string, secret string) bool

ワンタイムパスワードを検証する。 passcodeは、ユーザから取得したパスコード。 secretは、サーバー内で保存するkey

func ValidatePW

func ValidatePW(pw string, hashedPw []byte, salt []byte) bool

パスワードを検証します

Types

type HashedPassword

type HashedPassword struct {
	Key  []byte
	Salt []byte
}

func PWHash

func PWHash(pw string) (*HashedPassword, error)

パスワードとSEEDを使用してパスワードをハッシュ化します DBにパスワードを保存する場合は必ずハッシュ化して保存すること

最大長: 128文字

  • ハッシュアルゴリズム: Argon2
  • Time: 1s
  • Memory cost: 64*1024 (64MB)
  • Thured: 1
  • key length: 32

type OnetimePassword

type OnetimePassword struct {
	GenerateOpt *totp.GenerateOpts
	Key         *otp.Key
}

func NewOnetimePassword

func NewOnetimePassword(accountName string) (*OnetimePassword, error)

OTP認証を使用します

OTP仕様

  • 有効時間: 30s
  • Secretバイト長: 20byte
  • TOTPハッシュ桁: 6桁
  • ハッシュアルゴリズム: SHA1
  • 乱数生成: rand.Reader

func (*OnetimePassword) GetPublic

func (o *OnetimePassword) GetPublic() string

ワンタイムパスワードのpublic ユーザはこのkeyをAuthenticator appに入力することで、ワンタイムパスワードを登録できます。

func (*OnetimePassword) GetSecret

func (o *OnetimePassword) GetSecret() string

ワンタイムパスワードのSecret keyを取得する。 このSecretはサーバーサイドで検証するためユーザには提供しないようにしてください。

Example:

secret = o.GetSecret()
passcode = ""
if totp.Validate(passcode, secret) {
	...
}

type ReCaptcha

type ReCaptcha struct {
	ServerName string
	Secret     string
}

func NewReCaptcha

func NewReCaptcha() *ReCaptcha

func (*ReCaptcha) Validate

func (c *ReCaptcha) Validate(token string, remoteIp string) (bool, error)

reCAPTCHAの検証を行い、結果をboolで返す 0.5を閾値とする

func (*ReCaptcha) ValidateOrder

func (c *ReCaptcha) ValidateOrder(token string, remoteIp string) (*RecaptchaResponse, error)

reCAPTCHAの検証を行う

type RecaptchaResponse

type RecaptchaResponse struct {
	Success     bool      `json:"success"`
	Score       float64   `json:"score"`
	Action      string    `json:"action"`
	ChallengeTS time.Time `json:"challenge_ts"`
	Hostname    string    `json:"hostname"`
	ErrorCodes  []string  `json:"error-codes"`
}

Jump to

Keyboard shortcuts

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