X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.maps.server%2Fnode%2Fnode-v4.8.0-win-x64%2Fnode_modules%2Fnpm%2Fnode_modules%2Frequest%2Fnode_modules%2Fhawk%2Fexample%2Fusage.js;fp=org.simantics.maps.server%2Fnode%2Fnode-v4.8.0-win-x64%2Fnode_modules%2Fnpm%2Fnode_modules%2Frequest%2Fnode_modules%2Fhawk%2Fexample%2Fusage.js;h=64fe17674a5753edff0698a142317da98e8a3765;hb=2529be6d456deeb07c128603ce4971f1dc29b695;hp=0000000000000000000000000000000000000000;hpb=2636fc31c16c23711cf2b06a4ae8537bba9c1d35;p=simantics%2Fdistrict.git diff --git a/org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/hawk/example/usage.js b/org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/hawk/example/usage.js new file mode 100644 index 00000000..64fe1767 --- /dev/null +++ b/org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/hawk/example/usage.js @@ -0,0 +1,77 @@ +// Load modules + +var Http = require('http'); +var Request = require('request'); +var Hawk = require('../lib'); + + +// Declare internals + +var internals = { + credentials: { + dh37fgj492je: { + id: 'dh37fgj492je', // Required by Hawk.client.header + key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn', + algorithm: 'sha256', + user: 'Steve' + } + } +}; + + +// Credentials lookup function + +var credentialsFunc = function (id, callback) { + + return callback(null, internals.credentials[id]); +}; + + +// Create HTTP server + +var handler = function (req, res) { + + Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) { + + var payload = (!err ? 'Hello ' + credentials.user + ' ' + artifacts.ext : 'Shoosh!'); + var headers = { + 'Content-Type': 'text/plain', + 'Server-Authorization': Hawk.server.header(credentials, artifacts, { payload: payload, contentType: 'text/plain' }) + }; + + res.writeHead(!err ? 200 : 401, headers); + res.end(payload); + }); +}; + +Http.createServer(handler).listen(8000, '127.0.0.1'); + + +// Send unauthenticated request + +Request('http://127.0.0.1:8000/resource/1?b=1&a=2', function (error, response, body) { + + console.log(response.statusCode + ': ' + body); +}); + + +// Send authenticated request + +credentialsFunc('dh37fgj492je', function (err, credentials) { + + var header = Hawk.client.header('http://127.0.0.1:8000/resource/1?b=1&a=2', 'GET', { credentials: credentials, ext: 'and welcome!' }); + var options = { + uri: 'http://127.0.0.1:8000/resource/1?b=1&a=2', + method: 'GET', + headers: { + authorization: header.field + } + }; + + Request(options, function (error, response, body) { + + var isValid = Hawk.client.authenticate(response, credentials, header.artifacts, { payload: body }); + console.log(response.statusCode + ': ' + body + (isValid ? ' (valid)' : ' (invalid)')); + process.exit(0); + }); +});