<commit_before>passport = require 'passport'
deviceManager = require '../models/device'
{getProxy} = require '../lib/proxy'

# helper functions
extractCredentials = (header) ->
    if header?
        authDevice = header.replace 'Basic ', ''
        authDevice = new Buffer(authDevice, 'base64').toString 'ascii'
        return authDevice.split ':'
    else
        return ["", ""]

getCredentialsHeader = ->
    credentials = "#{process.env.NAME}:#{process.env.TOKEN}"
    basicCredentials = new Buffer(credentials).toString 'base64'
    return "Basic #{basicCredentials}"

# controller actions
module.exports.management = (req, res, next) ->

    authenticator = passport.authenticate 'local', (err, user) ->
        if err
            console.log err
            next err
        else if user is undefined or not user
            error = new Error "Bad credentials"
            error.status = 401
            next error
        else
            # Send request to the Data System
            req.headers['authorization'] = getCredentialsHeader()
            res.end = -> deviceManager.update()
            getProxy().web req, res, target: "http://localhost:9101"

    # Authenticate the request
    [username, password] = extractCredentials req.headers['authorization']

    # Initialize user
    user = {}
    user.body = password: password

    req.headers['authorization'] = undefined
    # Check if request is authenticated
    authenticator user, res

module.exports.replication = (req, res, next) ->

    # Authenticate the request
    [username, password] = extractCredentials req.headers['authorization']
    if deviceManager.isAuthenticated username, password
        # Add his creadentials for CouchDB
        if process.env.NODE_ENV is "production"
            req.headers['authorization'] = getCredentialsHeader()

        # Forward request to Couch
        # the request didn't go through the DS because for some unknown
        # reason, the double proxy was failing. We should try again with
        # the new node-http-proxy version.
        # That would make a device an application that is not running on
        # the Cozy itself which is awesome because it would be easy to
        # add a permission layer and makes Cozy a true open platform
        # (easy desktop/mobile clients)
        getProxy().web req, res, target: "http://localhost:5984"
    else
        error = new Error "Request unauthorized"
        error.status = 401
        next error<commit_msg>[fix] Do not forward 'authorization' header in a development environment<commit_after>passport = require 'passport'
deviceManager = require '../models/device'
{getProxy} = require '../lib/proxy'

# helper functions
extractCredentials = (header) ->
    if header?
        authDevice = header.replace 'Basic ', ''
        authDevice = new Buffer(authDevice, 'base64').toString 'ascii'
        return authDevice.split ':'
    else
        return ["", ""]

getCredentialsHeader = ->
    credentials = "#{process.env.NAME}:#{process.env.TOKEN}"
    basicCredentials = new Buffer(credentials).toString 'base64'
    return "Basic #{basicCredentials}"

# controller actions
module.exports.management = (req, res, next) ->

    authenticator = passport.authenticate 'local', (err, user) ->
        if err
            console.log err
            next err
        else if user is undefined or not user
            error = new Error "Bad credentials"
            error.status = 401
            next error
        else
            # Send request to the Data System
            req.headers['authorization'] = getCredentialsHeader()
            res.end = -> deviceManager.update()
            getProxy().web req, res, target: "http://localhost:9101"

    # Authenticate the request
    [username, password] = extractCredentials req.headers['authorization']

    # Initialize user
    user = {}
    user.body = password: password

    req.headers['authorization'] = undefined
    # Check if request is authenticated
    authenticator user, res

module.exports.replication = (req, res, next) ->

    # Authenticate the request
    [username, password] = extractCredentials req.headers['authorization']
    if deviceManager.isAuthenticated username, password
        # Add his creadentials for CouchDB
        if process.env.NODE_ENV is "production"
            req.headers['authorization'] = getCredentialsHeader()
        else
            req.headers['authorization'] = null

        # Forward request to Couch
        # the request didn't go through the DS because for some unknown
        # reason, the double proxy was failing. We should try again with
        # the new node-http-proxy version.
        # That would make a device an application that is not running on
        # the Cozy itself which is awesome because it would be easy to
        # add a permission layer and makes Cozy a true open platform
        # (easy desktop/mobile clients)
        getProxy().web req, res, target: "http://localhost:5984"
    else
        error = new Error "Request unauthorized"
        error.status = 401
        next error
{'fraction_non_alphanumeric': 0.09276729559748427, 'fraction_numerical': 0.007075471698113208, 'mean_word_length': 2.5863284002818885, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6939473', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1279, 'n_words': 449}