Skip to content

Instantly share code, notes, and snippets.

@Bouhnosaure
Created November 14, 2016 16:18
Show Gist options
  • Save Bouhnosaure/5e0e8fe0f995a4f09dba2e2db1ce671c to your computer and use it in GitHub Desktop.
Save Bouhnosaure/5e0e8fe0f995a4f09dba2e2db1ce671c to your computer and use it in GitHub Desktop.
/*!
* OS.js - JavaScript Cloud/Web Desktop Platform
*
* Copyright (c) 2011-2016, Anders Evenrud <andersevenrud@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Anders Evenrud <andersevenrud@gmail.com>
* @licence Simplified BSD License
*/
(function (API, Utils, Authenticator) {
'use strict';
var keycloak = null;
function KeycloakAuthenticator() {
Authenticator.apply(this, arguments);
}
KeycloakAuthenticator.prototype = Object.create(Authenticator.prototype);
KeycloakAuthenticator.constructor = Authenticator;
KeycloakAuthenticator.prototype.login = function (data, callback) {
console.info('Handler::login()');
this.handler.callAPI('login', {}, function (response) {
console.log(response);
if (response.result) {
callback(false, response.result);
} else {
var error = response.error || API._('ERR_LOGIN_INVALID');
callback(API._('ERR_LOGIN_FMT', error), false);
}
}, function (error) {
callback(API._('ERR_LOGIN_FMT', error), false);
});
};
KeycloakAuthenticator.prototype.onLogin = function (data, callback) {
callback = callback || function () {
};
var userSettings = data.userSettings;
if (!userSettings || userSettings instanceof Array) {
userSettings = {};
}
this.userData = data.userData;
// Ensure we get the user-selected locale configured from WM
function getUserLocale() {
var curLocale = Utils.getUserLocale() || API.getConfig('Locale');
var result = OSjs.Core.getSettingsManager().get('CoreWM');
if (!result) {
try {
result = userSettings.CoreWM;
} catch (e) {
}
}
return result ? (result.language || curLocale) : curLocale;
}
API.setLocale(getUserLocale());
OSjs.Core.getSettingsManager().init(userSettings);
if (data.blacklistedPackages) {
OSjs.Core.getPackageManager().setBlacklist(data.blacklistedPackages);
}
this.loggedIn = true;
callback();
};
KeycloakAuthenticator.prototype.logout = function (callback) {
};
KeycloakAuthenticator.prototype.onCreateUI = function (callback) {
var self = this;
keycloak = Keycloak('/keycloak.json');
document.getElementById('LoadingScreen').style.display = 'block';
keycloak.init({onLoad: 'login-required', flow: 'implicit'}).success(function () {
console.debug('Handlers::init()', 'login response');
if (keycloak.resourceAccess.osjs === undefined) {
callback('Accès non autorisé - Permission manquante', false);
}
console.debug('Handlers::init()', 'store token');
localStorage.setItem('token', keycloak.token);
self.login(function (error, result) {
self.onLogin(result, function () {
callback();
});
});
}).error(function (error) {
alert(error);
});
};
/////////////////////////////////////////////////////////////////////////////
// EXPORTS
/////////////////////////////////////////////////////////////////////////////
OSjs.Auth = OSjs.Auth || {};
OSjs.Auth.keycloak = KeycloakAuthenticator;
})(OSjs.API, OSjs.Utils, OSjs.Core.Authenticator);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment