Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect home server from matrix id #810

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4de8ab4
Adding the functionnality to use his own matrix id (@username:server.…
Kaki-In Jul 21, 2022
254cea5
Added some forgot changes
Kaki-In Jul 21, 2022
968dbac
Removed a console.log
Kaki-In Jul 21, 2022
05c3afb
Added some forgot changes
Kaki-In Jul 21, 2022
1298d43
Removed a console.dir
Kaki-In Jul 21, 2022
dcb1534
Fixed minor bug
Kaki-In Jul 21, 2022
bd29e09
Fixed the problem of having given a bad password/username
Kaki-In Jul 21, 2022
d8c6847
Improve the connection frame actualisation
Kaki-In Jul 22, 2022
28785d9
Fixed a bug that always showed the password and login views when load…
Kaki-In Jul 22, 2022
d75c937
New corrections (from base of #810 pull request commentaries) :
Kaki-In Jul 25, 2022
c7c32a6
Update about the serverName constant : missing the result id (from th…
Kaki-In Jul 25, 2022
17730e8
Detected inattention mistake.
Kaki-In Jul 25, 2022
5542335
Merge branch 'vector-im:master' into detect-hs-from-mxid
Kaki-In Jul 25, 2022
35e830b
Resolved the comments at https://github.com/vector-im/hydrogen-web/pu…
Kaki-In Jul 26, 2022
acd1eea
Fixed an error while writing boolean
Kaki-In Jul 27, 2022
2b1805b
Canceled the indenting modification.
Kaki-In Jul 27, 2022
d47a552
Merge branch 'master' into detect-hs-from-mxid
Kaki-In Jul 29, 2022
a060591
Resolved morge conflict with LoginViewmodel
Kaki-In Jul 29, 2022
af3f900
Merge branch 'vector-im:master' into detect-hs-from-mxid
Kaki-In Jul 29, 2022
46c2822
Fixed last checked on https://github.com/vector-im/hydrogen-web/pull/810
Kaki-In Jul 29, 2022
d65a5a3
Merge branch 'vector-im:master' into detect-hs-from-mxid
Kaki-In Jul 29, 2022
51acbf5
Merge branch 'vector-im:master' into detect-hs-from-mxid
Kaki-In Jul 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions src/domain/login/LoginViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export class LoginViewModel extends ViewModel {
client: this._client,
attemptLogin: loginMethod => this.attemptLogin(loginMethod),
loginToken: this._loginToken
})));
})
));
this.emitChange("completeSSOLoginViewModel");
}
else {
Expand All @@ -81,26 +82,38 @@ export class LoginViewModel extends ViewModel {
this._passwordLoginViewModel = this.track(new PasswordLoginViewModel(
this.childOptions({
loginOptions: this._loginOptions,
attemptLogin: loginMethod => this.attemptLogin(loginMethod)
})));
attemptLogin: loginMethod => this.attemptLogin(loginMethod),
setHomeserver: homeserver => this.setHomeserver(homeserver)
})
));
this.emitChange("passwordLoginViewModel");
}


_hidePasswordLogin() {
this._passwordLoginViewModel = null;
this.emitChange("passwordLoginViewModel");
}

_showSSOLogin() {
this._startSSOLoginViewModel = this.track(
new StartSSOLoginViewModel(this.childOptions({loginOptions: this._loginOptions}))
);
this.emitChange("startSSOLoginViewModel");
}

_hideSSOLogin() {
this._startSSOLoginViewModel = null;
this.emitChange("startSSOLoginViewModel");
}

_showError(message) {
this._errorMessage = message;
this.emitChange("errorMessage");
}

_setBusy(status) {
this._isBusy = status;
this._passwordLoginViewModel?.setBusy(status);
this._passwordLoginViewModel?.setEnabled(!status);
this._startSSOLoginViewModel?.setBusy(status);
this.emitChange("isBusy");
}
Expand All @@ -116,9 +129,9 @@ export class LoginViewModel extends ViewModel {
if (status === LoadStatus.LoginFailed) {
return this._client.loginFailure;
}
this._hidePasswordLogin()
this._hideHomeserver = true;
this.emitChange("hideHomeserver");
this._disposeViewModels();
this._createLoadViewModel();
return null;
}
Expand Down Expand Up @@ -151,20 +164,17 @@ export class LoginViewModel extends ViewModel {
);
}

