mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-11-03 15:45:51 +00:00
fix: return json errors when authorization header is present
This commit is contained in:
@@ -127,6 +127,14 @@ func (api *API) SetupRoutes() {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
if c.GetHeader("Authorization") != "" {
|
||||||
|
log.Error().Err(appAllowedErr).Msg("Failed to check if resource is allowed")
|
||||||
|
c.JSON(501, gin.H{
|
||||||
|
"status": 501,
|
||||||
|
"message": "Internal Server Error",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
if api.handleError(c, "Failed to check if resource is allowed", appAllowedErr) {
|
if api.handleError(c, "Failed to check if resource is allowed", appAllowedErr) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -153,6 +161,14 @@ func (api *API) SetupRoutes() {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
if c.GetHeader("Authorization") != "" {
|
||||||
|
log.Error().Err(appAllowedErr).Msg("Failed to build query")
|
||||||
|
c.JSON(501, gin.H{
|
||||||
|
"status": 501,
|
||||||
|
"message": "Internal Server Error",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
if api.handleError(c, "Failed to build query", queryErr) {
|
if api.handleError(c, "Failed to build query", queryErr) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -167,6 +183,13 @@ func (api *API) SetupRoutes() {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
if c.GetHeader("Authorization") != "" {
|
||||||
|
c.JSON(401, gin.H{
|
||||||
|
"status": 401,
|
||||||
|
"message": "Unauthorized",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/unauthorized?%s", api.Config.AppURL, queries.Encode()))
|
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/unauthorized?%s", api.Config.AppURL, queries.Encode()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -187,6 +210,14 @@ func (api *API) SetupRoutes() {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
if c.GetHeader("Authorization") != "" {
|
||||||
|
c.JSON(401, gin.H{
|
||||||
|
"status": 401,
|
||||||
|
"message": "Unauthorized",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
queries, queryErr := query.Values(types.LoginQuery{
|
queries, queryErr := query.Values(types.LoginQuery{
|
||||||
RedirectURI: fmt.Sprintf("%s://%s%s", proto, host, uri),
|
RedirectURI: fmt.Sprintf("%s://%s%s", proto, host, uri),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ type Hooks struct {
|
|||||||
|
|
||||||
func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
|
func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
|
||||||
cookie := hooks.Auth.GetSessionCookie(c)
|
cookie := hooks.Auth.GetSessionCookie(c)
|
||||||
basic := hooks.Auth.GetBasicAuth(c) // TODO: return json instead of redirect when basic auth
|
basic := hooks.Auth.GetBasicAuth(c)
|
||||||
|
|
||||||
if basic.Username != "" {
|
if basic.Username != "" {
|
||||||
log.Debug().Msg("Got basic auth")
|
log.Debug().Msg("Got basic auth")
|
||||||
@@ -33,7 +33,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
|
|||||||
Username: basic.Username,
|
Username: basic.Username,
|
||||||
IsLoggedIn: true,
|
IsLoggedIn: true,
|
||||||
OAuth: false,
|
OAuth: false,
|
||||||
Provider: "",
|
Provider: "basic",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
|
|||||||
Username: cookie.Username,
|
Username: cookie.Username,
|
||||||
IsLoggedIn: true,
|
IsLoggedIn: true,
|
||||||
OAuth: false,
|
OAuth: false,
|
||||||
Provider: "",
|
Provider: "username",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user