Skip to content

Commit b6df4b9

Browse files
committed
fix: coderabbit comments
1 parent c298d0b commit b6df4b9

6 files changed

Lines changed: 18 additions & 7 deletions

File tree

internal/bootstrap/router_bootstrap.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package bootstrap
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"net"
78
"net/http"
89
"os"
10+
"time"
911

1012
"github.com/tinyauthapp/tinyauth/internal/controller"
1113
"github.com/tinyauthapp/tinyauth/internal/middleware"
14+
"github.com/tinyauthapp/tinyauth/internal/model"
1215

1316
"github.com/gin-gonic/gin"
1417
)
@@ -199,7 +202,12 @@ func (app *BootstrapApp) serveTailscale() error {
199202

200203
func (app *BootstrapApp) serve(listener net.Listener, server *http.Server, name string) error {
201204
shutdown := func() {
202-
server.Shutdown(app.ctx)
205+
ctx, cancel := context.WithTimeout(context.Background(), model.GracefulShutdownTimeout*time.Second)
206+
defer cancel()
207+
err := server.Shutdown(ctx)
208+
if err != nil {
209+
app.log.App.Error().Err(err).Msgf("Failed to shutdown %s listener gracefully", name)
210+
}
203211
listener.Close()
204212
}
205213

internal/controller/user_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func (controller *UserController) tailscaleHandler(c *gin.Context) {
424424
cookie, err := controller.auth.CreateSession(c, sessionCookie)
425425

426426
if err != nil {
427-
controller.log.App.Error().Err(err).Str("username", context.GetUsername()).Msg("Failed to create session cookie after successful TOTP verification")
427+
controller.log.App.Error().Err(err).Str("username", context.GetUsername()).Msg("Failed to create session cookie after successful Tailscale login")
428428
c.JSON(500, gin.H{
429429
"status": 500,
430430
"message": "Internal Server Error",

internal/middleware/context_middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
116116
if tailscaleContext != nil {
117117
c.Set("context", &model.UserContext{
118118
Authenticated: false,
119+
Provider: model.ProviderTailscale,
119120
Tailscale: tailscaleContext,
120121
})
121122
}

internal/model/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ const SessionCookieName = "tinyauth-session"
2121
const CSRFCookieName = "tinyauth-csrf"
2222
const RedirectCookieName = "tinyauth-redirect"
2323
const OAuthSessionCookieName = "tinyauth-oauth"
24+
25+
const GracefulShutdownTimeout = 5 // seconds

internal/service/auth_service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ func (auth *AuthService) IsEmailWhitelisted(email string) bool {
292292
}
293293

294294
func (auth *AuthService) CreateSession(ctx context.Context, data repository.Session) (*http.Cookie, error) {
295+
if data.Provider == "tailscale" && auth.tailscale == nil {
296+
return nil, fmt.Errorf("tailscale service not configured, cannot create session for tailscale user")
297+
}
298+
295299
uuid, err := uuid.NewRandom()
296300

297301
if err != nil {
@@ -329,10 +333,6 @@ func (auth *AuthService) CreateSession(ctx context.Context, data repository.Sess
329333
}
330334

331335
if data.Provider == "tailscale" {
332-
if auth.tailscale == nil {
333-
return nil, fmt.Errorf("tailscale service not configured, cannot create session for tailscale user")
334-
}
335-
336336
auth.log.App.Trace().Str("url", fmt.Sprintf("https://%s", auth.tailscale.GetHostname())).Msg("Extracting root domain from Tailscale hostname")
337337

338338
tsCookieDomain, err := utils.GetCookieDomain(fmt.Sprintf("https://%s", auth.tailscale.GetHostname()))

internal/service/tailscale_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func NewTailscaleService(log *logger.Logger, config model.Config, ctx context.Co
6464
lc: lc,
6565
}
6666

67-
connectCtx, cancel := context.WithTimeout(ctx, 2*time.Minute)
67+
connectCtx, cancel := context.WithTimeout(ctx, 2*time.Minute) // large enough timeout to allow for user to manually authenticate with link if needed
6868
defer cancel()
6969

7070
err = service.waitForConn(connectCtx)

0 commit comments

Comments
 (0)