TT#17984 CSC JWT client implmentation followup

Previous implementatation was still using dummy data.
With this changes the integration of JWT iin the client is completed.

Change-Id: I4dde4fc209ec878d87e6477d70cf7a5b02fb9b34
changes/30/14130/3
Carlo 8 years ago
parent 17498c91ee
commit eb5330942e

@ -2,20 +2,25 @@
Ext.Ajax.on("beforerequest", function(con, options) { Ext.Ajax.on("beforerequest", function(con, options) {
con.setUseDefaultXhrHeader(false); con.setUseDefaultXhrHeader(false);
con.setWithCredentials(true); con.setWithCredentials(true);
if (options.params && localStorage.getItem('jwt_token')) { if (options.params && localStorage.getItem('jwt')) {
delete options.params.page; delete options.params.page;
delete options.params.start; delete options.params.start;
delete options.params.limit; delete options.params.limit;
options.params['jwt_token'] = localStorage.getItem('jwt_token'); this.setDefaultHeaders({
'Authorization': 'Bearer ' + localStorage.getItem('jwt')
});
} }
}); });
// in case of 401, user is redirected to login screen // in case of 401 or 403, user is redirected to login screen
Ext.Ajax.on("requestexception", function(con, response) { Ext.Ajax.on("requestexception", function(con, response) {
var httpStatus = response.status; var httpStatus = response.status;
switch (httpStatus) { switch (httpStatus) {
case 401: case 401:
NgcpCsc.getApplication().showLogin(); case 403:
if (response.request.url !== '/login_jwt/') {
NgcpCsc.getApplication().showLogin();
}
break; break;
} }
}); });
@ -40,21 +45,30 @@ Ext.application({
if (mainCmp) { if (mainCmp) {
mainCmp.destroy(); mainCmp.destroy();
} }
window.location.hash = '';
Ext.create({ Ext.create({
xtype: 'ngcp-login' xtype: 'ngcp-login'
}); });
localStorage.removeItem('jwt_token'); localStorage.removeItem('jwt');
localStorage.removeItem('subscriber_id');
localStorage.removeItem('type');
localStorage.removeItem('username');
window.location.hash = '';
}, },
showMain: function() { showMain: function() {
var me = this;
var winLogin = Ext.ComponentQuery.query('[name=loginWin]')[0];
var loginCont = Ext.ComponentQuery.query('[id=loginCont]')[0];
if (winLogin) {
winLogin.destroy();
}
if (loginCont) {
loginCont.destroy();
}
window.location.hash = '';
Ext.create({ Ext.create({
xtype: 'ngcp-main' xtype: 'ngcp-main'
}); });
window.location.hash = '#inbox'; Ext.Function.defer(function() {
me.redirectTo('inbox');
}, 100)
} }
}); });

