Skip to content

Commit 1267739

Browse files
committed
Merge branch 'master' of github.com:nezhahq/nezha
2 parents 828588e + 3216ad4 commit 1267739

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

cmd/dashboard/controller/controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ func (we *wsError) Error() string {
198198
return fmt.Sprintf(we.msg, we.a...)
199199
}
200200

201+
var errNoop = errors.New("wrote")
202+
201203
func commonHandler[T any](handler handlerFunc[T]) func(*gin.Context) {
202204
return func(c *gin.Context) {
203205
handle(c, handler)
@@ -240,7 +242,9 @@ func handle[T any](c *gin.Context, handler handlerFunc[T]) {
240242
}
241243
return
242244
default:
243-
c.JSON(http.StatusOK, newErrorResponse(err))
245+
if !errors.Is(err, errNoop) {
246+
c.JSON(http.StatusOK, newErrorResponse(err))
247+
}
244248
return
245249
}
246250
}

cmd/dashboard/controller/jwt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func authenticator() func(c *gin.Context) (interface{}, error) {
9090
var user model.User
9191
realip := c.GetString(model.CtxKeyRealIPStr)
9292

93-
if err := singleton.DB.Select("id", "password").Where("username = ?", loginVals.Username).First(&user).Error; err != nil {
93+
if err := singleton.DB.Select("id", "password", "reject_password").Where("username = ?", loginVals.Username).First(&user).Error; err != nil {
9494
if err == gorm.ErrRecordNotFound {
9595
model.BlockIP(singleton.DB, realip, model.WAFBlockReasonTypeLoginFail, model.BlockIDUnknownUser)
9696
}

cmd/dashboard/controller/oauth2.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88
"strconv"
99
"strings"
10-
"time"
1110

1211
jwt "github.com/appleboy/gin-jwt/v2"
1312
"github.com/gin-gonic/gin"
@@ -114,10 +113,10 @@ func unbindOauth2(c *gin.Context) (any, error) {
114113
// @Produce json
115114
// @Param state query string true "state"
116115
// @Param code query string true "code"
117-
// @Success 200 {object} model.LoginResponse
116+
// @Success 200 {object} model.CommonResponse[any]
118117
// @Router /api/v1/oauth2/callback [get]
119-
func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*model.LoginResponse, error) {
120-
return func(c *gin.Context) (*model.LoginResponse, error) {
118+
func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (any, error) {
119+
return func(c *gin.Context) (any, error) {
121120
callbackData := &model.Oauth2Callback{
122121
State: c.Query("state"),
123122
Code: c.Query("code"),
@@ -146,6 +145,7 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
146145
}
147146

148147
var bind model.Oauth2Bind
148+
state.Provider = strings.ToLower(state.Provider)
149149
switch state.Action {
150150
case model.RTypeBind:
151151
u, authorized := c.Get(model.CtxKeyAuthorizedUser)
@@ -154,7 +154,7 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
154154
}
155155
user := u.(*model.User)
156156

157-
result := singleton.DB.Where("provider = ? AND open_id = ?", strings.ToLower(state.Provider), openId).Limit(1).Find(&bind)
157+
result := singleton.DB.Where("provider = ? AND open_id = ?", state.Provider, openId).Limit(1).Find(&bind)
158158
if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
159159
return nil, newGormError("%v", result.Error)
160160
}
@@ -171,20 +171,20 @@ func oauth2callback(jwtConfig *jwt.GinJWTMiddleware) func(c *gin.Context) (*mode
171171
return nil, newGormError("%v", result.Error)
172172
}
173173
default:
174-
if err := singleton.DB.Where("provider = ? AND open_id = ?", strings.ToLower(state.Provider), openId).First(&bind).Error; err != nil {
174+
if err := singleton.DB.Where("provider = ? AND open_id = ?", state.Provider, openId).First(&bind).Error; err != nil {
175175
return nil, singleton.Localizer.ErrorT("oauth2 user not binded yet")
176176
}
177177
}
178178

179-
tokenString, expire, err := jwtConfig.TokenGenerator(fmt.Sprintf("%d", bind.UserID))
179+
tokenString, _, err := jwtConfig.TokenGenerator(fmt.Sprintf("%d", bind.UserID))
180180
if err != nil {
181181
return nil, err
182182
}
183183

184184
jwtConfig.SetCookie(c, tokenString)
185185
c.Redirect(http.StatusFound, utils.IfOr(state.Action == model.RTypeBind, "/dashboard/profile?oauth2=true", "/dashboard/login?oauth2=true"))
186186

187-
return &model.LoginResponse{Token: tokenString, Expire: expire.Format(time.RFC3339)}, nil
187+
return nil, errNoop
188188
}
189189
}
190190

cmd/dashboard/controller/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func updateProfile(c *gin.Context) (any, error) {
7474
}
7575

7676
var bindCount int64
77-
if err := singleton.DB.Where("user_id = ?", auth.(*model.User).ID).Count(&bindCount).Error; err != nil {
77+
if err := singleton.DB.Model(&model.Oauth2Bind{}).Where("user_id = ?", auth.(*model.User).ID).Count(&bindCount).Error; err != nil {
7878
return nil, newGormError("%v", err)
7979
}
8080

model/user_api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ type ProfileForm struct {
1010
OriginalPassword string `json:"original_password,omitempty"`
1111
NewUsername string `json:"new_username,omitempty"`
1212
NewPassword string `json:"new_password,omitempty"`
13-
RejectPassword bool `json:"reject_password,omitempty"`
13+
RejectPassword bool `json:"reject_password,omitempty" validate:"optional"`
1414
}

0 commit comments

Comments
 (0)