<commit_before>var request     = require('request');
var printf      = require('printf');
var Q           = require('q');
var url         = require('url');
var debug       = require('debug')('node-auth-sdk');

var UserToken = function(apiUrl, clientToken, userId, userPwd) {
    'use strict';

    var tokenKey = 'X-Ftven-User';
    var tokenInfos = null;


    this.getKey = function() {
        return tokenKey;
    };


    this.getObject = function() {
        return tokenInfos;
    };


    this.getValue = function() {
        return printf('id: %s, expire: %s, token: %s', tokenInfos.id, tokenInfos.expireString, tokenInfos.token);
    };


    this.hasToken = function() {
        return tokenInfos !== null;
    };


    this.isExpiredToken = function() {
        return new Date() >= clientToken.expire - 20000; // 20 seconds security
    };


    this.requestToken = function() {
        var deferred = Q.defer();

        debug('Requesting the user token');

        var headers = {};
        headers[clientToken.getKey()] = clientToken.getValue();
        headers[tokenKey] = printf('id: %s, password: %s', userId, userPwd);

        var requestOptions = {
            url: url.resolve(apiUrl, '/user_token'),
            method: 'POST',
            headers: headers
        };

        debug(requestOptions);

        request(requestOptions, function(err, httpResponse, body) {
            
            if (err) {

                debug('Could not retrieve user token');
                debug(err);
                deferred.reject(err);

            } else {

                debug('Response from API code:%d', httpResponse.statusCode);
                debug('Headers:');
                debug(httpResponse.headers);

                if (httpResponse.statusCode !== 204 && httpResponse.statusCode !== 201) {
                    if (body) {
                        try {
                            var data = JSON.parse(body);
                            if (data.code && data.message) {
                                
                                debug(printf('Error %d %s (API User Token)', data.code, data.message));
                                deferred.reject(printf('Error %d %s (API User Token)', data.code, data.message));
                                return;
                            }
                        } catch(e) {}
                    }
                    debug(printf('Error %d (API User Token)', httpResponse.statusCode));
                    deferred.reject(printf('Error %d (API User Token)', httpResponse.statusCode));
                    return;
                }

                if (httpResponse.headers && httpResponse.headers[tokenKey.toLowerCase()]) {

                    // Hey, the token is here!
                    saveToken(httpResponse.headers[tokenKey.toLowerCase()]);
                    debug('User token is ok and saved');
                    deferred.resolve();

                } else {
                    deferred.reject();
                }
            }
        });

        return deferred.promise;
    };


    function saveToken(data) {
        // Ensure the given access token is valid
        if (typeof data !== "string" ||
            /^id: [a-zA-Z0-9_-]+, expire: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{4}, token: [a-zA-Z0-9]+==$/.test(data) !== true) {
            throw "The passed access token is not valid (API User Token)";
        }

        var expireString = data.match(/expire: ([^,]+)/)[1];

        // Extract the data from the access token
        tokenInfos = {
            id: data.match(/id: ([^,]+)/)[1],
            expire: new Date(expireString),
            expireString: expireString,
            token: data.match(/token: ([^,]+)/)[1]
        };
    }

};

module.exports = UserToken;<commit_msg>fix userToken.js for apiUrl with path<commit_after>var request     = require('request');
var printf      = require('printf');
var Q           = require('q');
var url         = require('url');
var debug       = require('debug')('node-auth-sdk');

var UserToken = function(apiUrl, clientToken, userId, userPwd) {
    'use strict';

    var tokenKey = 'X-Ftven-User';
    var tokenInfos = null;


    this.getKey = function() {
        return tokenKey;
    };


    this.getObject = function() {
        return tokenInfos;
    };


    this.getValue = function() {
        return printf('id: %s, expire: %s, token: %s', tokenInfos.id, tokenInfos.expireString, tokenInfos.token);
    };


    this.hasToken = function() {
        return tokenInfos !== null;
    };


    this.isExpiredToken = function() {
        return new Date() >= clientToken.expire - 20000; // 20 seconds security
    };


    this.requestToken = function() {
        var deferred = Q.defer();

        debug('Requesting the user token');

        var headers = {};
        headers[clientToken.getKey()] = clientToken.getValue();
        headers[tokenKey] = printf('id: %s, password: %s', userId, userPwd);

        var requestOptions = {
            url: url.resolve(apiUrl, 'user_token'),
            method: 'POST',
            headers: headers
        };

        debug(requestOptions);

        request(requestOptions, function(err, httpResponse, body) {
            
            if (err) {

                debug('Could not retrieve user token');
                debug(err);
                deferred.reject(err);

            } else {

                debug('Response from API code:%d', httpResponse.statusCode);
                debug('Headers:');
                debug(httpResponse.headers);

                if (httpResponse.statusCode !== 204 && httpResponse.statusCode !== 201) {
                    if (body) {
                        try {
                            var data = JSON.parse(body);
                            if (data.code && data.message) {
                                
                                debug(printf('Error %d %s (API User Token)', data.code, data.message));
                                deferred.reject(printf('Error %d %s (API User Token)', data.code, data.message));
                                return;
                            }
                        } catch(e) {}
                    }
                    debug(printf('Error %d (API User Token)', httpResponse.statusCode));
                    deferred.reject(printf('Error %d (API User Token)', httpResponse.statusCode));
                    return;
                }

                if (httpResponse.headers && httpResponse.headers[tokenKey.toLowerCase()]) {

                    // Hey, the token is here!
                    saveToken(httpResponse.headers[tokenKey.toLowerCase()]);
                    debug('User token is ok and saved');
                    deferred.resolve();

                } else {
                    deferred.reject();
                }
            }
        });

        return deferred.promise;
    };


    function saveToken(data) {
        // Ensure the given access token is valid
        if (typeof data !== "string" ||
            /^id: [a-zA-Z0-9_-]+, expire: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{4}, token: [a-zA-Z0-9]+==$/.test(data) !== true) {
            throw "The passed access token is not valid (API User Token)";
        }

        var expireString = data.match(/expire: ([^,]+)/)[1];

        // Extract the data from the access token
        tokenInfos = {
            id: data.match(/id: ([^,]+)/)[1],
            expire: new Date(expireString),
            expireString: expireString,
            token: data.match(/token: ([^,]+)/)[1]
        };
    }

};

module.exports = UserToken;

{'fraction_non_alphanumeric': 0.1373885684320923, 'fraction_numerical': 0.007079181961195595, 'mean_word_length': 1.5219834710743803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1950792', 'n_tokens_mistral': 2147, 'n_tokens_neox': 1999, 'n_words': 476}