_disposeViewModels() {
this._startSSOLoginViewModel = this.disposeTracked(this._ssoLoginViewModel);
this._passwordLoginViewModel = this.disposeTracked(this._passwordLoginViewModel);
this._completeSSOLoginViewModel = this.disposeTracked(this._completeSSOLoginViewModel);
this.emitChange("disposeViewModels");
updateLoginOptions() {
this._passwordLoginViewModel?.setLoginOptions(this._loginOptions);
}

async setHomeserver(newHomeserver) {
this._homeserver = newHomeserver;
this._passwordLoginViewModel?.setEnabled(false);
// clear everything set by queryHomeserver
this._loginOptions = null;
this._queriedHomeserver = null;
this._showError("");
this._disposeViewModels();
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
this.emitChange(); // multiple fields changing
// also clear the timeout if it is still running
Expand All @@ -181,7 +191,7 @@ export class LoginViewModel extends ViewModel {
}
}
this._abortHomeserverQueryTimeout = this.disposeTracked(this._abortHomeserverQueryTimeout);
this.queryHomeserver();
await this.queryHomeserver();
}

async queryHomeserver() {
Expand Down Expand Up @@ -211,21 +221,29 @@ export class LoginViewModel extends ViewModel {
return; //aborted, bail out
} else {
this._loginOptions = null;
this._hideSSOLogin();
this._hidePasswordLogin();
}
} finally {
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
this.emitChange("isFetchingLoginOptions");
}
this.updateLoginOptions();
if (this._loginOptions) {
if (this._loginOptions.sso) { this._showSSOLogin(); }
if (this._loginOptions.password) { this._showPasswordLogin(); }
if (this._loginOptions.sso && !this._startSSOLoginViewModel) { this._showSSOLogin(); }
if (!this._loginOptions.sso && this._startSSOLoginViewModel) { this._hideSSOLogin(); }
if (this._loginOptions.password && !this._passwordLoginViewModel) { this._showPasswordLogin(); }
if (!this._loginOptions.password && this._passwordLoginViewModel) { this._hidePasswordLogin(); }
if (!this._loginOptions.sso && !this._loginOptions.password) {
this._showError("This homeserver supports neither SSO nor password based login flows");
}
}
}
else {
this._showError(`Could not query login methods supported by ${this.homeserver}`);
this._hideSSOLogin();
this._hidePasswordLogin();
}
this._passwordLoginViewModel?.setEnabled(false);
}

