Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for encoding extra information in magic token #27

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions devise-passwordless.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = ">= 2.1.0"

spec.add_dependency "devise"
spec.add_development_dependency "timecop"
end
10 changes: 6 additions & 4 deletions lib/devise/passwordless/login_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ module Devise::Passwordless
class LoginToken
class InvalidOrExpiredTokenError < StandardError; end

def self.encode(resource)
def self.encode(resource, extra=nil)
now = Time.current
len = ActiveSupport::MessageEncryptor.key_len
salt = SecureRandom.random_bytes(len)
key = ActiveSupport::KeyGenerator.new(self.secret_key).generate_key(salt, len)
crypt = ActiveSupport::MessageEncryptor.new(key, serializer: JSON)
encrypted_data = crypt.encrypt_and_sign({
data = {
data: {
resource: {
key: resource.to_key,
email: resource.email,
},
},
created_at: now.to_f,
})
}
data[:data][:extra] = extra if extra
encrypted_data = crypt.encrypt_and_sign(data)
salt_base64 = Base64.strict_encode64(salt)
"#{salt_base64}:#{encrypted_data}"
end
Expand Down Expand Up @@ -54,4 +56,4 @@ def self.secret_key
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/devise/strategies/magic_link_authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def authenticate!
if validate(resource)
remember_me(resource)
resource.after_magic_link_authentication
env['warden.magic_link_extra'] = data['data'].delete('extra')
success!(resource)
else
fail!(:magic_link_invalid)
Expand Down
56 changes: 56 additions & 0 deletions spec/devise/passwordless/login_token_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'timecop'

RSpec.describe Devise::Passwordless::LoginToken do
describe 'encryption and decryption' do
let(:user) { double(:user, email: 'email@example.com', to_key: 12345) }

before do
Timecop.freeze(Time.utc(2023, 1, 1))

allow(described_class).to receive(:secret_key).and_return('sekret')
end

after do
Timecop.return
end

it 'can encrypt and decrypt a resource', freeze_time: Time.utc(2023, 1, 1) do
token = described_class.encode(user)
decrypted = described_class.decode(token)

expected_decrypt =
{
"created_at" => 1672531200.0,
"data" => {
"resource" => {
"email" => "email@example.com",
"key" => 12345
}
}
}

expect(decrypted).to eq(expected_decrypt)
end

it 'can encrypt and decrpt a resource with extra data supplied', freeze_time: Time.utc(2023, 1, 1) do
token = described_class.encode(user, { foo: :bar })
decrypted = described_class.decode(token)

expected_decrypt =
{
"created_at" => 1672531200.0,
"data" => {
"resource" => {
"email" => "email@example.com",
"key" => 12345
},
"extra" => {
"foo" => "bar"
},
}
}

expect(decrypted).to eq(expected_decrypt)
end
end
end