Class: Fradium

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

Defined Under Namespace

Classes: CorruptedUserDatabaseError, UserAlreadyExistsError, UserNotFoundError, UsernameEmptyError

Constant Summary collapse

VERSION =
"0.1.6"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Fradium

Returns a new instance of Fradium.



12
13
14
15
# File 'lib/fradium.rb', line 12

def initialize(params)
  @params = params
  @sequel = Sequel.connect(@params)
end

Class Method Details

.generate_random_password(length = 10) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/fradium.rb', line 119

def self.generate_random_password(length=10)
  r = SecureRandom.urlsafe_base64.delete('-_')
  while r.length < length
    r << SecureRandom.urlsafe_base64.delete('-_')
  end
  r[0..length-1]
end

Instance Method Details

#all_usersObject



21
22
23
# File 'lib/fradium.rb', line 21

def all_users
  @sequel[:radcheck].where{attribute.like '%-Password'}
end

#create_user(username) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
34
# File 'lib/fradium.rb', line 25

def create_user(username)
  raise UsernameEmptyError if username&.empty?
  raise UserAlreadyExistsError if user_exists?(username)
  password = Fradium.generate_random_password

  @sequel[:radcheck].insert(username: username,
                            attribute: 'Cleartext-Password',
                            op: ':=',
                            value: password)
end

#dbconsoleObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fradium.rb', line 98

def dbconsole
  case @sequel.adapter_scheme
  when :mysql2
    # I know this is not safe.
    Kernel.exec({'MYSQL_PWD' => @params['password']},
                'mysql',
                "--pager=less -SF",
                "--user=#{@params['username']}",
                "--host=#{@params['host']}" ,
                "#{@params['database']}")
  when :postgres
    Kernel.exec({'PGPASSWORD' => @params['password']},
                'psql',
                "--username=#{@params['username']}",
                "--dbname=#{@params['database']}",
                "--host=#{@params['host']}")
  when :sqlite
    Kernel.exec('sqlite3', @params)
  end
end

#expire_user(username) ⇒ Object



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

def expire_user(username)
  set_expiration(username, Time.now)
end

#expired_usersObject



55
56
57
58
59
60
# File 'lib/fradium.rb', line 55

def expired_users
  now = Time.now
  # a little bit redundant but for consistency
  id = @sequel[:radcheck].where(attribute: 'Expiration').collect{|e| e[:id] if Time.now > Time.parse(e[:value])}
  @sequel[:radcheck].where(id: id)
end

#expiryObject



51
52
53
# File 'lib/fradium.rb', line 51

def expiry
  @sequel[:radcheck].where(attribute: 'Expiration')
end

#find_user(username) ⇒ Object

Raises:



36
37
38
39
# File 'lib/fradium.rb', line 36

def find_user(username)
  raise UsernameEmptyError if username&.empty?
  reult = @sequel[:radcheck].where(username: username).where{attribute.like '%-Password'}
end

#is_expired?(username) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/fradium.rb', line 72

def is_expired?(username)
  expiration_date = query_expiration(username)&.fetch('value')
  return false if expiration_date.nil? || expiration_date.empty? # if expiration info not found, not expired yet
  Time.now > Time.parse(expiration_date)
end

#modify_user(username) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
# File 'lib/fradium.rb', line 41

def modify_user(username)
  raise UsernameEmptyError if username&.empty?
  raise UserNotFoundError unless user_exists?(username)
  password = Fradium.generate_random_password

  target = find_user(username)
  raise CorruptedUserDatabaseError if target.count > 1
  target.update(value: password, attribute: 'Cleartext-Password')
end

#query_expiration(username) ⇒ Object

private

Raises:



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fradium.rb', line 129

def query_expiration(username)
  raise UsernameEmptyError if username&.empty?
  raise UserNotFoundError unless user_exists?(username)

  r = @sequel[:radcheck].where(username: username, attribute: 'Expiration')

  raise CorruptedUserDatabaseError if r.count > 1
  return nil if r.count == 0 # if no expiration info found
  return nil if r&.first[:value]&.empty?

  r
end

#set_expiration(username, expiration_date) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fradium.rb', line 78

def set_expiration(username, expiration_date)
  expiration_info = query_expiration(username)

  value = ''
  if expiration_date.instance_of?(Time)
    value = expiration_date.strftime("%d %b %Y %H:%M:%S")
  else
    value = Time.parse(expiration_date).strftime("%d %b %Y %H:%M:%S")
  end

  if expiration_info.nil? # add new entry
    @sequel[:radcheck].insert(username: username,
                              attribute: 'Expiration',
                              op: ':=',
                              value: value)
  else # update existing entry
    expiration_info.update(value: value)
  end
end

#unexpire_user(username) ⇒ Object

Raises:



66
67
68
69
70
# File 'lib/fradium.rb', line 66

def unexpire_user(username)
  raise UsernameEmptyError if username&.empty?
  raise UserNotFoundError unless user_exists?(username)
  @sequel[:radcheck].where(username: username, attribute: 'Expiration').delete
end

#user_exists?(username) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/fradium.rb', line 17

def user_exists?(username)
  find_user(username).count > 0
end