Skip to content

Commit

Permalink
Merge pull request #12 from moul/dev/moul/new-captchas
Browse files Browse the repository at this point in the history
  • Loading branch information
moul authored Mar 28, 2021
2 parents 5ad88c7 + b00df32 commit 84872db
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 31 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15.x
go-version: 1.16.x
- name: Cache Go modules
uses: actions/cache@v2.1.4
with:
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:
strategy:
matrix:
golang:
- 1.15.x
- 1.16.x
steps:
- uses: actions/checkout@v2
- name: Install Go
Expand All @@ -83,7 +83,7 @@ jobs:
strategy:
matrix:
golang:
- 1.15.x
- 1.16.x
env:
OS: macos-latest
GOLANG: ${{ matrix.golang }}
Expand Down Expand Up @@ -123,10 +123,10 @@ jobs:
strategy:
matrix:
golang:
- 1.12.x
- 1.13.x
- 1.14.x
- 1.15.x
- 1.16.x
env:
OS: ubuntu-latest
GOLANG: ${{ matrix.golang }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
if: steps.semantic.outputs.new-release-published == 'true'
uses: actions/setup-go@v2
with:
go-version: 1.15.x
go-version: 1.16.x
-
name: Cache Go modules
if: steps.semantic.outputs.new-release-published == 'true'
Expand Down
34 changes: 34 additions & 0 deletions captcha/banner.go
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)
46 changes: 46 additions & 0 deletions captcha/banner_test.go
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
}
6 changes: 6 additions & 0 deletions captcha/captcha.go
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)
}
39 changes: 39 additions & 0 deletions captcha/math.go
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)
38 changes: 38 additions & 0 deletions captcha/math_test.go
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
}
13 changes: 13 additions & 0 deletions captcha/random.go
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
}
30 changes: 30 additions & 0 deletions captcha/random_test.go
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
// _ _
// _ __ | |_ | |_ _ _ ___
// | '_ \| _|| _|| || |/ -_)
// | .__/ \__| \__| \_,_|\___|
// |_|
}
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 84872db

Please sign in to comment.