Git Product home page Git Product logo

pwned's Introduction

Pwned

An easy, Ruby way to use the Pwned Passwords API.

Gem Version Build Status Maintainability Inline docs

API docs | GitHub repo

Table of Contents

About

Troy Hunt's Pwned Passwords API V2 allows you to check if a password has been found in any of the huge data breaches.

Pwned is a Ruby library to use the Pwned Passwords API's k-Anonymity model to test a password against the API without sending the entire password to the service.

The data from this API is provided by Have I been pwned?. Before using the API, please check the acceptable uses and license of the API.

Here is a blog post I wrote on how to use this gem in your Ruby applications to make your users' passwords better.

Installation

Add this line to your application's Gemfile:

gem 'pwned'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pwned

Usage

There are a few ways you can use this gem:

  1. Plain Ruby
  2. Rails
  3. Rails and Devise

Plain Ruby

To test a password against the API, instantiate a Pwned::Password object and then ask if it is pwned?.

password = Pwned::Password.new("password")
password.pwned?
#=> true
password.pwned_count
#=> 3303003

You can also check how many times the password appears in the dataset.

password = Pwned::Password.new("password")
password.pwned_count
#=> 3303003

Since you are likely using this as part of a signup flow, it is recommended that you rescue errors so if the service does go down, your user journey is not disturbed.

begin
  password = Pwned::Password.new("password")
  password.pwned?
rescue Pwned::Error => e
  # Ummm... don't worry about it, I guess?
end

Most of the times you only care if the password has been pwned before or not. You can use simplified accessors to check whether the password has been pwned, or how many times it was pwned:

Pwned.pwned?("password")
#=> true
Pwned.pwned_count("password")
#=> 3303003

Advanced

You can set options and headers to be used with open-uri when making the request to the API. HTTP headers must be string keys and the other options are available in the OpenURI::OpenRead module.

password = Pwned::Password.new("password", { 'User-Agent' => 'Super fun new user agent' })

ActiveRecord Validator

There is a custom validator available for your ActiveRecord models:

class User < ApplicationRecord
  validates :password, not_pwned: true
  # or
  validates :password, not_pwned: { message: "has been pwned %{count} times" }
end

I18n

You can change the error message using I18n (use %{count} to interpolate the number of times the password was seen in the data breaches):

en:
  errors:
    messages:
      not_pwned: has been pwned %{count} times
      pwned_error: might be pwned

Threshold

If you are ok with the password appearing a certain number of times before you decide it is invalid, you can set a threshold. The validator will check whether the pwned_count is greater than the threshold.

class User < ApplicationRecord
  # The record is marked as valid if the password has been used once in the breached data
  validates :password, not_pwned: { threshold: 1 }
end

Network Error Handling

By default the record will be treated as valid when we cannot reach the haveibeenpwned.com servers. This can be changed with the :on_error validator parameter:

class User < ApplicationRecord
  # The record is marked as valid on network errors.
  validates :password, not_pwned: true
  validates :password, not_pwned: { on_error: :valid }

  # The record is marked as invalid on network errors
  # (error message "could not be verified against the past data breaches".)
  validates :password, not_pwned: { on_error: :invalid }

  # The record is marked as invalid on network errors with custom error.
  validates :password, not_pwned: { on_error: :invalid, error_message: "might be pwned" }

  # We will raise an error on network errors.
  # This means that `record.valid?` will raise `Pwned::Error`.
  # Not recommended to use in production.
  validates :password, not_pwned: { on_error: :raise_error }

  # Call custom proc on error. For example, capture errors in Sentry,
  # but do not mark the record as invalid.
  validates :password, not_pwned: {
    on_error: ->(record, error) { Raven.capture_exception(error) }
  }
end

Custom Request Options

You can configure network requests made from the validator using :request_options (see OpenURI::OpenRead#open for the list of available options, string keys represent custom network request headers, e.g. "User-Agent"):

  validates :password, not_pwned: {
    request_options: { read_timeout: 5, open_timeout: 1, "User-Agent" => "Super fun user agent" }
  }

Devise

If you are using Devise I recommend you use the devise-pwned_password extension which is now powered by this gem.

How Pwned is Pi?

@daz shared a fantastic example of using this gem to show how many times the digits of Pi have been used as passwords and leaked.

require 'pwned'

PI = '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111'

for n in 1..40
  password = Pwned::Password.new PI[0..(n + 1)]
  str = [ n.to_s.rjust(2) ]
  str << (password.pwned? ? '😑' : 'πŸ˜ƒ')
  str << password.pwned_count.to_s.rjust(4)
  str << password.password

  puts str.join ' '
end

The results may, or may not, surprise you.

 1 😑   16 3.1
 2 😑  238 3.14
 3 😑   34 3.141
 4 😑 1345 3.1415
 5 😑 2552 3.14159
 6 😑  791 3.141592
 7 😑 9582 3.1415926
 8 😑 1591 3.14159265
 9 😑  637 3.141592653
10 😑  873 3.1415926535
11 😑  137 3.14159265358
12 😑  103 3.141592653589
13 😑   65 3.1415926535897
14 😑  201 3.14159265358979
15 😑   41 3.141592653589793
16 😑   57 3.1415926535897932
17 😑   28 3.14159265358979323
18 😑   29 3.141592653589793238
19 😑    1 3.1415926535897932384
20 😑    7 3.14159265358979323846
21 😑    5 3.141592653589793238462
22 😑    2 3.1415926535897932384626
23 😑    2 3.14159265358979323846264
24 πŸ˜ƒ    0 3.141592653589793238462643
25 😑    3 3.1415926535897932384626433
26 πŸ˜ƒ    0 3.14159265358979323846264338
27 πŸ˜ƒ    0 3.141592653589793238462643383
28 πŸ˜ƒ    0 3.1415926535897932384626433832
29 πŸ˜ƒ    0 3.14159265358979323846264338327
30 πŸ˜ƒ    0 3.141592653589793238462643383279
31 πŸ˜ƒ    0 3.1415926535897932384626433832795
32 πŸ˜ƒ    0 3.14159265358979323846264338327950
33 πŸ˜ƒ    0 3.141592653589793238462643383279502
34 πŸ˜ƒ    0 3.1415926535897932384626433832795028
35 πŸ˜ƒ    0 3.14159265358979323846264338327950288
36 πŸ˜ƒ    0 3.141592653589793238462643383279502884
37 πŸ˜ƒ    0 3.1415926535897932384626433832795028841
38 πŸ˜ƒ    0 3.14159265358979323846264338327950288419
39 πŸ˜ƒ    0 3.141592653589793238462643383279502884197
40 πŸ˜ƒ    0 3.1415926535897932384626433832795028841971

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/philnash/pwned. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Pwned project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

pwned's People

Contributors

kpumuk avatar philnash avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.