2017-03-16 11:46:09 +00:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
import { Promise as EmberPromise } from 'rsvp';
|
|
|
|
import netUtil from '../utils/net';
|
2018-09-04 17:19:26 +01:00
|
|
|
import Service, { inject as service } from '@ember/service';
|
2017-03-16 11:46:09 +00:00
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
export default Service.extend({
|
2017-03-16 11:46:09 +00:00
|
|
|
sessionService: service('session'),
|
|
|
|
ajax: service(),
|
|
|
|
appMeta: service(),
|
|
|
|
keycloak: null,
|
2018-01-22 10:31:03 +00:00
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.config = {};
|
|
|
|
},
|
2017-03-20 13:54:53 +00:00
|
|
|
|
|
|
|
boot() {
|
2017-11-16 13:28:05 +00:00
|
|
|
return new EmberPromise((resolve, reject) => {
|
2019-03-03 13:10:04 +00:00
|
|
|
if (!_.isUndefined(this.get('keycloak')) && !_.isNull(this.get('keycloak')) ) {
|
2017-03-20 17:56:15 +00:00
|
|
|
resolve(this.get('keycloak'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let keycloak = new Keycloak(JSON.parse(this.get('appMeta.authConfig')));
|
|
|
|
this.set('keycloak', keycloak);
|
|
|
|
|
2017-03-28 10:25:32 +01:00
|
|
|
// keycloak.onTokenExpired = function () {
|
|
|
|
// keycloak.clearToken();
|
|
|
|
// };
|
2017-03-20 17:56:15 +00:00
|
|
|
|
2017-03-28 10:25:32 +01:00
|
|
|
// keycloak.onAuthRefreshError = function () {
|
|
|
|
// keycloak.clearToken();
|
|
|
|
// };
|
2017-03-20 17:56:15 +00:00
|
|
|
|
2017-03-20 13:54:53 +00:00
|
|
|
this.get('keycloak').init().success(() => {
|
2017-03-16 11:46:09 +00:00
|
|
|
resolve(this.get('keycloak'));
|
|
|
|
}).error((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
login() {
|
2017-11-16 13:28:05 +00:00
|
|
|
return new EmberPromise((resolve, reject) => {
|
2017-03-20 17:56:15 +00:00
|
|
|
this.boot().then((keycloak) => {
|
|
|
|
let url = netUtil.getAppUrl(netUtil.getSubdomain()) + '/auth/keycloak?mode=login';
|
|
|
|
|
|
|
|
keycloak.login({redirectUri: url}).success(() => {
|
2017-03-20 13:54:53 +00:00
|
|
|
return resolve();
|
|
|
|
}).error(() => {
|
|
|
|
return reject(new Error('login failed'));
|
2018-09-04 17:19:26 +01:00
|
|
|
});
|
2017-03-20 13:54:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
logout() {
|
2017-11-16 13:28:05 +00:00
|
|
|
return new EmberPromise((resolve, reject) => {
|
2017-03-20 17:56:15 +00:00
|
|
|
this.boot().then((keycloak) => {
|
|
|
|
keycloak.logout(JSON.parse(this.get('appMeta.authConfig'))).success(() => {
|
2017-03-20 13:54:53 +00:00
|
|
|
this.get('keycloak').clearToken();
|
|
|
|
resolve();
|
|
|
|
}).error((error) => {
|
|
|
|
this.get('keycloak').clearToken();
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
2017-03-16 11:46:09 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-03-20 17:56:15 +00:00
|
|
|
fetchProfile() {
|
2017-11-16 13:28:05 +00:00
|
|
|
return new EmberPromise((resolve, reject) => {
|
2017-03-20 17:56:15 +00:00
|
|
|
this.boot().then((keycloak) => {
|
|
|
|
keycloak.loadUserProfile().success((profile) => {
|
|
|
|
resolve(profile);
|
|
|
|
}).error((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
2017-03-16 11:46:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-03-20 17:56:15 +00:00
|
|
|
mapProfile(profile) {
|
2017-03-16 11:46:09 +00:00
|
|
|
return {
|
2017-03-17 08:46:33 +00:00
|
|
|
domain: '',
|
2017-03-20 17:56:15 +00:00
|
|
|
token: this.get('keycloak').token,
|
2019-03-03 13:10:04 +00:00
|
|
|
remoteId: _.isNull(profile.id) || _.isUndefined(profile.id) ? profile.email: profile.id,
|
|
|
|
email: _.isNull(profile.email) || _.isUndefined(profile.email) ? '': profile.email,
|
|
|
|
username: _.isNull(profile.username) || _.isUndefined(profile.username) ? '': profile.username,
|
|
|
|
firstname: _.isNull(profile.firstName) || _.isUndefined(profile.firstName) ? profile.username: profile.firstName,
|
|
|
|
lastname: _.isNull(profile.lastName) || _.isUndefined(profile.lastName) ? profile.username: profile.lastName,
|
|
|
|
enabled: _.isNull(profile.enabled) || _.isUndefined(profile.enabled) ? true: profile.enabled
|
2017-03-16 11:46:09 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|