Support authentication flows for login/register

This commit is contained in:
N Pex 2023-04-04 14:30:50 +00:00
parent d86ee3b1e3
commit 0d3781f3aa
11 changed files with 481 additions and 139 deletions

View file

@ -104,35 +104,59 @@ export default {
console.log("create crypto store");
return new LocalStorageCryptoStore(this.$store.getters.storage);
},
login(user) {
const tempMatrixClient = sdk.createClient({baseUrl: user.home_server});
login(user, registrationFlowHandler) {
const tempMatrixClient = sdk.createClient({baseUrl: user.home_server, idBaseUrl: this.$config.identityServer});
var promiseLogin;
const self = this;
if (user.access_token) {
// Logged in on "real" account
promiseLogin = Promise.resolve(user);
} else if (user.is_guest && !user.user_id) {
} else if (user.is_guest && (!user.user_id || user.registration_session)) {
// Generate random username and password. We don't user REAL matrix
// guest accounts because 1. They are not allowed to post media, 2. They
// can not use avatars and 3. They can not seamlessly be upgraded to real accounts.
//
// Instead, we use an ILAG approach, Improved Landing as Guest.
const user = util.randomUser(this.$config.userIdPrefix);
const pass = util.randomPass();
const userId = user.registration_session ? user.user_id : util.randomUser(this.$config.userIdPrefix);
const pass = user.registration_session ? user.password : util.randomPass();
const extractAndSaveUser = (response) => {
var u = Object.assign({}, response);
u.home_server = tempMatrixClient.baseUrl; // Don't use deprecated field from response.
u.password = pass;
u.is_guest = true;
this.$store.commit("setUser", u);
return u;
};
promiseLogin = tempMatrixClient
.register(user, pass, null, {
.register(userId, pass, user.registration_session || null, {
type: "m.login.dummy",
initial_device_display_name: this.$config.appName,
})
.then((response) => {
console.log("Response", response);
var u = Object.assign({}, response);
u.home_server = tempMatrixClient.baseUrl; // Don't use deprecated field from response.
u.password = pass;
u.is_guest = true;
this.$store.commit("setUser", u);
return u;
return extractAndSaveUser(response);
})
.catch(error => {
if (registrationFlowHandler && error.httpStatus == 401 && error.data) {
const registrationSession = error.data.session;
// Store user, pass and session, so we can resume if network failure occurs etc.
//
var u = {};
u.user_id = userId;
u.home_server = tempMatrixClient.baseUrl; // Don't use deprecated field from response.
u.password = pass;
u.is_guest = true;
u.registration_session = registrationSession;
this.$store.commit("setUser", u);
return registrationFlowHandler(tempMatrixClient, error.data).then((response) => extractAndSaveUser(response));
} else {
console.error(error);
}
throw error;
});
} else {
var data = {
@ -286,11 +310,11 @@ export default {
* Will use a real account, if we have one, otherwise will create
* a random account.
*/
getLoginPromise() {
getLoginPromise(registrationFlowHandler) {
if (this.ready) {
return Promise.resolve(this.currentUser);
}
return this.$store.dispatch("login", this.currentUser || new User(this.$config.defaultServer, "", "", true));
return this.$store.dispatch("login", { user: this.currentUser || new User(this.$config.defaultServer, "", "", true), registrationFlowHandler });
},
addMatrixClientListeners(client) {
@ -432,10 +456,14 @@ export default {
}
});
Vue.set(this, "rooms", updatedRooms);
const currentRoom = this.getRoom(this.$store.state.currentRoomId);
if (this.currentRoom != currentRoom) {
this.currentRoom = currentRoom;
}
const resolvedId = (this.currentRoomId && this.currentRoomId.startsWith("#")) ? this.matrixClient.resolveRoomAlias(this.currentRoomId).then(r => r.room_id) : Promise.resolve(this.currentRoomId);
resolvedId.then(roomId => {
const currentRoom = this.getRoom(roomId);
if (this.currentRoom != currentRoom) {
this.currentRoom = currentRoom;
}
}).catch(ignorederror => {});
},
setCurrentRoomId(roomId) {
@ -560,6 +588,28 @@ export default {
}
},
/**
* Returns true if the current user is joined to the given room.
* @param roomIdOrAlias
* @returns Promise<Bool> - Whether the user is joined to the room or not
*/
isJoinedToRoom(roomIdOrAlias) {
if (roomIdOrAlias && this.matrixClient) {
try {
const resolvedRoomId = roomIdOrAlias.startsWith("#") ? this.matrixClient.resolveRoomAlias(roomIdOrAlias).then(res => res.room_id) : Promise.resolve(roomIdOrAlias);
return resolvedRoomId.then(roomId => {
return this.matrixClient.getJoinedRooms().then(rooms => {
return rooms.joined_rooms.includes(roomId);
});
});
} catch (ignorederror) {
console.error(ignorederror);
return Promise.resolve(false);
}
}
return Promise.resolve(false);
},
isReadOnlyRoom(roomId) {
if (this.matrixClient && roomId) {
const room = this.getRoom(roomId);