Skip to content

Commit ad4fc7e

Browse files
authored
refactor: don't export non-needed fields (#336)
* refactor: don't export non-needed fields * feat: coderabbit suggestions * fix: avoid queries panic
1 parent 5184c96 commit ad4fc7e

16 files changed

Lines changed: 325 additions & 347 deletions

‎internal/controller/context_controller.go‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ type ContextControllerConfig struct {
4646
}
4747

4848
type ContextController struct {
49-
Config ContextControllerConfig
50-
Router *gin.RouterGroup
49+
config ContextControllerConfig
50+
router *gin.RouterGroup
5151
}
5252

5353
func NewContextController(config ContextControllerConfig, router *gin.RouterGroup) *ContextController {
5454
return &ContextController{
55-
Config: config,
56-
Router: router,
55+
config: config,
56+
router: router,
5757
}
5858
}
5959

6060
func (controller *ContextController) SetupRoutes() {
61-
contextGroup := controller.Router.Group("/context")
61+
contextGroup := controller.router.Group("/context")
6262
contextGroup.GET("/user", controller.userContextHandler)
6363
contextGroup.GET("/app", controller.appContextHandler)
6464
}
@@ -91,18 +91,18 @@ func (controller *ContextController) userContextHandler(c *gin.Context) {
9191
}
9292

9393
func (controller *ContextController) appContextHandler(c *gin.Context) {
94-
appUrl, _ := url.Parse(controller.Config.AppURL) // no need to check error, validated on startup
94+
appUrl, _ := url.Parse(controller.config.AppURL) // no need to check error, validated on startup
9595

9696
c.JSON(200, AppContextResponse{
9797
Status: 200,
9898
Message: "Success",
99-
ConfiguredProviders: controller.Config.ConfiguredProviders,
100-
Title: controller.Config.Title,
101-
GenericName: controller.Config.GenericName,
99+
ConfiguredProviders: controller.config.ConfiguredProviders,
100+
Title: controller.config.Title,
101+
GenericName: controller.config.GenericName,
102102
AppURL: fmt.Sprintf("%s://%s", appUrl.Scheme, appUrl.Host),
103-
RootDomain: controller.Config.RootDomain,
104-
ForgotPasswordMessage: controller.Config.ForgotPasswordMessage,
105-
BackgroundImage: controller.Config.BackgroundImage,
106-
OAuthAutoRedirect: controller.Config.OAuthAutoRedirect,
103+
RootDomain: controller.config.RootDomain,
104+
ForgotPasswordMessage: controller.config.ForgotPasswordMessage,
105+
BackgroundImage: controller.config.BackgroundImage,
106+
OAuthAutoRedirect: controller.config.OAuthAutoRedirect,
107107
})
108108
}

‎internal/controller/health_controller.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ package controller
33
import "github.com/gin-gonic/gin"
44

55
type HealthController struct {
6-
Router *gin.RouterGroup
6+
router *gin.RouterGroup
77
}
88

99
func NewHealthController(router *gin.RouterGroup) *HealthController {
1010
return &HealthController{
11-
Router: router,
11+
router: router,
1212
}
1313
}
1414

1515
func (controller *HealthController) SetupRoutes() {
16-
controller.Router.GET("/health", controller.healthHandler)
17-
controller.Router.HEAD("/health", controller.healthHandler)
16+
controller.router.GET("/health", controller.healthHandler)
17+
controller.router.HEAD("/health", controller.healthHandler)
1818
}
1919

