summaryrefslogtreecommitdiff
path: root/app/models/user.rb
blob: 786f8d145b95f3ea06591bda6a6b1b3843aa4ddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
require 'digest/sha1'

class User < ApplicationRecord
  has_secure_password(validations: false)

  alias_method :authenticate_bcrypt, :authenticate

  # Mixins and Plugins
  include Authentication
  include Authentication::ByPassword

  ROLES = %w[redaktion admin alumni].freeze
  STALE_AMBER_YEARS = 3
  STALE_RED_YEARS   = 10

  # Validations
  validates_presence_of     :login
  validates_length_of       :login, :within => 1..40
  validates_uniqueness_of   :login
  validates_format_of       :login, :with => Authentication.login_regex,
                            :message => Authentication.bad_login_message

  validates_presence_of     :email
  validates_length_of       :email, :within => 6..100 #r@a.wk
  validates_uniqueness_of   :email
  validates_format_of       :email, :with => Authentication.email_regex,
                            :message => Authentication.bad_email_message

  validate :roles_are_known
  validate :admin_needs_second_factor

  # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  def self.authenticate(login, password)
    return if login.blank? || password.blank?

    user = find_by(login: login)
    return unless user

    user&.authenticate(password)
  end

  def authenticate(password)
    if password_digest.present?
      return self if authenticate_bcrypt(password)
      return nil
    end

    return nil unless authenticate_legacy(password)

    transaction do
      self.password = password
      self.crypted_password = nil
      self.salt = nil
      save!(validate: false)
    end

    self
  end

  # TODO: Do we really want to have downcase logins only?
  def login=(value)
    write_attribute :login, (value ? value.downcase : nil)
  end

  def email=(value)
    write_attribute :email, (value ? value.downcase : nil)
  end
  
  def is_admin?
    roles.include?("admin")
  end

  # Compatibility shims for the users form, which posts user[admin] as a
  # checkbox. Goes away when that form learns about roles.
  def admin
    is_admin?
  end

  def admin?
    is_admin?
  end

  def admin=(value)
    if ActiveModel::Type::Boolean.new.cast(value)
      self.roles = (roles | ["admin"])
    else
      self.roles = (roles - ["admin"])
    end
  end

  def redaktion?
    roles.include?("redaktion")
  end

  def alumni?
    roles.include?("alumni")
  end

  def role_group
    return :alumni    if alumni?
    return :admin     if is_admin?
    return :redaktion if redaktion?
    :editor
  end

  # Human-readable role names, for the list and the forms.
  def role_labels
    roles.map { |r| I18n.t("users.roles.#{r}", :default => r) }
  end

  def may_change_live?(subject)
    return true unless subject.restricted?
    redaktion?
  end

  def may_change_live_at?(path)
    return true if path.nil?
    return true unless Node.restricted_path?(path)
    redaktion?
  end

  def save_witnessed(actor:)
    saved = false
    transaction do
      saved = save
      raise ActiveRecord::Rollback unless saved
      NodeAction.record!(:participants => [self], :user => actor,
                          :action => "user_create", :target_login => login)
    end
    saved
  end

  def deactivate!(actor:)
    return false if alumni?
    return false if actor == self
    transaction do
      update_column(:roles, (roles | ["alumni"]).sort)
      NodeAction.record!(:participants => [self], :user => actor,
                          :action => "user_deactivate", :target_login => login)
    end
    true
  end

  def reactivate!(actor:)
    return false unless alumni?
    transaction do
      update_column(:roles, (roles - ["alumni"]).sort)
      NodeAction.record!(:participants => [self], :user => actor,
                          :action => "user_reactivate", :target_login => login)
    end
    true
  end

  def grant_redaktion!(actor:)
    return :already if redaktion?
    return :no_second_factor unless otp_enrolled?

    transaction do
      update_column(:roles, (roles | ["redaktion"]).sort)
      NodeAction.record!(:participants => [self], :user => actor,
                          :action => "redaktion_grant", :target_login => login)
    end
    :granted
  end

  def revoke_redaktion!(actor:)
    return :already unless redaktion?
    return :self unless actor != self

    transaction do
      update_column(:roles, (roles - ["redaktion"]).sort)
      NodeAction.record!(:participants => [self], :user => actor,
                          :action => "redaktion_revoke", :target_login => login)
    end
    :revoked
  end

  def grant_admin!(actor:)
    return :already if is_admin?
    return :no_second_factor unless otp_enrolled?

    transaction do
      update_column(:roles, (roles | ["admin"]).sort)
      NodeAction.record!(:participants => [self], :user => actor,
                          :action => "admin_grant", :target_login => login)
    end
    :granted
  end

  def revoke_admin!(actor:)
    return :already unless is_admin?
    return :self unless actor != self

    transaction do
      update_column(:roles, (roles - ["admin"]).sort)
      NodeAction.record!(:participants => [self], :user => actor,
                          :action => "admin_revoke", :target_login => login)
    end
    :revoked
  end

  def update_roles!(desired, actor:)
    desired = Array(desired).map(&:to_s) & ROLES
    refusals = []

    transaction do
      refusals << :admin_not_self         if is_admin?  && !desired.include?("admin")     && actor == self
      refusals << :redaktion_not_self     if redaktion? && !desired.include?("redaktion") && actor == self
      refusals << :cannot_deactivate_self if !alumni?   &&  desired.include?("alumni")    && actor == self
      refusals << :admin_needs_otp        if !is_admin?  && desired.include?("admin")     && !otp_enrolled?
      refusals << :redaktion_needs_otp    if !redaktion? && desired.include?("redaktion") && !otp_enrolled?

      raise ActiveRecord::Rollback if refusals.any?

      revoke_admin!(:actor => actor)     if is_admin?   && !desired.include?("admin")
      revoke_redaktion!(:actor => actor) if redaktion?  && !desired.include?("redaktion")
      reactivate!(:actor => actor)       if alumni?     && !desired.include?("alumni")

      grant_admin!(:actor => actor)     if !is_admin?  && desired.include?("admin")
      grant_redaktion!(:actor => actor) if !redaktion? && desired.include?("redaktion")

      deactivate!(:actor => actor) if !alumni? && desired.include?("alumni")
    end

    refusals
  end

  # otp_secret present == enrolled. otp_pending_secret holds the secret
  # between QR display and first-code confirmation. otp_consumed_timestep
  # makes every accepted code single-use (replay guard within the drift
  # window).

  def otp_enrolled?
    otp_secret.present?
  end

  # Starts (or restarts) enrollment. Returns the provisioning URI the QR
  # encodes; otp_pending_secret itself doubles as the manual-entry string.
  def begin_otp_enrollment!
    update!(:otp_pending_secret => ROTP::Base32.random)
    pending_otp_provisioning_uri
  end

  def pending_otp_provisioning_uri
    return nil if otp_pending_secret.blank?
    ROTP::TOTP.new(otp_pending_secret, :issuer => OTP_ISSUER)
              .provisioning_uri(login)
  end

  # Confirms enrollment with the first generated code. Promotion and
  # witnessing are one transaction; the consumed timestep is recorded so
  # the confirmation code cannot be replayed at login.
  def confirm_otp_enrollment!(code, actor: self)
    return false if otp_pending_secret.blank?
    timestep = ROTP::TOTP.new(otp_pending_secret)
                          .verify(code.to_s.strip,
                                  :drift_behind => OTP_DRIFT,
                                  :drift_ahead  => OTP_DRIFT)
    return false unless timestep

    transaction do
      update!(:otp_secret => otp_pending_secret,
               :otp_pending_secret => nil,
               :otp_consumed_timestep => timestep)
      NodeAction.record!(:participants => [self], :user => actor,
                          :action => "otp_enroll", :target_login => login)
    end
    true
  end

  # Login-time verification. Each code is accepted at most once.
  def verify_otp!(code)
    return false unless otp_enrolled?
    timestep = ROTP::TOTP.new(otp_secret)
                          .verify(code.to_s.strip,
                                  :drift_behind => OTP_DRIFT,
                                  :drift_ahead  => OTP_DRIFT,
                                  :after => otp_consumed_timestep)
    return false unless timestep

    update!(:otp_consumed_timestep => timestep)
    true
  end

  # Self-service disable and administrative reset share one witnessed
  # teardown; the verb records which of the two it was. The controller
  # is responsible for the self-service guards (password + current code).
  def disable_otp!(actor:)
    verb = (actor == self) ? "otp_disable" : "otp_reset"
    transaction do
      update!(:otp_secret => nil, :otp_pending_secret => nil,
               :otp_consumed_timestep => nil)
      NodeAction.record!(:participants => [self], :user => actor,
                          :action => verb, :target_login => login)
    end
    true
  end

  def staleness_tier(now = Time.zone.now)
    return nil if alumni?
    return :never if last_login_at.nil?

    return :red   if last_login_at <= now - STALE_RED_YEARS.years
    return :amber if roles.any? && last_login_at <= now - STALE_AMBER_YEARS.years
    nil
    end
  private

    def roles_are_known
      unknown = roles.to_a - ROLES
      errors.add(:roles, :unknown, :list => unknown.join(", ")) if unknown.any?
    end

    def admin_needs_second_factor
      return unless roles.include?("admin")
      return if otp_secret.present?
      return if persisted? && roles_in_database.to_a.include?("admin")
      errors.add(:roles, :admin_needs_otp)
    end
end