Skip to content

Commit

Permalink
first release
Browse files Browse the repository at this point in the history
  • Loading branch information
حمید رضا حسنی یاقوتی authored and حمید رضا حسنی یاقوتی committed Nov 10, 2020
1 parent ad1b8cd commit 12d8bcb
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 4 deletions.
38 changes: 38 additions & 0 deletions _example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"log"
"net/http"

ic "github.com/IrisDev-net/irisCaptchaGo"
)

const myIrisCaptchaSecret = `533a64afb4c496cc34dfd00d1ecbd45cfa2784b2c3eba4aa02e7a4dcbe081aa40x2711`

func main() {
ICH, err := ic.NewIrisCaptchaHandler(myIrisCaptchaSecret)
if err != nil {
log.Panic(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
c := r.FormValue("irisCaptcha")
//! !DOSE NOT RECOMMENDED - for validation without ip Checking
// res, err := ICH.Validate(c, "")

// * Note that is not working on local host, if you want to test it on your localhost use ICH.Validate(c, "") instead.
res, err := ICH.Validate(c, r.RemoteAddr)
if err != nil {
log.Panic(err)
}
if !res.Success {
w.Write([]byte("Wrong Captcha Answer"))
return
} else {
w.Write([]byte("Hooraa Captcha is Solved Correctly"))
return
}

})

http.ListenAndServe(":8090", nil)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/IrisDev-net/irisCaptchaGo

go 1.15

require github.com/dgrijalva/jwt-go v3.2.0+incompatible
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github.com/dgrijalva/jwt-go v1.0.2 h1:KPldsxuKGsS2FPWsNeg9ZO18aCrGKujPoWXn2yo+KQM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
39 changes: 35 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package irisCaptchaGo
package iriscaptchago

import (
"crypto/rsa"
Expand All @@ -12,6 +12,7 @@ import (
"net/url"
"strings"
"sync"
"testing"
"time"

"log"
Expand All @@ -21,6 +22,7 @@ import (

//validationUrl : is where the http request sent to
const validationUrl = `https://captcha.irisdev.net/check`
const IrisDevServer = `https://captcha.irisdev.net`

var (
// ErrHandlerTimeout = http.ErrHandlerTimeout
Expand Down Expand Up @@ -62,7 +64,7 @@ func NewIrisCaptchaHandler(secret string) (IrisCaptchaHandler, error) {
h := new(irisCaptchaHandler)
var err error

h.provider = "https://captcha.irisdev.net"
h.provider = IrisDevServer
err = h.init(secret)
return h, err
}
Expand Down Expand Up @@ -112,6 +114,7 @@ func (c *irisCaptchaHandler) init(secret string) error {
}
if err := c.loadPublicKey(); err != nil {
log.Printf("Couldn't Load Public-Key From %s , Using request mode\n", c.provider)
log.Panic(err)
}

c.js = fmt.Sprintf(`<script src="https://captcha.irisdev.net/js/%s></script>`, c.appUid)
Expand All @@ -135,11 +138,12 @@ func (c *irisCaptchaHandler) loadPublicKey() error {

pembock, _ := pem.Decode([]byte(bs))

c.publicKey, err = x509.ParsePKCS1PublicKey(pembock.Bytes)
i, err := x509.ParsePKIXPublicKey(pembock.Bytes)
if err != nil {
c.Unlock()
return err
}
c.publicKey = i.(*rsa.PublicKey)
c.Unlock()
return nil
}
Expand Down Expand Up @@ -178,20 +182,37 @@ func (c *irisCaptchaHandler) validateSig(userResponse, remoteIP string) (UserRes
tkn, err := jwt.ParseWithClaims(userResponse, &ur, func(token *jwt.Token) (interface{}, error) {
return c.publicKey, nil
})

if err != nil {
ur.Success = false
return ur, err
}
if !tkn.Valid {
err = ErrSignatureInvalid
ur.Success = false
return ur, err
}
if remoteIP != "" {
if ur.IP != remoteIP {
err = ErrMisMachIP
ur.Success = false
return ur, err
}
}

return ur, err
}
func (c *irisCaptchaHandler) TestValidateSig(t *testing.T) {
ur, err := c.validateSig("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWNjZXNzIjp0cnVlLCJob3N0bmFtZSI6ImlyaXNkZXYubmV0IiwiaXAiOiIzNy41OS4xNzMuMjAiLCJleHAiOjE2MDUwMzE2NTcsImlhdCI6MTYwNTAzMTU5N30.KXk2VFUuE9YxyVZRlsEvT9wMYibXqp77WvF3nxRPnqSXA3gKaD6Pjm0UIP2HUkwD-sEW114q5HmSMrB2IGENoN5X4AUal5cmbKUisoCiAiM7JXHxmjqmBzUWG7P6dGSMDjWfKPQ46TMuyQMtyZmrOMcxIgL1gPRbzW7yrPZqPEOGySdr-DLi1NGBUR8y_SEHesP_romn4P3qBc3M7B183Oe0tYP6q0REKboNuRwfhnA7cnpcmCT-_dDV4TpLpIPJcIRW4uAuEDr9POTvBv_96iV7XzxyVnmrsf8bbfUUD0ZyhnP3eK8VDjtqrpCIBNP7Zwm_6VqqhGAioUcrgZfH8A", "")
if err == nil {
t.Errorf("got %v, want time expired", err)
}
if err == jwt.ErrSignatureInvalid {
t.Errorf("got %v, want %v", err, nil)
}
if ur.Success == true {
t.Errorf("got %v, want %v", ur.Success, false)
}
}

//validateReq : validate the user response using requesting to irisdev server
func (c *irisCaptchaHandler) validateReq(userResponse, remoteIP string) (UserResponse, error) {
Expand Down Expand Up @@ -230,3 +251,13 @@ func (c *irisCaptchaHandler) validateReq(userResponse, remoteIP string) (UserRes
ur.IP = remoteIP
return ur, err
}

func (c *irisCaptchaHandler) TestValidateReq(t *testing.T) {
ur, err := c.validateReq("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWNjZXNzIjp0cnVlLCJob3N0bmFtZSI6ImlyaXNkZXYubmV0IiwiaXAiOiIzNy41OS4xNzMuMjAiLCJleHAiOjE2MDUwMzE2NTcsImlhdCI6MTYwNTAzMTU5N30.KXk2VFUuE9YxyVZRlsEvT9wMYibXqp77WvF3nxRPnqSXA3gKaD6Pjm0UIP2HUkwD-sEW114q5HmSMrB2IGENoN5X4AUal5cmbKUisoCiAiM7JXHxmjqmBzUWG7P6dGSMDjWfKPQ46TMuyQMtyZmrOMcxIgL1gPRbzW7yrPZqPEOGySdr-DLi1NGBUR8y_SEHesP_romn4P3qBc3M7B183Oe0tYP6q0REKboNuRwfhnA7cnpcmCT-_dDV4TpLpIPJcIRW4uAuEDr9POTvBv_96iV7XzxyVnmrsf8bbfUUD0ZyhnP3eK8VDjtqrpCIBNP7Zwm_6VqqhGAioUcrgZfH8A", "")
if err != nil {
t.Errorf("got %v, want %v", err, nil)
}
if ur.Success == true {
t.Errorf("got %v, want %v", ur.Success, false)
}
}
59 changes: 59 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package iriscaptchago

import (
"fmt"
"testing"
)

func TestNewIrisCaptchaHandler(t *testing.T) {
var secretCases = []struct {
secret string
want error
}{
{"", ErrInvalidSecret},
{"abcabcabc", ErrInvalidSecret},
{"533a64afb4c496ca4dcbe081aa40x2711", nil},
{"533a64afb4c496ca4dcb0x2711e081aa40x2711", nil},
}

for _, tt := range secretCases {
testname := fmt.Sprintf("%s\n", tt.secret)
t.Run(testname, func(t *testing.T) {
_, err := NewIrisCaptchaHandler(tt.secret)
if err != tt.want {
t.Errorf("got %v, want %v", err, tt.want)
}
})
}
}
func TestGetJs(t *testing.T) {

c, err := NewIrisCaptchaHandler("533a64afb4c496ca4dcb0x2711e081aa40x2711")
if err != nil {
t.Errorf("got %v, want %v", err, nil)
}
ans := c.GetJs()
const cres = `<script src="https://captcha.irisdev.net/js/0x2711></script>`
if ans != cres {
t.Errorf("got %s, want %s", ans, cres)
}
}
func TestLoadPublicKey(t *testing.T) {
c := new(irisCaptchaHandler)
c.provider = IrisDevServer
err := c.loadPublicKey()
if err != nil {
t.Errorf("got %v, want %v", err, nil)
}
}

func TestValidateReq(t *testing.T) {
c, err := NewIrisCaptchaHandler("533a64afb4c496cc34dfd00d1ecbd45cfa2784b2c3eba4aa02e7a4dcbe081aa40x2711")
if err != nil {
t.Errorf("got %v, want %v", err, nil)
}
h := c.(*irisCaptchaHandler)
h.TestValidateReq(t)
h.TestValidateSig(t)

}

0 comments on commit 12d8bcb

Please sign in to comment.