Compare commits
2
Commits
main
..
ec5671ec9f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec5671ec9f | ||
|
|
9be459b522 |
+12
-12
@@ -25,18 +25,24 @@ 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: |
|
||||||
docker run --rm \
|
cd kotosoft-id-frontend
|
||||||
-v "$PWD/kotosoft-id-frontend:/app" \
|
bun install
|
||||||
-w /app \
|
bun run lint
|
||||||
node:20-alpine \
|
|
||||||
sh -c 'npm install && npm run lint'
|
|
||||||
|
|
||||||
build-backend:
|
build-backend:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -108,12 +114,6 @@ 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+"/login?session="+token)
|
c.Redirect(http.StatusFound, h.frontendURL+"/oauth?session="+token)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *OAuthHandler) GetSession(c *gin.Context) {
|
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
|
pipe := r.redis.Pipeline()
|
||||||
// sets the TTL, so a burst never extends the window and the counter is
|
incrCmd := pipe.Incr(ctx, counterKey)
|
||||||
// guaranteed to reset after cfg.Window even under sustained traffic.
|
pipe.Expire(ctx, counterKey, cfg.Window)
|
||||||
count64, err := r.redis.Incr(ctx, counterKey).Result()
|
if _, err := pipe.Exec(ctx); err != nil {
|
||||||
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(count64)
|
count := int(incrCmd.Val())
|
||||||
|
remaining := max(cfg.Max-count, 0)
|
||||||
|
|
||||||
if count > cfg.Max {
|
if count > cfg.Max {
|
||||||
if cfg.BlockFor > 0 {
|
if cfg.BlockFor > 0 {
|
||||||
// Reset the counter when blocking so that once the block expires
|
_ = r.redis.Set(ctx, blockKey, 1, cfg.BlockFor).Err()
|
||||||
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,10 @@ 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 (hasRefreshToken && session && pathname === "/login") {
|
if (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 {
|
||||||
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",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -23,11 +20,6 @@ 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));
|
||||||
@@ -35,7 +27,11 @@ 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 (hasRefreshToken && (isPublicPath || pathname === "/")) {
|
if (session && pathname === "/consent") {
|
||||||
|
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
Reference in New Issue
Block a user