Compare commits

..
5 Commits
Author SHA1 Message Date
Ivan Rezchikov af2358811a fix: ratelimiter fixed
Build and Deploy / lint-frontend (push) Canceled after 0s
Build and Deploy / build-backend (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 0s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / test-backend (push) Canceled after 10s
2026-09-18 16:02:48 +03:00
Ivan Rezchikov 63badb0857 fix: oauth flow
Build and Deploy / test-backend (push) Successful in 1m5s
Build and Deploy / lint-frontend (push) Successful in 1m28s
Build and Deploy / build-backend (push) Successful in 3s
Build and Deploy / build-frontend (push) Successful in 41s
Build and Deploy / deploy (push) Successful in 5s
2026-09-17 23:50:36 +03:00
Ivan Rezchikov 2597e5b352 fix: fixed redirecting
Build and Deploy / test-backend (push) Successful in 1m15s
Build and Deploy / lint-frontend (push) Successful in 1m13s
Build and Deploy / build-backend (push) Successful in 50s
Build and Deploy / build-frontend (push) Successful in 40s
Build and Deploy / deploy (push) Successful in 6s
2026-09-17 13:12:54 +03:00
Ivan Rezchikov 6347e108fe fix: proxy fixed
Build and Deploy / test-backend (push) Successful in 1m31s
Build and Deploy / lint-frontend (push) Successful in 1m54s
Build and Deploy / build-backend (push) Successful in 7s
Build and Deploy / build-frontend (push) Successful in 1m0s
Build and Deploy / deploy (push) Successful in 8s
2026-09-16 17:40:25 +03:00
Ivan Rezchikov c05bdaf679 ci/cd: test
Build and Deploy / test-backend (push) Successful in 56s
Build and Deploy / lint-frontend (push) Successful in 1m26s
Build and Deploy / build-backend (push) Successful in 5s
Build and Deploy / build-frontend (push) Successful in 9s
Build and Deploy / deploy (push) Successful in 4s
2026-09-15 19:36:41 +03:00
5 changed files with 43 additions and 28 deletions
+12 -12
View File
@@ -25,24 +25,18 @@ jobs:
lint-frontend: lint-frontend:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 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 - name: Lint frontend
run: | run: |
cd kotosoft-id-frontend docker run --rm \
bun install -v "$PWD/kotosoft-id-frontend:/app" \
bun run lint -w /app \
node:20-alpine \
sh -c 'npm install && npm run lint'
build-backend: build-backend:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@@ -114,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 -7
View File
@@ -9,10 +9,13 @@ export async function proxy(request: NextRequest) {
const isPublicPath = PUBLIC_PATHS.includes(pathname); const isPublicPath = PUBLIC_PATHS.includes(pathname);
const session = request.nextUrl.searchParams.get("session"); const session = request.nextUrl.searchParams.get("session");
if (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));
@@ -27,11 +35,7 @@ export async function proxy(request: NextRequest) {
return NextResponse.redirect(new URL(`/consent?session=${session}`, request.url)); return NextResponse.redirect(new URL(`/consent?session=${session}`, request.url));
} }
if (session && pathname === "/consent") { if (hasRefreshToken && (isPublicPath || pathname === "/")) {
return NextResponse.next();
}
if (hasRefreshToken && !session && (isPublicPath || pathname === "/")) {
return NextResponse.redirect(new URL("/me", request.url)); return NextResponse.redirect(new URL("/me", request.url));
} }
File diff suppressed because one or more lines are too long