Fix NPE in compatHandler

This commit is contained in:
Mike Schwörer 2023-12-01 22:05:36 +01:00
parent 9f3e183d72
commit 35a97be4c4
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 28 additions and 19 deletions

View File

@ -296,12 +296,12 @@ func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
} }
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey) keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if err != nil { if err != nil {
return ginresp.CompatAPIError(0, "Failed to query token") return ginresp.CompatAPIError(0, "Failed to query token")
} }
if keytok == nil {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if !keytok.IsAdmin(user.UserID) { if !keytok.IsAdmin(user.UserID) {
return ginresp.CompatAPIError(204, "Authentification failed") return ginresp.CompatAPIError(204, "Authentification failed")
} }
@ -404,12 +404,12 @@ func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
} }
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey) keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if err != nil { if err != nil {
return ginresp.CompatAPIError(0, "Failed to query token") return ginresp.CompatAPIError(0, "Failed to query token")
} }
if keytok == nil {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if !keytok.IsAdmin(user.UserID) { if !keytok.IsAdmin(user.UserID) {
return ginresp.CompatAPIError(204, "Authentification failed") return ginresp.CompatAPIError(204, "Authentification failed")
} }
@ -506,12 +506,12 @@ func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
} }
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey) keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if err != nil { if err != nil {
return ginresp.CompatAPIError(0, "Failed to query token") return ginresp.CompatAPIError(0, "Failed to query token")
} }
if keytok == nil {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if !keytok.IsAdmin(user.UserID) { if !keytok.IsAdmin(user.UserID) {
return ginresp.CompatAPIError(204, "Authentification failed") return ginresp.CompatAPIError(204, "Authentification failed")
} }
@ -623,12 +623,12 @@ func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
} }
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey) keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if err != nil { if err != nil {
return ginresp.CompatAPIError(0, "Failed to query token") return ginresp.CompatAPIError(0, "Failed to query token")
} }
if keytok == nil {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if !keytok.IsAdmin(user.UserID) { if !keytok.IsAdmin(user.UserID) {
return ginresp.CompatAPIError(204, "Authentification failed") return ginresp.CompatAPIError(204, "Authentification failed")
} }
@ -663,7 +663,7 @@ func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
_, err = h.database.CreateClient(ctx, user.UserID, models.ClientTypeAndroid, *data.FCMToken, "compat", "compat") _, err = h.database.CreateClient(ctx, user.UserID, models.ClientTypeAndroid, *data.FCMToken, "compat", "compat")
if err != nil { if err != nil {
return ginresp.CompatAPIError(0, "Failed to delete client") return ginresp.CompatAPIError(0, "Failed to create client")
} }
} }
@ -753,12 +753,12 @@ func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
} }
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey) keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if err != nil { if err != nil {
return ginresp.CompatAPIError(0, "Failed to query token") return ginresp.CompatAPIError(0, "Failed to query token")
} }
if keytok == nil {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if !keytok.IsAdmin(user.UserID) { if !keytok.IsAdmin(user.UserID) {
return ginresp.CompatAPIError(204, "Authentification failed") return ginresp.CompatAPIError(204, "Authentification failed")
} }
@ -872,12 +872,12 @@ func (h CompatHandler) Upgrade(g *gin.Context) ginresp.HTTPResponse {
} }
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey) keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if err != nil { if err != nil {
return ginresp.CompatAPIError(0, "Failed to query token") return ginresp.CompatAPIError(0, "Failed to query token")
} }
if keytok == nil {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if !keytok.IsAdmin(user.UserID) { if !keytok.IsAdmin(user.UserID) {
return ginresp.CompatAPIError(204, "Authentification failed") return ginresp.CompatAPIError(204, "Authentification failed")
} }

View File

@ -535,6 +535,15 @@ func TestCompatUpdateFCM(t *testing.T) {
s1 := tt.RequestPost[gin.H](t, baseUrl, fmt.Sprintf("/send.php?user_id=%d&user_key=%s&title=%s", userid, newkey, url.QueryEscape("msg_2")), nil) s1 := tt.RequestPost[gin.H](t, baseUrl, fmt.Sprintf("/send.php?user_id=%d&user_key=%s&title=%s", userid, newkey, url.QueryEscape("msg_2")), nil)
tt.AssertEqual(t, "success", true, s1["success"]) tt.AssertEqual(t, "success", true, s1["success"])
tt.AssertEqual(t, "fcm", "NEW_FCM", pusher.Last().Client.FCMToken) tt.AssertEqual(t, "fcm", "NEW_FCM", pusher.Last().Client.FCMToken)
upd2 := tt.RequestGet[gin.H](t, baseUrl, fmt.Sprintf("/api/update.php?user_id=%d&user_key=%s&fcm_token=%s", userid, newkey, "NEW_FCM_2"))
tt.AssertEqual(t, "success", true, upd2["success"])
newkey = upd2["user_key"].(string)
r1 = tt.RequestGet[gin.H](t, baseUrl, fmt.Sprintf("/api/info.php?user_id=%d&user_key=%s", userid, newkey))
tt.AssertEqual(t, "success", true, r1["success"])
tt.AssertEqual(t, "fcm_token_set", true, r1["fcm_token_set"])
} }
func TestCompatUpgrade(t *testing.T) { func TestCompatUpgrade(t *testing.T) {