<commit_before>/*!
 * Copyright 2012 Sakai Foundation (SF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var util = require('util');

var AuthzUtil = require('oae-authz/lib/util');
var Config = require('oae-config').config('oae-content');
var Signature = require('oae-util/lib/signature');

/**
 * If a URI is known a download link can be generated which doesn't go trough to Cassandra.
 *
 * @param  {Context}    ctx             Standard context object, representing the currently logged in user and its tenant
 * @param  {String}     tenantAlias     The alias of the tenant where the content that the uri points to originated from.
 * @param  {String}     uri             The content uri
 * @return {String}                     The url that can be used in a browser to download the file.
 */
var getDownloadUrlForUri = module.exports.getDownloadUrlForUri = function(ctx, tenantAlias, uri) {
    var principalId = (ctx.user()) ? ctx.user().principalId : '';
    var body = principalId + ':' + uri;
    var settings = _getPreviewsExpireSettings(tenantAlias);
    var sig = Signature.createRollingSignature(tenantAlias, settings.duration, settings.offset, body);
    return util.format('/api/download/signed?uri=%s&signature=%s&expires=%s', uri, sig.signature, sig.expires);
};

/**
 * Creates the appropriate signature for a piece of content.
 *
 * @param  {Context}    ctx             Standard context object, representing the currently logged in user and its tenant
 * @param  {String}     contentId       The ID of the piece of content that needs to be signed.
 * @param  {Number}     lastModified    The timestamp when the content was last modified. This will ensure that the signature changes if a new version is uploaded.
 * @return {Object}                     A signature.
 */
var signContent = module.exports.signContent = function(ctx, contentId, lastModified) {
    var tenantAlias = (contentId) ? AuthzUtil.getResourceFromId(contentId).tenantAlias : '';
    var principalId = (ctx && ctx.user()) ? ctx.user().id : '';
    var settings = _getPreviewsExpireSettings(tenantAlias);
    var body = principalId + ':' + contentId + ':' + lastModified;
    var signature = Signature.createRollingSignature(tenantAlias, settings.duration, settings.offset, body);
    signature.lastModified = lastModified;
    return signature;
};

/**
 * Retrieves the expire settings from the config.
 *
 * @param  {String} tenantAlias The alias of the tenant for which we should retrieve the preview expire settings.
 * @return {Object}             An object that holds a duration and offset to generate a rolling expiring signature.
 * @api private
 */
var _getPreviewsExpireSettings = function(tenantAlias) {
    // These settings will be used to generate rolling signatures,
    // A rolling signature always has a minimum value (the offset) which we set to the `expiration_minimum` config value.
    // A value that is situated in the interval [0, duration] then gets added to calculate the expiration time for the signature.
    // In order to obtain a signature that is valid for an `expiration_maximum` time, we subtract the minimum from the maximum.
    var min = parseInt(Config.getValue(tenantAlias, 'previews', 'expiration_minimum'), 10);
    var max = parseInt(Config.getValue(tenantAlias, 'previews', 'expiration_maximum'), 10);
    var offset = min;
    var duration = max - min;

    // In case the expiration settings are misconfigured,
    // we'll fall back to some hardcoded defaults
    // TODO: This is really a validation issue in the global admin UI
    // and should not happen here.
    if (duration < 0 || offset < 0) {
        duration = 15*60;
        offset = 15*60;
    }
    return {
        'duration': duration,
        'offset': offset
    };
};

<commit_msg>change context to principalid for internal api<commit_after>/*!
 * Copyright 2012 Sakai Foundation (SF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var util = require('util');

var AuthzUtil = require('oae-authz/lib/util');
var Config = require('oae-config').config('oae-content');
var Signature = require('oae-util/lib/signature');

/**
 * If a URI is known a download link can be generated which doesn't go trough to Cassandra.
 *
 * @param  {String}     principalId     The principal that will be using this url
 * @param  {String}     tenantAlias     The alias of the tenant where the content that the uri points to originated from.
 * @param  {String}     uri             The content uri
 * @return {String}                     The url that can be used in a browser to download the file.
 */
var getDownloadUrlForUri = module.exports.getDownloadUrlForUri = function(principalId, tenantAlias, uri) {
    principalId = principalId || '';
    var body = principalId + ':' + uri;
    var settings = _getPreviewsExpireSettings(tenantAlias);
    var sig = Signature.createRollingSignature(tenantAlias, settings.duration, settings.offset, body);
    return util.format('/api/download/signed?uri=%s&signature=%s&expires=%s', uri, sig.signature, sig.expires);
};

/**
 * Creates the appropriate signature for a piece of content.
 *
 * @param  {Context}    ctx             Standard context object, representing the currently logged in user and its tenant
 * @param  {String}     contentId       The ID of the piece of content that needs to be signed.
 * @param  {Number}     lastModified    The timestamp when the content was last modified. This will ensure that the signature changes if a new version is uploaded.
 * @return {Object}                     A signature.
 */
var signContent = module.exports.signContent = function(ctx, contentId, lastModified) {
    var tenantAlias = (contentId) ? AuthzUtil.getResourceFromId(contentId).tenantAlias : '';
    var principalId = (ctx && ctx.user()) ? ctx.user().id : '';
    var settings = _getPreviewsExpireSettings(tenantAlias);
    var body = principalId + ':' + contentId + ':' + lastModified;
    var signature = Signature.createRollingSignature(tenantAlias, settings.duration, settings.offset, body);
    signature.lastModified = lastModified;
    return signature;
};

/**
 * Retrieves the expire settings from the config.
 *
 * @param  {String} tenantAlias The alias of the tenant for which we should retrieve the preview expire settings.
 * @return {Object}             An object that holds a duration and offset to generate a rolling expiring signature.
 * @api private
 */
var _getPreviewsExpireSettings = function(tenantAlias) {
    // These settings will be used to generate rolling signatures,
    // A rolling signature always has a minimum value (the offset) which we set to the `expiration_minimum` config value.
    // A value that is situated in the interval [0, duration] then gets added to calculate the expiration time for the signature.
    // In order to obtain a signature that is valid for an `expiration_maximum` time, we subtract the minimum from the maximum.
    var min = parseInt(Config.getValue(tenantAlias, 'previews', 'expiration_minimum'), 10);
    var max = parseInt(Config.getValue(tenantAlias, 'previews', 'expiration_maximum'), 10);
    var offset = min;
    var duration = max - min;

    // In case the expiration settings are misconfigured,
    // we'll fall back to some hardcoded defaults
    // TODO: This is really a validation issue in the global admin UI
    // and should not happen here.
    if (duration < 0 || offset < 0) {
        duration = 15*60;
        offset = 15*60;
    }
    return {
        'duration': duration,
        'offset': offset
    };
};


{'fraction_non_alphanumeric': 0.09133271202236719, 'fraction_numerical': 0.005358807082945014, 'mean_word_length': 3.3980532786885247, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2315', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2214, 'n_words': 946}