-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
provisioning.go
152 lines (137 loc) · 5.46 KB
/
provisioning.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// mautrix-meta - A Matrix-Facebook Messenger and Instagram DM 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 distributed 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 (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"strings"
"github.com/beeper/libserv/pkg/requestlog"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-meta/database"
"go.mau.fi/mautrix-meta/messagix"
"go.mau.fi/mautrix-meta/messagix/cookies"
)
type provisioningContextKey int
const (
provisioningUserKey provisioningContextKey = iota
)
type ProvisioningAPI struct {
bridge *MetaBridge
log zerolog.Logger
}
func (prov *ProvisioningAPI) Init() {
prov.log.Debug().Str("prefix", prov.bridge.Config.Bridge.Provisioning.Prefix).Msg("Enabling provisioning API")
r := prov.bridge.AS.Router.PathPrefix(prov.bridge.Config.Bridge.Provisioning.Prefix).Subrouter()
r.Use(hlog.NewHandler(prov.log))
r.Use(requestlog.AccessLogger(true))
r.Use(prov.AuthMiddleware)
r.HandleFunc("/v1/login", prov.Login).Methods(http.MethodPost)
r.HandleFunc("/v1/logout", prov.Logout).Methods(http.MethodPost)
if prov.bridge.Config.Bridge.Provisioning.DebugEndpoints {
prov.log.Debug().Msg("Enabling debug API at /debug")
r := prov.bridge.AS.Router.PathPrefix("/debug").Subrouter()
r.Use(prov.AuthMiddleware)
r.PathPrefix("/pprof").Handler(http.DefaultServeMux)
}
}
func jsonResponse(w http.ResponseWriter, status int, response any) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(response)
}
func (prov *ProvisioningAPI) AuthMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
if auth != prov.bridge.Config.Bridge.Provisioning.SharedSecret {
zerolog.Ctx(r.Context()).Warn().Msg("Authentication token does not match shared secret")
jsonResponse(w, http.StatusForbidden, &mautrix.RespError{
Err: "Authentication token does not match shared secret",
ErrCode: mautrix.MForbidden.ErrCode,
})
return
}
userID := r.URL.Query().Get("user_id")
user := prov.bridge.GetUserByMXID(id.UserID(userID))
h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), provisioningUserKey, user)))
})
}
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"`
}
func (prov *ProvisioningAPI) Login(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value(provisioningUserKey).(*User)
log := prov.log.With().
Str("action", "login").
Str("user_id", user.MXID.String()).
Logger()
ctx := log.WithContext(r.Context())
var newCookies cookies.Cookies
newCookies.Platform = database.MessagixPlatform
err := json.NewDecoder(r.Body).Decode(&newCookies)
if err != nil {
jsonResponse(w, http.StatusBadRequest, Error{ErrCode: mautrix.MBadJSON.ErrCode, Error: err.Error()})
return
}
missingRequiredCookies := newCookies.GetMissingCookieNames()
if len(missingRequiredCookies) > 0 {
log.Debug().Any("missing_cookies", missingRequiredCookies).Msg("Missing cookies in login request")
jsonResponse(w, http.StatusBadRequest, Error{ErrCode: mautrix.MBadJSON.ErrCode, Error: fmt.Sprintf("Missing cookies: %v", missingRequiredCookies)})
return
}
err = user.Login(ctx, &newCookies)
if err != nil {
log.Err(err).Msg("Failed to log in")
if errors.Is(err, messagix.ErrChallengeRequired) {
jsonResponse(w, http.StatusBadRequest, Error{ErrCode: "FI.MAU.META_CHALLENGE_ERROR", Error: "Challenge required, please check the Instagram website and then try again"})
} else if errors.Is(err, messagix.ErrConsentRequired) {
if prov.bridge.Config.Meta.Mode.IsMessenger() {
jsonResponse(w, http.StatusBadRequest, Error{ErrCode: "FI.MAU.META_CONSENT_ERROR", Error: "Consent required, please check the Facebook website and then try again"})
} else {
jsonResponse(w, http.StatusBadRequest, Error{ErrCode: "FI.MAU.META_CONSENT_ERROR", Error: "Consent required, please check the Instagram website and then try again"})
}
} else if errors.Is(err, messagix.ErrTokenInvalidated) {
jsonResponse(w, http.StatusBadRequest, Error{ErrCode: "FI.MAU.META_TOKEN_ERROR", Error: "Got logged out immediately"})
} else {
jsonResponse(w, http.StatusInternalServerError, Error{ErrCode: "M_UNKNOWN", Error: "Internal error logging in"})
}
} else {
jsonResponse(w, http.StatusOK, Response{
Success: true,
Status: "logged_in",
})
}
}
func (prov *ProvisioningAPI) Logout(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value(provisioningUserKey).(*User)
user.DeleteSession()
jsonResponse(w, http.StatusOK, Response{
Success: true,
Status: "logged_out",
})
}