Module: Sequel::Plugins::SequelAuth

Defined in:
lib/sequel_auth.rb,
lib/sequel_auth/version.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

DEFAULT_STRENGTH_OPTIONS =

Default password strength options

{
  min_length: 8,
  require_lowercase: true,
  require_uppercase: true,
  require_number: true,
  require_special_char: false,
  max_length: 128
}.freeze
VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.configure(model, opts = {}) ⇒ Object

Plugin configuration



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sequel_auth.rb', line 30

def self.configure(model, opts = {})
  model.instance_eval do
    # Core configuration
    @digest_column       = opts.fetch(:digest_column, :password_digest)
    @include_validations = opts.fetch(:include_validations, true)
    
    # Provider configuration
    @provider = SequelAuth.provider(
      opts.fetch(:provider, :bcrypt),
      opts.fetch(:provider_opts, {})
    )
    
    # Optional columns
    @access_token_column        = opts.fetch(:access_token_column, nil)
    @login_count_column         = opts.fetch(:login_count_column, nil)
    @failed_login_count_column  = opts.fetch(:failed_login_count_column, nil)
    @last_login_at_column       = opts.fetch(:last_login_at_column, nil)
    
    # Password strength configuration
    if opts[:password_strength]
      @password_strength_options = SequelAuth.validate_strength_options(opts[:password_strength])
    end
  end
end

.provider(provider, opts = {}) ⇒ Object

Provider factory method



13
14
15
16
17
# File 'lib/sequel_auth.rb', line 13

def self.provider(provider, opts = {})
  provider_class = Kernel.const_get("SequelAuth::Providers::#{provider.to_s.capitalize}")
  opts.each { |k, v| provider_class.public_send("#{k}=", v) }
  provider_class
end

.validate_strength_options(options) ⇒ Object

Validate strength options



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sequel_auth.rb', line 56

def self.validate_strength_options(options)
  # Merge with defaults
  merged = DEFAULT_STRENGTH_OPTIONS.merge(options)
  
  # Validate options
  unless merged[:min_length].is_a?(Integer) && merged[:min_length] > 0
    raise ArgumentError, ':min_length must be a positive integer'
  end
  
  unless merged[:max_length].is_a?(Integer) && merged[:max_length] >= merged[:min_length]
    raise ArgumentError, ':max_length must be an integer greater than or equal to min_length'
  end
  
  [ :require_lowercase, :require_uppercase, :require_number, :require_special_char ].each do |key|
    unless [true, false].include?(merged[key])
      raise ArgumentError, ":#{key} must be true or false"
    end
  end
  
  merged.freeze
end