cordova-plugin-ete-authdialog
Version:
Adds support for authentication dialogs into Apache Cordova.
91 lines (74 loc) • 3.29 kB
JavaScript
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*global cordova, module*/
var authDialog = {};
function authenticateOnce(uri, successCallback, errorCallback, userName, password, allowBypassAuth, alertTitle, alertMessage, alertCancelButton, alertLoginButton) {
cordova.exec(successCallback, errorCallback, 'AuthDialog', 'authenticate', [uri, userName, password, allowBypassAuth, alertTitle, alertMessage, alertCancelButton, alertLoginButton]);
}
authDialog.authenticate = function (
uri,
/* optional*/ successCallback,
/* optional*/ errorCallback,
/* optional*/ userName,
/* optional*/ password,
/* optional*/ maxAttempts,
/* optional*/ alertTitle,
/* optional*/ alertMessage,
/* optional*/ alertCancelButton,
/* optional*/ alertLoginButton) {
if (!alertTitle) {
alertTitle = 'Authentication Dialog';
}
if (!alertMessage) {
alertMessage = 'Active directory authentication';
}
if (!alertCancelButton) {
alertCancelButton = 'Cancel';
}
if (!alertLoginButton) {
alertLoginButton = 'Login';
}
if (!uri) {
throw new Error('Uri is not specified');
}
userName = userName || null;
password = password || null;
maxAttempts = maxAttempts || 5;
errorCallback = errorCallback || function () { };
successCallback = successCallback || function () { };
var onError = function (err) {
if (maxAttempts-- <= 0 || err === "cancelled") {
errorCallback(err);
return;
}
// if first attemp has failed then user name and password are invalid
// so we should ask user to provide another credentials so we don't pass user/password
setTimeout(function () {
authenticateOnce(uri, successCallback, onError, null, null, false, alertTitle, alertMessage, alertCancelButton, alertLoginButton);
});
};
// allowBypass specifies whether app should try to resolve authentication challenge automatically
// first (cached credentials). This should be done only if no user and password are provided;
// this makes it possible to avoid passing credentials every app start
authenticateOnce(uri, successCallback, onError, userName, password, !(userName || password), alertTitle, alertMessage, alertCancelButton, alertLoginButton);
};
authDialog.logout = function (successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, 'AuthDialog', 'logout', []);
}
module.exports = authDialog;