extend sasl password to include server domain for non-matrix.org accounts. Part of #12

This commit is contained in:
emerson 2021-12-09 17:29:09 -05:00
parent 31a869f04b
commit 05d6973506
No known key found for this signature in database
GPG key ID: 270669502DA603E3
4 changed files with 14 additions and 10 deletions

View file

@ -44,4 +44,4 @@ That said, it is usable for basic chatting.
Copy `config.example.json` to `config.json`, edit the values if needed, then `npm run build` and then `node reflection.js` to start it
## Authentication
Authentication is done via `SASL PLAIN`, the username is your mxid and the password is an access token from another session.
Authentication is done via `SASL PLAIN`, the username is your mxid and the password is an access token from another session plus your server domain, separated by a `:` (so `access_token:matrix.org` if your server is matrix.org). Note this is the domain for `m.server`, not necessarily the homeserver domain.

View file

@ -156,8 +156,12 @@ export class Client {
this.closeConnectionWithError('Invalid authentication')
}
const mxid = authArray[1];
const accessToken = authArray[2];
const thisIRCUser = this.server.getOrCreateIRCUser(mxid, accessToken);
const accessTokenAndServer = authArray[2];
const sepIndex = accessTokenAndServer.indexOf(":");
const accessToken = accessTokenAndServer.slice(0, sepIndex);
const homeserver = accessTokenAndServer.slice(sepIndex+1);
console.log(accessToken, homeserver);
const thisIRCUser = this.server.getOrCreateIRCUser(mxid, accessToken, homeserver);
thisIRCUser.getVerification().then((response) => {
if (response.status === 401 || response.status === 403) {
this.sendMessage(this.server.name, '904', numerics['904']('*'), message.tags)
@ -180,7 +184,7 @@ export class Client {
if (this.user.isSynced()) {
this.user.addClient(this, message.tags);
} else {
axios.get(`https://matrix.org/_matrix/client/v3/sync?access_token=${accessToken}`).then(response => {
axios.get(`https://${this.user.homeserver}/_matrix/client/v3/sync?access_token=${accessToken}`).then(response => {
const data = response.data;
const rooms = data.rooms;
if (this.user === null)

View file

@ -17,7 +17,7 @@ export class IRCUser {
public nextBatch: string
private initialSync: boolean
private syncIntervalID: NodeJS.Timeout;
constructor(public mxid: string, private accessToken: string, private server: Server) {
constructor(public mxid: string, private accessToken: string, public homeserver: string, private server: Server) {
this.clients = new Set();
this.channels = new Set();
const mxidSplit = mxid.split(':')
@ -37,7 +37,7 @@ export class IRCUser {
}
getVerification() {
return axios.get(`https://matrix.org/_matrix/client/v3/account/whoami?access_token=${this.accessToken}`);
return axios.get(`https://${this.homeserver}/_matrix/client/v3/account/whoami?access_token=${this.accessToken}`);
}
getMask(): string {
@ -58,7 +58,7 @@ export class IRCUser {
console.log("not syncing, initial sync not completed");
return;
}
const endpoint = `https://matrix.org/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}`;
const endpoint = `https://${this.homeserver}/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}`;
axios.get(endpoint).then(response => {
const data = response.data;
this.nextBatch = data.next_batch;
@ -94,7 +94,7 @@ export class IRCUser {
}
const newTxnid = randomUUID();
this.txnIdStore.set(newTxnid, client);
axios.put(`https://matrix.org/_matrix/client/v3/rooms/${channel.roomId}/send/m.room.message/${newTxnid}?access_token=${this.accessToken}`, content);
axios.put(`https://${this.homeserver}/_matrix/client/v3/rooms/${channel.roomId}/send/m.room.message/${newTxnid}?access_token=${this.accessToken}`, content);
}
sendToAll(prefix: string, command: string, params: string[], tags: Map<string, string> = new Map(), skipClient: Client|null = null) {

View file

@ -22,12 +22,12 @@ export class Server {
this.nickToMxid = new Map();
}
getOrCreateIRCUser(mxid: string, accessToken: string): IRCUser {
getOrCreateIRCUser(mxid: string, accessToken: string, homeserver: string): IRCUser {
const maybeUser = this.ircUsers.get(mxid);
if (maybeUser) {
return maybeUser;
}
return new IRCUser(mxid, accessToken, this);
return new IRCUser(mxid, accessToken, homeserver, this);
}
getOrCreateIRCChannel(roomId: string): Channel {