Skip to content

Commit 42cf22f

Browse files
Added an integration test and updated some code based on code review feedback
1 parent 84386c0 commit 42cf22f

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

src/lua/api-gateway/validation/factory.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
-- Time: 23:36
3535
--
3636

37-
local BaseValidator = require "api-gateway.validation.validator"
3837
local ValidatorsHandler = require "api-gateway.validation.validatorsHandler"
3938
local ApiKeyValidatorCls = require "api-gateway.validation.key.redisApiKeyValidator"
4039
local HmacSignatureValidator = require "api-gateway.validation.signing.hmacGenericSignatureValidator"
@@ -104,7 +103,7 @@ end
104103

105104
local function _validateOAuthToken(obj)
106105
local oauthTokenValidator = OAuthTokenValidator:new()
107-
BaseValidator:exitFn(oauthTokenValidator:validateRequest(obj))
106+
return oauthTokenValidator:validateRequest(obj)
108107
end
109108

110109
local function _validateUserProfile()

src/lua/api-gateway/validation/oauth2/oauthTokenValidator.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,18 @@ function _M:getTokenFromCache(cacheLookupKey)
166166
return nil;
167167
end
168168

169-
function _M:validateOAuthToken(oauth_token, validation_config)
169+
function _M:validateOAuthToken(validation_config)
170+
171+
validation_config = validation_config or {}
172+
validation_config.RESPONSES = validation_config.RESPONSES or RESPONSES;
173+
174+
local oauth_token = validation_config.authtoken or ngx.var.authtoken
170175
local oauth_host = ngx.var.oauth_host
171176

177+
if oauth_token == nil or oauth_token == "" then
178+
return validation_config.RESPONSES.MISSING_TOKEN.error_code, cjson.encode(validation_config.RESPONSES.MISSING_TOKEN)
179+
end
180+
172181
--1. try to get token info from the cache first ( local or redis cache )
173182
local oauth_token_hash = ngx.md5(oauth_token)
174183
local cacheLookupKey = self:getOauthTokenForCaching(oauth_token_hash, oauth_host)
@@ -216,16 +225,7 @@ function _M:validateOAuthToken(oauth_token, validation_config)
216225
end
217226

218227
function _M:validateRequest(validation_config)
219-
validation_config = validation_config or {}
220-
validation_config.RESPONSES = validation_config.RESPONSES or RESPONSES;
221-
222-
local oauth_token = validation_config.authtoken or ngx.var.authtoken
223-
224-
if oauth_token == nil or oauth_token == "" then
225-
return validation_config.RESPONSES.MISSING_TOKEN.error_code, cjson.encode(validation_config.RESPONSES.MISSING_TOKEN)
226-
end
227-
228-
return self:validateOAuthToken(oauth_token, validation_config)
228+
return self:exitFn(self:validateOAuthToken(validation_config))
229229
end
230230

231231

test/perl/api-gateway/validation/oauth2/oauthTokenValidator.t

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use Cwd qw(cwd);
3131

3232
repeat_each(2);
3333

34-
plan tests => repeat_each() * (blocks() * 9);
34+
plan tests => repeat_each() * (blocks() * 9) - 6;
3535

3636
my $pwd = cwd();
3737

@@ -350,3 +350,56 @@ GET /test-oauth-validation
350350
--- no_error_log
351351
[error]
352352
353+
=== TEST 6: test that validation behaviour can be customized
354+
--- http_config eval: $::HttpConfig
355+
--- config
356+
include ../../api-gateway/default_validators.conf;
357+
358+
error_log ../test-logs/oauthTokenValidator_test6_error.log debug;
359+
360+
location /validate_custom_oauth_token {
361+
internal;
362+
363+
content_by_lua_block {
364+
365+
ngx.apiGateway.validation.validateOAuthToken({
366+
authtoken = ngx.var.custom_token_var,
367+
RESPONSES = {
368+
MISSING_TOKEN = { error_code = "401110", message = "User token is missing" },
369+
INVALID_TOKEN = { error_code = "403113", message = "User token is not valid" },
370+
TOKEN_MISSMATCH = { error_code = "401114", message = "User token not allowed in the current context" },
371+
SCOPE_MISMATCH = { error_code = "401115", message = "User token scope mismatch" },
372+
UNKNOWN_ERROR = { error_code = "503110", message = "Could not validate the user token" }
373+
}
374+
});
375+
}
376+
}
377+
378+
location /test-custom-oauth {
379+
set $validate_oauth_token "on; path=/validate_custom_oauth_token; order=1;";
380+
set $custom_token_var $arg_custom_token;
381+
access_by_lua "ngx.apiGateway.validation.validateRequest()";
382+
content_by_lua "ngx.say('ims token is valid.')";
383+
}
384+
385+
location /validate-token {
386+
internal;
387+
set_by_lua $generated_expires_at 'return ((os.time() + 4) * 1000 )';
388+
return 200 '{"valid":false,"expires_at":$generated_expires_at,"token":{"id":"1234","scope":"openid email profile","user_id":"21961FF44F97F8A10A490D36","expires_in":"86400000","client_id":"test_Client_ID","type":"access_token"}}';
389+
}
390+
391+
--- pipelined_requests eval
392+
[
393+
"GET /test-custom-oauth",
394+
"GET /test-custom-oauth?custom_token=SOME_OAUTH_TOKEN_TEST6"
395+
]
396+
--- response_body_like eval
397+
[
398+
'^{"error_code":"401110","message":"User token is missing"}+',
399+
'^{"error_code":"403113","message":"User token is not valid"}+'
400+
]
401+
--- error_code_like eval
402+
[401,403]
403+
--- no_error_log
404+
[error]
405+

0 commit comments

Comments
 (0)