Skip to content

Commit 8ade1a6

Browse files
author
Christian Roessner
committed
Feat: Add Accept header handling for account list and method check in auth
Enhance REST API to handle different Accept headers for listing accounts, supporting JSON and form-encoded responses. Also, add a method check in the authentication setup to process only POST requests, ensuring correct handling of content types. Signed-off-by: Christian Roessner <[email protected]>
1 parent a99c1c6 commit 8ade1a6

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

server/core/auth.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -2149,15 +2149,17 @@ func setAuthenticationFields(auth *AuthState, request *JSONRequest) {
21492149
// If neither of the above conditions match, it sets the error associated with unsupported media type
21502150
// and sets the error type to gin.ErrorTypeBind on the Context.
21512151
func setupBodyBasedAuth(ctx *gin.Context, auth *AuthState) {
2152-
contentType := ctx.GetHeader("Content-Type")
2152+
if ctx.Request.Method == "POST" {
2153+
contentType := ctx.GetHeader("Content-Type")
21532154

2154-
if strings.HasPrefix(contentType, "application/x-www-form-urlencoded") {
2155-
processApplicationXWWWFormUrlencoded(ctx, auth)
2156-
} else if contentType == "application/json" {
2157-
processApplicationJSON(ctx, auth)
2158-
} else {
2159-
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unsupported media type"})
2160-
ctx.Error(errors.ErrUnsupportedMediaType).SetType(gin.ErrorTypeBind)
2155+
if strings.HasPrefix(contentType, "application/x-www-form-urlencoded") {
2156+
processApplicationXWWWFormUrlencoded(ctx, auth)
2157+
} else if contentType == "application/json" {
2158+
processApplicationJSON(ctx, auth)
2159+
} else {
2160+
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unsupported media type"})
2161+
ctx.Error(errors.ErrUnsupportedMediaType).SetType(gin.ErrorTypeBind)
2162+
}
21612163
}
21622164
}
21632165

server/core/rest.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,22 @@ func (a *AuthState) generic(ctx *gin.Context) {
131131
if a.ListAccounts {
132132
allAccountsList := a.listUserAccounts()
133133

134-
for _, account := range allAccountsList {
135-
ctx.Data(http.StatusOK, "text/plain", []byte(account+"\r\n"))
134+
acceptHeader := ctx.GetHeader("Accept")
135+
136+
switch acceptHeader {
137+
case "application/json":
138+
ctx.JSON(http.StatusOK, allAccountsList)
139+
case "*/*", "text/plain":
140+
for _, account := range allAccountsList {
141+
ctx.Data(http.StatusOK, "text/plain", []byte(account+"\r\n"))
142+
}
143+
case "application/x-www-form-urlencoded":
144+
for _, account := range allAccountsList {
145+
ctx.Data(http.StatusOK, "application/x-www-form-urlencoded", []byte(account+"\r\n"))
146+
}
147+
default:
148+
ctx.Error(errors.ErrUnsupportedMediaType).SetType(gin.ErrorTypeBind)
149+
ctx.AbortWithStatus(http.StatusUnsupportedMediaType)
136150
}
137151

138152
level.Info(log.Logger).Log(global.LogKeyGUID, a.GUID, global.LogKeyMode, mode)

0 commit comments

Comments
 (0)