active_model-email_confirmation 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bbbd8c6fd557d779d89dbaa280361da273368eed
4
+ data.tar.gz: aa48ed884b58b84e9085d2bcfc8a9c0be1e21086
5
+ SHA512:
6
+ metadata.gz: 98641f6d16d2ffbd31498617255165f0139c4fc758b5e6c7872d092815a4154bb341abbfb19bc26769dfe353db9895087afd1c1e028323e192580aac72d4a26f
7
+ data.tar.gz: f2a10b220d387bf551ec27ba3079ff601a8117ea006214840cf9d890e76d2e36c5f7a9c108e5343c8463bd0b2585ec112cd4f1934fb6a6aad2b4f63ee747a0a3
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kuba Kuźma
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # ActiveModel::EmailConfirmation
2
+
3
+ `ActiveModel::EmailConfirmation` is a lightweight email confirmation model implemented on top of `ActiveModel::Model`. It does not require storing any additional information in the database. Resulting token is signed by `ActiveSupport::MessageVerifier` class, using `secret_key_base` and salt.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem "active_model-email_confirmation"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_model-email_confirmation
18
+
19
+ ## Usage
20
+
21
+ The most popular workflow is:
22
+
23
+ class UsersController < ApplicationController
24
+ def create
25
+ # ...
26
+ @email_confirmation = ActiveModel::EmailConfirmation.new(user: @user)
27
+ UserMailer.confirm_email(@user.email, @email_confirmation.token).deliver
28
+ # ...
29
+ end
30
+ end
31
+
32
+ class EmailConfirmationsController < ApplicationController
33
+ def show
34
+ # find raises TokenInvalid, EmailInvalid exceptions
35
+ @email_confirmation = ActiveModel::EmailConfirmation.find(params[:id])
36
+ @user = @email_confirmation.user
37
+ @user.update(confirmed_at: DateTime.now)
38
+ # ...
39
+ rescue ActiveModel::EmailConfirmation::Error
40
+ raise ActiveRecord::RecordNotFound # display 404
41
+ end
42
+ end
43
+
44
+ If you don't like the default behavior, you can always inherit the model and override some defaults:
45
+
46
+ class EmailConfirmation < ActiveModel::EmailConfirmation
47
+ def email=(email)
48
+ @email = email
49
+ @user = Admin.find_by(email: email)
50
+ end
51
+ end
52
+
53
+ ## Copyright
54
+
55
+ Copyright © 2014 Kuba Kuźma. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs += %w[lib test]
6
+ t.test_files = FileList["test/*_test.rb"]
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_model/email_confirmation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_model-email_confirmation"
8
+ spec.version = ActiveModel::EmailConfirmation::VERSION
9
+ spec.authors = ["Kuba Kuźma"]
10
+ spec.email = ["kuba@jah.pl"]
11
+ spec.description = %q{Simple email confirmation model implemented on top of ActiveModel::Model}
12
+ spec.summary = %q{Simple email confirmation model implemented on top of ActiveModel::Model}
13
+ spec.homepage = "https://github.com/cowbell/active_model-email_confirmation"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activemodel", ">= 4.0.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,55 @@
1
+ require "active_model/email_confirmation/version"
2
+ require "active_model/email_confirmation/error"
3
+ require "active_model"
4
+
5
+ module ActiveModel
6
+ class EmailConfirmation
7
+ include Model
8
+
9
+ attr_reader :email
10
+ attr_writer :user
11
+
12
+ validates :email, presence: true
13
+ validate :existence, if: -> { email.present? }
14
+ delegate :id, to: :user, prefix: true, allow_nil: true
15
+
16
+ def email=(email)
17
+ remove_instance_variable(:@user) if defined?(@user)
18
+ @email = email
19
+ end
20
+
21
+ def user
22
+ return @user if defined?(@user)
23
+ @user = User.find_by(email: email)
24
+ end
25
+
26
+ def token
27
+ self.class.generate_token(user.email)
28
+ end
29
+
30
+ def self.find(token)
31
+ email = verify_token(token)
32
+ new(email: email).tap { |email_confirmation| raise EmailInvalid if email_confirmation.invalid? }
33
+ end
34
+
35
+ private
36
+
37
+ def self.message_verifier
38
+ Rails.application.message_verifier("email confirmation salt")
39
+ end
40
+
41
+ def self.generate_token(*args)
42
+ Base64.urlsafe_encode64(message_verifier.generate(*args))
43
+ end
44
+
45
+ def self.verify_token(string)
46
+ message_verifier.verify(Base64.urlsafe_decode64(string))
47
+ rescue ActiveSupport::MessageVerifier::InvalidSignature, ArgumentError
48
+ raise TokenInvalid
49
+ end
50
+
51
+ def existence
52
+ errors.add(:email, :invalid) if user.blank?
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveModel
2
+ class EmailConfirmation
3
+ class Error < StandardError; end
4
+ class EmailInvalid < Error; end
5
+ class TokenInvalid < Error; end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ class EmailConfirmation
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,71 @@
1
+ require "test_helper"
2
+
3
+ class User
4
+ attr_accessor :id, :email
5
+
6
+ RECORDS = {
7
+ {email: "alice@example.com"} => {id: 1, email: "alice@example.com"}
8
+ }
9
+
10
+ def self.find_by(options)
11
+ attributes = RECORDS[options]
12
+ new(attributes) if attributes.present?
13
+ end
14
+
15
+ def initialize(options)
16
+ self.id = options[:id]
17
+ self.email = options[:email]
18
+ end
19
+ end
20
+
21
+ module ActiveModel
22
+ def EmailConfirmation.message_verifier
23
+ key_generator = ActiveSupport::KeyGenerator.new("12345678901234567890123456789012345678901234567890123456789012345678901234567890", iterations: 1000)
24
+ secret = key_generator.generate_key("email confirmation salt")
25
+ ActiveSupport::MessageVerifier.new(secret)
26
+ end
27
+ end
28
+
29
+ class EmailConfirmationTest < Test::Unit::TestCase
30
+ include ActiveModel::Lint::Tests
31
+
32
+ def setup
33
+ @model = @email_confirmation = ActiveModel::EmailConfirmation.new
34
+ end
35
+
36
+ def test_basic_workflow
37
+ @email_confirmation.email = "alice@example.com"
38
+ @email_confirmation.valid?
39
+ token = @email_confirmation.token
40
+ assert token.present?
41
+ assert !token.include?("/")
42
+ email_confirmation = ActiveModel::EmailConfirmation.find(token)
43
+ assert_equal @email_confirmation.email, email_confirmation.email
44
+ assert email_confirmation.user.present?
45
+ end
46
+
47
+ def test_is_invalid_with_invalid_email
48
+ @email_confirmation.email = "invalid@example.com"
49
+ assert @email_confirmation.invalid?
50
+ assert @email_confirmation.errors[:email].present?
51
+ end
52
+
53
+ def test_is_invalid_without_email
54
+ @email_confirmation.email = nil
55
+ assert @email_confirmation.invalid?
56
+ assert @email_confirmation.errors[:email].present?
57
+ end
58
+
59
+ def test_find_raises_exception_with_invalid_email
60
+ token = ActiveModel::EmailConfirmation.generate_token("invalid@example.com")
61
+ assert_raises(ActiveModel::EmailConfirmation::EmailInvalid) { ActiveModel::EmailConfirmation.find(token) }
62
+ end
63
+
64
+ def test_find_raises_exception_with_invalid_token
65
+ assert_raises(ActiveModel::EmailConfirmation::TokenInvalid) { ActiveModel::EmailConfirmation.find("invalidtoken") }
66
+ end
67
+
68
+ def test_find_raises_exception_with_non_base64_token
69
+ assert_raises(ActiveModel::EmailConfirmation::TokenInvalid) { ActiveModel::EmailConfirmation.find("%%%%%%%%%") }
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ require "test/unit"
2
+ require "active_model/email_confirmation"
3
+ require "ostruct"
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model-email_confirmation
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kuba Kuźma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple email confirmation model implemented on top of ActiveModel::Model
56
+ email:
57
+ - kuba@jah.pl
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - active_model-email_confirmation.gemspec
68
+ - lib/active_model/email_confirmation.rb
69
+ - lib/active_model/email_confirmation/error.rb
70
+ - lib/active_model/email_confirmation/version.rb
71
+ - test/email_confirmation_test.rb
72
+ - test/test_helper.rb
73
+ homepage: https://github.com/cowbell/active_model-email_confirmation
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.0
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Simple email confirmation model implemented on top of ActiveModel::Model
97
+ test_files:
98
+ - test/email_confirmation_test.rb
99
+ - test/test_helper.rb