generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from moul/dev/moul/new-captchas
- Loading branch information
Showing
13 changed files
with
280 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package captcha | ||
|
||
import ( | ||
"math/rand" | ||
"strings" | ||
|
||
"moul.io/banner" | ||
) | ||
|
||
type bannerCaptcha struct { | ||
word string | ||
} | ||
|
||
func NewBannerCaptcha() Captcha { | ||
letterRunes := []rune("abcdefghijklmnopqrstuvwxyz") | ||
b := make([]rune, 5) | ||
for i := range b { | ||
b[i] = letterRunes[rand.Intn(len(letterRunes))] // nolint:gosec | ||
} | ||
return bannerCaptcha{word: string(b)} | ||
} | ||
|
||
func (c bannerCaptcha) Question() (string, error) { | ||
return banner.Inline(c.word), nil | ||
} | ||
|
||
func (c bannerCaptcha) Validate(input string) (bool, error) { | ||
input = strings.ToLower(input) | ||
input = strings.TrimSpace(input) | ||
word := strings.ToLower(c.word) | ||
return strings.EqualFold(input, word), nil | ||
} | ||
|
||
var _ Captcha = (*bannerCaptcha)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package captcha_test | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
|
||
"moul.io/captcha/captcha" | ||
) | ||
|
||
func ExampleNewBannerCaptcha() { | ||
rand.Seed(42) | ||
captcha := captcha.NewBannerCaptcha() | ||
question, _ := captcha.Question() | ||
fmt.Println(question) | ||
|
||
valid, _ := captcha.Validate("lorem") | ||
fmt.Println(valid) | ||
|
||
valid, _ = captcha.Validate("ipsum") | ||
fmt.Println(valid) | ||
|
||
valid, _ = captcha.Validate("HRUKP") | ||
fmt.Println(valid) | ||
|
||
valid, _ = captcha.Validate("hrukp") | ||
fmt.Println(valid) | ||
|
||
valid, _ = captcha.Validate("\n hRuKP\t \n\n \t") | ||
fmt.Println(valid) | ||
|
||
valid, _ = captcha.Validate("dolor") | ||
fmt.Println(valid) | ||
|
||
// Output: | ||
// _ _ | ||
// | |_ _ _ _ _ | |__ _ __ | ||
// | ' \ | '_|| || || / /| '_ \ | ||
// |_||_||_| \_,_||_\_\| .__/ | ||
// |_| | ||
// false | ||
// false | ||
// true | ||
// true | ||
// true | ||
// false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package captcha | ||
|
||
type Captcha interface { | ||
Question() (string, error) | ||
Validate(input string) (bool, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package captcha | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type mathCaptcha struct { | ||
equation string | ||
result int | ||
} | ||
|
||
func NewMathCaptcha() Captcha { | ||
a := rand.Intn(5) + 1 // nolint:gosec,gomnd | ||
b := rand.Intn(5) + 1 // nolint:gosec,gomnd | ||
equation := fmt.Sprintf("%d + %d", a, b) | ||
result := a + b | ||
return mathCaptcha{equation, result} | ||
} | ||
|
||
func (c mathCaptcha) Question() (string, error) { | ||
return c.equation, nil | ||
} | ||
|
||
func (c mathCaptcha) Validate(input string) (bool, error) { | ||
input = strings.TrimSpace(input) | ||
if input == "" { | ||
return false, nil | ||
} | ||
nb, err := strconv.Atoi(input) | ||
if err != nil { | ||
return false, fmt.Errorf("invalid input: %w", err) | ||
} | ||
return nb == c.result, nil | ||
} | ||
|
||
var _ Captcha = (*mathCaptcha)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package captcha_test | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
|
||
"moul.io/captcha/captcha" | ||
) | ||
|
||
func ExampleNewMathCaptcha() { | ||
rand.Seed(42) | ||
captcha := captcha.NewMathCaptcha() | ||
question, _ := captcha.Question() | ||
fmt.Println(question) | ||
|
||
valid, _ := captcha.Validate("42") | ||
fmt.Println(valid) | ||
|
||
valid, _ = captcha.Validate("lorem ipsum") | ||
fmt.Println(valid) | ||
|
||
valid, _ = captcha.Validate("1 + 3") | ||
fmt.Println(valid) | ||
|
||
valid, _ = captcha.Validate("4") | ||
fmt.Println(valid) | ||
|
||
valid, _ = captcha.Validate("\n 4\t \n\n \t") | ||
fmt.Println(valid) | ||
|
||
// Output: | ||
// 1 + 3 | ||
// false | ||
// false | ||
// false | ||
// true | ||
// true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package captcha | ||
|
||
import ( | ||
"math/rand" | ||
) | ||
|
||
func NewRandomCaptcha() Captcha { | ||
availables := []func() Captcha{ | ||
NewBannerCaptcha, | ||
NewMathCaptcha, | ||
} | ||
return availables[rand.Intn(len(availables))]() // nolint:gosec | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package captcha_test | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
|
||
"moul.io/captcha/captcha" | ||
) | ||
|
||
func ExampleNewRandomCaptcha() { | ||
rand.Seed(42) | ||
|
||
// first captcha | ||
c := captcha.NewRandomCaptcha() | ||
question, _ := c.Question() | ||
fmt.Println(question) | ||
|
||
// second captcha | ||
c = captcha.NewRandomCaptcha() | ||
question, _ = c.Question() | ||
fmt.Println(question) | ||
|
||
// Output: | ||
// 3 + 4 | ||
// _ _ | ||
// _ __ | |_ | |_ _ _ ___ | ||
// | '_ \| _|| _|| || |/ -_) | ||
// | .__/ \__| \__| \_,_|\___| | ||
// |_| | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.