Fix returning an API Key that was size + 1.

0..size iterates through size + 1 elements. So asking for an API
that was 16 was actually returning a string of 17 characters. This
broke trying to shove it into a varchar(16) in PostgreSQL.
This commit is contained in:
Andrew Ruthven 2013-04-21 01:51:21 +12:00 committed by Andrew Ruthven
parent 75d1cd06b5
commit 3075563564

View File

@ -3,7 +3,7 @@ module KeyUtilities
# generates a database unique api key
def generate_api_key(size = 16)
alphanumerics = ('0'..'9').to_a + ('A'..'Z').to_a
k = (0..size).map {alphanumerics[Kernel.rand(36)]}.join
k = (0..(size - 1)).map {alphanumerics[Kernel.rand(36)]}.join
# if key exists in database, regenerate key
k = generate_api_key if ApiKey.find_by_api_key(k)
@ -11,4 +11,4 @@ module KeyUtilities
# output the key
k
end
end
end