Compare commits

..
2 Commits
Author SHA1 Message Date
Ivan Rezchikov ec5671ec9f fix: proxy fixed 2026-09-16 17:39:23 +03:00
Ivan Rezchikov 9be459b522 ci: migrate to bun 2026-09-15 20:11:57 +03:00
5 changed files with 28 additions and 43 deletions
+12 -12
View File
@@ -25,18 +25,24 @@ jobs:
lint-frontend:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v1
- name: Restore Bun cache
uses: actions/cache@v4
with:
path: ~/.bun/install/cache
key: ${{ runner.os }}-bun-${{ hashFiles('kotosoft-id-frontend/bun.lock') }}
- name: Lint frontend
run: |
docker run --rm \
-v "$PWD/kotosoft-id-frontend:/app" \
-w /app \
node:20-alpine \
sh -c 'npm install && npm run lint'
cd kotosoft-id-frontend
bun install
bun run lint
build-backend:
runs-on: ubuntu-latest
@@ -108,12 +114,6 @@ jobs:
chmod 600 ~/.ssh/deploy_key
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
run: |
cat > .env <<EOF
@@ -32,7 +32,7 @@ func (h *OAuthHandler) Authorize(c *gin.Context) {
return
}
c.Redirect(http.StatusFound, h.frontendURL+"/login?session="+token)
c.Redirect(http.StatusFound, h.frontendURL+"/oauth?session="+token)
}
func (h *OAuthHandler) GetSession(c *gin.Context) {
@@ -66,34 +66,23 @@ func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig
}
}
// Fixed-window counter: only the request that brings the counter to 1
// sets the TTL, so a burst never extends the window and the counter is
// guaranteed to reset after cfg.Window even under sustained traffic.
count64, err := r.redis.Incr(ctx, counterKey).Result()
if err != nil {
pipe := r.redis.Pipeline()
incrCmd := pipe.Incr(ctx, counterKey)
pipe.Expire(ctx, counterKey, cfg.Window)
if _, err := pipe.Exec(ctx); err != nil {
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(count64)
count := int(incrCmd.Val())
remaining := max(cfg.Max-count, 0)
if count > cfg.Max {
if cfg.BlockFor > 0 {
// 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)
}
_ = r.redis.Set(ctx, blockKey, 1, cfg.BlockFor).Err()
}
return false, 0, nil
}
remaining := max(cfg.Max-count, 0)
return true, remaining, nil
}
+7 -11
View File
@@ -9,13 +9,10 @@ export async function proxy(request: NextRequest) {
const isPublicPath = PUBLIC_PATHS.includes(pathname);
const session = request.nextUrl.searchParams.get("session");
if (hasRefreshToken && session && pathname === "/login") {
if (session && pathname === "/login") {
const accessToken = request.cookies.get("access_token")?.value;
if (!accessToken) {
return NextResponse.next();
}
try {
const res = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/oauth/session/attach`, {
await fetch(`${process.env.KOTOSOFT_ID_API_URL}/oauth/session/attach`, {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -23,11 +20,6 @@ export async function proxy(request: NextRequest) {
},
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) {
console.error("Failed to attach session:", error);
return NextResponse.redirect(new URL("/callback/error", request.url));
@@ -35,7 +27,11 @@ export async function proxy(request: NextRequest) {
return NextResponse.redirect(new URL(`/consent?session=${session}`, request.url));
}
if (hasRefreshToken && (isPublicPath || pathname === "/")) {
if (session && pathname === "/consent") {
return NextResponse.next();
}
if (hasRefreshToken && !session && (isPublicPath || pathname === "/")) {
return NextResponse.redirect(new URL("/me", request.url));
}
File diff suppressed because one or more lines are too long