-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(basic-auth) password encryption
On the contrary of #527, this only allows for sha1 encryption. The reason is that due to the current architecture, we cannot support two types at the same time (supporting plain is a bad practice anyways). Because a basicauth_credential has no relation to a plugin entity (they are **not** semantically related anyways), we cannot now how the password is stored/encrypted. I also took the opportunity of 0.5.0 and the migration script to make that decision. The migration script will be updated to also migrate the current passwords. This does a bit more than #527: - unit tests - support encryption in unit test mode (with a mock using a vendor sha1 library) - comparison of the hash at the proxy level (for actual authentication) Resolves #33
- Loading branch information
1 parent
69a2776
commit 8c253cb
Showing
9 changed files
with
417 additions
and
37 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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
-- Module to encrypt the basic-auth credentials password field | ||
|
||
local utils = require "kong.tools.utils" | ||
local format = string.format | ||
|
||
--- Salt the password | ||
-- Password is salted with the credential's consumer_id (long enough, unique) | ||
-- @param credential The basic auth credential table | ||
local function salt_password(credential) | ||
return format("%s%s", credential.password, credential.consumer_id) | ||
end | ||
|
||
local in_openresty = utils.load_module_if_exists("resty.string") | ||
if not in_openresty then | ||
--- Mock for usage outside of Openresty (unit testing) | ||
return {encrypt = function(credential) | ||
local sha1 = require "kong.vendor.sha1" | ||
local salted = salt_password(credential) | ||
return sha1(salted) | ||
end} | ||
end | ||
|
||
local resty_sha1 = require "resty.sha1" | ||
local resty_string = require "resty.string" | ||
|
||
--- Return a sha1 hash of the given string | ||
-- @param string String (password) to hash | ||
-- @return sha1 hash of the given string | ||
local function sha1(string) | ||
local sha1 = resty_sha1:new() | ||
if not sha1 then | ||
return nil, "failed to create the sha1 object" | ||
end | ||
|
||
local ok = sha1:update(string) | ||
if not ok then | ||
return nil, "failed to add data" | ||
end | ||
|
||
local digest = sha1:final() | ||
return resty_string.to_hex(digest) | ||
end | ||
|
||
return { | ||
--- Encrypt the password field credential table | ||
-- @param credential The basic auth credential table | ||
-- @return hash of the salted credential's password | ||
encrypt = function(credential) | ||
local salted = salt_password(credential) | ||
return sha1(salted) | ||
end | ||
} |
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,6 +1,6 @@ | ||
return { | ||
no_consumer = true, | ||
fields = { | ||
hide_credentials = { type = "boolean", default = false } | ||
hide_credentials = {type = "boolean", default = false} | ||
} | ||
} |
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
Oops, something went wrong.