{
  "name": "api-token",
  "version": "0.1.1",
  "description": "api-token",
  "main": "index.js",
  "scripts": {},
  "repository": {
    "type": "git",
    "url": "git://github.com/laardee/api-token.git"
  },
  "keywords": [
    "api",
    "token",
    "session"
  ],
  "author": {
    "name": "Eetu Tuomala",
    "email": "hopeatussi@gmail.com"
  },
  "license": "MIT",
  "readmeFilename": "README.md",
  "gitHead": "bb7431f3580bef12251b427a860e682690a71a97",
  "bugs": {
    "url": "https://github.com/laardee/api-token/issues"
  },
  "dependencies": {
    "uuid": "~1.4.1"
  },
  "readme": "api-token\n=========\n\nSimple module for Node.js that can be used e.g. for REST api session management.\n\n## Installation\n\n```\nnpm install api-token --save\n```\n\n## Usage\n\n### Node.js server\n\n**setup api-token**\n\n```javascript\nvar apiToken = require('api-token');\n/* set expiration time to 2 minutes */\napiToken.setExpirationTime(2);\n```\n\n**expressjs**\n\nExample how to use api-token module in Express application. For more details see [Example project](#example-project).\n\n\n```javascript\napp.all('/api/*', function(req, res, next){\n    if(req.url === '/api/authenticate'){\n        /* token is not needed when authenticating */\n        next();\n    }else if(apiToken.isTokenValid(req.get('API-Token'))){\n        /* if token is valid continue */\n        next();\n    }else{\n        /* if token is not valid send unauthorized http statuscode to client */\n        res.send(401);\n    }\n});\n```\n\n```javascript\napp.post('/api/authenticate', function(req, res){\n    var authenticated = false;\n    /* do your authentication tricks */\n    if(!authenticated){\n        res.send(401);\n    }else{\n        var user = apiToken.addUser(req.body.username);\n        /* send token back to client */\n        res.send(200,{'token':user.token});\n    }\n});\n```\n\n**plain http server**\n\nExample how to use api-token with http server.\n\n```javascript\nhttp.createServer(function (req, res) {\n    /* if requesting API */\n    if((req.url.indexOf('/api')>-1)){\n        /* if authenticating */\n        if(req.url.indexOf('/api/authenticate')>-1){\n            if (req.method == 'POST') {\n                var payload = \"\";\n                req.on('data', function(chunk) {\n                    payload= chunk.toString();\n                });\n                req.on('end', function() {\n                    var json = JSON.parse(payload);\n                    var authenticated = false;\n                    /* do your authentication tricks */\n                    if(!authenticated){\n                        res.writeHead(401, \"Unauthorized\", {'Content-Type': 'text/plain'});\n                        res.end();\n                    }else{\n                        var user = apiToken.addUser(json.username);\n                        res.writeHead(200, \"OK\", {'Content-Type': 'application/json'});\n                        /* send token back to client */\n                        res.end(JSON.stringify({'token':user.token}));\n                    }\n                });\n            }\n        }else{\n            if(apiToken.isTokenValid(req.headers['api-token'])){\n                /* if token is valid continue */\n                res.writeHead(200, \"OK\", {'Content-Type': 'application/json'});\n                res.end(JSON.stringify({'message':'Here is the data you requested, sir.'}));\n            }else{\n                /* if token is not valid send unauthorized http statuscode to client */\n                res.writeHead(401, \"Unauthorized\", {'Content-Type': 'text/plain'});\n                res.end();\n            }\n        }\n    }else{\n        /* not api request */\n        res.writeHead(200, \"OK\", {'Content-Type': 'application/json'});\n        res.end();\n    }\n}).listen(1337, '127.0.0.1');\n```\n\n\n### Example project\n\nExample application which demonstrates the use of the module.\nhttps://github.com/laardee/api-token-expressjs-demo\n \n\n## Release History\n\n* 0.1.0 Initial release\n",
  "_id": "api-token@0.1.1",
  "dist": {
    "shasum": "3064f384484f6af6599d723280932cf8ed34da5b"
  },
  "_resolved": "git://github.com/laardee/api-token.git#678e95f422f5e9478ad34191c6b0eb16f1ce95f8",
  "_from": "git://github.com/laardee/api-token.git"
}
