<commit_before>/*!
 * Copyright 2014 Apereo Foundation (AF) 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://opensource.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.
 */

define(['exports', 'jquery', 'oae.api.config', 'oae.api.i18n', 'oae.api.user', 'oae.api.util'], function(exports, $, configAPI, i18nAPI, userAPI, utilAPI) {

    var STRATEGY_CAS = exports.STRATEGY_CAS = 'cas';
    var STRATEGY_FACEBOOK = exports.STRATEGY_FACEBOOK = 'facebook';
    var STRATEGY_GOOGLE = exports.STRATEGY_GOOGLE = 'google';
    var STRATEGY_GOOGLE_APPS = exports.STRATEGY_GOOGLE_APPS = 'googleApps';
    var STRATEGY_LDAP = exports.STRATEGY_LDAP = 'ldap';
    var STRATEGY_LOCAL = exports.STRATEGY_LOCAL = 'local';
    var STRATEGY_SHIBBOLETH = exports.STRATEGY_SHIBBOLETH = 'shibboleth';
    var STRATEGY_TWITTER = exports.STRATEGY_TWITTER = 'twitter';

    // Classify all known authentication strategies
    var STRATEGIES_LOCAL = [STRATEGY_LOCAL, STRATEGY_LDAP];
    var STRATEGIES_EXTERNAL = [
        STRATEGY_CAS,
        STRATEGY_FACEBOOK,
        STRATEGY_GOOGLE,
        STRATEGY_GOOGLE_APPS,
        STRATEGY_SHIBBOLETH,
        STRATEGY_TWITTER
    ];
    var STRATEGIES_INSTITUTIONAL = [
        STRATEGY_CAS,
        STRATEGY_GOOGLE_APPS,
        STRATEGY_SHIBBOLETH
    ];

    /**
     * Use the known authentication strategies to determine some important characteristics about how
     * to offer a user their authentication method
     *
     * @param  {String}     [contextLabel]                                      Specifies in which context the strategy info is being requested. Either "SIGN_IN" or "SIGN_UP" (Default: SIGN_IN)
     * @return {Object}     authStrategyInfo                                    Authentication strategy information
     * @return {Object}     authStrategyInfo.enabledStrategies                  All the enabled strategies keyed by strategy id
     * @return {Object}     authStrategyInfo.enabledLocalStrategy               The single local strategy that is enabled. There should not be 2
     * @return {Object}     authStrategyInfo.enabledExternalStrategies          All the enabled external strategies keyed by strategy id
     * @return {Object}     authStrategyInfo.enabledInstitutionalStrategies     All the enabled institutional strategies keyed by strategy id
     * @return {Boolean}    authStrategyInfo.hasLocalAuth                       True if there is at least one local (e.g., username and password, ldap) authentication method enabled
     * @return {Boolean}    authStrategyInfo.hasExternalAuth                    True if there is at least one external (e.g., cas, shibboleth, twitter, etc...) authentication method enabled
     * @return {Boolean}    authStrategyInfo.hasInstitutionalAuth               True if there is at least one institutional (e.g., shibboleth, cas, google apps) authentication method enabled
     * @return {Boolean}    authStrategyInfo.hasSingleExternalAuth              True if there is only one authentication method enabled and it is external
     * @return {Boolean}    authStrategyInfo.hasSingleInstitutionalAuth         True if there is only one authentication method enabled and it is institutional
     */
    var getStrategyInfo = exports.getStrategyInfo = function(contextLabel) {
        contextLabel = contextLabel || 'SIGN_IN';

        var enabledStrategies = getEnabledStrategies(contextLabel);
        var enabledStrategyNames = _.keys(enabledStrategies);

        var hasLocalAuth = (!_.chain(enabledStrategyNames).intersection(STRATEGIES_LOCAL).isEmpty().value());
        var hasExternalAuth = (!_.chain(enabledStrategyNames).intersection(STRATEGIES_EXTERNAL).isEmpty().value());
        var hasInstitutionalAuth = (!_.chain(enabledStrategyNames).intersection(STRATEGIES_INSTITUTIONAL).isEmpty().value());

        var hasSingleAuth = (_.size(enabledStrategyNames) === 1);
        var hasSingleExternalAuth = (hasSingleAuth && hasExternalAuth);
        var hasSingleInstitutionalAuth = (hasSingleAuth && hasInstitutionalAuth);

        return {
            'allowAccountCreation': configAPI.getValue('oae-authentication', STRATEGY_LOCAL, 'allowAccountCreation'),
            'enabledStrategies': enabledStrategies,
            'enabledLocalStrategy': _.chain(enabledStrategies).pick(STRATEGIES_LOCAL).values().first().value(),
            'enabledExternalStrategies': _.pick(enabledStrategies, STRATEGIES_EXTERNAL),
            'enabledInstitutionalStrategies': _.pick(enabledStrategies, STRATEGIES_INSTITUTIONAL),
            'hasLocalAuth': hasLocalAuth,
            'hasExternalAuth': hasExternalAuth,
            'hasInstitutionalAuth': hasInstitutionalAuth,
            'hasSingleExternalAuth': hasSingleExternalAuth,
            'hasSingleInstitutionalAuth': hasSingleInstitutionalAuth
        };
    };

    /*!
     * Between the different context label and authentication strategy permutations, we have the
     * following possible permutations of message keys:
     *
     *  * __MSG__SIGN_IN_WITH_STRATEGY
     *  * __MSG__SIGN_IN_WITH_FACEBOOK
     *  * __MSG__SIGN_IN_WITH_GOOGLE
     *  * __MSG__SIGN_IN_WITH_TWITTER
     *  * __MSG__SIGN_UP_WITH_STRATEGY
     *  * __MSG__SIGN_UP_WITH_FACEBOOK
     *  * __MSG__SIGN_UP_WITH_GOOGLE
     *  * __MSG__SIGN_UP_WITH_TWITTER
     */

    var strategyName = function(strategyId, contextLabel) {
        var translatedName = i18nAPI.translate('__MSG__' + contextLabel + '_WITH_STRATEGY__', null, {
            'strategyName': configAPI.getValue('oae-authentication', strategyId, 'name')
        });
        return translatedName;
    };

    /**
     * Get the list of all enabled authentication strategies for the current tenant
     *
     * @param  {String}     [contextLabel]  Specifies in which context the strategy info is being requested. Either "SIGN_IN" or "SIGN_UP" (Default: SIGN_IN)
     * @return {Object}                     List of all enabled authentication strategies for the current tenant keyed by authentication strategy id. Each enabled authentication strategy will contain a `url` property with the URL to which to POST to initiate the authentication process for that strategy and a `name` property with the custom configured name for that strategy
     */
    var getEnabledStrategies = exports.getEnabledStrategies = function(contextLabel) {
        var enabledStrategies = {};
<commit_msg>(InvitingGuests) Default contextLabel on lower level<commit_after>/*!
 * Copyright 2014 Apereo Foundation (AF) 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://opensource.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.
 */

define(['exports', 'jquery', 'oae.api.config', 'oae.api.i18n', 'oae.api.user', 'oae.api.util'], function(exports, $, configAPI, i18nAPI, userAPI, utilAPI) {

    var STRATEGY_CAS = exports.STRATEGY_CAS = 'cas';
    var STRATEGY_FACEBOOK = exports.STRATEGY_FACEBOOK = 'facebook';
    var STRATEGY_GOOGLE = exports.STRATEGY_GOOGLE = 'google';
    var STRATEGY_GOOGLE_APPS = exports.STRATEGY_GOOGLE_APPS = 'googleApps';
    var STRATEGY_LDAP = exports.STRATEGY_LDAP = 'ldap';
    var STRATEGY_LOCAL = exports.STRATEGY_LOCAL = 'local';
    var STRATEGY_SHIBBOLETH = exports.STRATEGY_SHIBBOLETH = 'shibboleth';
    var STRATEGY_TWITTER = exports.STRATEGY_TWITTER = 'twitter';

    // Classify all known authentication strategies
    var STRATEGIES_LOCAL = [STRATEGY_LOCAL, STRATEGY_LDAP];
    var STRATEGIES_EXTERNAL = [
        STRATEGY_CAS,
        STRATEGY_FACEBOOK,
        STRATEGY_GOOGLE,
        STRATEGY_GOOGLE_APPS,
        STRATEGY_SHIBBOLETH,
        STRATEGY_TWITTER
    ];
    var STRATEGIES_INSTITUTIONAL = [
        STRATEGY_CAS,
        STRATEGY_GOOGLE_APPS,
        STRATEGY_SHIBBOLETH
    ];

    /**
     * Use the known authentication strategies to determine some important characteristics about how
     * to offer a user their authentication method
     *
     * @param  {String}     [contextLabel]                                      Specifies in which context the strategy info is being requested. Either "SIGN_IN" or "SIGN_UP" (Default: SIGN_IN)
     * @return {Object}     authStrategyInfo                                    Authentication strategy information
     * @return {Object}     authStrategyInfo.enabledStrategies                  All the enabled strategies keyed by strategy id
     * @return {Object}     authStrategyInfo.enabledLocalStrategy               The single local strategy that is enabled. There should not be 2
     * @return {Object}     authStrategyInfo.enabledExternalStrategies          All the enabled external strategies keyed by strategy id
     * @return {Object}     authStrategyInfo.enabledInstitutionalStrategies     All the enabled institutional strategies keyed by strategy id
     * @return {Boolean}    authStrategyInfo.hasLocalAuth                       True if there is at least one local (e.g., username and password, ldap) authentication method enabled
     * @return {Boolean}    authStrategyInfo.hasExternalAuth                    True if there is at least one external (e.g., cas, shibboleth, twitter, etc...) authentication method enabled
     * @return {Boolean}    authStrategyInfo.hasInstitutionalAuth               True if there is at least one institutional (e.g., shibboleth, cas, google apps) authentication method enabled
     * @return {Boolean}    authStrategyInfo.hasSingleExternalAuth              True if there is only one authentication method enabled and it is external
     * @return {Boolean}    authStrategyInfo.hasSingleInstitutionalAuth         True if there is only one authentication method enabled and it is institutional
     */
    var getStrategyInfo = exports.getStrategyInfo = function(contextLabel) {
        var enabledStrategies = getEnabledStrategies(contextLabel);
        var enabledStrategyNames = _.keys(enabledStrategies);

        var hasLocalAuth = (!_.chain(enabledStrategyNames).intersection(STRATEGIES_LOCAL).isEmpty().value());
        var hasExternalAuth = (!_.chain(enabledStrategyNames).intersection(STRATEGIES_EXTERNAL).isEmpty().value());
        var hasInstitutionalAuth = (!_.chain(enabledStrategyNames).intersection(STRATEGIES_INSTITUTIONAL).isEmpty().value());

        var hasSingleAuth = (_.size(enabledStrategyNames) === 1);
        var hasSingleExternalAuth = (hasSingleAuth && hasExternalAuth);
        var hasSingleInstitutionalAuth = (hasSingleAuth && hasInstitutionalAuth);

        return {
            'allowAccountCreation': configAPI.getValue('oae-authentication', STRATEGY_LOCAL, 'allowAccountCreation'),
            'enabledStrategies': enabledStrategies,
            'enabledLocalStrategy': _.chain(enabledStrategies).pick(STRATEGIES_LOCAL).values().first().value(),
            'enabledExternalStrategies': _.pick(enabledStrategies, STRATEGIES_EXTERNAL),
            'enabledInstitutionalStrategies': _.pick(enabledStrategies, STRATEGIES_INSTITUTIONAL),
            'hasLocalAuth': hasLocalAuth,
            'hasExternalAuth': hasExternalAuth,
            'hasInstitutionalAuth': hasInstitutionalAuth,
            'hasSingleExternalAuth': hasSingleExternalAuth,
            'hasSingleInstitutionalAuth': hasSingleInstitutionalAuth
        };
    };

    /*!
     * Between the different context label and authentication strategy permutations, we have the
     * following possible permutations of message keys:
     *
     *  * __MSG__SIGN_IN_WITH_STRATEGY
     *  * __MSG__SIGN_IN_WITH_FACEBOOK
     *  * __MSG__SIGN_IN_WITH_GOOGLE
     *  * __MSG__SIGN_IN_WITH_TWITTER
     *  * __MSG__SIGN_UP_WITH_STRATEGY
     *  * __MSG__SIGN_UP_WITH_FACEBOOK
     *  * __MSG__SIGN_UP_WITH_GOOGLE
     *  * __MSG__SIGN_UP_WITH_TWITTER
     */

    var strategyName = function(strategyId, contextLabel) {
        var translatedName = i18nAPI.translate('__MSG__' + contextLabel + '_WITH_STRATEGY__', null, {
            'strategyName': configAPI.getValue('oae-authentication', strategyId, 'name')
        });
        return translatedName;
    };

    /**
     * Get the list of all enabled authentication strategies for the current tenant
     *
     * @param  {String}     [contextLabel]  Specifies in which context the strategy info is being requested. Either "SIGN_IN" or "SIGN_UP" (Default: SIGN_IN)
     * @return {Object}                     List of all enabled authentication strategies for the current tenant keyed by authentication strategy id. Each enabled authentication strategy will contain a `url` property with the URL to which to POST to initiate the authentication process for that strategy and a `name` property with the custom configured name for that strategy
     */
    var getEnabledStrategies = exports.getEnabledStrategies = function(contextLabel) {
        contextLabel = contextLabel || 'SIGN_IN';

        var enabledStrategies = {};

{'fraction_non_alphanumeric': 0.07636708768343432, 'fraction_numerical': 0.002336278017084033, 'mean_word_length': 3.296737766624843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1201203', 'n_tokens_mistral': 3793, 'n_tokens_neox': 3650, 'n_words': 1053}