-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martin Jirku <martin@jirku.sk>
- Loading branch information
1 parent
b47590f
commit f48c533
Showing
10 changed files
with
191 additions
and
29 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
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
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,18 @@ | ||
package middleware | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/justinas/nosurf" | ||
) | ||
|
||
func Csrf(h http.Handler) http.Handler { | ||
surfing := nosurf.New(h) | ||
surfing.SetBaseCookie(http.Cookie{Path: "/"}) | ||
surfing.SetFailureHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
log.Println("Failed to validate CSRF token:", nosurf.Reason(r)) | ||
w.WriteHeader(http.StatusBadRequest) | ||
})) | ||
return surfing | ||
} |
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,55 @@ | ||
package services | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type RecaptchaService struct { | ||
apiKey string | ||
siteKey string | ||
} | ||
|
||
func NewRecaptchaService(apiKey, siteKey string) *RecaptchaService { | ||
return &RecaptchaService{ | ||
apiKey: apiKey, | ||
siteKey: siteKey, | ||
} | ||
} | ||
|
||
type googleCaptchaResponse struct { | ||
Success bool `json:"success"` | ||
ChallengeTs string `json:"challenge_ts"` | ||
Hostname string `json:"hostname"` | ||
ErrorCodes []string `json:"error-codes,omitempty"` | ||
} | ||
|
||
func (s *RecaptchaService) ValidateCaptcha(r *http.Request) error { | ||
if err := r.ParseForm(); err != nil { | ||
return fmt.Errorf("parsing form: %w", err) | ||
} | ||
token := r.Form.Get("g-recaptcha-response") | ||
if token == "" { | ||
return fmt.Errorf("missing captcha token") | ||
} | ||
resp, err := http.PostForm(fmt.Sprintf("https://www.google.com/recaptcha/api/siteverify?%s", s.apiKey), url.Values{ | ||
"siteKey": {s.siteKey}, | ||
"response": {token}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("sending captcha request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
var response googleCaptchaResponse | ||
err = json.NewDecoder(resp.Body).Decode(&response) | ||
if err != nil { | ||
return fmt.Errorf("decoding captcha response: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *RecaptchaService) Key() string { | ||
return s.siteKey | ||
} |
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