@ -9,10 +9,6 @@ proxy: {
Ext.define('NgcpCsc.proxy.NgcpApi', { Ext.define('NgcpCsc.proxy.NgcpApi', {
extend: 'Ext.data.proxy.Ajax', extend: 'Ext.data.proxy.Ajax',
alias: 'proxy.ngcp-api', alias: 'proxy.ngcp-api',
/* temporary authentication, should be bound to auth token once implemented*/
withCredentials: true,
username: 'administrator',
password: 'administrator',
baseApiUrl: '/api/', baseApiUrl: '/api/',
autoLoad: true, autoLoad: true,
appendId: false, appendId: false,
@ -35,7 +31,7 @@ Ext.define('NgcpCsc.proxy.NgcpApi', {
me.headers = { me.headers = {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}; };
url = Ext.String.format('{0}{1}/{2}?{3}', me.baseApiUrl, me.route, me.subscriberId, me.params); url = Ext.String.format('{0}{1}/{2}?{3}', me.baseApiUrl, me.route, localStorage.getItem('subscriber_id'), me.params);
break; break;
case 'update': case 'update':
me.headers = (me.actionMethods.update == 'PUT') ? { me.headers = (me.actionMethods.update == 'PUT') ? {
@ -45,7 +41,7 @@ Ext.define('NgcpCsc.proxy.NgcpApi', {
}; };
records = request._records; records = request._records;
url = Ext.String.format('{0}{1}/{2}', me.baseApiUrl, me.route, me.subscriberId || records[0].get('id')); url = Ext.String.format('{0}{1}/{2}', me.baseApiUrl, me.route, localStorage.getItem('subscriber_id'));
break; break;
} }
request._url = url; request._url = url;

@ -10,7 +10,6 @@ Ext.define('NgcpCsc.store.CallBlocking', {
proxy: { proxy: {
type: 'ngcp-api', type: 'ngcp-api',
route: 'subscriberpreferences', route: 'subscriberpreferences',
subscriberId: localStorage.getItem('subscriber_id'),
actionMethods: { actionMethods: {
read: 'GET', read: 'GET',
update: 'PATCH' update: 'PATCH'

@ -41,9 +41,19 @@ Ext.define('NgcpCsc.Application', {
launch: function() { launch: function() {
Ext.tip.QuickTipManager.init(); Ext.tip.QuickTipManager.init();
// TODO to be replaced with request if (localStorage.getItem('username') && localStorage.getItem('password') && localStorage.getItem('jwt')) {
if (localStorage.getItem('jwt_token')) { // checks that the jwt token is valid
this.showMain() Ext.Ajax.request({
url: '/login_jwt/',
method: 'POST',
jsonData: {
username: localStorage.getItem('username'),
password: localStorage.getItem('password')
},
success: this.showMain,
failure: this.showLogin,
scope: this
});
} else { } else {
this.showLogin(); this.showLogin();
} }

@ -1,6 +1,7 @@
Ext.define('NgcpCsc.view.login.Login', { Ext.define('NgcpCsc.view.login.Login', {
extend: 'Ext.window.Window', extend: 'Ext.window.Window',
controller: 'login', controller: 'login',
name: 'loginWin',
autoShow: true, autoShow: true,
bodyPadding: 20, bodyPadding: 20,
closable: false, closable: false,

@ -1,10 +1,14 @@
Ext.define('NgcpCsc.view.login.LoginContiner', { Ext.define('NgcpCsc.view.login.LoginContiner', {
extend: 'Ext.container.Viewport', extend: 'Ext.container.Viewport',
id: 'loginCont',
xtype: 'ngcp-login', xtype: 'ngcp-login',
cls: 'login-container', cls: 'login-container',
listeners:{ listeners: {
render:function(){ afterrender: function() {
Ext.create('NgcpCsc.view.login.Login'); var winLogin = Ext.ComponentQuery.query('[name=loginWin]')[0];
if (!winLogin) {
Ext.create('NgcpCsc.view.login.Login');
}
} }
} }
}); });

@ -5,8 +5,7 @@ Ext.define('NgcpCsc.view.login.LoginController', {
onPressEnter: function(field, e) { onPressEnter: function(field, e) {
var inputUsername = this.getViewModel().get('username'); var inputUsername = this.getViewModel().get('username');
var inputPassword = this.getViewModel().get('password'); var inputPassword = this.getViewModel().get('password');
var isAdminUser = inputUsername == 'admin' && inputPassword == 'admin'; // hardcoded credentials TODO remove when jwt is ready in backend if (e.getKey() == e.ENTER) {
if (e.getKey() == e.ENTER && isAdminUser) {
this.onLoginClick(); this.onLoginClick();
} }
}, },
@ -27,8 +26,8 @@ Ext.define('NgcpCsc.view.login.LoginController', {
var inputUsername = this.getViewModel().get('username'); var inputUsername = this.getViewModel().get('username');
var inputPassword = this.getViewModel().get('password'); var inputPassword = this.getViewModel().get('password');
Ext.Ajax.request({ Ext.Ajax.request({
url: Ext.manifest.resources.path + '/data/auth.json', // will be '/login_jwt/', url: '/login_jwt/',
method: 'GET', // will be POST method: 'POST',
jsonData: { jsonData: {
username: inputUsername, username: inputUsername,
password: inputPassword password: inputPassword
@ -40,21 +39,19 @@ Ext.define('NgcpCsc.view.login.LoginController', {
}, },
successLogin: function(response) { successLogin: function(response) {
var data = Ext.decode(response.responseText).data; var data = Ext.decode(response.responseText);
if (data.token) { if (data.jwt) {
localStorage.setItem('username', this.getViewModel().get('username')); localStorage.setItem('username', this.getViewModel().get('username'));
localStorage.setItem('password', this.getViewModel().get('password'));
localStorage.setItem('subscriber_id', data.subscriber_id); localStorage.setItem('subscriber_id', data.subscriber_id);
localStorage.setItem('jwt_token', data.token); localStorage.setItem('jwt', data.jwt);
localStorage.setItem('type', data.type || 'admin'); // this is the user Role, which shows/hides the modules in navigation tree localStorage.setItem('type', data.type || 'admin'); // this is the user Role, which shows/hides the modules in navigation tree
this.getView().close(); NgcpCsc.app.showMain();
Ext.create({
xtype: 'ngcp-main'
});
} }
}, },
unsuccessLogin: function(response) { unsuccessLogin: function(response) {
localStorage.removeItem('jwt_token'); localStorage.removeItem('jwt');
Ext.Msg.alert('Error', 'Username or Password not valid!'); Ext.Msg.alert('Error', 'Username or Password not valid!');
} }
}); });

@ -9,15 +9,12 @@ Ext.define('NgcpCsc.view.login.LoginModel', {
// left inline by purpose until auth specs // left inline by purpose until auth specs
data: { data: {
username: localStorage.getItem('username') || '', username: localStorage.getItem('username') || '',
password: localStorage.getItem('password') || '', password: localStorage.getItem('password') || ''
defaultCredentials: ['admin', 'restricted', 'host']
}, },
formulas: { formulas: {
authValid: function(get) { authValid: function(get) {
return (get('username') == get('password') && return ( get('username').length > 0 && get('password').length > 0);
get('defaultCredentials').indexOf(get('username')) > -1 &&
get('defaultCredentials').indexOf(get('password')) > -1);
} }
} }

@ -37,7 +37,7 @@ Ext.define('NgcpCsc.view.main.MainController', {
store = navigationList.getStore(), store = navigationList.getStore(),
node = store.findNode('routeId', hashTag) || node = store.findNode('routeId', hashTag) ||
store.findNode('viewType', hashTag) || 'inbox', store.findNode('viewType', hashTag) || 'inbox',
view = (node && node.get('viewType')), view = (node && Ext.isObject(node) && node.get('viewType')),
lastView = me.lastView, lastView = me.lastView,
existingItem = mainCard.child('component[routeId=' + hashTag + ']'), existingItem = mainCard.child('component[routeId=' + hashTag + ']'),
newView; newView;
@ -246,7 +246,7 @@ Ext.define('NgcpCsc.view.main.MainController', {
}, },
logout: function() { logout: function() {
localStorage.removeItem('jwt_token'); localStorage.removeItem('jwt');
location.reload(); location.reload();
}, },

Loading…
Cancel
Save