-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from npauzenga/tasks/encryptor-model
Tasks/encryptor-model + CreatePasswordReset Token Interactor
- Loading branch information
Showing
4 changed files
with
101 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
class CreatePasswordResetToken | ||
include Interactor | ||
class CreatePasswordResetToken < StandardInteraction | ||
def validate_input | ||
context.fail!(errors: "invalid input") unless context.user | ||
end | ||
|
||
def execute | ||
context.user.update(reset_digest: Encryptor.generate_token[0]) | ||
end | ||
|
||
def call | ||
def validate_output | ||
context.fail!(errors: "server error") unless context.user.reset_digest | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Encryptor | ||
include BCrypt | ||
|
||
TOKEN_KEY = Rails.application.secrets.secret_key_base | ||
TOKEN_HASH = "SHA512" | ||
|
||
def self.digest_token(token) | ||
OpenSSL::HMAC.hexdigest(TOKEN_HASH, TOKEN_KEY, token).to_s | ||
end | ||
|
||
def self.generate_token | ||
raw = SecureRandom.urlsafe_base64.to_s | ||
[digest_token(raw), raw] | ||
end | ||
|
||
def self.tokens_match?(raw_token, enc_token) | ||
Encryptor.digest_token(raw_token) == enc_token | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require "rails_helper" | ||
include BCrypt | ||
|
||
RSpec.describe Encryptor do | ||
context "methods" do | ||
describe "self.digest_token" do | ||
subject do | ||
described_class.digest_token("test") | ||
end | ||
|
||
it "returns a string" do | ||
is_expected.to be_a(String) | ||
end | ||
end | ||
|
||
describe "self.generate_token" do | ||
subject do | ||
described_class.generate_token | ||
end | ||
|
||
it "returns a raw and a hashed token" do | ||
is_expected.to be_an(Array) | ||
end | ||
|
||
it "sets the hashed token correctly" do | ||
enc, raw = described_class.generate_token | ||
expect(described_class.digest_token(raw)).to eq(enc) | ||
end | ||
end | ||
|
||
describe "self.tokens_match?" do | ||
context "when successful" do | ||
subject do | ||
enc, raw = described_class.generate_token | ||
described_class.tokens_match?(raw, enc) | ||
end | ||
|
||
it "returns true when the tokens match" do | ||
is_expected.to be_truthy | ||
end | ||
end | ||
|
||
context "when unsuccessful" do | ||
subject do | ||
raw = described_class.generate_token | ||
enc = "nope" | ||
described_class.tokens_match?(raw[0], enc) | ||
end | ||
|
||
it "returns false when the tokens don't match" do | ||
is_expected.to be_falsey | ||
end | ||
end | ||
end | ||
end | ||
end |