3 var Http = require('http');
\r
4 var Request = require('request');
\r
5 var Hawk = require('../lib');
\r
13 id: 'dh37fgj492je', // Required by Hawk.client.header
\r
14 key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
\r
15 algorithm: 'sha256',
\r
22 // Credentials lookup function
\r
24 var credentialsFunc = function (id, callback) {
\r
26 return callback(null, internals.credentials[id]);
\r
30 // Create HTTP server
\r
32 var handler = function (req, res) {
\r
34 Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
\r
36 var payload = (!err ? 'Hello ' + credentials.user + ' ' + artifacts.ext : 'Shoosh!');
\r
38 'Content-Type': 'text/plain',
\r
39 'Server-Authorization': Hawk.server.header(credentials, artifacts, { payload: payload, contentType: 'text/plain' })
\r
42 res.writeHead(!err ? 200 : 401, headers);
\r
47 Http.createServer(handler).listen(8000, '127.0.0.1');
\r
50 // Send unauthenticated request
\r
52 Request('http://127.0.0.1:8000/resource/1?b=1&a=2', function (error, response, body) {
\r
54 console.log(response.statusCode + ': ' + body);
\r
58 // Send authenticated request
\r
60 credentialsFunc('dh37fgj492je', function (err, credentials) {
\r
62 var header = Hawk.client.header('http://127.0.0.1:8000/resource/1?b=1&a=2', 'GET', { credentials: credentials, ext: 'and welcome!' });
\r
64 uri: 'http://127.0.0.1:8000/resource/1?b=1&a=2',
\r
67 authorization: header.field
\r
71 Request(options, function (error, response, body) {
\r
73 var isValid = Hawk.client.authenticate(response, credentials, header.artifacts, { payload: body });
\r
74 console.log(response.statusCode + ': ' + body + (isValid ? ' (valid)' : ' (invalid)'));
\r