Skip to content

Commit a3ec072

Browse files
committed
fix: fix oauth and oidc controller imports and context
1 parent b4eb709 commit a3ec072

4 files changed

Lines changed: 22 additions & 19 deletions

File tree

internal/controller/controller.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package controller
2+
3+
type UnauthorizedQuery struct {
4+
Username string `url:"username"`
5+
Resource string `url:"resource"`
6+
GroupErr bool `url:"groupErr"`
7+
IP string `url:"ip"`
8+
}
9+
10+
type RedirectQuery struct {
11+
RedirectURI string `url:"redirect_uri"`
12+
}

internal/controller/oauth_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77
"time"
88

9-
"github.com/tinyauthapp/tinyauth/internal/config"
109
"github.com/tinyauthapp/tinyauth/internal/repository"
1110
"github.com/tinyauthapp/tinyauth/internal/service"
1211
"github.com/tinyauthapp/tinyauth/internal/utils"
@@ -176,7 +175,7 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
176175
tlog.App.Warn().Str("email", user.Email).Msg("Email not whitelisted")
177176
tlog.AuditLoginFailure(c, user.Email, req.Provider, "email not whitelisted")
178177

179-
queries, err := query.Values(config.UnauthorizedQuery{
178+
queries, err := query.Values(UnauthorizedQuery{
180179
Username: user.Email,
181180
})
182181

@@ -236,14 +235,16 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
236235

237236
tlog.App.Trace().Interface("session_cookie", sessionCookie).Msg("Creating session cookie")
238237

239-
err = controller.auth.CreateSessionCookie(c, &sessionCookie)
238+
cookie, err := controller.auth.CreateSession(c, sessionCookie)
240239

241240
if err != nil {
242241
tlog.App.Error().Err(err).Msg("Failed to create session cookie")
243242
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/error", controller.config.AppURL))
244243
return
245244
}
246245

246+
http.SetCookie(c.Writer, cookie)
247+
247248
tlog.AuditLoginSuccess(c, sessionCookie.Username, sessionCookie.Provider)
248249

249250
if controller.isOidcRequest(oauthPendingSession.CallbackParams) {
@@ -259,7 +260,7 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
259260
}
260261

261262
if oauthPendingSession.CallbackParams.RedirectURI != "" {
262-
queries, err := query.Values(config.RedirectQuery{
263+
queries, err := query.Values(RedirectQuery{
263264
RedirectURI: oauthPendingSession.CallbackParams.RedirectURI,
264265
})
265266

internal/controller/oidc_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/gin-gonic/gin"
1111
"github.com/google/go-querystring/query"
1212

13+
"github.com/tinyauthapp/tinyauth/internal/model"
1314
"github.com/tinyauthapp/tinyauth/internal/service"
1415
"github.com/tinyauthapp/tinyauth/internal/utils"
1516
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
@@ -111,14 +112,14 @@ func (controller *OIDCController) Authorize(c *gin.Context) {
111112
return
112113
}
113114

114-
userContext, err := utils.GetContext(c)
115+
userContext, err := new(model.UserContext).NewFromGin(c)
115116

116117
if err != nil {
117118
controller.authorizeError(c, err, "Failed to get user context", "User is not logged in or the session is invalid", "", "", "")
118119
return
119120
}
120121

121-
if !userContext.IsLoggedIn {
122+
if !userContext.Authenticated {
122123
controller.authorizeError(c, errors.New("err user not logged in"), "User not logged in", "The user is not logged in", "", "", "")
123124
return
124125
}
@@ -151,7 +152,7 @@ func (controller *OIDCController) Authorize(c *gin.Context) {
151152
}
152153

153154
// WARNING: Since Tinyauth is stateless, we cannot have a sub that never changes. We will just create a uuid out of the username and client name which remains stable, but if username or client name changes then sub changes too.
154-
sub := utils.GenerateUUID(fmt.Sprintf("%s:%s", userContext.Username, client.ID))
155+
sub := utils.GenerateUUID(fmt.Sprintf("%s:%s", userContext.GetUsername(), client.ID))
155156
code := utils.GenerateString(32)
156157

157158
// Before storing the code, delete old session
@@ -170,7 +171,7 @@ func (controller *OIDCController) Authorize(c *gin.Context) {
170171

171172
// We also need a snapshot of the user that authorized this (skip if no openid scope)
172173
if slices.Contains(strings.Fields(req.Scope), "openid") {
173-
err = controller.oidc.StoreUserinfo(c, sub, userContext, req)
174+
err = controller.oidc.StoreUserinfo(c, sub, *userContext, req)
174175

175176
if err != nil {
176177
tlog.App.Error().Err(err).Msg("Failed to insert user info into database")

internal/controller/proxy_controller.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@ import (
1717
"github.com/google/go-querystring/query"
1818
)
1919

20-
type UnauthorizedQuery struct {
21-
Username string `url:"username"`
22-
Resource string `url:"resource"`
23-
GroupErr bool `url:"groupErr"`
24-
IP string `url:"ip"`
25-
}
26-
27-
type RedirectQuery struct {
28-
RedirectURI string `url:"redirect_uri"`
29-
}
30-
3120
type AuthModuleType int
3221

3322
const (

0 commit comments

Comments
 (0)