Skip to content

Commit

Permalink
Photo eidas parser
Browse files Browse the repository at this point in the history
  • Loading branch information
apozohue10 committed Mar 13, 2019
1 parent 565a37a commit 753becd
Show file tree
Hide file tree
Showing 7 changed files with 1,736 additions and 1,640 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if (config.debug) {
app.disable('x-powered-by');

// Parse request
app.use(body_parser.json());
app.use(body_parser.json({ limit: '5mb' }));
app.use(body_parser.urlencoded());

// CORS Enable
Expand Down
100 changes: 59 additions & 41 deletions controllers/saml2/saml2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const debug = require('debug')('idm:saml2_controller');
const exec = require('child_process').exec;
const saml2 = require('../../lib/saml2.js');
const image = require('../../lib/image.js');

const config_attributes = require('../../etc/eidas/requested_attributes.json');
const config_attributes_natural = Object.keys(config_attributes.NaturalPerson);
Expand Down Expand Up @@ -376,50 +377,67 @@ exports.saml2_application_login = function(req, res, next) {

// Create a user when Saml flow has already finished
function create_user(name_id, new_eidas_profile) {
return models.user
.findOne({
where: { eidas_id: name_id },
})
.then(function(user) {
if (user) {
// Update de eidas profile
const actual_eidas_profile_keys = Object.keys(user.extra.eidas_profile);
const new_eidas_profile_keys = Object.keys(new_eidas_profile);

const difference = new_eidas_profile_keys.filter(
x => !actual_eidas_profile_keys.includes(x)
);
const new_attributes = user.extra.eidas_profile;

for (let i = 0; i < difference.length; i++) {
new_attributes[difference[i]] = new_eidas_profile[difference[i]];
}

const user_extra = user.extra;
Object.assign(user_extra.eidas_profile, new_attributes);
user.extra = user_extra;
return user.save({
fields: ['extra'],
});
let image_name = 'default';
return image
.toJpg(new_eidas_profile.CurrentPhoto, 'public/img/users')
.then(function(file_name) {
if (file_name) {
image_name = file_name;
delete new_eidas_profile.CurrentPhoto;
}

return models.user
.build({
username:
new_eidas_profile.FirstName + ' ' + new_eidas_profile.FamilyName,
eidas_id: name_id,
extra: { eidas_profile: new_eidas_profile },
enabled: true,
.findOne({
where: { eidas_id: name_id },
})
.save();
})
.then(function(user) {
debug(user);
return user;
})
.catch(function(error) {
debug('Error', error);
return Promise.reject(error);
.then(function(user) {
if (user) {
// Update de eidas profile
const actual_eidas_profile_keys = Object.keys(
user.extra.eidas_profile
);
const new_eidas_profile_keys = Object.keys(new_eidas_profile);

const difference = new_eidas_profile_keys.filter(
x => !actual_eidas_profile_keys.includes(x)
);
const new_attributes = user.extra.eidas_profile;

for (let i = 0; i < difference.length; i++) {
new_attributes[difference[i]] = new_eidas_profile[difference[i]];
}
const user_extra = user.extra;
Object.assign(user_extra.eidas_profile, new_attributes);
user.extra = user_extra;
user.email = new_eidas_profile.Email
? new_eidas_profile.Email
: user.email;
user.image = image_name !== 'default' ? image_name : user.image;
return user.save({
fields: ['extra', 'email', 'image'],
});
}

return models.user
.build({
username:
new_eidas_profile.FirstName +
' ' +
new_eidas_profile.FamilyName,
eidas_id: name_id,
email: new_eidas_profile.Email ? new_eidas_profile.Email : null,
image: image_name !== 'default' ? image_name : 'default',
extra: { eidas_profile: new_eidas_profile },
enabled: true,
})
.save();
})
.then(function(user) {
return user;
})
.catch(function(error) {
debug('Error', error);
return Promise.reject(error);
});
});
}

Expand Down
24 changes: 24 additions & 0 deletions lib/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const fs = require('fs');
const Jimp = require('jimp');
const mime = require('mime-types');
const debug = require('debug')('idm:image');
const base64_img = require('base64-img');
const uuid = require('uuid');

const types = ['jpg', 'jpeg', 'png'];

Expand Down Expand Up @@ -48,3 +50,25 @@ exports.crop = function(image_path, crop_points) {
return Promise.reject('Fail image crop');
});
};

// TODO: ¿Make this async?
exports.toJpg = function(image64, image_path) {
const image_name = uuid.v4();
return new Promise(function(resolve, reject) {
if (image64) {
base64_img.img(
'data:image/png;base64,' + image64,
image_path,
image_name,
function(err) {
if (err) {
reject(err);
}
resolve(image_name + '.png');
}
);
} else {
resolve();
}
});
};
15 changes: 13 additions & 2 deletions models/model_oauth_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const oauth2 = require('../config').oauth2;
const _ = require('lodash');
const jsonwebtoken = require('jsonwebtoken');
const debug = require('debug')('idm:oauth2-model_oauth_server');
const config_authzforce = require('./../config.js').authorization.authzforce;
const config_oauth2 = require('./../config.js').oauth2;
const config = require('./../config.js');
const config_authzforce = config.authorization.authzforce;
const config_oauth2 = config.oauth2;
const config_cors = config.cors;
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

Expand Down Expand Up @@ -38,6 +40,7 @@ function getAccessToken(bearerToken) {
'username',
'email',
'gravatar',
'image',
'extra',
'eidas_id',
],
Expand Down Expand Up @@ -130,6 +133,7 @@ function getIdentity(id, password, oauth_client_id) {
'id',
'username',
'gravatar',
'image',
'email',
'salt',
'password',
Expand Down Expand Up @@ -588,6 +592,13 @@ function create_oauth_response(
user_info.email = identity.email;
user_info.id = identity.id;

if (config.cors && config_cors.enabled) {
user_info.image =
identity.image !== 'default'
? config.host + '/img/users/' + identity.image
: '';
}

if (identity.eidas_id) {
user_info.eidas_profile = identity.extra.eidas_profile;
}
Expand Down
Loading

0 comments on commit 753becd

Please sign in to comment.