remember_me 0.0.1

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: a42680f6177ae0ca5cc01c7811058949a2ed7095
4
+ data.tar.gz: 250d11dc425905c81dcd9c108b68633620906631
5
+ SHA512:
6
+ metadata.gz: c942be2252b30de128c564e7d74a9dab48bad605be679ae9991fe41e6f1b2a9ed07d1d1506b70bcf88fab4841c2c2e4fd73766c285d9c555856f4b8f2eefbcfa
7
+ data.tar.gz: 9bfb468798bf19a50f2744afaab404c1c35d49e7e68656606fcd38b442748279bb8a5dde3e873fb16f7d02193d8d49840f9fc6ea7413bef868018224f2d31062
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ vendor
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --order random
data/.travis.yml ADDED
@@ -0,0 +1,23 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+
7
+ before_install:
8
+ - gem update bundler
9
+
10
+ script:
11
+ - bundle exec rake spec
12
+
13
+ notifications:
14
+ email:
15
+ recipients:
16
+ - linyows@gmail.com
17
+ on_success: change
18
+ on_failure: always
19
+ irc:
20
+ on_success: change
21
+ on_failure: always
22
+ channels:
23
+ - "irc.freenode.org#linyows"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in remember_me.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 linyows
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,92 @@
1
+ RememberMe
2
+ ==========
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/remember_me.png)][gem]
5
+ [![Build Status](https://secure.travis-ci.org/linyows/remember_me.png?branch=master)][travis]
6
+ [![Dependency Status](https://gemnasium.com/linyows/remember_me.png?travis)][gemnasium]
7
+ [![Code Climate](https://codeclimate.com/github/linyows/remember_me.png)][codeclimate]
8
+ [![Coverage Status](https://coveralls.io/repos/linyows/remember_me/badge.png?branch=master)][coveralls]
9
+
10
+ [gem]: https://rubygems.org/gems/remember_me
11
+ [travis]: http://travis-ci.org/linyows/remember_me
12
+ [gemnasium]: https://gemnasium.com/linyows/remember_me
13
+ [codeclimate]: https://codeclimate.com/github/linyows/remember_me
14
+ [coveralls]: https://coveralls.io/r/linyows/remember_me
15
+
16
+ RememberMe is a simple remember-me login solution on Rails.
17
+ Manages generating and clearing a token for remembering the user from a saved cookie.
18
+
19
+ ![RememberMe](http://www.movieposterdb.com/posters/10_04/2010/1403981/l_1403981_94a56aed.jpg)
20
+
21
+ Installation
22
+ ------------
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ gem 'remember_me'
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install remember_me
35
+
36
+ Usage
37
+ -----
38
+
39
+ controllers/application_controller.rb:
40
+
41
+ ```ruby
42
+ class ApplicationController < ActionController::Base
43
+ include RememberMe::Controller
44
+ end
45
+ ```
46
+
47
+ models/user.rb:
48
+
49
+ ```ruby
50
+ class User
51
+ include RememberMe::Model
52
+ end
53
+ ```
54
+
55
+ controllers/sessions_controller.rb:
56
+
57
+ ```ruby
58
+ class SessionsController < ApplicationController
59
+ def create
60
+ # ... authenticated ...
61
+
62
+ self.current_user = @user
63
+ remember_me(current_user) if remember_me?
64
+
65
+ redirect_to root_path, notice: 'Success'
66
+ end
67
+
68
+ def destroy
69
+ forget_me(current_user)
70
+ self.current_user = nil
71
+ redirect_to login_path, notice: 'Success'
72
+ end
73
+ ```
74
+
75
+ Contributing
76
+ ------------
77
+
78
+ 1. Fork it
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
83
+
84
+ Author
85
+ ------
86
+
87
+ - [linyows](https://github.com/linyows)
88
+
89
+ License
90
+ -------
91
+
92
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,2 @@
1
+ require "remember_me/model"
2
+ require "remember_me/controller"
@@ -0,0 +1,48 @@
1
+ module RememberMe
2
+ module Controller
3
+ def cookie_values
4
+ Rails.configuration.session_options.slice(:path, :domain, :secure)
5
+ end
6
+
7
+ def remember_me?
8
+ params.has_key?(:remember_me) && params[:remember_me]
9
+ end
10
+
11
+ def scope_of(resource)
12
+ resource.class.to_s.underscore
13
+ end
14
+
15
+ def remember_me(resource)
16
+ resource.remember_me!
17
+ cookies.signed[remember_key(scope_of resource)] = remember_cookie_values(resource)
18
+ end
19
+
20
+ def forget_me(resource)
21
+ resource.forget_me!
22
+ cookies.delete remember_key(scope_of resource), forget_cookie_values(resource)
23
+ end
24
+
25
+ def remember_cookie_values(resource)
26
+ options = { httponly: true }
27
+ options.merge!(forget_cookie_values(resource))
28
+ options.merge!(
29
+ value: resource.class.serialize_into_cookie(resource),
30
+ expires: resource.remember_expires_at
31
+ )
32
+ end
33
+
34
+ def forget_cookie_values(resource)
35
+ cookie_values.merge!(resource.rememberable_options)
36
+ end
37
+
38
+ def remember_key(scope)
39
+ # remember_token_for_user
40
+ :"_rtf#{scope}"
41
+ end
42
+
43
+ def remember(scope)
44
+ cookie = cookies.signed[remember_key(scope)]
45
+ "#{scope.classify}".constantize.serialize_from_cookie(*cookie) if cookie
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,53 @@
1
+ module RememberMe
2
+ module Model
3
+ extend ::ActiveSupport::Concern
4
+
5
+ included { field :remember_created_at, type: Time } if defined?(Mongoid)
6
+
7
+ def remember_me!
8
+ self.remember_created_at = Time.now.utc
9
+ save(validate: false) if self.changed?
10
+ end
11
+
12
+ def forget_me!
13
+ return if self.remember_created_at.nil?
14
+ self.remember_created_at = nil
15
+ save(validate: false)
16
+ end
17
+
18
+ def remember_expired?
19
+ remember_created_at.nil? || (remember_expires_at <= Time.now.utc)
20
+ end
21
+
22
+ def remember_expires_at
23
+ remember_created_at + self.class.remember_for
24
+ end
25
+
26
+ def rememberable_options
27
+ self.class.rememberable_options
28
+ end
29
+
30
+ def rememberable_value
31
+ Digest::SHA1.hexdigest "#{self.id}"
32
+ end
33
+
34
+ module ClassMethods
35
+ def serialize_into_cookie(record)
36
+ [record.id, record.rememberable_value]
37
+ end
38
+
39
+ def serialize_from_cookie(id, remember_token)
40
+ record = where(id: id).first
41
+ record if record && record.rememberable_value == remember_token && !record.remember_expired?
42
+ end
43
+
44
+ def rememberable_options
45
+ {}
46
+ end
47
+
48
+ def remember_for
49
+ 2.weeks
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module RememberMe
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'remember_me/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'remember_me'
8
+ spec.version = RememberMe::VERSION
9
+ spec.authors = ['linyows']
10
+ spec.email = ['linyows@gmail.com']
11
+ spec.description = %q{Manages generating and clearing a token for remembering the user from a saved cookie.}
12
+ spec.summary = %q{RememberMe is a simple remember-me login solution on Rails.}
13
+ spec.homepage = 'https://github.com/linyows/remember_me'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
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_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'simplecov'
25
+ spec.add_development_dependency 'coveralls'
26
+
27
+ spec.add_dependency 'rails', '>= 3.2'
28
+ spec.add_dependency 'mongoid', '>= 3'
29
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,17 @@
1
+ if ENV["CI"]
2
+ require 'simplecov'
3
+ require 'coveralls'
4
+
5
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ Coveralls::SimpleCov::Formatter
8
+ ]
9
+ SimpleCov.start
10
+ end
11
+
12
+ require 'active_support/concern'
13
+ require 'active_record'
14
+ require 'mongoid'
15
+ require 'remember_me'
16
+ require 'rspec'
17
+
@@ -0,0 +1,27 @@
1
+ require 'helper'
2
+
3
+ describe RememberMe::Model do
4
+ before :all do
5
+ class User
6
+ include Mongoid::Document
7
+ include RememberMe::Model
8
+ def save(arg = {})
9
+ true
10
+ end
11
+ end
12
+ @model = User.new
13
+ end
14
+
15
+ it { expect(@model.fields.has_key? 'remember_created_at').to be_true }
16
+ it { expect(@model.fields['remember_created_at'].options[:type]).to eq Time }
17
+ it { expect(@model.respond_to? :remember_me!).to be_true }
18
+
19
+ describe '#remember_me!' do
20
+ before { @model.remember_me! }
21
+ it { expect(@model.remember_created_at.class).to eq Time }
22
+ end
23
+
24
+ describe '#forget_me!' do
25
+ it { expect(@model.respond_to? :forget_me!).to be_true }
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remember_me
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - linyows
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '3.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mongoid
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '3'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '3'
111
+ description: Manages generating and clearing a token for remembering the user from
112
+ a saved cookie.
113
+ email:
114
+ - linyows@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - .travis.yml
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - lib/remember_me.rb
127
+ - lib/remember_me/controller.rb
128
+ - lib/remember_me/model.rb
129
+ - lib/remember_me/version.rb
130
+ - remember_me.gemspec
131
+ - spec/helper.rb
132
+ - spec/model_spec.rb
133
+ homepage: https://github.com/linyows/remember_me
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.0.0
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: RememberMe is a simple remember-me login solution on Rails.
157
+ test_files:
158
+ - spec/helper.rb
159
+ - spec/model_spec.rb