<commit_before>var request = require('request')
  , Principal = require('./principal')
  , Session = require('./session')
  , ursa = require('ursa');

/**
 * A Service is a Nitrogen endpoint that a Session is established against for interactions with
 * it.  How that Session is established depends on the Principal type. For device and service
 * principals, authentication is done based on a shared secret between the device and the
 * service.  For user principals, authentication is via email and password. Sessions can also
 * be resumed if the principal has stored an authToken. The Service object is also responsible
 * for querying the headwaiter endpoint to fetch the service endpoints that this Session should
 * use.
 *
 * @class Service
 * @namespace nitrogen
 **/

function Service(config) {
    this.config = config || {};
    this.config.host = this.config.host || 'api.nitrogen.io';
    this.config.protocol = this.config.protocol || 'https';
    this.config.http_port = this.config.http_port || 443;
    this.config.max_session_lifetime = 24 * 60 * 60 * 1000; // ms

    this.config.base_url = this.config.protocol + "://" + this.config.host + ":" + this.config.http_port + "/api/v1";
    this.config.endpoints = {
        headwaiter: this.config.base_url + "/headwaiter"
    };

    // Need this for password reset.  Can be overridden by response from headwaiter.
    if (!this.config.endpoints.principals)
        this.config.endpoints.principals = this.config.base_url + "/principals";

    this.sessions = {};
    this.store = config.store;
}

Service.BACKOFF_RATE_MILLIS = 500;
Service.MAXIMUM_BACKOFF_STEPS = 7;

Service.PUBLIC_KEY_BITS = 2048;
Service.PUBLIC_KEY_EXPONENT = 65537;

/**
 * Authenticate this principal with the Nitrogen service.  The mechanism used to authenticate
 * depends on the type of principal. For users, an email and password is used. For other principals
 * public key encryption is verify a signed nonce value for authentication.
 *
 * @method authenticate
 * @async
 * @param {Object} principal The principal to authenticate with this service. The principal should include the email/password for a user principal or the private_key for other principal types.
 * @param {Function} callback Callback function with signature f(err, session, principal).
 **/

Service.prototype.authenticate = function(principal, callback) {
    this.authenticateSession(principal, principal.authenticate, callback);
};

/**
 * Creates the principal with this service.
 *
 * @method create<commit_msg>constant-ize max session lifetime<commit_after>var request = require('request')
  , Principal = require('./principal')
  , Session = require('./session')
  , ursa = require('ursa');

/**
 * A Service is a Nitrogen endpoint that a Session is established against for interactions with
 * it.  How that Session is established depends on the Principal type. For device and service
 * principals, authentication is done based on a shared secret between the device and the
 * service.  For user principals, authentication is via email and password. Sessions can also
 * be resumed if the principal has stored an authToken. The Service object is also responsible
 * for querying the headwaiter endpoint to fetch the service endpoints that this Session should
 * use.
 *
 * @class Service
 * @namespace nitrogen
 **/

function Service(config) {
    this.config = config || {};
    this.config.host = this.config.host || 'api.nitrogen.io';
    this.config.protocol = this.config.protocol || 'https';
    this.config.http_port = this.config.http_port || 443;
    this.config.max_session_lifetime = Service.MAX_SESSION_LIFETIME_DEFAULT;

    this.config.base_url = this.config.protocol + "://" + this.config.host + ":" + this.config.http_port + "/api/v1";
    this.config.endpoints = {
        headwaiter: this.config.base_url + "/headwaiter"
    };

    // Need this for password reset.  Can be overridden by response from headwaiter.
    if (!this.config.endpoints.principals)
        this.config.endpoints.principals = this.config.base_url + "/principals";

    this.sessions = {};
    this.store = config.store;
}

Service.BACKOFF_RATE_MILLIS = 500;
Service.MAXIMUM_BACKOFF_STEPS = 7;

Service.PUBLIC_KEY_BITS = 2048;
Service.PUBLIC_KEY_EXPONENT = 65537;

Service.MAX_SESSION_LIFETIME_DEFAULT = 24 * 60 * 60 * 1000; // ms

/**
 * Authenticate this principal with the Nitrogen service.  The mechanism used to authenticate
 * depends on the type of principal. For users, an email and password is used. For other principals
 * public key encryption is verify a signed nonce value for authentication.
 *
 * @method authenticate
 * @async
 * @param {Object} principal The principal to authenticate with this service. The principal should include the email/password for a user principal or the private_key for other principal types.
 * @param {Function} callback Callback function with signature f(err, session, principal).
 **/

Service.prototype.authenticate = function(principal, callback) {
    this.authenticateSession(principal, principal.authenticate, callback);
};

/**
 * Creates the principal with this service.
 *
 * @method create
{'fraction_non_alphanumeric': 0.09709302325581395, 'fraction_numerical': 0.010465116279069767, 'mean_word_length': 3.9293218720152816, 'pattern_counts': {'":': 4, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '711737', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1411, 'n_words': 504}