Compare commits
5
Commits
950a38b2e2
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af2358811a | ||
|
|
63badb0857 | ||
|
|
2597e5b352 | ||
|
|
6347e108fe | ||
|
|
c05bdaf679 |
@@ -108,6 +108,12 @@ jobs:
|
|||||||
chmod 600 ~/.ssh/deploy_key
|
chmod 600 ~/.ssh/deploy_key
|
||||||
ssh-keyscan -p 1650 -H "${{ vars.DEPLOY_HOST }}" >> ~/.ssh/known_hosts
|
ssh-keyscan -p 1650 -H "${{ vars.DEPLOY_HOST }}" >> ~/.ssh/known_hosts
|
||||||
|
|
||||||
|
- name: Verify SSH key
|
||||||
|
run: |
|
||||||
|
ls -la ~/.ssh/deploy_key
|
||||||
|
head -1 ~/.ssh/deploy_key
|
||||||
|
tail -1 ~/.ssh/deploy_key
|
||||||
|
|
||||||
- name: Build .env for remote
|
- name: Build .env for remote
|
||||||
run: |
|
run: |
|
||||||
cat > .env <<EOF
|
cat > .env <<EOF
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func (h *OAuthHandler) Authorize(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Redirect(http.StatusFound, h.frontendURL+"/oauth?session="+token)
|
c.Redirect(http.StatusFound, h.frontendURL+"/login?session="+token)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *OAuthHandler) GetSession(c *gin.Context) {
|
func (h *OAuthHandler) GetSession(c *gin.Context) {
|
||||||
|
|||||||
@@ -66,23 +66,34 @@ func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pipe := r.redis.Pipeline()
|
// Fixed-window counter: only the request that brings the counter to 1
|
||||||
incrCmd := pipe.Incr(ctx, counterKey)
|
// sets the TTL, so a burst never extends the window and the counter is
|
||||||
pipe.Expire(ctx, counterKey, cfg.Window)
|
// guaranteed to reset after cfg.Window even under sustained traffic.
|
||||||
if _, err := pipe.Exec(ctx); err != nil {
|
count64, err := r.redis.Incr(ctx, counterKey).Result()
|
||||||
|
if err != nil {
|
||||||
return false, 0, fmt.Errorf("rate limiter: %w", err)
|
return false, 0, fmt.Errorf("rate limiter: %w", err)
|
||||||
}
|
}
|
||||||
|
if count64 == 1 {
|
||||||
|
if err := r.redis.Expire(ctx, counterKey, cfg.Window).Err(); err != nil {
|
||||||
|
return false, 0, fmt.Errorf("rate limiter: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
count := int(incrCmd.Val())
|
count := int(count64)
|
||||||
remaining := max(cfg.Max-count, 0)
|
|
||||||
|
|
||||||
if count > cfg.Max {
|
if count > cfg.Max {
|
||||||
if cfg.BlockFor > 0 {
|
if cfg.BlockFor > 0 {
|
||||||
_ = r.redis.Set(ctx, blockKey, 1, cfg.BlockFor).Err()
|
// Reset the counter when blocking so that once the block expires
|
||||||
|
// the client is not instantly re-blocked by the stale count.
|
||||||
|
_ = r.redis.Del(ctx, counterKey).Err()
|
||||||
|
if err := r.redis.Set(ctx, blockKey, 1, cfg.BlockFor).Err(); err != nil {
|
||||||
|
return false, 0, fmt.Errorf("rate limiter: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false, 0, nil
|
return false, 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remaining := max(cfg.Max-count, 0)
|
||||||
return true, remaining, nil
|
return true, remaining, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,11 @@ export async function proxy(request: NextRequest) {
|
|||||||
|
|
||||||
if (hasRefreshToken && session && pathname === "/login") {
|
if (hasRefreshToken && session && pathname === "/login") {
|
||||||
const accessToken = request.cookies.get("access_token")?.value;
|
const accessToken = request.cookies.get("access_token")?.value;
|
||||||
|
if (!accessToken) {
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await fetch(`${process.env.KOTOSOFT_ID_API_URL}/oauth/session/attach`, {
|
const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/oauth/session/attach`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -20,6 +23,11 @@ export async function proxy(request: NextRequest) {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({ session }),
|
body: JSON.stringify({ session }),
|
||||||
});
|
});
|
||||||
|
if (!res.ok) throw new Error("attach failed");
|
||||||
|
const data = await res.json().catch(() => null);
|
||||||
|
if (data?.redirect_url) {
|
||||||
|
return NextResponse.redirect(new URL(data.redirect_url));
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to attach session:", error);
|
console.error("Failed to attach session:", error);
|
||||||
return NextResponse.redirect(new URL("/callback/error", request.url));
|
return NextResponse.redirect(new URL("/callback/error", request.url));
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user