Skip to content

Commit 1382ab4

Browse files
authored
refactor: rework user context handling throughout tinyauth (#829)
* wip * fix: fix util imports * fix: fix bootstrap import issues * fix: fix cli imports * fix: context controller * fix: use new context in user controller * fix: fix imports and context in proxy controller * fix: fix oauth and oidc controller imports and context * feat: finalize context functionality * refactor: simplify acls checking logic by passing the entire acl struct * chore: rename get basic auth to encode basic auth for clarity * fix: fix controller tests * tests: fix service tests * tests: fix utils tests * tests: move to testify for testing in utils * fix: fix config reference generator * tests: add tests for context parsing * tests: add tests for context middleware * tests: remove error wrapper from context tests * tests: fix log wrapper tests * fix: fix verion setting in cd and dockerfiles * fix: review comments batch 1 * fix: review comments batch 2 * fix: review comments batch 3 * fix: delete totp pending session cookie on totp success * tests: fix user controller tests * fix: don't audit login too early * fix: own comments
1 parent 24f2da4 commit 1382ab4

58 files changed

Lines changed: 2052 additions & 1099 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/nightly.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ jobs:
8484
- name: Build
8585
run: |
8686
cp -r frontend/dist internal/assets/dist
87-
go build -ldflags "-s -w -X github.com/tinyauthapp/tinyauth/internal/config.Version=${{ needs.generate-metadata.outputs.VERSION }} -X github.com/tinyauthapp/tinyauth/internal/config.CommitHash=${{ needs.generate-metadata.outputs.COMMIT_HASH }} -X github.com/tinyauthapp/tinyauth/internal/config.BuildTimestamp=${{ needs.generate-metadata.outputs.BUILD_TIMESTAMP }}" -o tinyauth-amd64 ./cmd/tinyauth
87+
go build -ldflags "-s -w -X github.com/tinyauthapp/tinyauth/internal/model.Version=${{ needs.generate-metadata.outputs.VERSION }} -X github.com/tinyauthapp/tinyauth/internal/model.CommitHash=${{ needs.generate-metadata.outputs.COMMIT_HASH }} -X github.com/tinyauthapp/tinyauth/internal/model.BuildTimestamp=${{ needs.generate-metadata.outputs.BUILD_TIMESTAMP }}" -o tinyauth-amd64 ./cmd/tinyauth
8888
env:
8989
CGO_ENABLED: 0
9090

@@ -130,7 +130,7 @@ jobs:
130130
- name: Build
131131
run: |
132132
cp -r frontend/dist internal/assets/dist
133-
go build -ldflags "-s -w -X github.com/tinyauthapp/tinyauth/internal/config.Version=${{ needs.generate-metadata.outputs.VERSION }} -X github.com/tinyauthapp/tinyauth/internal/config.CommitHash=${{ needs.generate-metadata.outputs.COMMIT_HASH }} -X github.com/tinyauthapp/tinyauth/internal/config.BuildTimestamp=${{ needs.generate-metadata.outputs.BUILD_TIMESTAMP }}" -o tinyauth-arm64 ./cmd/tinyauth
133+
go build -ldflags "-s -w -X github.com/tinyauthapp/tinyauth/internal/model.Version=${{ needs.generate-metadata.outputs.VERSION }} -X github.com/tinyauthapp/tinyauth/internal/model.CommitHash=${{ needs.generate-metadata.outputs.COMMIT_HASH }} -X github.com/tinyauthapp/tinyauth/internal/model.BuildTimestamp=${{ needs.generate-metadata.outputs.BUILD_TIMESTAMP }}" -o tinyauth-arm64 ./cmd/tinyauth
134134
env:
135135
CGO_ENABLED: 0
136136

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
- name: Build
6161
run: |
6262
cp -r frontend/dist internal/assets/dist
63-
go build -ldflags "-s -w -X github.com/tinyauthapp/tinyauth/internal/config.Version=${{ needs.generate-metadata.outputs.VERSION }} -X github.com/tinyauthapp/tinyauth/internal/config.CommitHash=${{ needs.generate-metadata.outputs.COMMIT_HASH }} -X github.com/tinyauthapp/tinyauth/internal/config.BuildTimestamp=${{ needs.generate-metadata.outputs.BUILD_TIMESTAMP }}" -o tinyauth-amd64 ./cmd/tinyauth
63+
go build -ldflags "-s -w -X github.com/tinyauthapp/tinyauth/internal/model.Version=${{ needs.generate-metadata.outputs.VERSION }} -X github.com/tinyauthapp/tinyauth/internal/model.CommitHash=${{ needs.generate-metadata.outputs.COMMIT_HASH }} -X github.com/tinyauthapp/tinyauth/internal/model.BuildTimestamp=${{ needs.generate-metadata.outputs.BUILD_TIMESTAMP }}" -o tinyauth-amd64 ./cmd/tinyauth
6464
env:
6565
CGO_ENABLED: 0
6666

@@ -103,7 +103,7 @@ jobs:
103103
- name: Build
104104
run: |
105105
cp -r frontend/dist internal/assets/dist
106-
go build -ldflags "-s -w -X github.com/tinyauthapp/tinyauth/internal/config.Version=${{ needs.generate-metadata.outputs.VERSION }} -X github.com/tinyauthapp/tinyauth/internal/config.CommitHash=${{ needs.generate-metadata.outputs.COMMIT_HASH }} -X github.com/tinyauthapp/tinyauth/internal/config.BuildTimestamp=${{ needs.generate-metadata.outputs.BUILD_TIMESTAMP }}" -o tinyauth-arm64 ./cmd/tinyauth
106+
go build -ldflags "-s -w -X github.com/tinyauthapp/tinyauth/internal/model.Version=${{ needs.generate-metadata.outputs.VERSION }} -X github.com/tinyauthapp/tinyauth/internal/model.CommitHash=${{ needs.generate-metadata.outputs.COMMIT_HASH }} -X github.com/tinyauthapp/tinyauth/internal/model.BuildTimestamp=${{ needs.generate-metadata.outputs.BUILD_TIMESTAMP }}" -o tinyauth-arm64 ./cmd/tinyauth
107107
env:
108108
CGO_ENABLED: 0
109109

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ COPY ./internal ./internal
3838
COPY --from=frontend-builder /frontend/dist ./internal/assets/dist
3939

4040
RUN CGO_ENABLED=0 go build -ldflags "-s -w \
41-
-X github.com/tinyauthapp/tinyauth/internal/config.Version=${VERSION} \
42-
-X github.com/tinyauthapp/tinyauth/internal/config.CommitHash=${COMMIT_HASH} \
43-
-X github.com/tinyauthapp/tinyauth/internal/config.BuildTimestamp=${BUILD_TIMESTAMP}" ./cmd/tinyauth
41+
-X github.com/tinyauthapp/tinyauth/internal/model.Version=${VERSION} \
42+
-X github.com/tinyauthapp/tinyauth/internal/model.CommitHash=${COMMIT_HASH} \
43+
-X github.com/tinyauthapp/tinyauth/internal/model.BuildTimestamp=${BUILD_TIMESTAMP}" ./cmd/tinyauth
4444

4545
# Runner
4646
FROM alpine:3.23 AS runner

Dockerfile.distroless

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ COPY --from=frontend-builder /frontend/dist ./internal/assets/dist
4040
RUN mkdir -p data
4141

4242
RUN CGO_ENABLED=0 go build -ldflags "-s -w \
43-
-X github.com/tinyauthapp/tinyauth/internal/config.Version=${VERSION} \
44-
-X github.com/tinyauthapp/tinyauth/internal/config.CommitHash=${COMMIT_HASH} \
45-
-X github.com/tinyauthapp/tinyauth/internal/config.BuildTimestamp=${BUILD_TIMESTAMP}" ./cmd/tinyauth
43+
-X github.com/tinyauthapp/tinyauth/internal/model.Version=${VERSION} \
44+
-X github.com/tinyauthapp/tinyauth/internal/model.CommitHash=${COMMIT_HASH} \
45+
-X github.com/tinyauthapp/tinyauth/internal/model.BuildTimestamp=${BUILD_TIMESTAMP}" ./cmd/tinyauth
4646

4747
# Runner
4848
FROM gcr.io/distroless/static-debian12:latest AS runner

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ webui: clean-webui
3737
# Build the binary
3838
binary: webui
3939
CGO_ENABLED=$(CGO_ENABLED) go build -ldflags "-s -w \
40-
-X github.com/tinyauthapp/tinyauth/internal/config.Version=${TAG_NAME} \
41-
-X github.com/tinyauthapp/tinyauth/internal/config.CommitHash=${COMMIT_HASH} \
42-
-X github.com/tinyauthapp/tinyauth/internal/config.BuildTimestamp=${BUILD_TIMESTAMP}" \
40+
-X github.com/tinyauthapp/tinyauth/internal/model.Version=${TAG_NAME} \
41+
-X github.com/tinyauthapp/tinyauth/internal/model.CommitHash=${COMMIT_HASH} \
42+
-X github.com/tinyauthapp/tinyauth/internal/model.BuildTimestamp=${BUILD_TIMESTAMP}" \
4343
-o ${BIN_NAME} ./cmd/tinyauth
4444

4545
# Build for amd64

cmd/tinyauth/generate_totp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func generateTotpCmd() *cli.Command {
7373
docker = true
7474
}
7575

76-
if user.TotpSecret != "" {
76+
if user.TOTPSecret != "" {
7777
return fmt.Errorf("user already has a TOTP secret")
7878
}
7979

@@ -102,14 +102,14 @@ func generateTotpCmd() *cli.Command {
102102

103103
qrterminal.GenerateWithConfig(key.URL(), config)
104104

105-
user.TotpSecret = secret
105+
user.TOTPSecret = secret
106106

107107
// If using docker escape re-escape it
108108
if docker {
109109
user.Password = strings.ReplaceAll(user.Password, "$", "$$")
110110
}
111111

112-
tlog.App.Info().Str("user", fmt.Sprintf("%s:%s:%s", user.Username, user.Password, user.TotpSecret)).Msg("Add the totp secret to your authenticator app then use the verify command to ensure everything is working correctly.")
112+
tlog.App.Info().Str("user", fmt.Sprintf("%s:%s:%s", user.Username, user.Password, user.TOTPSecret)).Msg("Add the totp secret to your authenticator app then use the verify command to ensure everything is working correctly.")
113113

114114
return nil
115115
},

cmd/tinyauth/tinyauth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"charm.land/huh/v2"
77
"github.com/tinyauthapp/tinyauth/internal/bootstrap"
8-
"github.com/tinyauthapp/tinyauth/internal/config"
8+
"github.com/tinyauthapp/tinyauth/internal/model"
99
"github.com/tinyauthapp/tinyauth/internal/utils/loaders"
1010
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
1111

@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func main() {
17-
tConfig := config.NewDefaultConfiguration()
17+
tConfig := model.NewDefaultConfiguration()
1818

1919
loaders := []cli.ResourceLoader{
2020
&loaders.FileLoader{},
@@ -108,11 +108,11 @@ func main() {
108108
}
109109
}
110110

111-
func runCmd(cfg config.Config) error {
111+
func runCmd(cfg model.Config) error {
112112
logger := tlog.NewLogger(cfg.Log)
113113
logger.Init()
114114

115-
tlog.App.Info().Str("version", config.Version).Msg("Starting tinyauth")
115+
tlog.App.Info().Str("version", model.Version).Msg("Starting tinyauth")
116116

117117
app := bootstrap.NewBootstrapApp(cfg)
118118

cmd/tinyauth/verify_user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ func verifyUserCmd() *cli.Command {
9595
return fmt.Errorf("password is incorrect: %w", err)
9696
}
9797

98-
if user.TotpSecret == "" {
98+
if user.TOTPSecret == "" {
9999
if tCfg.Totp != "" {
100100
tlog.App.Warn().Msg("User does not have TOTP secret")
101101
}
102102
tlog.App.Info().Msg("User verified")
103103
return nil
104104
}
105105

106-
ok := totp.Validate(tCfg.Totp, user.TotpSecret)
106+
ok := totp.Validate(tCfg.Totp, user.TOTPSecret)
107107

108108
if !ok {
109109
return fmt.Errorf("TOTP code incorrect")

cmd/tinyauth/version.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/tinyauthapp/tinyauth/internal/config"
7-
86
"github.com/tinyauthapp/paerser/cli"
7+
"github.com/tinyauthapp/tinyauth/internal/model"
98
)
109

1110
func versionCmd() *cli.Command {
@@ -15,9 +14,9 @@ func versionCmd() *cli.Command {
1514
Configuration: nil,
1615
Resources: nil,
1716
Run: func(_ []string) error {
18-
fmt.Printf("Version: %s\n", config.Version)
19-
fmt.Printf("Commit Hash: %s\n", config.CommitHash)
20-
fmt.Printf("Build Timestamp: %s\n", config.BuildTimestamp)
17+
fmt.Printf("Version: %s\n", model.Version)
18+
fmt.Printf("Commit Hash: %s\n", model.CommitHash)
19+
fmt.Printf("Build Timestamp: %s\n", model.BuildTimestamp)
2120
return nil
2221
},
2322
}

gen/gen_env.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"reflect"
1111
"strings"
1212

13-
"github.com/tinyauthapp/tinyauth/internal/config"
13+
"github.com/tinyauthapp/tinyauth/internal/model"
1414
)
1515

1616
type EnvEntry struct {
@@ -20,7 +20,7 @@ type EnvEntry struct {
2020
}
2121

2222
func generateExampleEnv() {
23-
cfg := config.NewDefaultConfiguration()
23+
cfg := model.NewDefaultConfiguration()
2424
entries := make([]EnvEntry, 0)
2525

2626
root := reflect.TypeOf(cfg).Elem()

0 commit comments

Comments
 (0)