Skip to content

Commit 2e2b3bb

Browse files
committed
Replace auth handlers with commands
1 parent 3600292 commit 2e2b3bb

5 files changed

Lines changed: 17 additions & 107 deletions

File tree

backend/command/account.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/theandrew168/bloggulus/backend/repository"
1010
)
1111

12-
var ErrAccountNotFound = errors.New("account not found")
13-
var ErrDeleteAdminAccount = errors.New("cannot delete admin account")
12+
var ErrAccountNotFound = errors.New("account: not found")
13+
var ErrDeleteAdminAccount = errors.New("account: cannot delete admin account")
1414

1515
func (cmd *Command) FollowBlog(accountID uuid.UUID, blogID uuid.UUID) error {
1616
return cmd.repo.WithTransaction(func(tx *repository.Repository) error {

backend/command/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/theandrew168/bloggulus/backend/web/util"
1313
)
1414

15-
var ErrSessionNotFound = errors.New("session not found")
15+
var ErrSessionNotFound = errors.New("session: not found")
1616

1717
func (cmd *Command) SignIn(username string) (string, error) {
1818
// NOTE: Handling state outside the transaciton is the exception, not the rule.

backend/command/blog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/theandrew168/bloggulus/backend/repository"
1111
)
1212

13-
var ErrBlogNotFound = errors.New("blog not found")
13+
var ErrBlogNotFound = errors.New("blog: not found")
1414

1515
func (cmd *Command) DeleteBlog(blogID uuid.UUID) error {
1616
return cmd.repo.WithTransaction(func(tx *repository.Repository) error {

backend/web/auth.go

Lines changed: 9 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import (
1010

1111
"golang.org/x/oauth2"
1212

13-
"github.com/theandrew168/bloggulus/backend/model"
14-
"github.com/theandrew168/bloggulus/backend/postgres"
13+
"github.com/theandrew168/bloggulus/backend/command"
1514
"github.com/theandrew168/bloggulus/backend/random"
16-
"github.com/theandrew168/bloggulus/backend/repository"
1715
"github.com/theandrew168/bloggulus/backend/web/page"
1816
"github.com/theandrew168/bloggulus/backend/web/util"
1917
)
@@ -134,7 +132,7 @@ func HandleOAuthSignIn(conf *oauth2.Config) http.Handler {
134132

135133
func HandleOAuthCallback(
136134
secretKey string,
137-
repo *repository.Repository,
135+
cmd *command.Command,
138136
conf *oauth2.Config,
139137
fetchUserID FetchUserID,
140138
) http.Handler {
@@ -174,54 +172,16 @@ func HandleOAuthCallback(
174172
}
175173

176174
username := util.HashUserID(userID, secretKey)
177-
account, err := repo.Account().ReadByUsername(username)
178-
if err != nil {
179-
if !errors.Is(err, postgres.ErrNotFound) {
180-
util.InternalServerErrorResponse(w, r, err)
181-
return
182-
}
183-
184-
// We need to create a new account at this point.
185-
account, err = model.NewAccount(username)
186-
if err != nil {
187-
util.InternalServerErrorResponse(w, r, err)
188-
return
189-
}
190-
191-
err = repo.Account().Create(account)
192-
if err != nil {
193-
slog.Error("failed create user account", "error", err.Error())
194-
util.BadRequestResponse(w, r)
195-
return
196-
}
197-
198-
slog.Info("account created",
199-
"account_id", account.ID(),
200-
)
201-
}
202-
203-
// Create a new session for the account.
204-
session, sessionID, err := model.NewSession(account, util.SessionCookieTTL)
175+
sessionID, err := cmd.SignIn(username)
205176
if err != nil {
206177
util.InternalServerErrorResponse(w, r, err)
207178
return
208179
}
209180

210-
err = repo.Session().Create(session)
211-
if err != nil {
212-
util.CreateErrorResponse(w, r, err)
213-
return
214-
}
215-
216181
// Set a permanent cookie after sign in.
217182
sessionCookie := util.NewPermanentCookie(util.SessionCookieName, sessionID, util.SessionCookieTTL)
218183
http.SetCookie(w, &sessionCookie)
219184

220-
slog.Info("account signed in",
221-
"account_id", account.ID(),
222-
"session_id", session.ID(),
223-
)
224-
225185
next := "/"
226186
nextCookie, err := r.Cookie(util.NextCookieName)
227187
if err == nil {
@@ -235,7 +195,7 @@ func HandleOAuthCallback(
235195
})
236196
}
237197

238-
func HandleDebugSignIn(secretKey string, repo *repository.Repository) http.Handler {
198+
func HandleDebugSignIn(secretKey string, cmd *command.Command) http.Handler {
239199
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
240200
// Generate a random userID for the debug sign in.
241201
userID, err := random.BytesBase64(16)
@@ -247,54 +207,16 @@ func HandleDebugSignIn(secretKey string, repo *repository.Repository) http.Handl
247207
userID = "debug_" + userID
248208
username := util.HashUserID(userID, secretKey)
249209

250-
account, err := repo.Account().ReadByUsername(username)
251-
if err != nil {
252-
if !errors.Is(err, postgres.ErrNotFound) {
253-
util.InternalServerErrorResponse(w, r, err)
254-
return
255-
}
256-
257-
// We need to create a new account at this point.
258-
account, err = model.NewAccount(username)
259-
if err != nil {
260-
util.InternalServerErrorResponse(w, r, err)
261-
return
262-
}
263-
264-
err = repo.Account().Create(account)
265-
if err != nil {
266-
slog.Error("failed create user account", "error", err.Error())
267-
util.BadRequestResponse(w, r)
268-
return
269-
}
270-
271-
slog.Info("account created",
272-
"account_id", account.ID(),
273-
)
274-
}
275-
276-
// Create a new session for the account.
277-
session, sessionID, err := model.NewSession(account, util.SessionCookieTTL)
210+
sessionID, err := cmd.SignIn(username)
278211
if err != nil {
279212
util.InternalServerErrorResponse(w, r, err)
280213
return
281214
}
282215

283-
err = repo.Session().Create(session)
284-
if err != nil {
285-
util.CreateErrorResponse(w, r, err)
286-
return
287-
}
288-
289216
// Set a permanent cookie after sign in.
290217
sessionCookie := util.NewPermanentCookie(util.SessionCookieName, sessionID, util.SessionCookieTTL)
291218
http.SetCookie(w, &sessionCookie)
292219

293-
slog.Info("account signed in",
294-
"account_id", account.ID(),
295-
"session_id", session.ID(),
296-
)
297-
298220
next := "/"
299221
nextCookie, err := r.Cookie(util.NextCookieName)
300222
if err == nil {
@@ -308,7 +230,7 @@ func HandleDebugSignIn(secretKey string, repo *repository.Repository) http.Handl
308230
})
309231
}
310232

311-
func HandleSignOutForm(repo *repository.Repository) http.Handler {
233+
func HandleSignOutForm(cmd *command.Command) http.Handler {
312234
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
313235
// Check for a session ID. If there isn't one, just redirect back home.
314236
sessionID, err := r.Cookie(util.SessionCookieName)
@@ -321,23 +243,11 @@ func HandleSignOutForm(repo *repository.Repository) http.Handler {
321243
cookie := util.NewExpiredCookie(util.SessionCookieName)
322244
http.SetCookie(w, &cookie)
323245

324-
// Lookup the session by its client-side session ID.
325-
session, err := repo.Session().ReadBySessionID(sessionID.Value)
326-
if err != nil {
327-
switch {
328-
case errors.Is(err, postgres.ErrNotFound):
329-
http.Redirect(w, r, "/", http.StatusSeeOther)
330-
default:
331-
util.InternalServerErrorResponse(w, r, err)
332-
}
333-
return
334-
}
335-
336-
// Delete the session from the database.
337-
err = repo.Session().Delete(session)
246+
err = cmd.SignOut(sessionID.Value)
338247
if err != nil {
339248
switch {
340-
case errors.Is(err, postgres.ErrNotFound):
249+
case errors.Is(err, command.ErrSessionNotFound):
250+
// If the session was not found, just redirect back home.
341251
http.Redirect(w, r, "/", http.StatusSeeOther)
342252
default:
343253
util.InternalServerErrorResponse(w, r, err)

backend/web/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ func Handler(
9696
// Check if the debug auth method should be enabled.
9797
enableDebugAuth := os.Getenv("ENABLE_DEBUG_AUTH") != ""
9898
if enableDebugAuth {
99-
mux.Handle("POST /debug/signin", HandleDebugSignIn(conf.SecretKey, repo))
99+
mux.Handle("POST /debug/signin", HandleDebugSignIn(conf.SecretKey, cmd))
100100
}
101101

102102
// Authenication routes.
103103
mux.Handle("GET /signin", HandleSignIn(enableDebugAuth))
104104
mux.Handle("GET /github/signin", HandleOAuthSignIn(&githubConf))
105-
mux.Handle("GET /github/callback", HandleOAuthCallback(conf.SecretKey, repo, &githubConf, FetchGithubUserID))
105+
mux.Handle("GET /github/callback", HandleOAuthCallback(conf.SecretKey, cmd, &githubConf, FetchGithubUserID))
106106
mux.Handle("GET /google/signin", HandleOAuthSignIn(&googleConf))
107-
mux.Handle("GET /google/callback", HandleOAuthCallback(conf.SecretKey, repo, &googleConf, FetchGoogleUserID))
108-
mux.Handle("POST /signout", HandleSignOutForm(repo))
107+
mux.Handle("GET /google/callback", HandleOAuthCallback(conf.SecretKey, cmd, &googleConf, FetchGoogleUserID))
108+
mux.Handle("POST /signout", HandleSignOutForm(cmd))
109109

110110
// Public blog routes.
111111
mux.Handle("GET /blogs", requireAccount(HandleBlogList(qry)))

0 commit comments

Comments
 (0)