Skip to content

Commit 962ac2c

Browse files
author
Your Name
committed
add some comments :)
1 parent ab43999 commit 962ac2c

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

contribs/gnofaucet/coins.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ func getAccountBalanceMiddleware(tm2Client *tm2Client.Client, maxBalance int64)
1717
return func(next http.Handler) http.Handler {
1818
return http.HandlerFunc(
1919
func(w http.ResponseWriter, r *http.Request) {
20-
var data request
2120
body, err := io.ReadAll(r.Body)
2221
if err != nil {
2322
http.Error(w, err.Error(), http.StatusBadRequest)
2423
return
2524
}
2625

26+
var data request
2727
err = json.Unmarshal(body, &data)
28+
if err != nil {
29+
http.Error(w, err.Error(), http.StatusBadRequest)
30+
return
31+
}
2832
r.Body = io.NopCloser(bytes.NewBuffer(body))
2933
balance, err := checkAccountBalance(tm2Client, data.To)
3034
if err != nil {

contribs/gnofaucet/cooldown.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// CooldownLimiter is a Limiter using an in-memory map
99
type CooldownLimiter struct {
1010
cooldowns map[string]time.Time
11-
mu sync.Mutex
11+
mu sync.RWMutex
1212
cooldownTime time.Duration
1313
}
1414

@@ -22,15 +22,23 @@ func NewCooldownLimiter(cooldown time.Duration) *CooldownLimiter {
2222

2323
// CheckCooldown checks if a key has done some action before the cooldown period has passed
2424
func (rl *CooldownLimiter) CheckCooldown(key string) bool {
25-
rl.mu.Lock()
26-
defer rl.mu.Unlock()
27-
28-
if lastClaim, found := rl.cooldowns[key]; found {
29-
if time.Since(lastClaim) < rl.cooldownTime {
30-
return false // Deny claim if within cooldown period
31-
}
25+
lastClaim := rl.get(key)
26+
if time.Since(lastClaim) < rl.cooldownTime {
27+
return false // Deny claim if within cooldown period
3228
}
3329

34-
rl.cooldowns[key] = time.Now()
30+
rl.set(key, time.Now())
3531
return true
3632
}
33+
34+
func (rl *CooldownLimiter) get(key string) time.Time {
35+
rl.mu.RLock()
36+
defer rl.mu.RUnlock()
37+
return rl.cooldowns[key]
38+
}
39+
40+
func (rl *CooldownLimiter) set(key string, value time.Time) {
41+
rl.mu.Lock()
42+
defer rl.mu.Unlock()
43+
rl.cooldowns[key] = value
44+
}

contribs/gnofaucet/gh.go

+18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ import (
1111
"github.com/google/go-github/v64/github"
1212
)
1313

14+
// getGithubMiddleware sets up authentication middleware for GitHub OAuth.
15+
// If clientID and secret are empty, the middleware does nothing.
16+
//
17+
// Parameters:
18+
// - clientID: The OAuth client ID issued by GitHub when registering the application.
19+
// - secret: The OAuth client secret used to securely authenticate API requests.
20+
// - cooldown: A cooldown duration to prevent several claims from the same user.
21+
//
22+
// GitHub OAuth applications require a client ID and secret to authenticate users securely.
23+
// These credentials are obtained when registering an application on GitHub at:
24+
// https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authenticating-to-the-rest-api-with-an-oauth-app#registering-your-app
1425
func getGithubMiddleware(clientID, secret string, cooldown time.Duration) func(next http.Handler) http.Handler {
1526
coolDownLimiter := NewCooldownLimiter(cooldown)
1627
return func(next http.Handler) http.Handler {
@@ -24,6 +35,13 @@ func getGithubMiddleware(clientID, secret string, cooldown time.Duration) func(n
2435
return
2536
}
2637

38+
// Extracts the authorization code returned by the GitHub OAuth flow.
39+
//
40+
// When a user successfully authenticates via GitHub OAuth, GitHub redirects them
41+
// to the registered callback URL with a `code` query parameter. This code is then
42+
// exchanged for an access token.
43+
//
44+
// Reference: https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github
2745
code := r.URL.Query().Get("code")
2846
if code == "" {
2947
http.Error(w, "missing code", http.StatusBadRequest)

contribs/gnofaucet/serve.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ func execServe(ctx context.Context, cfg *serveCfg, io commands.IO) error {
219219
middlewares := []faucet.Middleware{
220220
getIPMiddleware(cfg.isBehindProxy, st),
221221
getCaptchaMiddleware(cfg.captchaSecret),
222-
getGithubMiddleware(cfg.ghClientID, cfg.ghClientSecret, 1*time.Hour),
223222
getAccountBalanceMiddleware(cli, cfg.maxBalance),
223+
getGithubMiddleware(cfg.ghClientID, cfg.ghClientSecret, 1*time.Hour),
224224
}
225225

226226
// Create a new faucet with

0 commit comments

Comments
 (0)