2020
func (controller *HealthController) healthHandler(c *gin.Context) {

‎internal/controller/oauth_controller.go‎

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ type OAuthControllerConfig struct {
2727
}
2828

2929
type OAuthController struct {
30-
Config OAuthControllerConfig
31-
Router *gin.RouterGroup
32-
Auth *service.AuthService
33-
Broker *service.OAuthBrokerService
30+
config OAuthControllerConfig
31+
router *gin.RouterGroup
32+
auth *service.AuthService
33+
broker *service.OAuthBrokerService
3434
}
3535

3636
func NewOAuthController(config OAuthControllerConfig, router *gin.RouterGroup, auth *service.AuthService, broker *service.OAuthBrokerService) *OAuthController {
3737
return &OAuthController{
38-
Config: config,
39-
Router: router,
40-
Auth: auth,
41-
Broker: broker,
38+
config: config,
39+
router: router,
40+
auth: auth,
41+
broker: broker,
4242
}
4343
}
4444

4545
func (controller *OAuthController) SetupRoutes() {
46-
oauthGroup := controller.Router.Group("/oauth")
46+
oauthGroup := controller.router.Group("/oauth")
4747
oauthGroup.GET("/url/:provider", controller.oauthURLHandler)
4848
oauthGroup.GET("/callback/:provider", controller.oauthCallbackHandler)
4949
}
@@ -61,7 +61,7 @@ func (controller *OAuthController) oauthURLHandler(c *gin.Context) {
6161
return
6262
}
6363

64-
service, exists := controller.Broker.GetService(req.Provider)
64+
service, exists := controller.broker.GetService(req.Provider)
6565

6666
if !exists {
6767
log.Warn().Msgf("OAuth provider not found: %s", req.Provider)
@@ -74,13 +74,13 @@ func (controller *OAuthController) oauthURLHandler(c *gin.Context) {
7474

7575
state := service.GenerateState()
7676
authURL := service.GetAuthURL(state)
77-
c.SetCookie(controller.Config.CSRFCookieName, state, int(time.Hour.Seconds()), "/", fmt.Sprintf(".%s", controller.Config.RootDomain), controller.Config.SecureCookie, true)
77+
c.SetCookie(controller.config.CSRFCookieName, state, int(time.Hour.Seconds()), "/", fmt.Sprintf(".%s", controller.config.RootDomain), controller.config.SecureCookie, true)
7878

7979
redirectURI := c.Query("redirect_uri")
8080

81-
if redirectURI != "" && utils.IsRedirectSafe(redirectURI, controller.Config.RootDomain) {
81+
if redirectURI != "" && utils.IsRedirectSafe(redirectURI, controller.config.RootDomain) {
8282
log.Debug().Msg("Setting redirect URI cookie")
83-
c.SetCookie(controller.Config.RedirectCookieName, redirectURI, int(time.Hour.Seconds()), "/", fmt.Sprintf(".%s", controller.Config.RootDomain), controller.Config.SecureCookie, true)
83+
c.SetCookie(controller.config.RedirectCookieName, redirectURI, int(time.Hour.Seconds()), "/", fmt.Sprintf(".%s", controller.config.RootDomain), controller.config.SecureCookie, true)
8484
}
8585

8686
c.JSON(200, gin.H{
@@ -104,58 +104,58 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
104104
}
105105

106106
state := c.Query("state")
107-
csrfCookie, err := c.Cookie(controller.Config.CSRFCookieName)
107+
csrfCookie, err := c.Cookie(controller.config.CSRFCookieName)
108108

109109
if err != nil || state != csrfCookie {
110110
log.Warn().Err(err).Msg("CSRF token mismatch or cookie missing")
111-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.Config.AppURL))
111+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
112112
return
113113
}
114114

115-
c.SetCookie(controller.Config.CSRFCookieName, "", -1, "/", fmt.Sprintf(".%s", controller.Config.RootDomain), controller.Config.SecureCookie, true)
115+
c.SetCookie(controller.config.CSRFCookieName, "", -1, "/", fmt.Sprintf(".%s", controller.config.RootDomain), controller.config.SecureCookie, true)
116116

117117
code := c.Query("code")
118-
service, exists := controller.Broker.GetService(req.Provider)
118+
service, exists := controller.broker.GetService(req.Provider)
119119

120120
if !exists {
121121
log.Warn().Msgf("OAuth provider not found: %s", req.Provider)
122-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.Config.AppURL))
122+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
123123
return
124124
}
125125

126126
err = service.VerifyCode(code)
127127
if err != nil {
128128
log.Error().Err(err).Msg("Failed to verify OAuth code")
129-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.Config.AppURL))
129+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
130130
return
131131
}
132132

