-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify random_token implementation to use set_secure_random_alphanum.
Use nginx NDK to use set_secure_random_alphanum for our random_token implementation. This is both faster and simpler than our previous implementation. It's limited to 64 characters in length, but that should be fine for our purposes.
- Loading branch information
Showing
3 changed files
with
4 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,3 @@ | ||
local resty_random = require "resty.random" | ||
|
||
local encode_base64 = ngx.encode_base64 | ||
local gsub = ngx.re.gsub | ||
local random_bytes = resty_random.bytes | ||
|
||
return function(length) | ||
local token = "" | ||
-- Loop until we've generated a valid token. The basic process: | ||
-- | ||
-- 1. Generate secure random bytes. | ||
-- 2. Convert random bytes to base64. | ||
-- 3. Strip out special characters from base64 result, so we're left with | ||
-- just alphanumerics. | ||
-- | ||
-- It should be extraordinarily rare that this needs to loop, but since we | ||
-- strip out some of the special characters from the resulting base64 string, | ||
-- this loops in case we strip more than expected. | ||
while string.len(token) < length do | ||
-- Attempt to generate cryptographically secure random bytes. We | ||
-- purposefully generate more bytes than we need, since we'll be stripping | ||
-- some of the base64 characters out. | ||
local num_bytes = length + 10 | ||
local strong_random = random_bytes(num_bytes, true) | ||
if not strong_random then | ||
ngx.log(ngx.WARN, "Could not generate cryptographically secure random data. Falling back to non-secure random data.") | ||
strong_random = random_bytes(num_bytes, false) | ||
end | ||
|
||
-- Encode with base64. | ||
token = token .. encode_base64(strong_random) | ||
|
||
-- Strip +, /, and = out of the base64 result, since we just want a-z, A-Z, | ||
-- and 0-9 in our tokens. | ||
token = gsub(token, "[+/=]", "", "jo") | ||
|
||
-- Take just the number of characters requested. | ||
token = string.sub(token, 1, length) | ||
end | ||
|
||
return token | ||
return ndk.set_var.set_secure_random_alphanum(length) | ||
end |