@@ -16,6 +16,15 @@ import (
1616 "github.com/tinyauthapp/tinyauth/internal/utils/logger"
1717)
1818
19+ type authorizeErrorParams struct {
20+ err error
21+ reason string
22+ reasonPublic string
23+ callback string
24+ callbackError string
25+ state string
26+ }
27+
1928type OIDCController struct {
2029 log * logger.Logger
2130 oidc * service.OIDCService
@@ -119,34 +128,55 @@ func (controller *OIDCController) GetClientInfo(c *gin.Context) {
119128
120129func (controller * OIDCController ) Authorize (c * gin.Context ) {
121130 if controller .oidc == nil {
122- controller .authorizeError (c , errors .New ("err_oidc_not_configured" ), "OIDC not configured" , "This instance is not configured for OIDC" , "" , "" , "" )
131+ controller .authorizeError (c , authorizeErrorParams {
132+ err : errors .New ("err_oidc_not_configured" ),
133+ reason : "OIDC not configured" ,
134+ reasonPublic : "This instance is not configured for OIDC" ,
135+ })
123136 return
124137 }
125138
126139 userContext , err := new (model.UserContext ).NewFromGin (c )
127140
128141 if err != nil {
129- controller .authorizeError (c , err , "Failed to get user context" , "User is not logged in or the session is invalid" , "" , "" , "" )
142+ controller .authorizeError (c , authorizeErrorParams {
143+ err : err ,
144+ reason : "Failed to get user context" ,
145+ reasonPublic : "User is not logged in or the session is invalid" ,
146+ })
130147 return
131148 }
132149
133150 if ! userContext .Authenticated {
134- controller .authorizeError (c , errors .New ("err user not logged in" ), "User not logged in" , "The user is not logged in" , "" , "" , "" )
151+ controller .authorizeError (c , authorizeErrorParams {
152+ err : errors .New ("err user not logged in" ),
153+ reason : "User not logged in" ,
154+ reasonPublic : "The user is not logged in" ,
155+ })
135156 return
136157 }
137158
138159 var req service.AuthorizeRequest
139160
140- err = c .BindJSON (& req )
161+ err = c .Bind (& req )
162+
141163 if err != nil {
142- controller .authorizeError (c , err , "Failed to bind JSON" , "The client provided an invalid authorization request" , "" , "" , "" )
164+ controller .authorizeError (c , authorizeErrorParams {
165+ err : err ,
166+ reason : "Failed to bind JSON" ,
167+ reasonPublic : "The client provided an invalid authorization request" ,
168+ })
143169 return
144170 }
145171
146172 client , ok := controller .oidc .GetClient (req .ClientID )
147173
148174 if ! ok {
149- controller .authorizeError (c , fmt .Errorf ("client not found: %s" , req .ClientID ), "Client not found" , "The client ID is invalid" , "" , "" , "" )
175+ controller .authorizeError (c , authorizeErrorParams {
176+ err : fmt .Errorf ("client not found: %s" , req .ClientID ),
177+ reason : "Client not found" ,
178+ reasonPublic : "The client ID is invalid" ,
179+ })
150180 return
151181 }
152182
@@ -155,10 +185,21 @@ func (controller *OIDCController) Authorize(c *gin.Context) {
155185 if err != nil {
156186 controller .log .App .Warn ().Err (err ).Msg ("Failed to validate authorize params" )
157187 if err .Error () != "invalid_request_uri" {
158- controller .authorizeError (c , err , "Failed validate authorize params" , "Invalid request parameters" , req .RedirectURI , err .Error (), req .State )
188+ controller .authorizeError (c , authorizeErrorParams {
189+ err : err ,
190+ reason : "Failed validate authorize params" ,
191+ reasonPublic : "Invalid request parameters" ,
192+ callback : req .RedirectURI ,
193+ callbackError : err .Error (),
194+ state : req .State ,
195+ })
159196 return
160197 }
161- controller .authorizeError (c , err , "Redirect URI not trusted" , "The provided redirect URI is not trusted" , "" , "" , "" )
198+ controller .authorizeError (c , authorizeErrorParams {
199+ err : err ,
200+ reason : "Redirect URI not trusted" ,
201+ reasonPublic : "The provided redirect URI is not trusted" ,
202+ })
162203 return
163204 }
164205
@@ -169,14 +210,28 @@ func (controller *OIDCController) Authorize(c *gin.Context) {
169210 // Before storing the code, delete old session
170211 err = controller .oidc .DeleteOldSession (c , sub )
171212 if err != nil {
172- controller .authorizeError (c , err , "Failed to delete old sessions" , "Failed to delete old sessions" , req .RedirectURI , "server_error" , req .State )
213+ controller .authorizeError (c , authorizeErrorParams {
214+ err : err ,
215+ reason : "Failed to delete old sessions" ,
216+ reasonPublic : "Failed to delete old sessions" ,
217+ callback : req .RedirectURI ,
218+ callbackError : "server_error" ,
219+ state : req .State ,
220+ })
173221 return
174222 }
175223
176224 err = controller .oidc .StoreCode (c , sub , code , req )
177225
178226 if err != nil {
179- controller .authorizeError (c , err , "Failed to store code" , "Failed to store code" , req .RedirectURI , "server_error" , req .State )
227+ controller .authorizeError (c , authorizeErrorParams {
228+ err : err ,
229+ reason : "Failed to store code" ,
230+ reasonPublic : "Failed to store code" ,
231+ callback : req .RedirectURI ,
232+ callbackError : "server_error" ,
233+ state : req .State ,
234+ })
180235 return
181236 }
182237
@@ -186,7 +241,14 @@ func (controller *OIDCController) Authorize(c *gin.Context) {
186241
187242 if err != nil {
188243 controller .log .App .Error ().Err (err ).Msg ("Failed to store user info" )
189- controller .authorizeError (c , err , "Failed to store user info" , "Failed to store user info" , req .RedirectURI , "server_error" , req .State )
244+ controller .authorizeError (c , authorizeErrorParams {
245+ err : err ,
246+ reason : "Failed to store user info" ,
247+ reasonPublic : "Failed to store user info" ,
248+ callback : req .RedirectURI ,
249+ callbackError : "server_error" ,
250+ state : req .State ,
251+ })
190252 return
191253 }
192254 }
@@ -197,7 +259,14 @@ func (controller *OIDCController) Authorize(c *gin.Context) {
197259 })
198260
199261 if err != nil {
200- controller .authorizeError (c , err , "Failed to build query" , "Failed to build query" , req .RedirectURI , "server_error" , req .State )
262+ controller .authorizeError (c , authorizeErrorParams {
263+ err : err ,
264+ reason : "Failed to build query" ,
265+ reasonPublic : "Failed to build query" ,
266+ callback : req .RedirectURI ,
267+ callbackError : "server_error" ,
268+ state : req .State ,
269+ })
201270 return
202271 }
203272
@@ -478,20 +547,20 @@ func (controller *OIDCController) Userinfo(c *gin.Context) {
478547 c .JSON (200 , controller .oidc .CompileUserinfo (user , entry .Scope ))
479548}
480549
481- func (controller * OIDCController ) authorizeError (c * gin.Context , err error , reason string , reasonUser string , callback string , callbackError string , state string ) {
482- controller .log .App .Warn ().Err (err ).Str ("reason" , reason ).Msg ("Authorization error" )
550+ func (controller * OIDCController ) authorizeError (c * gin.Context , params authorizeErrorParams ) {
551+ controller .log .App .Error ().Err (params . err ).Str ("reason" , params . reason ).Msg ("Authorization error" )
483552
484- if callback != "" {
553+ if params . callback != "" {
485554 errorQueries := CallbackError {
486- Error : callbackError ,
555+ Error : params . callbackError ,
487556 }
488557
489- if reasonUser != "" {
490- errorQueries .ErrorDescription = reasonUser
558+ if params . reasonPublic != "" {
559+ errorQueries .ErrorDescription = params . reasonPublic
491560 }
492561
493- if state != "" {
494- errorQueries .State = state
562+ if params . state != "" {
563+ errorQueries .State = params . state
495564 }
496565
497566 queries , err := query .Values (errorQueries )
@@ -503,13 +572,13 @@ func (controller *OIDCController) authorizeError(c *gin.Context, err error, reas
503572
504573 c .JSON (200 , gin.H {
505574 "status" : 200 ,
506- "redirect_uri" : fmt .Sprintf ("%s?%s" , callback , queries .Encode ()),
575+ "redirect_uri" : fmt .Sprintf ("%s?%s" , params . callback , queries .Encode ()),
507576 })
508577 return
509578 }
510579
511580 errorQueries := ErrorScreen {
512- Error : reasonUser ,
581+ Error : params . reasonPublic ,
513582 }
514583
515584 queries , err := query .Values (errorQueries )
0 commit comments