-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2: add backwards-compatible resolve identifier provisioning API
- Loading branch information
Showing
7 changed files
with
280 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// mautrix-signal - A Matrix-Signal puppeting bridge. | ||
// Copyright (C) 2024 Tulir Asokan | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is istributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/rs/zerolog" | ||
|
||
"go.mau.fi/mautrix-signal/legacyprovision" | ||
"maunium.net/go/mautrix" | ||
"maunium.net/go/mautrix/bridgev2" | ||
) | ||
|
||
func legacyProvLinkNew(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} | ||
|
||
func legacyProvLinkWaitScan(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} | ||
|
||
func legacyProvLinkWaitAccount(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} | ||
|
||
func legacyProvLogout(w http.ResponseWriter, r *http.Request) { | ||
user := m.Matrix.Provisioning.GetUser(r) | ||
for { | ||
login := user.GetDefaultLogin() | ||
if login == nil { | ||
break | ||
} | ||
login.Logout(r.Context()) | ||
} | ||
legacyprovision.JSONResponse(w, http.StatusOK, nil) | ||
} | ||
|
||
func legacyResolveIdentifierOrStartChat(w http.ResponseWriter, r *http.Request, create bool) { | ||
login := m.Matrix.Provisioning.GetLoginForRequest(w, r) | ||
if login == nil { | ||
return | ||
} | ||
api := login.Client.(bridgev2.IdentifierResolvingNetworkAPI) | ||
resp, err := api.ResolveIdentifier(r.Context(), mux.Vars(r)["phonenum"], create) | ||
if err != nil { | ||
zerolog.Ctx(r.Context()).Err(err).Msg("Failed to resolve identifier") | ||
legacyprovision.JSONResponse(w, http.StatusInternalServerError, &legacyprovision.Error{ | ||
Error: fmt.Sprintf("Failed to resolve identifier: %v", err), | ||
ErrCode: "M_UNKNOWN", | ||
}) | ||
return | ||
} else if resp == nil { | ||
legacyprovision.JSONResponse(w, http.StatusNotFound, &legacyprovision.Error{ | ||
ErrCode: mautrix.MNotFound.ErrCode, | ||
Error: "User not found on Signal", | ||
}) | ||
return | ||
} | ||
status := http.StatusOK | ||
apiResp := &legacyprovision.ResolveIdentifierResponse{ | ||
ChatID: legacyprovision.ResolveIdentifierResponseChatID{ | ||
UUID: string(resp.UserID), | ||
Number: "", | ||
}, | ||
} | ||
if resp.Ghost != nil { | ||
if resp.UserInfo != nil { | ||
resp.Ghost.UpdateInfo(r.Context(), resp.UserInfo) | ||
} | ||
apiResp.OtherUser = &legacyprovision.ResolveIdentifierResponseOtherUser{ | ||
MXID: resp.Ghost.Intent.GetMXID(), | ||
DisplayName: resp.Ghost.Name, | ||
AvatarURL: resp.Ghost.AvatarMXC.ParseOrIgnore(), | ||
} | ||
} | ||
if resp.Chat != nil { | ||
if resp.Chat.Portal == nil { | ||
resp.Chat.Portal, err = m.Bridge.GetPortalByID(r.Context(), resp.Chat.PortalID) | ||
if err != nil { | ||
zerolog.Ctx(r.Context()).Err(err).Msg("Failed to get portal") | ||
legacyprovision.JSONResponse(w, http.StatusInternalServerError, &mautrix.RespError{ | ||
Err: "Failed to get portal", | ||
ErrCode: "M_UNKNOWN", | ||
}) | ||
return | ||
} | ||
} | ||
if create && resp.Chat.Portal.MXID == "" { | ||
apiResp.JustCreated = true | ||
status = http.StatusCreated | ||
err = resp.Chat.Portal.CreateMatrixRoom(r.Context(), login, resp.Chat.PortalInfo) | ||
if err != nil { | ||
zerolog.Ctx(r.Context()).Err(err).Msg("Failed to create portal room") | ||
legacyprovision.JSONResponse(w, http.StatusInternalServerError, &mautrix.RespError{ | ||
Err: "Failed to create portal room", | ||
ErrCode: "M_UNKNOWN", | ||
}) | ||
return | ||
} | ||
} | ||
apiResp.RoomID = resp.Chat.Portal.MXID | ||
} | ||
legacyprovision.JSONResponse(w, status, &legacyprovision.Response{ | ||
Success: true, | ||
Status: "ok", | ||
ResolveIdentifierResponse: apiResp, | ||
}) | ||
} | ||
|
||
func legacyProvResolveIdentifier(w http.ResponseWriter, r *http.Request) { | ||
legacyResolveIdentifierOrStartChat(w, r, false) | ||
} | ||
|
||
func legacyProvPM(w http.ResponseWriter, r *http.Request) { | ||
legacyResolveIdentifierOrStartChat(w, r, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// mautrix-signal - A Matrix-Signal puppeting bridge. | ||
// Copyright (C) 2024 Tulir Asokan | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is istributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package legacyprovision | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"maunium.net/go/mautrix/id" | ||
) | ||
|
||
func JSONResponse(w http.ResponseWriter, status int, response any) { | ||
w.Header().Add("Content-Type", "application/json") | ||
w.WriteHeader(status) | ||
_ = json.NewEncoder(w).Encode(response) | ||
} | ||
|
||
type Error struct { | ||
Success bool `json:"success"` | ||
Error string `json:"error"` | ||
ErrCode string `json:"errcode"` | ||
} | ||
|
||
type Response struct { | ||
Success bool `json:"success"` | ||
Status string `json:"status"` | ||
|
||
// For response in LinkNew | ||
SessionID string `json:"session_id,omitempty"` | ||
URI string `json:"uri,omitempty"` | ||
|
||
// For response in LinkWaitForAccount | ||
UUID string `json:"uuid,omitempty"` | ||
Number string `json:"number,omitempty"` | ||
|
||
// For response in ResolveIdentifier | ||
*ResolveIdentifierResponse | ||
} | ||
|
||
type WhoAmIResponse struct { | ||
Permissions int `json:"permissions"` | ||
MXID string `json:"mxid"` | ||
Signal *WhoAmIResponseSignal `json:"signal,omitempty"` | ||
} | ||
|
||
type WhoAmIResponseSignal struct { | ||
Number string `json:"number"` | ||
UUID string `json:"uuid"` | ||
Name string `json:"name"` | ||
Ok bool `json:"ok"` | ||
} | ||
|
||
type ResolveIdentifierResponse struct { | ||
RoomID id.RoomID `json:"room_id"` | ||
ChatID ResolveIdentifierResponseChatID `json:"chat_id"` | ||
JustCreated bool `json:"just_created"` | ||
OtherUser *ResolveIdentifierResponseOtherUser `json:"other_user,omitempty"` | ||
} | ||
|
||
type ResolveIdentifierResponseChatID struct { | ||
UUID string `json:"uuid"` | ||
Number string `json:"number"` | ||
} | ||
|
||
type ResolveIdentifierResponseOtherUser struct { | ||
MXID id.UserID `json:"mxid"` | ||
DisplayName string `json:"displayname"` | ||
AvatarURL id.ContentURI `json:"avatar_url"` | ||
} | ||
|
||
type LinkWaitForScanRequest struct { | ||
SessionID string `json:"session_id"` | ||
} | ||
|
||
type LinkWaitForAccountRequest struct { | ||
SessionID string `json:"session_id"` | ||
DeviceName string `json:"device_name"` // TODO this seems to not be used anywhere | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.