detached f3a8d97

Improve Oauth and Session handling, fix session validation logic flaw

commit
f3a8d9740c38c1c968906b5e7ae653e7a2d758e2
author
bnema · · 2023-10-21 08:40 UTC
parent
1a9b0aa
signature
unsigned
4 files · +30 -24
M internal/httpserve/handler/oauth.go +8 -2
@@ -54,12 +54,18 @@ func RenderLoginPage(c echo.Context, a *app.App) error {
5454
5555 // StartOAuthGithub starts the Github OAuth flow
5656 func StartOAuthGithub(c echo.Context, a *app.App) error {
57- // Clear the session in case it is already set or corrupted
58- err := ResetSession(c)
57+ // Check if the session is already here and valid before going for the oauth flow
58+ sess, err := getSession(c)
5959 if err != nil {
6060 return err
6161 }
6262
63+ // Check if the session values are valid
64+ accountID, ok := sess.Values["accountID"].(string)
65+ if ok && accountID != "" {
66+ return c.Redirect(http.StatusSeeOther, a.Config.Admin.Path) // StatusSeeOther is HTTP 303
67+ }
68+
6369 //Initiate the Github OAuth flow
6470 clientID := os.Getenv("GITHUB_APP_ID")
6571 redirectDomain := a.Config.GenerateOauthCallbackURL()
@@ -54,12 +54,18 @@ func RenderLoginPage(c echo.Context, a *app.App) error {
54 54
55 // StartOAuthGithub starts the Github OAuth flow55 // StartOAuthGithub starts the Github OAuth flow
56 func StartOAuthGithub(c echo.Context, a *app.App) error {56 func StartOAuthGithub(c echo.Context, a *app.App) error {
57- // Clear the session in case it is already set or corrupted57+ // Check if the session is already here and valid before going for the oauth flow
58- err := ResetSession(c)58+ sess, err := getSession(c)
59 if err != nil {59 if err != nil {
60 return err60 return err
61 }61 }
62 62
63+ // Check if the session values are valid
64+ accountID, ok := sess.Values["accountID"].(string)
65+ if ok && accountID != "" {
66+ return c.Redirect(http.StatusSeeOther, a.Config.Admin.Path) // StatusSeeOther is HTTP 303
67+ }
68+
63 //Initiate the Github OAuth flow69 //Initiate the Github OAuth flow
64 clientID := os.Getenv("GITHUB_APP_ID")70 clientID := os.Getenv("GITHUB_APP_ID")
65 redirectDomain := a.Config.GenerateOauthCallbackURL()71 redirectDomain := a.Config.GenerateOauthCallbackURL()
M internal/httpserve/middleware/requirelogin.go +20 -15
@@ -1,13 +1,16 @@
11 package middleware
22
33 import (
4+ "fmt"
45 "log"
56 "net/http"
67 "time"
78
89 "github.com/bnema/gordon/internal/app"
910 "github.com/bnema/gordon/internal/db/queries"
11+
1012 // "github.com/labstack/echo-contrib/session"
13+ "github.com/labstack/echo-contrib/session"
1114 "github.com/labstack/echo/v4"
1215 )
1316
@@ -24,24 +27,26 @@ func RequireLogin(a *app.App) echo.MiddlewareFunc {
2427 }
2528
2629 func validateSessionAndUser(c echo.Context, a *app.App) error {
27- // sess, err := session.Get("session", c)
28- // if err != nil {
29- // return err
30- // }
30+ sess, err := session.Get("session", c)
31+ if err != nil {
32+ return err
33+ }
3134
32- // if cookieExpired, err := isCookieExpired(c); err != nil || cookieExpired {
33- // return err
34- // }
35+ fmt.Print(sess)
3536
36- // accountID, ok := sess.Values["accountID"].(string)
37- // if !ok {
38- // return fmt.Errorf("invalid account ID type")
39- // }
37+ if cookieExpired, err := isCookieExpired(c); err != nil || cookieExpired {
38+ return err
39+ }
4040
41- // sessionID, ok := sess.Values["sessionID"].(string)
42- // if !ok {
43- // return fmt.Errorf("invalid session ID type")
44- // }
41+ _, ok := sess.Values["accountID"].(string)
42+ if !ok {
43+ return fmt.Errorf("invalid account ID type")
44+ }
45+
46+ _, ok = sess.Values["sessionID"].(string)
47+ if !ok {
48+ return fmt.Errorf("invalid session ID type")
49+ }
4550
4651 // if accountCheck, err := isAccountIDInDB(a, accountID); err != nil || !accountCheck {
4752 // return err
@@ -1,13 +1,16 @@
1 package middleware1 package middleware
2 2
3 import (3 import (
4+ "fmt"
4 "log"5 "log"
5 "net/http"6 "net/http"
6 "time"7 "time"
7 8
8 "github.com/bnema/gordon/internal/app"9 "github.com/bnema/gordon/internal/app"
9 "github.com/bnema/gordon/internal/db/queries"10 "github.com/bnema/gordon/internal/db/queries"
11+
10 // "github.com/labstack/echo-contrib/session"12 // "github.com/labstack/echo-contrib/session"
13+ "github.com/labstack/echo-contrib/session"
11 "github.com/labstack/echo/v4"14 "github.com/labstack/echo/v4"
12 )15 )
13 16
@@ -24,24 +27,26 @@ func RequireLogin(a *app.App) echo.MiddlewareFunc {
24 }27 }
25 28
26 func validateSessionAndUser(c echo.Context, a *app.App) error {29 func validateSessionAndUser(c echo.Context, a *app.App) error {
27- // sess, err := session.Get("session", c)30+ sess, err := session.Get("session", c)
28- // if err != nil {31+ if err != nil {
29- // return err32+ return err
30- // }33+ }
31 34
32- // if cookieExpired, err := isCookieExpired(c); err != nil || cookieExpired {35+ fmt.Print(sess)
33- // return err
34- // }
35 36
36- // accountID, ok := sess.Values["accountID"].(string)37+ if cookieExpired, err := isCookieExpired(c); err != nil || cookieExpired {
37- // if !ok {38+ return err
38- // return fmt.Errorf("invalid account ID type")39+ }
39- // }
40 40
41- // sessionID, ok := sess.Values["sessionID"].(string)41+ _, ok := sess.Values["accountID"].(string)
42- // if !ok {42+ if !ok {
43- // return fmt.Errorf("invalid session ID type")43+ return fmt.Errorf("invalid account ID type")
44- // }44+ }
45+
46+ _, ok = sess.Values["sessionID"].(string)
47+ if !ok {
48+ return fmt.Errorf("invalid session ID type")
49+ }
45 50
46 // if accountCheck, err := isAccountIDInDB(a, accountID); err != nil || !accountCheck {51 // if accountCheck, err := isAccountIDInDB(a, accountID); err != nil || !accountCheck {
47 // return err52 // return err
M internal/httpserve/router.go +2 -2
@@ -11,14 +11,14 @@ import (
1111 // RegisterRoutes registers all routes for the application
1212 func RegisterRoutes(e *echo.Echo, a *app.App) *echo.Echo {
1313 AdminPath := a.Config.Admin.Path
14- // Use middlewares
15- e.Use(middleware.SecureRoutes())
1614 // SetCommonDataMiddleware will pass data to the renderer
1715 e.Use(middleware.SetCommonDataMiddleware(a))
1816 // Error handler middleware
1917 e.Use(middleware.ErrorHandler)
2018 // Initiate the session middleware
2119 e.Use(middleware.InitSessionMiddleware())
20+ // Use middlewares
21+ e.Use(middleware.SecureRoutes())
2222
2323 // Color scheme detection for dark/light mode
2424 e.Use(middleware.ColorSchemeDetection)
@@ -11,14 +11,14 @@ import (
11 // RegisterRoutes registers all routes for the application11 // RegisterRoutes registers all routes for the application
12 func RegisterRoutes(e *echo.Echo, a *app.App) *echo.Echo {12 func RegisterRoutes(e *echo.Echo, a *app.App) *echo.Echo {
13 AdminPath := a.Config.Admin.Path13 AdminPath := a.Config.Admin.Path
14- // Use middlewares
15- e.Use(middleware.SecureRoutes())
16 // SetCommonDataMiddleware will pass data to the renderer14 // SetCommonDataMiddleware will pass data to the renderer
17 e.Use(middleware.SetCommonDataMiddleware(a))15 e.Use(middleware.SetCommonDataMiddleware(a))
18 // Error handler middleware16 // Error handler middleware
19 e.Use(middleware.ErrorHandler)17 e.Use(middleware.ErrorHandler)
20 // Initiate the session middleware18 // Initiate the session middleware
21 e.Use(middleware.InitSessionMiddleware())19 e.Use(middleware.InitSessionMiddleware())
20+ // Use middlewares
21+ e.Use(middleware.SecureRoutes())
22 22
23 // Color scheme detection for dark/light mode23 // Color scheme detection for dark/light mode
24 e.Use(middleware.ColorSchemeDetection)24 e.Use(middleware.ColorSchemeDetection)
M internal/webui/public/assets/css/tailwind.css +0 -5
@@ -1252,11 +1252,6 @@ video {
12521252 padding-top: 8rem;
12531253 }
12541254
1255- .md\:text-2xl {
1256- font-size: 1.5rem;
1257- line-height: 2rem;
1258- }
1259-
12601255 .md\:text-4xl {
12611256 font-size: 2.25rem;
12621257 line-height: 2.5rem;
@@ -1252,11 +1252,6 @@ video {
1252 padding-top: 8rem;1252 padding-top: 8rem;
1253 }1253 }
1254 1254
1255- .md\:text-2xl {
1256- font-size: 1.5rem;
1257- line-height: 2rem;
1258- }
1259-
1260 .md\:text-4xl {1255 .md\:text-4xl {
1261 font-size: 2.25rem;1256 font-size: 2.25rem;
1262 line-height: 2.5rem;1257 line-height: 2.5rem;

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close