Skip to content

Commit 6f558b9

Browse files
author
Eran Hammer
committed
Include auth in validation context. Closes hapijs#2238
1 parent b9a7f95 commit 6f558b9

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

docs/Reference.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,12 +2175,12 @@ following options:
21752175
- `'ignore'` - take no action.
21762176

21772177
- `validate` - request input validation rules for various request components. When using a
2178-
[Joi](http://github.com/hapijs/joi) validation object, the values of the other inputs (e.g.
2179-
`headers`, `query`, and `params` when validating `payload`) are made available under the
2180-
validation context (accessible in rules as `Joi.ref('$query.key')`). Note that validation is
2181-
performed in order (i.e. headers, params, query, payload) and if type casting is used
2182-
(converting a string to number), the value of inputs not yet validated will reflect the raw,
2183-
unvalidated and unmodified values. The `validate` object supports:
2178+
[Joi](http://github.com/hapijs/joi) validation object, the values of the other inputs (i.e.
2179+
`headers`, `query`, `params`, `payload`, and `auth`) are made available under the validation
2180+
context (accessible in rules as `Joi.ref('$query.key')`). Note that validation is performed in
2181+
order (i.e. headers, params, query, payload) and if type casting is used (converting a string to
2182+
number), the value of inputs not yet validated will reflect the raw, unvalidated and unmodified
2183+
values. The `validate` object supports:
21842184

21852185
- `headers` - validation rules for incoming request headers. Values allowed:
21862186
- `true` - any headers allowed (no validation performed). This is the default.

lib/validation.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ internals.input = function (source, request, next) {
107107
headers: request.headers,
108108
params: request.params,
109109
query: request.query,
110-
payload: request.payload
110+
payload: request.payload,
111+
auth: {
112+
isAuthenticated: request.auth.isAuthenticated,
113+
credentials: request.auth.credentials
114+
}
111115
}
112116
};
113117

test/validation.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,63 @@ describe('validation', function () {
8585
});
8686
});
8787

88+
it('validates valid input using auth context', function (done) {
89+
90+
var server = new Hapi.Server();
91+
server.connection();
92+
93+
server.auth.scheme('none', function (server, options) {
94+
95+
return {
96+
authenticate: function (request, reply) {
97+
98+
return reply.continue({ credentials: { name: 'john' } });
99+
}
100+
};
101+
});
102+
103+
server.auth.strategy('default', 'none', true);
104+
105+
server.route({
106+
method: 'GET',
107+
path: '/{user?}',
108+
handler: function (request, reply) { return reply('ok'); },
109+
config: {
110+
validate: {
111+
query: {
112+
me: Joi.boolean().when('$auth.credentials.name', { is: Joi.ref('$params.user'), otherwise: Joi.forbidden() })
113+
}
114+
}
115+
}
116+
});
117+
118+
server.inject('/?me=true', function (res) {
119+
120+
expect(res.statusCode).to.equal(400);
121+
122+
server.inject('/', function (res) {
123+
124+
expect(res.statusCode).to.equal(200);
125+
126+
server.inject('/steve?me=true', function (res) {
127+
128+
expect(res.statusCode).to.equal(400);
129+
130+
server.inject('/john?me=true', function (res) {
131+
132+
expect(res.statusCode).to.equal(200);
133+
134+
server.inject('/john?me=x', function (res) {
135+
136+
expect(res.statusCode).to.equal(400);
137+
done();
138+
});
139+
});
140+
});
141+
});
142+
});
143+
});
144+
88145
it('fails valid input', function (done) {
89146

90147
var server = new Hapi.Server();

0 commit comments

Comments
 (0)