Skip to content

Commit 0986814

Browse files
committed
add auth via auth0
1 parent 8923de8 commit 0986814

File tree

26 files changed

+419
-152
lines changed

26 files changed

+419
-152
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ DATABASE_URL=postgres://postgres:password@localhost:5432/robswebhub?sslmode=disa
1212
# APP_ENVIRONMENT=production
1313
# APP_APPLICATION__HOST=0.0.0.0
1414
# APP_APPLICATION__PORT=8080
15+
16+
AUTH0_DOMAIN=...
17+
AUTH0_CLIENT_ID=...
18+
AUTH0_CLIENT_SECRET=...
19+
AUTH0_CALLBACK_URL=...

.github/workflows/go.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,6 @@ jobs:
145145
templ generate
146146
sqlc generate
147147
148-
- name: Run go fmt
149-
run: |
150-
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
151-
echo "The following files are not formatted:"
152-
gofmt -s -l .
153-
exit 1
154-
fi
155-
156148
- name: Run go vet
157149
run: go vet ./...
158150

@@ -165,8 +157,10 @@ jobs:
165157
- name: Check templ formatting
166158
run: |
167159
templ fmt .
160+
gofmt -l -w .
161+
goimports -w .
168162
if [ -n "$(git status --porcelain)" ]; then
169-
echo "templ files are not formatted. Please run 'templ fmt .'"
163+
echo "Files are not formatted."
170164
git diff
171165
exit 1
172166
fi

.pre-commit-config.yaml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,11 @@ repos:
2222
# First: Always generate code
2323
- id: generate-code
2424
name: Generate code (templ & sqlc)
25-
entry: bash -c 'templ generate && sqlc generate'
25+
entry: bash -c 'templ generate && sqlc generate && templ fmt . && gofmt -l -w . && goimports -w .'
2626
language: system
2727
pass_filenames: false
2828
always_run: true
2929

30-
# Then: Format code
31-
- id: go-fmt
32-
name: Format Go code
33-
entry: bash -c 'gofmt -l -w . && goimports -w .'
34-
language: system
35-
types: [go]
36-
pass_filenames: false
37-
38-
# Then: Format templ files
39-
- id: templ-fmt
40-
name: Format templ files
41-
entry: bash -c 'templ fmt .'
42-
language: system
43-
pass_filenames: false
44-
files: '\.templ$'
45-
4630
# Then: Check and tidy modules
4731
- id: go-mod-tidy
4832
name: Tidy go modules

cmd/server/main.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/gob"
56
"log"
67
"net/http"
78
"os"
@@ -11,11 +12,15 @@ import (
1112

1213
"github.com/getsentry/sentry-go"
1314
sentrygin "github.com/getsentry/sentry-go/gin"
15+
"github.com/gin-contrib/sessions"
16+
"github.com/gin-contrib/sessions/cookie"
1417

1518
"github.com/gin-gonic/gin"
19+
"github.com/gitznik/robswebhub/internal/auth"
1620
"github.com/gitznik/robswebhub/internal/config"
1721
"github.com/gitznik/robswebhub/internal/database"
1822
"github.com/gitznik/robswebhub/internal/handlers"
23+
"github.com/gitznik/robswebhub/internal/middleware"
1924
)
2025

