-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): allow to configure and customize a password generator for …
…the temporary password
- Loading branch information
Showing
8 changed files
with
94 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
# Changelog | ||
All notable changes to this project made by Monade Team are documented in this file. For info refer to [email protected] | ||
|
||
## [1.1.0] - 2023-04-27 | ||
### Added | ||
- A password generator that follows [Cognito policies](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-policies.html) | ||
|
||
### Fixed | ||
- Allow passing a custom password generator | ||
- Override default password generated when explicitly passing one to the model | ||
|
||
## [1.0.0] - 2023-03-30 | ||
### Added | ||
- `sync_from_cognito!` to create users in the local database from cognito | ||
|
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module CognitoRails | ||
class PasswordGenerator | ||
NUMERIC = (0..9).to_a.freeze | ||
LOWER_CASE = ('a'..'z').to_a.freeze | ||
UPPER_CASE = ('A'..'Z').to_a.freeze | ||
SPECIAL = [ | ||
'^', '$', '*', '.', '[', ']', '{', '}', | ||
'(', ')', '?', '"', '!', '@', '#', '%', | ||
'&', '/', '\\', ',', '>', '<', "'", ':', | ||
';', '|', '_', '~', '`', '=', '+', '-' | ||
].freeze | ||
|
||
# Generates a random password given a length range | ||
# | ||
# @param range [Range] | ||
# @return [String] | ||
def self.generate(range = 8..16) | ||
password_length = rand(range) | ||
numeric_count = rand(1..(password_length-3)) | ||
|
||
lower_case_count = rand(1..(password_length-(numeric_count+2))) | ||
upper_case_count = rand(1..(password_length-(numeric_count + lower_case_count + 1))) | ||
special_count = password_length-(numeric_count + lower_case_count + upper_case_count) | ||
|
||
numeric_characters = numeric_count.times.map { NUMERIC.sample } | ||
lower_case_characters = lower_case_count.times.map { LOWER_CASE.sample } | ||
upper_case_characters = upper_case_count.times.map { UPPER_CASE.sample } | ||
special_characters = special_count.times.map { SPECIAL.sample } | ||
|
||
(numeric_characters + lower_case_characters + upper_case_characters + special_characters).shuffle.join | ||
end | ||
end | ||
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 |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
module CognitoRails | ||
# @return [String] gem version | ||
VERSION = '1.0.0' | ||
VERSION = '1.1.0' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe CognitoRails::PasswordGenerator do | ||
it 'generates a password' do | ||
expect(described_class.generate).to be_a(String) | ||
end | ||
|
||
it 'generates a password with the correct length' do | ||
1000.times do | ||
expect(described_class.generate(8..8).length).to eq(8) | ||
end | ||
end | ||
|
||
it 'contains at least one letter, one number, one upper case letter, one symbol' do | ||
1000.times do | ||
password = described_class.generate | ||
expect(password).to match(/[a-z]/) | ||
expect(password).to match(/[A-Z]/) | ||
expect(password).to match(/[0-9]/) | ||
include_symbol = CognitoRails::PasswordGenerator::SPECIAL.any? do |symbol| | ||
password.include?(symbol) | ||
end | ||
expect(include_symbol).to be_truthy | ||
end | ||
end | ||
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