Add proper error codes in google login provisioning API

This commit is contained in:
Tulir Asokan 2024-02-26 15:30:57 +02:00
parent 207f152c04
commit 8eed8382cb
2 changed files with 43 additions and 12 deletions

View file

@ -33,6 +33,7 @@ import (
"maunium.net/go/mautrix/bridge/status" "maunium.net/go/mautrix/bridge/status"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-gmessages/libgm"
"go.mau.fi/mautrix-gmessages/libgm/gmproto" "go.mau.fi/mautrix-gmessages/libgm/gmproto"
) )
@ -340,11 +341,18 @@ func (prov *ProvisioningAPI) GoogleLoginStart(w http.ResponseWriter, r *http.Req
emoji, err := user.AsyncLoginGoogleStart(req.Cookies) emoji, err := user.AsyncLoginGoogleStart(req.Cookies)
if err != nil { if err != nil {
log.Err(err).Msg("Failed to start login") log.Err(err).Msg("Failed to start login")
// TODO proper error codes switch {
case errors.Is(err, libgm.ErrNoDevicesFound):
jsonResponse(w, http.StatusBadRequest, Error{
Error: err.Error(),
ErrCode: "no-devices-found",
})
default:
jsonResponse(w, http.StatusInternalServerError, Error{ jsonResponse(w, http.StatusInternalServerError, Error{
Error: "Failed to start login", Error: "Failed to start login",
ErrCode: "start login fail", ErrCode: "unknown",
}) })
}
return return
} }
jsonResponse(w, http.StatusOK, &RespGoogleLoginStart{Status: "emoji", Emoji: emoji}) jsonResponse(w, http.StatusOK, &RespGoogleLoginStart{Status: "emoji", Emoji: emoji})
@ -358,12 +366,34 @@ func (prov *ProvisioningAPI) GoogleLoginWait(w http.ResponseWriter, r *http.Requ
err := user.AsyncLoginGoogleWait() err := user.AsyncLoginGoogleWait()
if err != nil { if err != nil {
log.Err(err).Msg("Failed to start login") log.Err(err).Msg("Failed to wait for google login")
// TODO proper error codes switch {
case errors.Is(err, ErrNoLoginInProgress):
jsonResponse(w, http.StatusBadRequest, Error{
Error: "No login in progress",
ErrCode: "login-not-in-progress",
})
case errors.Is(err, libgm.ErrIncorrectEmoji):
jsonResponse(w, http.StatusBadRequest, Error{
Error: err.Error(),
ErrCode: "incorrect-emoji",
})
case errors.Is(err, libgm.ErrPairingCancelled):
jsonResponse(w, http.StatusBadRequest, Error{
Error: err.Error(),
ErrCode: "pairing-cancelled",
})
case errors.Is(err, libgm.ErrPairingTimeout):
jsonResponse(w, http.StatusBadRequest, Error{
Error: err.Error(),
ErrCode: "timeout",
})
default:
jsonResponse(w, http.StatusInternalServerError, Error{ jsonResponse(w, http.StatusInternalServerError, Error{
Error: "Failed to finish login", Error: "Failed to finish login",
ErrCode: "finish login fail", ErrCode: "unknown",
}) })
}
return return
} }
jsonResponse(w, http.StatusOK, LoginResponse{Status: "success"}) jsonResponse(w, http.StatusOK, LoginResponse{Status: "success"})

View file

@ -377,6 +377,7 @@ func (user *User) SetManagementRoom(roomID id.RoomID) {
var ErrAlreadyLoggedIn = errors.New("already logged in") var ErrAlreadyLoggedIn = errors.New("already logged in")
var ErrLoginInProgress = errors.New("login already in progress") var ErrLoginInProgress = errors.New("login already in progress")
var ErrNoLoginInProgress = errors.New("no login in progress")
var ErrLoginTimeout = errors.New("login timed out") var ErrLoginTimeout = errors.New("login timed out")
func (user *User) createClient(sess *libgm.AuthData) { func (user *User) createClient(sess *libgm.AuthData) {
@ -507,7 +508,7 @@ func (user *User) AsyncLoginGoogleStart(cookies map[string]string) (outEmoji str
func (user *User) AsyncLoginGoogleWait() error { func (user *User) AsyncLoginGoogleWait() error {
ch := user.googleAsyncPairErrChan.Swap(nil) ch := user.googleAsyncPairErrChan.Swap(nil)
if ch == nil { if ch == nil {
return fmt.Errorf("no login in progress") return ErrNoLoginInProgress
} }
return <-*ch return <-*ch
} }