making credential verification work better

This commit is contained in:
or-else
2018-08-21 14:58:09 +03:00
parent aca9286d63
commit 55c08373d0
+37 -23
View File
@@ -325,15 +325,15 @@ function stringHash(value) {
// path and arguments. // path and arguments.
function parseUrlHash(hash) { function parseUrlHash(hash) {
// Split path from args, path -> parts[0], args->path[1] // Split path from args, path -> parts[0], args->path[1]
var parts = hash.split('?', 2); let parts = hash.split('?', 2);
var params = {}; let params = {};
var path = []; let path = [];
if (parts[0]) { if (parts[0]) {
path = parts[0].substr(1).split("/"); path = parts[0].substr(1).split("/");
} }
if (parts[1]) { if (parts[1]) {
parts[1].split("&").forEach(function(part) { parts[1].split("&").forEach(function(part) {
var item = part.split("="); let item = part.split("=");
if (item[0]) { if (item[0]) {
params[decodeURIComponent(item[0])] = decodeURIComponent(item[1]); params[decodeURIComponent(item[0])] = decodeURIComponent(item[1]);
} }
@@ -4271,7 +4271,7 @@ class TinodeWeb extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = this.getInitialState(); this.state = this.getBlankState();
this.handleResize = this.handleResize.bind(this); this.handleResize = this.handleResize.bind(this);
this.handleHashRoute = this.handleHashRoute.bind(this); this.handleHashRoute = this.handleHashRoute.bind(this);
@@ -4318,9 +4318,11 @@ class TinodeWeb extends React.Component {
this.handleHideInfoView = this.handleHideInfoView.bind(this); this.handleHideInfoView = this.handleHideInfoView.bind(this);
this.handleMemberUpdateRequest = this.handleMemberUpdateRequest.bind(this); this.handleMemberUpdateRequest = this.handleMemberUpdateRequest.bind(this);
this.handleValidateCredentialsRequest = this.handleValidateCredentialsRequest.bind(this); this.handleValidateCredentialsRequest = this.handleValidateCredentialsRequest.bind(this);
this.handleHashRoute();
} }
getInitialState() { getBlankState() {
return { return {
connected: false, connected: false,
transport: null, transport: null,
@@ -4351,7 +4353,8 @@ class TinodeWeb extends React.Component {
serverVersion: 'no connection', serverVersion: 'no connection',
contactsSearchQuery: '', contactsSearchQuery: '',
foundContacts: [], foundContacts: [],
credMethod: '' credMethod: undefined,
credCode: undefined
}; };
} }
@@ -4440,10 +4443,17 @@ class TinodeWeb extends React.Component {
this.setState({topicSelected: null}); this.setState({topicSelected: null});
} }
} }
// Save validation credentials, if available.
if (hash.params.code && hash.params.method) {
this.setState({
credCode: hash.params.code,
credMethod: hash.params.method
});
}
// Additional parameters of panels. // Additional parameters of panels.
this.setState({ this.setState({
credCode: hash.params.code,
credMethod: hash.params.method,
showInfoPanel: hash.params.info, showInfoPanel: hash.params.info,
newTopicTabSelected: hash.params.tab newTopicTabSelected: hash.params.tab
}); });
@@ -4490,7 +4500,7 @@ class TinodeWeb extends React.Component {
this.handleError("", null); this.handleError("", null);
if (Tinode.isConnected()) { if (Tinode.isConnected()) {
this.doLogin(login, password); this.doLogin(login, password, {meth: this.state.credMethod, resp: this.state.credCode});
} else { } else {
Tinode.connect().catch(function(err) { Tinode.connect().catch(function(err) {
// Socket error // Socket error
@@ -4506,7 +4516,7 @@ class TinodeWeb extends React.Component {
this.setState({ this.setState({
serverVersion: params.ver + " " + (params.build ? params.build : "none") + "; " serverVersion: params.ver + " " + (params.build ? params.build : "none") + "; "
}); });
this.doLogin(this.state.login, this.state.password); this.doLogin(this.state.login, this.state.password, {meth: this.state.credMethod, resp: this.state.credCode});
} }
doLogin(login, password, cred) { doLogin(login, password, cred) {
@@ -4520,33 +4530,34 @@ class TinodeWeb extends React.Component {
cred = cred ? cred.cred : undefined; cred = cred ? cred.cred : undefined;
// Try to login with login/password. If they are not available, try token. If no token, ask for login/password. // Try to login with login/password. If they are not available, try token. If no token, ask for login/password.
var promise = null; let promise = null;
var token = Tinode.getAuthToken(); let token = Tinode.getAuthToken();
if (login && password) { if (login && password) {
this.setState({password: null}); this.setState({password: null});
promise = Tinode.loginBasic(login, password, cred); promise = Tinode.loginBasic(login, password, cred);
} else if (token) { } else if (token) {
promise = Tinode.loginToken(token.token, cred); promise = Tinode.loginToken(token.token, cred);
} else {
this.setState({loginDisabled: false});
} }
var instance = this;
if (promise) { if (promise) {
promise.then((ctrl) => { promise.then((ctrl) => {
if (ctrl.code >= 300 && ctrl.text === "validate credentials") { if (ctrl.code >= 300 && ctrl.text === "validate credentials") {
instance.handleCredentialsRequest(ctrl.params); this.handleCredentialsRequest(ctrl.params);
} else { } else {
instance.handleLoginSuccessful(instance); this.handleLoginSuccessful(this);
} }
}).catch((err) => { }).catch((err) => {
// Login failed, report error // Login failed, report error.
instance.setState({loginDisabled: false}); this.setState({loginDisabled: false, credMethod: undefined, credCode: undefined});
instance.handleError(err.message, "err"); this.handleError(err.message, "err");
localStorage.removeItem("auth-token"); localStorage.removeItem("auth-token");
window.location.hash = ""; window.location.hash = "";
}); });
} else {
// No login credentials provided.
// Make sure we are on the login page.
window.location.hash = "";
this.setState({loginDisabled: false});
} }
} }
@@ -4569,6 +4580,8 @@ class TinodeWeb extends React.Component {
me.onMetaDesc = instance.tnMeMetaDesc; me.onMetaDesc = instance.tnMeMetaDesc;
instance.setState({ instance.setState({
connected: true, connected: true,
credMethod: undefined,
credCode: undefined,
myUserId: Tinode.getCurrentUserID() myUserId: Tinode.getCurrentUserID()
}); });
// Subscribe, fetch topic desc, the list of subscriptions. Messages are not fetched. // Subscribe, fetch topic desc, the list of subscriptions. Messages are not fetched.
@@ -4877,7 +4890,7 @@ class TinodeWeb extends React.Component {
handleLogout() { handleLogout() {
localStorage.removeItem("auth-token"); localStorage.removeItem("auth-token");
this.setState(this.getInitialState()); this.setState(this.getBlankState());
TinodeWeb.tnSetup(this.state.serverAddress, this.state.transport); TinodeWeb.tnSetup(this.state.serverAddress, this.state.transport);
window.location.hash = ""; window.location.hash = "";
} }
@@ -4982,6 +4995,7 @@ class TinodeWeb extends React.Component {
} }
handleValidateCredentialsRequest(cred, code) { handleValidateCredentialsRequest(cred, code) {
this.setState({credMethod: cred, credCode: code});
this.doLogin(null, null, {meth: cred, resp: code}); this.doLogin(null, null, {meth: cred, resp: code});
} }