<commit_before>"use strict";
const crypto = require('crypto');
const builder = require('botbuilder');
const passport = require('passport');
const dialogs_1 = require("./dialogs");
exports.AuthDialog = dialogs_1.AuthDialog;
const resumption_1 = require("./resumption");
exports.CookieResumption = resumption_1.CookieResumption;
const DIALOG_LIBRARY = "botauth";
const DIALOG_ID = "auth";
const DIALOG_FULLNAME = `${DIALOG_LIBRARY}:${DIALOG_ID}`;
const defaultOptions = {
    basePath: "botauth",
    resumption: null,
    secret: null,
    baseUrl: null
};
class BotAuthenticator {
    constructor(server, bot, options) {
        this.server = server;
        this.bot = bot;
        this.options = options;
        if (!bot || !server) {
            throw new Error("Autenticator constructor failed because required parameters were null/undefined");
        }
        this.options = Object.assign({}, defaultOptions, options);
        if (!this.options.secret) {
            throw new Error("options.secret can not be null");
        }
        if (!this.options.resumption) {
            this.options.resumption = new resumption_1.CookieResumption(15 * 60, this.options.secret);
        }
        this.server.use(passport.initialize());
        this.server.get(`/${this.options.basePath}/:providerId`, this.options.resumption.persistHandler(), this.passport_redirect);
        this.server.get(`/${this.options.basePath}/:providerId/callback`, this.passport_callback, this.options.resumption.restoreHandler(), this.credential_callback.bind(this));
        this.bot.set("persistConversationData", true);
        this.bot.set("persistUserData", true);
        let lib = new builder.Library(DIALOG_LIBRARY);
        lib.dialog(DIALOG_ID, new dialogs_1.AuthDialog({ secret: this.options.secret }));
        this.bot.library(lib);
    }
    provider(name, factory) {
        let s = factory({
            callbackURL: this.callbackUrl(name)
        });
        passport.use(name, s);
        return this;
    }
    authenticate(providerId, options) {
        let authSteps = [
                (session, args, skip) => {
                let user = this.profile(session, providerId);
                if (user) {
                    skip({ response: user });
                }
                else {
                    let cxt = new Buffer(JSON.stringify(session.message.address)).toString('base64');
                    session.beginDialog(DIALOG_FULLNAME, {
                        providerId: providerId,
                        buttonUrl: this.authUrl(providerId, cxt)
                    });
                }
            },
                (session, args, skip) => {
                if (args) {
                    if (args.resumed === builder.ResumeReason.completed || args.resumed === builder.ResumeReason.forward) {
                        skip({ response: args.response });
                        return;
                    }
                    else if (args.resumed === builder.ResumeReason.back || args.resumed === builder.ResumeReason.canceled || args.resumed === builder.ResumeReason.notCompleted) {
                        session.endConversation("auth failed, ending conversation");
                        return;
                    }
                }
            }
        ];
        return authSteps;
    }
    profile(session, providerId) {
        if (session && session.userData && session.userData.botauth && session.userData.botauth.user && session.userData.botauth.user.hasOwnProperty(providerId)) {
            let encryptedProfile = session.userData.botauth.user[providerId];
            let decipher = crypto.createDecipher("aes192", this.options.secret);
            let json = decipher.update(encryptedProfile, 'base64', 'utf8') + decipher.final('utf8');
            return JSON.parse(json);
        }
        else {
            return null;
        }
    }
    logout(session, providerId) {
        if (session && session.userData && session.userData.botauth && session.userData.botauth.user && session.userData.botauth.user.hasOwnProperty(providerId)) {
            delete session.userData.botauth.user[providerId];
            session.save();
        }
    }
    callbackUrl(providerName) {
        return `${this.options.baseUrl}/${this.options.basePath}/${providerName}/callback`;
    }
    authUrl(providerName, state) {
        return `${this.options.baseUrl}/${this.options.basePath}/${providerName}?state=${encodeURIComponent(state)}`;
    }
    passport_redirect(req, res, next) {
        let providerId = req.params.providerId;
        return passport.authenticate(providerId, { session: false })(req, res, next);
    }
    passport_callback(req, res, next) {
        let providerId = req.params.providerId;
        return passport.authenticate(providerId, { session: false })(req, res, next);
    }
    credential_callback(req, res, next) {
        let providerId = req.params.providerId;
        let addr = JSON.parse(new Buffer(req.locals.resumption, "base64").toString());
        ;
        if (!addr) {<commit_msg>added option to use session<commit_after>"use strict";
const crypto = require('crypto');
const builder = require('botbuilder');
const passport = require('passport');
const dialogs_1 = require("./dialogs");
exports.AuthDialog = dialogs_1.AuthDialog;
const resumption_1 = require("./resumption");
exports.CookieResumption = resumption_1.CookieResumption;
const DIALOG_LIBRARY = "botauth";
const DIALOG_ID = "auth";
const DIALOG_FULLNAME = `${DIALOG_LIBRARY}:${DIALOG_ID}`;
const defaultOptions = {
    basePath: "botauth",
    resumption: null,
    secret: null,
    baseUrl: null,
    session: false
};
class BotAuthenticator {
    constructor(server, bot, options) {
        this.server = server;
        this.bot = bot;
        this.options = options;
        if (!bot || !server) {
            throw new Error("Autenticator constructor failed because required parameters were null/undefined");
        }
        this.options = Object.assign({}, defaultOptions, options);
        if (!this.options.secret) {
            throw new Error("options.secret can not be null");
        }
        if (!this.options.resumption) {
            this.options.resumption = new resumption_1.CookieResumption(15 * 60, this.options.secret);
        }
        this.server.use(passport.initialize());
        if (this.options.session) {
            passport.serializeUser((user, done) => {
                done(null, user);
            });
            passport.deserializeUser((userId, done) => {
                done(null, userId);
            });
        }
        this.server.get(`/${this.options.basePath}/:providerId`, this.options.resumption.persistHandler(), this.passport_redirect.bind(this));
        this.server.get(`/${this.options.basePath}/:providerId/callback`, this.passport_callback.bind(this), this.options.resumption.restoreHandler(), this.credential_callback.bind(this));
        this.bot.set("persistConversationData", true);
        this.bot.set("persistUserData", true);
        let lib = new builder.Library(DIALOG_LIBRARY);
        lib.dialog(DIALOG_ID, new dialogs_1.AuthDialog({ secret: this.options.secret }));
        this.bot.library(lib);
    }
    provider(name, factory) {
        let s = factory({
            callbackURL: this.callbackUrl(name)
        });
        passport.use(name, s);
        return this;
    }
    authenticate(providerId, options) {
        let authSteps = [
                (session, args, skip) => {
                let user = this.profile(session, providerId);
                if (user) {
                    skip({ response: user });
                }
                else {
                    let cxt = new Buffer(JSON.stringify(session.message.address)).toString('base64');
                    session.beginDialog(DIALOG_FULLNAME, {
                        providerId: providerId,
                        buttonUrl: this.authUrl(providerId, cxt)
                    });
                }
            },
                (session, args, skip) => {
                if (args) {
                    if (args.resumed === builder.ResumeReason.completed || args.resumed === builder.ResumeReason.forward) {
                        skip({ response: args.response });
                        return;
                    }
                    else if (args.resumed === builder.ResumeReason.back || args.resumed === builder.ResumeReason.canceled || args.resumed === builder.ResumeReason.notCompleted) {
                        session.endConversation("auth failed, ending conversation");
                        return;
                    }
                }
            }
        ];
        return authSteps;
    }
    profile(session, providerId) {
        if (session && session.userData && session.userData.botauth && session.userData.botauth.user && session.userData.botauth.user.hasOwnProperty(providerId)) {
            let encryptedProfile = session.userData.botauth.user[providerId];
            let decipher = crypto.createDecipher("aes192", this.options.secret);
            let json = decipher.update(encryptedProfile, 'base64', 'utf8') + decipher.final('utf8');
            return JSON.parse(json);
        }
        else {
            return null;
        }
    }
    logout(session, providerId) {
        if (session && session.userData && session.userData.botauth && session.userData.botauth.user && session.userData.botauth.user.hasOwnProperty(providerId)) {
            delete session.userData.botauth.user[providerId];
            session.save();
        }
    }
    callbackUrl(providerName) {
        return `${this.options.baseUrl}/${this.options.basePath}/${providerName}/callback`;
    }
    authUrl(providerName, state) {
        return `${this.options.baseUrl}/${this.options.basePath}/${providerName}?state=${encodeURIComponent(state)}`;
    }
    passport_redirect(req, res, next) {
        let providerId = req.params.providerId;
        return passport.authenticate(providerId, { session: this.options.session })(req, res, next);
    }
    passport_callback(req, res, next) {
        let providerId = req.params.providerId;
        return passport.authenticate(providerId, { session: this.options.session })(req, res, next);
    }
    credential_callback(req, res, next) {
        let providerId = req.params.providerId;
        let addr = JSON.parse(new Buffer(req.locals.resumption, "base64").toString());
        ;
        if (!addr) {
{'fraction_non_alphanumeric': 0.12755785044941673, 'fraction_numerical': 0.004016064257028112, 'mean_word_length': 2.267416432364886, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5671826', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2847, 'n_words': 528}