dispose() {
Expand Down
54 changes: 52 additions & 2 deletions src/domain/login/PasswordLoginViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export class PasswordLoginViewModel extends ViewModel {
this._loginOptions = loginOptions;
this._attemptLogin = attemptLogin;
this._isBusy = false;
this._enabled = true;
this._errorMessage = "";
}

get isBusy() { return this._isBusy; }
get isEnabled() { return this._isEnabled; }
get errorMessage() { return this._errorMessage; }

setBusy(status) {
Expand All @@ -39,10 +41,56 @@ export class PasswordLoginViewModel extends ViewModel {
this._errorMessage = message;
this.emitChange("errorMessage");
}

setLoginOptions(loginOptions) {
this._loginOptions = loginOptions;
}

setEnabled(enabled) {
this._enabled = enabled;
this.emitChange("isEnabled");
}

findHomeserverFromUserid (userid) {
const matches = userid.match(/@.*:(.+)/);
if (!matches) {
return;
}
const [, homeserverInMxid] = matches;
return homeserverInMxid;
}

findUsernameFromUsertext (userid) {
const matches = userid.match(/@(.+):.*/);
if (!matches) {
return userid;
}
const [, username] = matches;
return username;
}

async changeHomeserverFromUserid(userid) {
const currentHomeserver = this._loginOptions.homeserver;
const newHomeserver = this.findHomeserverFromUserid(userid);
if (this._loginOptions.homeserver != newHomeserver && newHomeserver) {
await this._options.setHomeserver(newHomeserver);
return true;
}
return false;
}

async parseUsernameLogin (username) {
await this.changeHomeserverFromUserid(username);
return this.findUsernameFromUsertext(username);
}

async login(username, password) {
this._errorMessage = "";
this.emitChange("errorMessage");
this.setBusy(true);
username = await this.parseUsernameLogin(username);
if (!this._loginOptions) {
return;
}
this._showError("");
const status = await this._attemptLogin(this._loginOptions.password(username, password));
let error = "";
switch (status) {
Expand All @@ -57,7 +105,9 @@ export class PasswordLoginViewModel extends ViewModel {
break;
}
if (error) {
console.log(error);
this._showError(error);
}
this.setBusy(false);
}
}
18 changes: 9 additions & 9 deletions src/matrix/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ export function makeTxnId() {
}

export function isTxnId(txnId) {
return txnId.startsWith("t") && txnId.length === 15;
return txnId.startsWith("t") && txnId.length === 15;
}

export function tests() {
return {
Kaki-In marked this conversation as resolved.
Show resolved Hide resolved
"isTxnId succeeds on result of makeTxnId": assert => {
assert(isTxnId(makeTxnId()));
},
"isTxnId fails on event id": assert => {
assert(!isTxnId("$yS_n5n3cIO2aTtek0_2ZSlv-7g4YYR2zKrk2mFCW_rm"));
},
}
return {
"isTxnId succeeds on result of makeTxnId": assert => {
assert(isTxnId(makeTxnId()));
},
"isTxnId fails on event id": assert => {
assert(!isTxnId("$yS_n5n3cIO2aTtek0_2ZSlv-7g4YYR2zKrk2mFCW_rm"));
},
}
}
2 changes: 1 addition & 1 deletion src/platform/web/ui/login/LoginView.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class LoginView extends TemplateView {
id: "homeserver",
type: "text",
placeholder: vm.i18n`Your matrix homeserver`,
value: vm.homeserver,
value: vm => vm.homeserver,
disabled,
onInput: event => vm.setHomeserver(event.target.value),
onChange: () => vm.queryHomeserver(),
Expand Down
45 changes: 25 additions & 20 deletions src/platform/web/ui/login/PasswordLoginView.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {TemplateView} from "../general/TemplateView";

export class PasswordLoginView extends TemplateView {
render(t, vm) {
const disabled = vm => !!vm.isBusy;
const disabled = vm => vm.isBusy && vm.isEnabled;
Kaki-In marked this conversation as resolved.
Show resolved Hide resolved
const username = t.input({
id: "username",
type: "text",
Expand All @@ -32,25 +32,30 @@ export class PasswordLoginView extends TemplateView {
disabled
});

return t.div({className: "PasswordLoginView form"}, [
t.if(vm => vm.error, t => t.div({ className: "error" }, vm => vm.error)),
t.form({
onSubmit: evnt => {
evnt.preventDefault();
vm.login(username.value, password.value);
}
}, [
t.if(vm => vm.errorMessage, (t, vm) => t.p({className: "error"}, vm.i18n(vm.errorMessage))),
t.div({ className: "form-row text" }, [t.label({ for: "username" }, vm.i18n`Username`), username]),
t.div({ className: "form-row text" }, [t.label({ for: "password" }, vm.i18n`Password`), password]),
t.div({ className: "button-row" }, [
t.button({
className: "button-action primary",
type: "submit",
disabled
}, vm.i18n`Log In`),
]),
])
return t.div({
className: {
PasswordLoginView: true,
form: true,
disabled: disabled
}}, [
t.if(vm => vm.error, t => t.div({ className: "error" }, vm => vm.error)),
t.form({
onSubmit: evnt => {
evnt.preventDefault();
vm.login(username.value, password.value);
}
}, [
t.if(vm => vm.errorMessage, (t, vm) => t.p({className: "error"}, vm.i18n(vm.errorMessage))),
t.div({ className: "form-row text" }, [t.label({ for: "username" }, vm.i18n`Username`), username]),
t.div({ className: "form-row text" }, [t.label({ for: "password" }, vm.i18n`Password`), password]),
t.div({ className: "button-row" }, [
t.button({
className: "button-action primary",
type: "submit",
disabled
}, vm.i18n`Log In`),
]),
])
]);
}
}
Expand Down