Skip to content

Commit

Permalink
removes passwordless use from Registration, makes tokens longer for r…
Browse files Browse the repository at this point in the history
…egistration
  • Loading branch information
fermion committed Jan 14, 2024
1 parent 594e459 commit 3589bd3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def create
def register
registration = Registration.find_by!(identifier: params[:id])

if registration.token_digest == Passwordless.digest(params[:token])
if registration.valid_token_digest?(params[:token])
registration.register!
sign_in(create_passwordless_session(registration.user))
redirect_to root_url, notice: "Thanks for registering! You're now signed in."
Expand Down
27 changes: 23 additions & 4 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RegistrationNotAvailableError < StandardError; end
attr_reader :token

def token=(plaintext)
self.token_digest = Passwordless.digest(plaintext)
self.token_digest = Registration.digest(plaintext)
@token = (plaintext)
end

Expand Down Expand Up @@ -51,21 +51,40 @@ def to_param
identifier
end

def valid_token_digest?(token_param)
token_digest == Registration.digest(token_param)
end

def self.digest(string)
key = ActiveSupport::KeyGenerator.new(
Rails.application.secret_key_base
).generate_key(
Rails.application.credentials[Rails.env].registration_salt
)
OpenSSL::HMAC.hexdigest("SHA256", key, string)
end

private

def token_digest_available?(token_digest)
Registration.available.where(token_digest: token_digest).none?
end

def generate_token
token = SecureRandom.hex(16)
end

def set_defaults
self.expires_at ||= Passwordless.config.expires_at.call
self.expires_at ||= 1.day.from_now

return if self.token_digest

self.token, self.token_digest = loop {
token = Passwordless.config.token_generator.call(self)
digest = Passwordless.digest(token)
token = generate_token
digest = Registration.digest(token)
break [token, digest] if token_digest_available?(digest)
}
end


end
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GgOqOAJqIgoG5LRaRBpnJ+BrP/2o7NgBJJz5dfEvZfX4haoEYbrpDJ/2z/BK4Wd/TAg2q6LfYWr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