Clean up provisioning API error codes
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
log "maunium.net/go/maulogger/v2"
|
log "maunium.net/go/maulogger/v2"
|
||||||
|
|
||||||
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
@@ -24,6 +25,21 @@ const (
|
|||||||
SecWebSocketProtocol = "com.gitlab.beeper.discord"
|
SecWebSocketProtocol = "com.gitlab.beeper.discord"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ErrCodeNotConnected = "FI.MAU.DISCORD.NOT_CONNECTED"
|
||||||
|
ErrCodeAlreadyLoggedIn = "FI.MAU.DISCORD.ALREADY_LOGGED_IN"
|
||||||
|
ErrCodeAlreadyConnected = "FI.MAU.DISCORD.ALREADY_CONNECTED"
|
||||||
|
ErrCodeConnectFailed = "FI.MAU.DISCORD.CONNECT_FAILED"
|
||||||
|
ErrCodeDisconnectFailed = "FI.MAU.DISCORD.DISCONNECT_FAILED"
|
||||||
|
ErrCodeGuildBridgeFailed = "M_UNKNOWN"
|
||||||
|
ErrCodeGuildUnbridgeFailed = "M_UNKNOWN"
|
||||||
|
ErrCodeGuildNotBridged = "FI.MAU.DISCORD.GUILD_NOT_BRIDGED"
|
||||||
|
ErrCodeLoginPrepareFailed = "FI.MAU.DISCORD.LOGIN_PREPARE_FAILED"
|
||||||
|
ErrCodeLoginConnectionFailed = "FI.MAU.DISCORD.LOGIN_CONN_FAILED"
|
||||||
|
ErrCodeLoginFailed = "FI.MAU.DISCORD.LOGIN_FAILED"
|
||||||
|
ErrCodePostLoginConnFailed = "FI.MAU.DISCORD.POST_LOGIN_CONNECTION_FAILED"
|
||||||
|
)
|
||||||
|
|
||||||
type ProvisioningAPI struct {
|
type ProvisioningAPI struct {
|
||||||
bridge *DiscordBridge
|
bridge *DiscordBridge
|
||||||
log log.Logger
|
log log.Logger
|
||||||
@@ -117,9 +133,9 @@ func (p *ProvisioningAPI) authMiddleware(h http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if auth != p.bridge.Config.Bridge.Provisioning.SharedSecret {
|
if auth != p.bridge.Config.Bridge.Provisioning.SharedSecret {
|
||||||
jsonResponse(w, http.StatusForbidden, map[string]interface{}{
|
jsonResponse(w, http.StatusUnauthorized, map[string]interface{}{
|
||||||
"error": "Invalid auth token",
|
"error": "Invalid auth token",
|
||||||
"errcode": "M_FORBIDDEN",
|
"errcode": mautrix.MUnknownToken.ErrCode,
|
||||||
})
|
})
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -152,16 +168,16 @@ func (p *ProvisioningAPI) disconnect(w http.ResponseWriter, r *http.Request) {
|
|||||||
if !user.Connected() {
|
if !user.Connected() {
|
||||||
jsonResponse(w, http.StatusConflict, Error{
|
jsonResponse(w, http.StatusConflict, Error{
|
||||||
Error: "You're not connected to discord",
|
Error: "You're not connected to discord",
|
||||||
ErrCode: "not connected",
|
ErrCode: ErrCodeNotConnected,
|
||||||
})
|
})
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := user.Disconnect(); err != nil {
|
if err := user.Disconnect(); err != nil {
|
||||||
|
p.log.Errorfln("Failed to disconnect %s: %v", user.MXID, err)
|
||||||
jsonResponse(w, http.StatusInternalServerError, Error{
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
||||||
Error: "Failed to disconnect from discord",
|
Error: "Failed to disconnect from discord",
|
||||||
ErrCode: "failed to disconnect",
|
ErrCode: ErrCodeDisconnectFailed,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
jsonResponse(w, http.StatusOK, Response{
|
jsonResponse(w, http.StatusOK, Response{
|
||||||
@@ -253,7 +269,7 @@ func (p *ProvisioningAPI) qrLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
if user.IsLoggedIn() {
|
if user.IsLoggedIn() {
|
||||||
_ = c.WriteJSON(Error{
|
_ = c.WriteJSON(Error{
|
||||||
Error: "You're already logged into Discord",
|
Error: "You're already logged into Discord",
|
||||||
ErrCode: "already logged in",
|
ErrCode: ErrCodeAlreadyLoggedIn,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -263,7 +279,7 @@ func (p *ProvisioningAPI) qrLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Errorln("Failed to prepare login:", err)
|
log.Errorln("Failed to prepare login:", err)
|
||||||
_ = c.WriteJSON(Error{
|
_ = c.WriteJSON(Error{
|
||||||
Error: "Failed to prepare login",
|
Error: "Failed to prepare login",
|
||||||
ErrCode: "connection error",
|
ErrCode: ErrCodeLoginPrepareFailed,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -279,8 +295,8 @@ func (p *ProvisioningAPI) qrLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
close(qrChan)
|
close(qrChan)
|
||||||
close(doneChan)
|
close(doneChan)
|
||||||
_ = c.WriteJSON(Error{
|
_ = c.WriteJSON(Error{
|
||||||
Error: "Failed to prepare login",
|
Error: "Failed to connect to Discord login websocket",
|
||||||
ErrCode: "connection error",
|
ErrCode: ErrCodeLoginConnectionFailed,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -305,7 +321,7 @@ func (p *ProvisioningAPI) qrLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Errorln("Discord login websocket returned error:", err)
|
log.Errorln("Discord login websocket returned error:", err)
|
||||||
_ = c.WriteJSON(Error{
|
_ = c.WriteJSON(Error{
|
||||||
Error: "Failed to log in",
|
Error: "Failed to log in",
|
||||||
ErrCode: "login fail",
|
ErrCode: ErrCodeLoginFailed,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -318,7 +334,7 @@ func (p *ProvisioningAPI) qrLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Errorln("Failed to connect after logging in:", err)
|
log.Errorln("Failed to connect after logging in:", err)
|
||||||
_ = c.WriteJSON(Error{
|
_ = c.WriteJSON(Error{
|
||||||
Error: "Failed to connect to Discord after logging in",
|
Error: "Failed to connect to Discord after logging in",
|
||||||
ErrCode: "connect fail",
|
ErrCode: ErrCodePostLoginConnFailed,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -357,7 +373,7 @@ func (p *ProvisioningAPI) tokenLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
if user.IsLoggedIn() {
|
if user.IsLoggedIn() {
|
||||||
jsonResponse(w, http.StatusConflict, Error{
|
jsonResponse(w, http.StatusConflict, Error{
|
||||||
Error: "You're already logged into Discord",
|
Error: "You're already logged into Discord",
|
||||||
ErrCode: "already logged in",
|
ErrCode: ErrCodeAlreadyLoggedIn,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -366,7 +382,7 @@ func (p *ProvisioningAPI) tokenLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Errorln("Failed to parse login request:", err)
|
log.Errorln("Failed to parse login request:", err)
|
||||||
jsonResponse(w, http.StatusBadRequest, Error{
|
jsonResponse(w, http.StatusBadRequest, Error{
|
||||||
Error: "Failed to parse request body",
|
Error: "Failed to parse request body",
|
||||||
ErrCode: "bad request",
|
ErrCode: mautrix.MBadJSON.ErrCode,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -374,7 +390,7 @@ func (p *ProvisioningAPI) tokenLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Errorln("Failed to connect with provided token:", err)
|
log.Errorln("Failed to connect with provided token:", err)
|
||||||
jsonResponse(w, http.StatusUnauthorized, Error{
|
jsonResponse(w, http.StatusUnauthorized, Error{
|
||||||
Error: "Failed to connect to Discord",
|
Error: "Failed to connect to Discord",
|
||||||
ErrCode: "connect fail",
|
ErrCode: ErrCodePostLoginConnFailed,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -393,7 +409,7 @@ func (p *ProvisioningAPI) reconnect(w http.ResponseWriter, r *http.Request) {
|
|||||||
if user.Connected() {
|
if user.Connected() {
|
||||||
jsonResponse(w, http.StatusConflict, Error{
|
jsonResponse(w, http.StatusConflict, Error{
|
||||||
Error: "You're already connected to discord",
|
Error: "You're already connected to discord",
|
||||||
ErrCode: "already connected",
|
ErrCode: ErrCodeAlreadyConnected,
|
||||||
})
|
})
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -402,7 +418,7 @@ func (p *ProvisioningAPI) reconnect(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err := user.Connect(); err != nil {
|
if err := user.Connect(); err != nil {
|
||||||
jsonResponse(w, http.StatusInternalServerError, Error{
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
||||||
Error: "Failed to connect to discord",
|
Error: "Failed to connect to discord",
|
||||||
ErrCode: "failed to connect",
|
ErrCode: ErrCodeConnectFailed,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
jsonResponse(w, http.StatusOK, Response{
|
jsonResponse(w, http.StatusOK, Response{
|
||||||
@@ -464,7 +480,7 @@ func (p *ProvisioningAPI) guildsBridge(w http.ResponseWriter, r *http.Request) {
|
|||||||
p.log.Errorln("Failed to parse bridge request:", err)
|
p.log.Errorln("Failed to parse bridge request:", err)
|
||||||
jsonResponse(w, http.StatusBadRequest, Error{
|
jsonResponse(w, http.StatusBadRequest, Error{
|
||||||
Error: "Failed to parse request body",
|
Error: "Failed to parse request body",
|
||||||
ErrCode: "bad request",
|
ErrCode: mautrix.MBadJSON.ErrCode,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -473,7 +489,7 @@ func (p *ProvisioningAPI) guildsBridge(w http.ResponseWriter, r *http.Request) {
|
|||||||
if guild == nil {
|
if guild == nil {
|
||||||
jsonResponse(w, http.StatusNotFound, Error{
|
jsonResponse(w, http.StatusNotFound, Error{
|
||||||
Error: "Guild not found",
|
Error: "Guild not found",
|
||||||
ErrCode: "M_NOT_FOUND",
|
ErrCode: mautrix.MNotFound.ErrCode,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -482,7 +498,7 @@ func (p *ProvisioningAPI) guildsBridge(w http.ResponseWriter, r *http.Request) {
|
|||||||
p.log.Errorfln("Error bridging %s: %v", guildID, err)
|
p.log.Errorfln("Error bridging %s: %v", guildID, err)
|
||||||
jsonResponse(w, http.StatusInternalServerError, Error{
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
||||||
Error: "Internal error while trying to bridge guild",
|
Error: "Internal error while trying to bridge guild",
|
||||||
ErrCode: "guild bridge failed",
|
ErrCode: ErrCodeGuildBridgeFailed,
|
||||||
})
|
})
|
||||||
} else if alreadyExists {
|
} else if alreadyExists {
|
||||||
jsonResponse(w, http.StatusOK, respBridgeGuild{
|
jsonResponse(w, http.StatusOK, respBridgeGuild{
|
||||||
@@ -503,23 +519,23 @@ func (p *ProvisioningAPI) guildsUnbridge(w http.ResponseWriter, r *http.Request)
|
|||||||
if user.PermissionLevel < bridgeconfig.PermissionLevelAdmin {
|
if user.PermissionLevel < bridgeconfig.PermissionLevelAdmin {
|
||||||
jsonResponse(w, http.StatusForbidden, Error{
|
jsonResponse(w, http.StatusForbidden, Error{
|
||||||
Error: "Only bridge admins can unbridge guilds",
|
Error: "Only bridge admins can unbridge guilds",
|
||||||
ErrCode: "M_FORBIDDEN",
|
ErrCode: mautrix.MForbidden.ErrCode,
|
||||||
})
|
})
|
||||||
} else if guild := user.bridge.GetGuildByID(guildID, false); guild == nil {
|
} else if guild := user.bridge.GetGuildByID(guildID, false); guild == nil {
|
||||||
jsonResponse(w, http.StatusNotFound, Error{
|
jsonResponse(w, http.StatusNotFound, Error{
|
||||||
Error: "Guild not found",
|
Error: "Guild not found",
|
||||||
ErrCode: "M_NOT_FOUND",
|
ErrCode: mautrix.MNotFound.ErrCode,
|
||||||
})
|
})
|
||||||
} else if !guild.AutoBridgeChannels && guild.MXID == "" {
|
} else if !guild.AutoBridgeChannels && guild.MXID == "" {
|
||||||
jsonResponse(w, http.StatusNotFound, Error{
|
jsonResponse(w, http.StatusNotFound, Error{
|
||||||
Error: "That guild is not bridged",
|
Error: "That guild is not bridged",
|
||||||
ErrCode: "not bridged",
|
ErrCode: ErrCodeGuildNotBridged,
|
||||||
})
|
})
|
||||||
} else if err := user.unbridgeGuild(guildID); err != nil {
|
} else if err := user.unbridgeGuild(guildID); err != nil {
|
||||||
p.log.Errorfln("Error unbridging %s: %v", guildID, err)
|
p.log.Errorfln("Error unbridging %s: %v", guildID, err)
|
||||||
jsonResponse(w, http.StatusInternalServerError, Error{
|
jsonResponse(w, http.StatusInternalServerError, Error{
|
||||||
Error: "Internal error while trying to unbridge guild",
|
Error: "Internal error while trying to unbridge guild",
|
||||||
ErrCode: "guild unbridge failed",
|
ErrCode: ErrCodeGuildUnbridgeFailed,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
|||||||
Reference in New Issue
Block a user