Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add configuration option for custom dictionaries #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ Or install it yourself as:

$ gem install zxcvbn

## Configuration

## Configuration

Zxcvbn allows you to load custom dictionaries to enhance the password strength estimation.

To load dictionaries, use the following syntax:

```ruby
Zxcvbn.configure do |config|
config.add_dictionary('path/to/custom_dictionary.txt')
# Add more dictionaries as needed
end
```

## Usage

```
Expand Down
1 change: 1 addition & 0 deletions lib/zxcvbn.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "zxcvbn/config"
require_relative "zxcvbn/adjacency_graphs"
require_relative "zxcvbn/frequency_lists"
require_relative "zxcvbn/matching"
Expand Down
23 changes: 23 additions & 0 deletions lib/zxcvbn/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Zxcvbn
module Config
extend self

def add_dictionary(path)
data = File.read(path)
lines = data.split("\n").map(&:strip)
filename = File.basename(path, ".*")
new_ranked_dictionary = Hash[filename, Zxcvbn::Matching.build_ranked_dict(lines.join(",").split(","))]
Zxcvbn::Matching::RANKED_DICTIONARIES.merge! new_ranked_dictionary
end
end

class << self
def configure
block_given? ? yield(Config) : Config
end

def config
Config
end
end
end
45 changes: 45 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
RSpec.describe "Zxcvbn::Config" do
describe "#add_dictionary" do
let(:dictionary_path) { 'custom_dictionary.txt' }
let(:dictionary_contents) { "password_1\npassword_2\npassword_3" }

before do
allow(File).to receive(:read).with(dictionary_path).and_return(dictionary_contents)
end

it "adds a custom dictionary to RANKED_DICTIONARIES" do
Zxcvbn.config.add_dictionary(dictionary_path)

expected_dictionary = {
'custom_dictionary' => {
'password_1' => 1,
'password_2' => 2,
'password_3' => 3
}
}

expect(Zxcvbn::Matching::RANKED_DICTIONARIES).to include(expected_dictionary)
end
end

describe ".configure" do

context "when called with a block" do
it "yields the Config module" do
expect { |b| Zxcvbn.configure(&b) }.to yield_with_args(Zxcvbn::Config)
end
end

context "when called without a block" do
it "returns the Config module" do
expect(Zxcvbn.configure).to eq(Zxcvbn.config)
end
end
end

describe ".config" do
it "returns the Config module" do
expect(Zxcvbn.config).to eq(Zxcvbn.config)
end
end
end