Skip to content

Commit f443ad3

Browse files
committed
feat: add auth token for api
1 parent 5cd8c6a commit f443ad3

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ API_TIMEOUT=30s
3434
API_ROOT_PATH=/
3535
API_TRACE_HEADER=Gotenberg-Trace
3636
API_ENABLE_BASIC_AUTH=false
37+
API_ENABLE_BEARER_TOKEN_AUTH=false
3738
GOTENBERG_API_BASIC_AUTH_USERNAME=
3839
GOTENBERG_API_BASIC_AUTH_PASSWORD=
40+
GOTENBERG_API_BEARER_TOKEN=
3941
API_DISABLE_HEALTH_CHECK_LOGGING=false
4042
CHROMIUM_RESTART_AFTER=0
4143
CHROMIUM_MAX_QUEUE_SIZE=0
@@ -94,6 +96,7 @@ run: ## Start a Gotenberg container
9496
--api-root-path=$(API_ROOT_PATH) \
9597
--api-trace-header=$(API_TRACE_HEADER) \
9698
--api-enable-basic-auth=$(API_ENABLE_BASIC_AUTH) \
99+
--api-enable-bearer-token-auth=$(API_ENABLE_BEARER_TOKEN_AUTH) \
97100
--api-disable-health-check-logging=$(API_DISABLE_HEALTH_CHECK_LOGGING) \
98101
--chromium-restart-after=$(CHROMIUM_RESTART_AFTER) \
99102
--chromium-auto-start=$(CHROMIUM_AUTO_START) \

pkg/modules/api/api.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Api struct {
3737
basicAuthUsername string
3838
basicAuthPassword string
3939
disableHealthCheckLogging bool
40+
bearerToken string
4041

4142
routes []Route
4243
externalMiddlewares []Middleware
@@ -169,6 +170,7 @@ func (a *Api) Descriptor() gotenberg.ModuleDescriptor {
169170
fs.String("api-trace-header", "Gotenberg-Trace", "Set the header name to use for identifying requests")
170171
fs.Bool("api-enable-basic-auth", false, "Enable basic authentication - will look for the GOTENBERG_API_BASIC_AUTH_USERNAME and GOTENBERG_API_BASIC_AUTH_PASSWORD environment variables")
171172
fs.Bool("api-disable-health-check-logging", false, "Disable health check logging")
173+
fs.Bool("api-enable-bearer-token-auth", false, "Enable bearer token authentication")
172174
return fs
173175
}(),
174176
New: func() gotenberg.Module { return new(Api) },
@@ -212,6 +214,15 @@ func (a *Api) Provision(ctx *gotenberg.Context) error {
212214
a.basicAuthPassword = basicAuthPassword
213215
}
214216

217+
enableBearerToken := flags.MustBool("api-enable-bearer-token-auth")
218+
if enableBearerToken {
219+
bearerToken, err := gotenberg.StringEnv("GOTENBERG_API_BEARER_TOKEN")
220+
if err != nil {
221+
return fmt.Errorf("get bearer token from env: %w", err)
222+
}
223+
a.bearerToken = bearerToken
224+
}
225+
215226
// Get routes from modules.
216227
mods, err := ctx.Modules(new(Router))
217228
if err != nil {
@@ -409,6 +420,7 @@ func (a *Api) Start() error {
409420
rootPathMiddleware(a.rootPath),
410421
traceMiddleware(a.traceHeader),
411422
loggerMiddleware(a.logger, disableLoggingForPaths),
423+
a.bearerTokenMiddleware(),
412424
)
413425

414426
// Add the modules' middlewares in their respective stacks.
@@ -524,6 +536,34 @@ func (a *Api) Stop(ctx context.Context) error {
524536
return a.srv.Shutdown(ctx)
525537
}
526538

539+
func (a *Api) bearerTokenMiddleware() echo.MiddlewareFunc {
540+
return func(next echo.HandlerFunc) echo.HandlerFunc {
541+
return func(c echo.Context) error {
542+
// Skip authentication for health and version endpoints
543+
if strings.HasSuffix(c.Path(), "/health") || strings.HasSuffix(c.Path(), "/version") {
544+
return next(c)
545+
}
546+
547+
authHeader := c.Request().Header.Get("Authorization")
548+
if authHeader == "" {
549+
return echo.NewHTTPError(http.StatusUnauthorized, "Missing Authorization header")
550+
}
551+
552+
tokenParts := strings.Split(authHeader, " ")
553+
if len(tokenParts) != 2 || strings.ToLower(tokenParts[0]) != "bearer" {
554+
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid Authorization header format")
555+
}
556+
557+
token := tokenParts[1]
558+
if token != a.bearerToken {
559+
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid API token")
560+
}
561+
562+
return next(c)
563+
}
564+
}
565+
}
566+
527567
// Interface guards.
528568
var (
529569
_ gotenberg.Module = (*Api)(nil)

0 commit comments

Comments
 (0)