-
Notifications
You must be signed in to change notification settings - Fork 461
turnstile #2749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
turnstile #2749
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"fmt" | ||
"math" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// Index will return the main "index" page using a go template | ||
|
@@ -112,3 +113,38 @@ func calculateChurn(page *types.IndexPageData) { | |
page.ValidatorsPerEpoch = *limit | ||
page.ValidatorsPerDay = limit_per_day | ||
} | ||
|
||
func VerifyTurnstile(w http.ResponseWriter, r *http.Request) { | ||
|
||
if utils.Config.Frontend.Turnstile.Enabled { | ||
|
||
err := utils.VerifyTurnstileToken(r) | ||
|
||
cookie := http.Cookie{ | ||
Name: "turnstile", | ||
Value: "verified", | ||
Path: "/", | ||
MaxAge: int(utils.Config.Frontend.Turnstile.CookieMaxAge), | ||
HttpOnly: false, | ||
Secure: true, | ||
SameSite: http.SameSiteLaxMode, | ||
} | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
// clear cookie | ||
cookie.MaxAge = -1 | ||
http.SetCookie(w, &cookie) | ||
http.Error(w, "Turnstile challenge failed", http.StatusServiceUnavailable) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depending on the error you might want to return a different status code. For example if the challenge was not successful, you return a specific error but returning service unavailable is not correct, something like Bad Request would be better suited. |
||
return | ||
} | ||
|
||
validuntil := time.Now().Add(time.Duration(utils.Config.Frontend.Turnstile.SessionMaxAge) * time.Second).Format(time.RFC3339) | ||
|
||
utils.SessionStore.SCS.Put(r.Context(), "TURNSTILE::VALIDUNTIL", validuntil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Store current timestamp in SessionStore and in the verify function check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not store and retrieve it as an int64, no need to format it |
||
http.SetCookie(w, &cookie) | ||
w.Write([]byte("Success")) | ||
} else { | ||
w.Write([]byte("Turnstile not enabled")) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use utils.LogError