Skip to content

Commit 27cb954

Browse files
Okabe-JunyaCopilot
andauthored
Feature: release v0.5.0 (#23)
* feat: release v0.5.0 * Update backend/middleware/middleware.go Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 86aa843 commit 27cb954

File tree

17 files changed

+1792
-38
lines changed

17 files changed

+1792
-38
lines changed

.github/workflows/backend-test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ jobs:
2626
working-directory: ./backend
2727

2828
- name: go test
29-
run: make test
29+
run: make test-all
3030
working-directory: ./backend

backend/Makefile

+54-10
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ vendor:
2323
@go mod tidy
2424
@go mod verify
2525

26-
.PHONY: test
27-
test:
28-
@echo "Running tests..."
29-
@go test -v ./...
26+
.PHONY: test-all
27+
test-all: test-unit test-e2e
28+
29+
.PHONY: test-unit
30+
test-unit:
31+
@echo "Running unit tests..."
32+
@go test -v ./... -run TestUnit
33+
34+
.PHONY: test-e2e
35+
test-e2e:
36+
@echo "Running E2E tests..."
37+
@LANG=C go test -v ./tests/e2e
3038

3139
.PHONY: run
3240
run:
@@ -58,14 +66,50 @@ cleanup-with-age: build-cleanup
5866
@echo "Running cleanup job with custom age..."
5967
@./bin/cleanup --older-than $(age)
6068

69+
.PHONY: build-migrate
70+
build-migrate:
71+
@echo "Building migration tool..."
72+
@go build -o bin/migrate cmd/migrate/main.go
73+
74+
.PHONY: migrate
75+
migrate: build-migrate
76+
@echo "Running migration..."
77+
@./bin/migrate $(ARGS)
78+
79+
.PHONY: migrate-create-stats
80+
migrate-create-stats: build-migrate
81+
@echo "Creating stats collection..."
82+
@./bin/migrate --create-stats
83+
84+
.PHONY: migrate-expired-links
85+
migrate-expired-links: build-migrate
86+
@echo "Migrating expired links..."
87+
@./bin/migrate --migrate-expired
88+
89+
.PHONY: migrate-dry-run
90+
migrate-dry-run: build-migrate
91+
@echo "Running migration (dry run)..."
92+
@./bin/migrate --dry-run $(ARGS)
93+
6194
.PHONY: help
6295
help:
6396
@echo "Usage: make [target]"
6497
@echo ""
6598
@echo "Targets:"
66-
@echo " build - Build the binary"
67-
@echo " lint - Run linter"
68-
@echo " test - Run tests"
69-
@echo " run - Run server"
70-
@echo " clean - Clean up"
71-
@echo " help - Show this help message"
99+
@echo " build - Build the server binary"
100+
@echo " lint - Run linter"
101+
@echo " fmt - Format code"
102+
@echo " vendor - Update vendor dependencies"
103+
@echo " test-all - Run all tests"
104+
@echo " test-unit - Run unit tests"
105+
@echo " test-e2e - Run E2E tests"
106+
@echo " run - Run server"
107+
@echo " clean - Clean up"
108+
@echo " cleanup - Run cleanup job"
109+
@echo " cleanup-dry-run - Run cleanup job (dry run)"
110+
@echo " cleanup-with-age - Run cleanup job with custom age"
111+
@echo " migrate - Run migrations with ARGS"
112+
@echo " migrate-create-stats - Create link stats collection"
113+
@echo " migrate-expired-links - Migrate expired links"
114+
@echo " migrate-dry-run - Run migrations in dry-run mode"
115+
@echo " help - Show this help message"

backend/auth/auth.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -289,27 +289,38 @@ func GetCurrentUser(r *http.Request) (*User, error) {
289289
}, nil
290290
}
291291

292-
// Get the session token from the cookie
292+
// First try to get user from cookie
293293
cookie, err := r.Cookie("session_token")
294-
if err != nil {
295-
return nil, err
294+
if err == nil {
295+
// Validate the session token
296+
user, err := ValidateSessionToken(cookie.Value)
297+
if err == nil {
298+
return user, nil
299+
}
300+
// Log error but continue with other methods
301+
logger.Warn("Failed to validate session token", logger.Fields{
302+
"error": err.Error(),
303+
})
296304
}
297305

298-
// Validate the session token
299-
user, err := ValidateSessionToken(cookie.Value)
300-
if err != nil {
301-
return nil, err
306+
// Fall back to header for compatibility or testing
307+
userID := r.Header.Get("X-User-ID")
308+
if userID != "" {
309+
return &User{
310+
ID: userID,
311+
Email: r.Header.Get("X-User-Email"),
312+
Name: r.Header.Get("X-User-Name"),
313+
}, nil
302314
}
303315

304-
return user, nil
316+
return nil, errors.New("not authenticated")
305317
}
306318

307319
// AuthMiddleware is a middleware that checks if the user is authenticated
308320
func AuthMiddleware(next http.Handler) http.Handler {
309321
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
310-
// Skip auth check if authentication is disabled
322+
// If authentication is disabled, add an anonymous user to the context
311323
if !authEnabled {
312-
// 認証が無効の場合は匿名ユーザーをコンテキストに追加
313324
anonymousUser := &User{
314325
ID: "anonymous",
315326

backend/auth/context.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package auth
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net/http"
7+
)
8+
9+
// contextKey is a private type for context keys
10+
type contextKey int
11+
12+
const (
13+
// userContextKey is the key for user information in request contexts
14+
userContextKey contextKey = iota
15+
)
16+
17+
var (
18+
// ErrNoUserInContext is returned when no user is found in the context
19+
ErrNoUserInContext = errors.New("no user found in context")
20+
)
21+
22+
// ContextWithUser adds a user to the given context
23+
func ContextWithUser(ctx context.Context, user *User) context.Context {
24+
return context.WithValue(ctx, userContextKey, user)
25+
}
26+
27+
// UserFromContext extracts a user from the given context
28+
func UserFromContext(ctx context.Context) (*User, error) {
29+
user, ok := ctx.Value(userContextKey).(*User)
30+
if !ok || user == nil {
31+
return nil, ErrNoUserInContext
32+
}
33+
return user, nil
34+
}
35+
36+
// GetUserFromRequest extracts a user from the request, either from session or header
37+
func GetUserFromRequest(r *http.Request) (*User, error) {
38+
// First try to get user from context (set by authentication middleware)
39+
if user, err := UserFromContext(r.Context()); err == nil {
40+
return user, nil
41+
}
42+
43+
// Try to get user via GetCurrentUser (which handles both session and headers)
44+
return GetCurrentUser(r)
45+
}

backend/auth/session.go

+5
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,8 @@ func createSignature(data string) (string, error) {
167167
signature := base64.URLEncoding.EncodeToString(h.Sum(nil))
168168
return signature, nil
169169
}
170+
171+
// IsSessionEnabled returns whether session management is enabled
172+
func IsSessionEnabled() bool {
173+
return IsAuthEnabled() && len(secretKey) > 0
174+
}

0 commit comments

Comments
 (0)