Class: Unpwn

Inherits:
Object
  • Object
show all
Defined in:
lib/unpwn.rb,
lib/unpwn/version.rb

Overview

Unpwn checks passwords locally against the top one million passwords, as provided by the nbp project. Then, it uses the haveibeenpwned API to check proposed passwords against the largest corpus of publicly dumped passwords in the world.

Constant Summary collapse

VERSION =
"1.0.0"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min: 8, max: nil, request_options: nil) ⇒ Unpwn

Set the options for an Unpwn instance. ‘request_options` will be passed verbatim to the `Pwned` library.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
# File 'lib/unpwn.rb', line 24

def initialize(min: 8, max: nil, request_options: nil)
  raise ArgumentError if min && min < 8
  raise ArgumentError if max && max < 64

  @min = min
  @max = max
  @request_options = request_options || {}
end

Class Attribute Details

.offlineObject

Set ‘offline` to true to disable requests to the haveibeenpwned.com API



10
11
12
# File 'lib/unpwn.rb', line 10

def offline
  @offline
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



20
21
22
# File 'lib/unpwn.rb', line 20

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



20
21
22
# File 'lib/unpwn.rb', line 20

def min
  @min
end

#request_optionsObject (readonly)

Returns the value of attribute request_options.



20
21
22
# File 'lib/unpwn.rb', line 20

def request_options
  @request_options
end

Class Method Details

.acceptable?(password) ⇒ Boolean

Check if a password is not already published. To set options like ‘min`, `max`, or on the Pwned API check, create a new instance of your own.

Returns:

  • (Boolean)


15
16
17
# File 'lib/unpwn.rb', line 15

def acceptable?(password)
  new.acceptable?(password)
end

Instance Method Details

#acceptable?(password) ⇒ Boolean

Check if a password meets the requirements and is not pwned.

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/unpwn.rb', line 34

def acceptable?(password)
  return false if min && password.size < min
  return false if max && password.size > max

  !pwned?(password)
end

#bloomObject



53
54
55
56
57
58
59
60
# File 'lib/unpwn.rb', line 53

def bloom
  @bloom ||= begin
    require "bloomer"
    require "bloomer/msgpackable"
    top = File.read File.expand_path("top1000000.msgpack", __dir__)
    Bloomer.from_msgpack(top)
  end
end

#inspectObject Also known as: to_s



62
63
64
# File 'lib/unpwn.rb', line 62

def inspect
  "<UnPwn bloomed=#{@bloom ? 'yes' : 'no'}>"
end

#pwned?(password) ⇒ Boolean

Checks if a password is pwned, via bloom filter then ‘Pwned`.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
# File 'lib/unpwn.rb', line 42

def pwned?(password)
  pwned = bloom.include?(password)

  unless self.class.offline
    require "pwned"
    pwned ||= Pwned.pwned?(password, request_options)
  end

  pwned
end