Updated rpcd version and fixed password page

This commit is contained in:
Martin Schröder 2015-05-19 16:28:40 +02:00 committed by Martin Schröder
parent c4ee777913
commit 4d234f0363
5 changed files with 85 additions and 27 deletions

View file

@ -3,6 +3,7 @@
(function(scope){
var RPC_HOST = ""; //(($config.rpc.host)?$config.rpc.host:"")
var RPC_SESSION_ID = "00000000000000000000000000000000";
var RPC_DEFAULT_SESSION_ID = "00000000000000000000000000000000";
var gettext = function(text){ return text; };
var default_calls = [
"session.access",
@ -74,14 +75,38 @@
if(sid) RPC_SESSION_ID = sid;
else return RPC_SESSION_ID;
},
$authenticate: function(){
var self = this;
var deferred = $.Deferred();
self.session.access({
"keys": ""
}).done(function(result){
if(!("username" in result.data)) {
console.log("Session: Not authenticated!");
deferred.reject();
} else {
self.$session = result;
if(!("data" in self.$session)) self.$session.data = {};
console.log("Session: Loggedin! ");
deferred.resolve(result);
}
}).fail(function err(result){
self.sid = RPC_DEFAULT_SESSION_ID;
deferred.reject();
});
return deferred.promise();
},
$login: function(opts){
var self = this;
var deferred = $.Deferred();
self.session.login({
"username": opts.username,
"password": opts.password
}).done(function(result){
RPC_SESSION_ID = result.ubus_rpc_session;
self.$session = result;
//JUCI.localStorage.setItem("sid", self.sid);
//if(result && result.acls && result.acls.ubus) setupUbusRPC(result.acls.ubus);
deferred.resolve(self.sid);

View file

@ -13,6 +13,7 @@
}
this.sid = (saved_sid)?saved_sid:default_sid;
this.data = {};
this.isLoggedIn = function(){
return this._loggedIn;
@ -21,22 +22,12 @@
var self = this;
var deferred = $.Deferred();
console.log("Checking session key with server: "+saved_sid);
$rpc.session.access({
"keys": ""
}).done(function(result){
if(result["access-group"] && result["access-group"].unauthenticated && Object.keys(result["access-group"]).length == 1) {
console.log("Session: Not authenticated!");
deferred.reject();
} else {
self.data = result;
console.log("Session: Loggedin!");
self._loggedIn = true;
deferred.resolve(result);
}
$rpc.$authenticate().done(function(){
self._loggedIn = true;
deferred.resolve();
}).fail(function err(result){
self.sid = default_sid;
$juci.localStorage.setItem("sid", self.sid);
$rpc.$sid(self.sid);
deferred.reject();
});
return deferred.promise();
@ -44,13 +35,13 @@
this.login = function(obj){
var self = this;
var deferred = $.Deferred();
// TODO: remove $session completely and use $rpc.$session instead!
$rpc.session.login({
"username": obj.username,
"password": obj.password
}).done(function(result){
self.sid = result.ubus_rpc_session;
$rpc.$sid(self.sid);
self.data = result;
self.data = result.data;
self._loggedIn = true;
$juci.localStorage.setItem("sid", self.sid);
//if(result && result.acls && result.acls.ubus) setupUbusRPC(result.acls.ubus);

View file

@ -1,5 +1,5 @@
$juci.module("settings")
.controller("SettingsPasswordCtrl", function($scope, $tr, gettext){
.controller("SettingsPasswordCtrl", function($scope, $rpc, $tr, $session, gettext){
$scope.showPassword = 0;
$scope.showModal = 0;
$scope.modal = {
@ -32,7 +32,7 @@ $juci.module("settings")
if($scope.modal.password != $scope.modal.password2) alert($tr(gettext("Passwords do not match!")));
else {
// TODO: change to correct username
$rpc.luci2.system.password_set({user: "root", password: $scope.modal.password}).done(function(data){
$rpc.luci2.system.password_set({user: $rpc.$session.data.username, password: $scope.modal.password, curpass: $scope.modal.old_password}).done(function(data){
$scope.showModal = 0;
$scope.$apply();
}).fail(function(){

View file

@ -12,4 +12,42 @@ describe("Settings", function(){
it("should be completed", function(){
expect(Object.keys(completed).filter(function(x){ return completed[x] == 0; })).to.be.empty();
});
it("should have required rpc functions", function(){
expect($rpc.luci2.system.password_set).to.be.a(Function);
});
it("should not be able to set password without providing current password", function(done){
console.log("Trying to set password for user "+PARAMS.username);
$rpc.luci2.system.password_set({"user": PARAMS.username, "password": "abrakadabra"}).done(function(){
// reset password to current password again!
$rpc.luci2.system.password_set({"user": PARAMS.username, "password": PARAMS.password, "curpass": "abrakadabra"}).always(function(){
throw new Error("Should not be able to set password without first providing current password!");
});
}).fail(function(){
done();
});
});
// TODO: this test should not work anyway when current password is a required feature
it("should not be able to set password for root", function(done){
if(PARAMS.username != "root"){
$rpc.luci2.system.password_set({"user": "root", "password": "foo"}).done(function(){
throw new Error("Should not be able to set password for root when not root (now you have to change it back from 'foo' to your previous password)!");
}).fail(function(){
done();
});
} else {
done();
}
});
it("should be able to set password for current user", function(done){
$rpc.luci2.system.password_set({"user": PARAMS.username, "password": PARAMS.password, "curpass": PARAMS.password}).done(function(){
// TODO: test also to login after changing password to something else!
done();
}).fail(function(){
throw new Error("Was unable to execute password_set()");
});
});
it("should be able to access $rpc.$session.data.username and it should be set to the name of the currently logged in user", function(){
expect($rpc.$session).to.be.ok();
expect($rpc.$session.data.username).to.be.eql(PARAMS.username);
});
});

View file

@ -12,30 +12,34 @@ var $rpc = global.$rpc = global.UBUS;
var $uci = global.$uci = global.UCI;
Object.prototype.assign = require("object-assign");
var host = "localhost";
var username;
var password;
var PARAMS = {
host: "localhost",
username: "",
password: ""
};
for(var i = 0; i < process.argv.length; i++){
switch(process.argv[i]){
case "--pass": password = process.argv[++i]; break;
case "--user": username = process.argv[++i]; break;
case "--host": host = process.argv[++i]; break;
case "--pass": PARAMS.password = process.argv[++i]; break;
case "--user": PARAMS.username = process.argv[++i]; break;
case "--host": PARAMS.host = process.argv[++i]; break;
};
}
if(!username || !password ){
if(!PARAMS.username || !PARAMS.password ){
console.error("Please specify --user <rpcuser> and --pass <rpcpassword> arguments!");
process.exit();
}
global.PARAMS = PARAMS;
function init(){
var deferred = $.Deferred();
async.series([
function(next){
console.log("Trying to connect to RPC host '"+host+"'...");
console.log("Trying to connect to RPC host '"+PARAMS.host+"'...");
$rpc.$init({host: "http://"+host}).done(function(){
$rpc.$init({host: "http://"+PARAMS.host}).done(function(){
//console.log("Initialized rpc: "+Object.keys($rpc));
next();
}).fail(function(){
@ -43,7 +47,7 @@ function init(){
});
},
function(next){
$rpc.$login({username: username, password: password}).done(function(){
$rpc.$login({username: PARAMS.username, password: PARAMS.password}).done(function(){
next();
}).fail(function(){
throw new Error("Could not login over RPC using specified username and password!");