133-
user, err := controller.Broker.GetUser(req.Provider)
133+
user, err := controller.broker.GetUser(req.Provider)
134134

135135
if err != nil {
136136
log.Error().Err(err).Msg("Failed to get user from OAuth provider")
137-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.Config.AppURL))
137+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
138138
return
139139
}
140140

141141
if user.Email == "" {
142142
log.Error().Msg("OAuth provider did not return an email")
143-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.Config.AppURL))
143+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
144144
return
145145
}
146146

147-
if !controller.Auth.IsEmailWhitelisted(user.Email) {
147+
if !controller.auth.IsEmailWhitelisted(user.Email) {
148148
queries, err := query.Values(config.UnauthorizedQuery{
149149
Username: user.Email,
150150
})
151151

152152
if err != nil {
153153
log.Error().Err(err).Msg("Failed to encode unauthorized query")
154-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.Config.AppURL))
154+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
155155
return
156156
}
157157

158-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/unauthorized?%s", controller.Config.AppURL, queries.Encode()))
158+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/unauthorized?%s", controller.config.AppURL, queries.Encode()))
159159
return
160160
}
161161

@@ -169,29 +169,35 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
169169
name = fmt.Sprintf("%s (%s)", utils.Capitalize(strings.Split(user.Email, "@")[0]), strings.Split(user.Email, "@")[1])
170170
}
171171

172-
var usename string
172+
var username string
173173

174174
if user.PreferredUsername != "" {
175175
log.Debug().Msg("Using preferred username from OAuth provider")
176-
usename = user.PreferredUsername
176+
username = user.PreferredUsername
177177
} else {
178178
log.Debug().Msg("No preferred username from OAuth provider, using pseudo username")
179-
usename = strings.Replace(user.Email, "@", "_", -1)
179+
username = strings.Replace(user.Email, "@", "_", -1)
180180
}
181181

182-
controller.Auth.CreateSessionCookie(c, &config.SessionCookie{
183-
Username: usename,
182+
err = controller.auth.CreateSessionCookie(c, &config.SessionCookie{
183+
Username: username,
184184
Name: name,
185185
Email: user.Email,
186186
Provider: req.Provider,
187187
OAuthGroups: utils.CoalesceToString(user.Groups),
188188
})
189189

190-
redirectURI, err := c.Cookie(controller.Config.RedirectCookieName)
190+
if err != nil {
191+
log.Error().Err(err).Msg("Failed to create session cookie")
192+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
193+
return
194+
}
195+
196+
redirectURI, err := c.Cookie(controller.config.RedirectCookieName)
191197

192-
if err != nil || !utils.IsRedirectSafe(redirectURI, controller.Config.RootDomain) {
198+
if err != nil || !utils.IsRedirectSafe(redirectURI, controller.config.RootDomain) {
193199
log.Debug().Msg("No redirect URI cookie found, redirecting to app root")
194-
c.Redirect(http.StatusTemporaryRedirect, controller.Config.AppURL)
200+
c.Redirect(http.StatusTemporaryRedirect, controller.config.AppURL)
195201
return
196202
}
197203

@@ -201,10 +207,10 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
201207

202208
if err != nil {
203209
log.Error().Err(err).Msg("Failed to encode redirect URI query")
204-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.Config.AppURL))
210+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
205211
return
206212
}
207213

208-
c.SetCookie(controller.Config.RedirectCookieName, "", -1, "/", fmt.Sprintf(".%s", controller.Config.RootDomain), controller.Config.SecureCookie, true)
209-
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/continue?%s", controller.Config.AppURL, queries.Encode()))
214+
c.SetCookie(controller.config.RedirectCookieName, "", -1, "/", fmt.Sprintf(".%s", controller.config.RootDomain), controller.config.SecureCookie, true)
215+
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/continue?%s", controller.config.AppURL, queries.Encode()))
210216
}

0 commit comments

Comments
 (0)