2126
func main() {
@@ -58,7 +63,11 @@ func main() {
5863
queries := database.New(db)
5964

6065
// Setup Gin router
61-
router := setupRouter(cfg, queries)
66+
auth, err := auth.New(&cfg.Auth)
67+
if err != nil {
68+
log.Fatalf("Could not setup authenticator: %v", err)
69+
}
70+
router := setupRouter(cfg, queries, auth)
6271

6372
// Create HTTP server
6473
srv := &http.Server{
@@ -90,7 +99,7 @@ func main() {
9099
log.Print("Server exiting")
91100
}
92101

93-
func setupRouter(cfg *config.Config, queries *database.Queries) *gin.Engine {
102+
func setupRouter(cfg *config.Config, queries *database.Queries, auth *auth.Authenticator) *gin.Engine {
94103
// Set Gin mode based on environment
95104
if os.Getenv("APP_ENVIRONMENT") == "production" {
96105
gin.SetMode(gin.ReleaseMode)
@@ -99,13 +108,20 @@ func setupRouter(cfg *config.Config, queries *database.Queries) *gin.Engine {
99108
router := gin.Default()
100109
router.Use(sentrygin.New(sentrygin.Options{Repanic: true}))
101110

111+
// Setup Auth
112+
gob.Register(map[string]interface{}{})
113+
114+
store := cookie.NewStore([]byte("secret"))
115+
router.Use(sessions.Sessions("auth-session", store))
116+
router.Use(middleware.LoginStatus)
117+
102118
// Serve static files
103119
router.Static("/static", "./static")
104120
router.Static("/images", "./static/images")
105121
router.StaticFile("/favicon.ico", "./static/images/favicon.ico")
106122

107123
// Create handlers
108-
h := handlers.New(queries)
124+
h := handlers.New(queries, cfg)
109125

110126
// Routes
111127
router.GET("/", h.Home)
@@ -123,5 +139,14 @@ func setupRouter(cfg *config.Config, queries *database.Queries) *gin.Engine {
123139
scores.GET("/chart/:id", h.ScoresChart)
124140
}
125141

142+
router.GET("/login", h.MakeLogin(auth))
143+
router.GET("/logout", h.Logout)
144+
router.GET("/callback", h.MakeCallback(auth))
145+
scoresV2 := router.Group("/scoresV2")
146+
scoresV2.Use(middleware.IsAuthenticated)
147+
{
148+
scoresV2.GET("", h.ScoresIndex)
149+
}
150+
126151
return router
127152
}

go.mod

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
module github.com/gitznik/robswebhub
22

3-
go 1.23.0
3+
go 1.24.0
44

55
toolchain go1.24.3
66

77
require (
88
github.com/a-h/templ v0.3.943
9+
github.com/coreos/go-oidc/v3 v3.15.0
910
github.com/getsentry/sentry-go v0.35.1
1011
github.com/getsentry/sentry-go/gin v0.35.1
11-
github.com/gin-gonic/gin v1.9.1
12+
github.com/gin-contrib/sessions v1.0.4
13+
github.com/gin-gonic/gin v1.10.1
1214
github.com/go-echarts/go-echarts/v2 v2.3.3
1315
github.com/golang-migrate/migrate/v4 v4.19.0
1416
github.com/google/uuid v1.6.0
@@ -18,16 +20,17 @@ require (
1820
github.com/spf13/viper v1.18.2
1921
github.com/testcontainers/testcontainers-go v0.38.0
2022
github.com/testcontainers/testcontainers-go/modules/postgres v0.38.0
23+
golang.org/x/oauth2 v0.31.0
2124
)
2225

2326
require (
2427
dario.cat/mergo v1.0.1 // indirect
2528
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
2629
github.com/Microsoft/go-winio v0.6.2 // indirect
27-
github.com/bytedance/sonic v1.10.2 // indirect
30+
github.com/bytedance/sonic v1.13.2 // indirect
31+
github.com/bytedance/sonic/loader v0.2.4 // indirect
2832
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
29-
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
30-
github.com/chenzhuoyu/iasm v0.9.1 // indirect
33+
github.com/cloudwego/base64x v0.1.5 // indirect
3134
github.com/containerd/errdefs v1.0.0 // indirect
3235
github.com/containerd/errdefs/pkg v0.3.0 // indirect
3336
github.com/containerd/log v0.1.0 // indirect
@@ -41,16 +44,20 @@ require (
4144
github.com/ebitengine/purego v0.8.4 // indirect
4245
github.com/felixge/httpsnoop v1.0.4 // indirect
4346
github.com/fsnotify/fsnotify v1.7.0 // indirect
44-
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
45-
github.com/gin-contrib/sse v0.1.0 // indirect
47+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
48+
github.com/gin-contrib/sse v1.0.0 // indirect
49+
github.com/go-jose/go-jose/v4 v4.1.1 // indirect
4650
github.com/go-logr/logr v1.4.3 // indirect
4751
github.com/go-logr/stdr v1.2.2 // indirect
4852
github.com/go-ole/go-ole v1.2.6 // indirect
4953
github.com/go-playground/locales v0.14.1 // indirect
5054
github.com/go-playground/universal-translator v0.18.1 // indirect
51-
github.com/go-playground/validator/v10 v10.20.0 // indirect
52-
github.com/goccy/go-json v0.10.2 // indirect
55+
github.com/go-playground/validator/v10 v10.26.0 // indirect
56+
github.com/goccy/go-json v0.10.5 // indirect
5357
github.com/gogo/protobuf v1.3.2 // indirect
58+
github.com/gorilla/context v1.1.2 // indirect
59+
github.com/gorilla/securecookie v1.1.2 // indirect
60+
github.com/gorilla/sessions v1.4.0 // indirect
5461
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2 // indirect
5562
github.com/hashicorp/errwrap v1.1.0 // indirect
5663
github.com/hashicorp/go-multierror v1.1.1 // indirect
@@ -60,7 +67,7 @@ require (
6067
github.com/jackc/puddle/v2 v2.2.1 // indirect
6168
github.com/json-iterator/go v1.1.12 // indirect
6269
github.com/klauspost/compress v1.18.0 // indirect
63-
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
70+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
6471
github.com/leodido/go-urn v1.4.0 // indirect
6572
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
6673
github.com/magiconair/properties v1.8.10 // indirect
@@ -78,7 +85,7 @@ require (
7885
github.com/morikuni/aec v1.0.0 // indirect
7986
github.com/opencontainers/go-digest v1.0.0 // indirect
8087
github.com/opencontainers/image-spec v1.1.1 // indirect
81-
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
88+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
8289
github.com/pkg/errors v0.9.1 // indirect
8390
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
8491
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
@@ -104,7 +111,7 @@ require (
104111
go.opentelemetry.io/otel/sdk v1.37.0 // indirect
105112
go.opentelemetry.io/otel/trace v1.37.0 // indirect
106113
go.uber.org/multierr v1.11.0 // indirect
107-
golang.org/x/arch v0.6.0 // indirect
114+
golang.org/x/arch v0.16.0 // indirect
108115
golang.org/x/crypto v0.40.0 // indirect
109116
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
110117
golang.org/x/net v0.42.0 // indirect

0 commit comments

Comments
 (0)