@@ -21,26 +21,25 @@ type OAuthRequest struct {
2121}
2222
2323type OAuthControllerConfig struct {
24- CSRFCookieName string
25- RedirectCookieName string
26- SecureCookie bool
27- AppURL string
28- CookieDomain string
24+ CSRFCookieName string
25+ OAuthSessionCookieName string
26+ RedirectCookieName string
27+ SecureCookie bool
28+ AppURL string
29+ CookieDomain string
2930}
3031
3132type OAuthController struct {
3233 config OAuthControllerConfig
3334 router * gin.RouterGroup
3435 auth * service.AuthService
35- broker * service.OAuthBrokerService
3636}
3737
38- func NewOAuthController (config OAuthControllerConfig , router * gin.RouterGroup , auth * service.AuthService , broker * service. OAuthBrokerService ) * OAuthController {
38+ func NewOAuthController (config OAuthControllerConfig , router * gin.RouterGroup , auth * service.AuthService ) * OAuthController {
3939 return & OAuthController {
4040 config : config ,
4141 router : router ,
4242 auth : auth ,
43- broker : broker ,
4443 }
4544}
4645
@@ -63,21 +62,32 @@ func (controller *OAuthController) oauthURLHandler(c *gin.Context) {
6362 return
6463 }
6564
66- service , exists := controller .broker . GetService (req .Provider )
65+ sessionId , session , err := controller .auth . NewOAuthSession (req .Provider )
6766
68- if ! exists {
69- tlog .App .Warn ().Msgf ("OAuth provider not found: %s" , req .Provider )
70- c .JSON (404 , gin.H {
71- "status" : 404 ,
72- "message" : "Not Found" ,
67+ if err != nil {
68+ tlog .App .Error ().Err (err ).Msg ("Failed to create OAuth session" )
69+ c .JSON (500 , gin.H {
70+ "status" : 500 ,
71+ "message" : "Internal Server Error" ,
72+ })
73+ return
74+ }
75+
76+ tlog .App .Debug ().Interface ("session" , session ).Msg ("Created new OAuth session" )
77+
78+ authUrl , err := controller .auth .GetOAuthURL (sessionId )
79+
80+ if err != nil {
81+ tlog .App .Error ().Err (err ).Msg ("Failed to get OAuth URL" )
82+ c .JSON (500 , gin.H {
83+ "status" : 500 ,
84+ "message" : "Internal Server Error" ,
7385 })
7486 return
7587 }
7688
77- service .GenerateVerifier ()
78- state := service .GenerateState ()
79- authURL := service .GetAuthURL (state )
80- c .SetCookie (controller .config .CSRFCookieName , state , int (time .Hour .Seconds ()), "/" , fmt .Sprintf (".%s" , controller .config .CookieDomain ), controller .config .SecureCookie , true )
89+ c .SetCookie (controller .config .OAuthSessionCookieName , sessionId , int (time .Hour .Seconds ()), "/" , fmt .Sprintf (".%s" , controller .config .CookieDomain ), controller .config .SecureCookie , true )
90+ c .SetCookie (controller .config .CSRFCookieName , session .State , int (time .Hour .Seconds ()), "/" , fmt .Sprintf (".%s" , controller .config .CookieDomain ), controller .config .SecureCookie , true )
8191
8292 redirectURI := c .Query ("redirect_uri" )
8393 isRedirectSafe := utils .IsRedirectSafe (redirectURI , controller .config .CookieDomain )
@@ -95,7 +105,7 @@ func (controller *OAuthController) oauthURLHandler(c *gin.Context) {
95105 c .JSON (200 , gin.H {
96106 "status" : 200 ,
97107 "message" : "OK" ,
98- "url" : authURL ,
108+ "url" : authUrl ,
99109 })
100110}
101111
@@ -112,6 +122,16 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
112122 return
113123 }
114124
125+ sessionIdCookie , err := c .Cookie (controller .config .OAuthSessionCookieName )
126+
127+ if err != nil {
128+ tlog .App .Warn ().Err (err ).Msg ("OAuth session cookie missing" )
129+ c .Redirect (http .StatusTemporaryRedirect , fmt .Sprintf ("%s/error" , controller .config .AppURL ))
130+ return
131+ }
132+
133+ c .SetCookie (controller .config .OAuthSessionCookieName , "" , - 1 , "/" , fmt .Sprintf (".%s" , controller .config .CookieDomain ), controller .config .SecureCookie , true )
134+
115135 state := c .Query ("state" )
116136 csrfCookie , err := c .Cookie (controller .config .CSRFCookieName )
117137
@@ -125,28 +145,17 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
125145 c .SetCookie (controller .config .CSRFCookieName , "" , - 1 , "/" , fmt .Sprintf (".%s" , controller .config .CookieDomain ), controller .config .SecureCookie , true )
126146
127147 code := c .Query ("code" )
128- service , exists := controller .broker .GetService (req .Provider )
129148
130- if ! exists {
131- tlog .App .Warn ().Msgf ("OAuth provider not found: %s" , req .Provider )
132- c .Redirect (http .StatusTemporaryRedirect , fmt .Sprintf ("%s/error" , controller .config .AppURL ))
133- return
134- }
149+ tlog .App .Debug ().Str ("code" , code ).Str ("state" , state ).Msg ("Received OAuth callback" )
150+ _ , err = controller .auth .GetOAuthToken (sessionIdCookie , code )
135151
136- err = service .VerifyCode (code )
137152 if err != nil {
138- tlog .App .Error ().Err (err ).Msg ("Failed to verify OAuth code" )
153+ tlog .App .Error ().Err (err ).Msg ("Failed to exchange code for token " )
139154 c .Redirect (http .StatusTemporaryRedirect , fmt .Sprintf ("%s/error" , controller .config .AppURL ))
140155 return
141156 }
142157
143- user , err := controller .broker .GetUser (req .Provider )
144-
145- if err != nil {
146- tlog .App .Error ().Err (err ).Msg ("Failed to get user from OAuth provider" )
147- c .Redirect (http .StatusTemporaryRedirect , fmt .Sprintf ("%s/error" , controller .config .AppURL ))
148- return
149- }
158+ user , err := controller .auth .GetOAuthUserinfo (sessionIdCookie )
150159
151160 if user .Email == "" {
152161 tlog .App .Error ().Msg ("OAuth provider did not return an email" )
@@ -192,13 +201,21 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
192201 username = strings .Replace (user .Email , "@" , "_" , 1 )
193202 }
194203
204+ service , err := controller .auth .GetOAuthService (sessionIdCookie )
205+
206+ if err != nil {
207+ tlog .App .Error ().Err (err ).Msg ("Failed to get OAuth service for session" )
208+ c .Redirect (http .StatusTemporaryRedirect , fmt .Sprintf ("%s/error" , controller .config .AppURL ))
209+ return
210+ }
211+
195212 sessionCookie := repository.Session {
196213 Username : username ,
197214 Name : name ,
198215 Email : user .Email ,
199216 Provider : req .Provider ,
200217 OAuthGroups : utils .CoalesceToString (user .Groups ),
201- OAuthName : service .GetName (),
218+ OAuthName : service .Name (),
202219 OAuthSub : user .Sub ,
203220 }
204221
@@ -214,6 +231,9 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) {
214231
215232 tlog .AuditLoginSuccess (c , sessionCookie .Username , sessionCookie .Provider )
216233
234+ // Clear OAuth session
235+ controller .auth .EndOAuthSession (sessionIdCookie )
236+
217237 redirectURI , err := c .Cookie (controller .config .RedirectCookieName )
218238
219239 if err != nil || ! utils .IsRedirectSafe (redirectURI , controller .config .CookieDomain ) {
0 commit comments