devise_capturable 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +165 -0
- data/Rakefile +1 -0
- data/devise_capturable.gemspec +25 -0
- data/lib/assets/javascripts/devise_capturable.js +45 -0
- data/lib/devise_capturable/api.rb +29 -0
- data/lib/devise_capturable/locales/en.yml +5 -0
- data/lib/devise_capturable/model.rb +48 -0
- data/lib/devise_capturable/strategy.rb +54 -0
- data/lib/devise_capturable/version.rb +5 -0
- data/lib/devise_capturable/view_helpers.rb +20 -0
- data/lib/devise_capturable.rb +28 -0
- data/rails/init.rb +1 -0
- data/spec/api_spec.rb +29 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/strategy_spec.rb +57 -0
- data/spec/view_helpers_spec.rb +15 -0
- metadata +135 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Rune Skjoldborg Madsen
|
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,165 @@
|
|
1
|
+
# Devise::Capturable
|
2
|
+
|
3
|
+
`Devise::Capturable` is a gem that makes it possible to use the Janrain Engage user registration widget, while still having a Devise authentication setup with a Rails `User` model.
|
4
|
+
|
5
|
+
In the following I use the name `User` for the Devise user model, but it will work with any Devise-enabled model.
|
6
|
+
|
7
|
+
## Flow
|
8
|
+
|
9
|
+
This gem will replace the normal Devise `registrations` and `sessions` views with the Janrain user registration widget. The flow is as follows.
|
10
|
+
|
11
|
+
* User clicks a login link
|
12
|
+
* User is presented with the user registration widget and either registers or logs in
|
13
|
+
* The gem automatically listens to the widget's `onCaptureLoginSuccess` event, grabs the oauth code, and makes a `POST` request with the OAuth code to the `sessions_controller`
|
14
|
+
* The gem will grab the oauth token in the `session_controller`, load the user data from the Capture API (to ensure the validity of the token), and either create a new user or log in the user if the user already exists in the Rails DB.
|
15
|
+
|
16
|
+
## Setup
|
17
|
+
|
18
|
+
You will need to perform these steps to setup the gem.
|
19
|
+
|
20
|
+
#### Add gem to Gemfile
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem "devise_capturable", :git => "git://github.com/runemadsen/devise-capturable.git"
|
24
|
+
```
|
25
|
+
|
26
|
+
#### Add `:capturable` to your `User` model
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class User < ActiveRecord::Base
|
30
|
+
devise ..., :capturable
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Update initializer
|
35
|
+
|
36
|
+
In your `config/initializers/devise.rb` initializer, add the following settings.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
Devise.setup do |config|
|
40
|
+
config.capturable_server = "https://myapp.janraincapture.com"
|
41
|
+
config.capturable_client_id = "myclientid"
|
42
|
+
config.capturable_client_secret = "myclientsecret"
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Add Gem Javascript to you asset pipeline
|
47
|
+
|
48
|
+
The gem ships with a JS file that automatically subscribes to the login event and makes a POST to the `sessions_controller`. Make sure to include it in you asset pipeline.
|
49
|
+
|
50
|
+
```javascript
|
51
|
+
//= require devise_capturable
|
52
|
+
```
|
53
|
+
|
54
|
+
#### Add Janrain Javascript
|
55
|
+
|
56
|
+
Now add the script provided by Janrain with `janrainDefaultSettings()`, `janrainInitLoad()`, and all the HTML template strings to your application layout. You might need to include a different script in your development environment.
|
57
|
+
|
58
|
+
Keep in mind that this script does not need to include any `onCaptureLoginSuccess` event listeners or other event code. The gem will handle that for you.
|
59
|
+
|
60
|
+
```html
|
61
|
+
<html>
|
62
|
+
<head>
|
63
|
+
...
|
64
|
+
<script type="text/javascript" id="janrainCaptureDevScript">
|
65
|
+
// YOUR CODE HERE
|
66
|
+
</script>
|
67
|
+
</head>
|
68
|
+
<body>
|
69
|
+
...
|
70
|
+
</body>
|
71
|
+
</html>
|
72
|
+
```
|
73
|
+
|
74
|
+
#### Add Janrain CSS
|
75
|
+
|
76
|
+
Now add the Janrain CSS to your asset pipeline. Simply copy `janrain.css` and `janrain-mobile.css` to `app/assets/stylesheets`.
|
77
|
+
|
78
|
+
#### Add links
|
79
|
+
|
80
|
+
The gem ships with a helper method to show a link that opens the widget. You will use this to show the login / registration form, but use the normal `destroy_user_session_path` helper to log out the user. This is because Devise, not Capture, is controlling the user cookie.
|
81
|
+
|
82
|
+
```erb
|
83
|
+
<ul class="nav">
|
84
|
+
<% if user_signed_in? %>
|
85
|
+
<li><%= link_to 'Logout', destroy_user_session_path, :method=>'delete' %></li>
|
86
|
+
<% else %>
|
87
|
+
<li><%= link_to_capturable "Sign In / Sign Up" %></li>
|
88
|
+
<% end %>
|
89
|
+
</ul>
|
90
|
+
```
|
91
|
+
|
92
|
+
#### Done!
|
93
|
+
|
94
|
+
That's it!
|
95
|
+
|
96
|
+
By default Devise will now create a database record when the user logs in for the first time. On following logins, the user will just be logged in. The only property Devise will save in the user model is the `email` address provided by Capture. You can however change this (See "Changing Defaults")
|
97
|
+
|
98
|
+
## Automated Settings
|
99
|
+
|
100
|
+
The Janrain User Registration widget relies on settings that are 1) never used and 2) breaks the widget if they are not present. To circumvent this madness, this gem will automatically set a bunch of janrain setting variables for you:
|
101
|
+
|
102
|
+
```javascript
|
103
|
+
// these settings will always be the same
|
104
|
+
janrain.settings.capture.flowName = 'signIn';
|
105
|
+
janrain.settings.capture.responseType = 'code';
|
106
|
+
|
107
|
+
// these settings are never used but crashes the widget if not present
|
108
|
+
janrain.settings.capture.redirectUri = 'http://stupidsettings.com';
|
109
|
+
```
|
110
|
+
|
111
|
+
You can delete these settings from your embed code, as the gem will set them for you. Remember that you still need a `tokenUrl` setting with a whitelisted URL, even though this setting is never used either.
|
112
|
+
|
113
|
+
## Changing defaults
|
114
|
+
|
115
|
+
|
116
|
+
#### Overriding `set_capturable_params`
|
117
|
+
|
118
|
+
There are times where you might want to save more than the `email` of your user in the Rails `User` model. You can override the `set_capturable_params` instance method to do this. Here's an example where I'm also saving the `uuid`. The `capture_data` parameter passed to the function is the Janrain Capture `entity` JSON result that has a bunch of information about the user.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
class User < ActiveRecord::Base
|
122
|
+
devise ..., :capturable
|
123
|
+
attr_accessible ..., :email, uuid
|
124
|
+
def set_capturable_params(capture_data)
|
125
|
+
self.email = capture_data["email"]
|
126
|
+
self.uuid = capture_data["uuid"]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
#### Overriding `find_with_capturable_params`
|
132
|
+
|
133
|
+
When a user logs in, Devise will call the Capture API and try to find a user with the email returned by the API. You can change this by overriding the `find_with_capturable_params` instance method in your `User` model. Here's an example where I'm telling Devise to find the user by the `uuid` instead.
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
def find_with_capturable_params(capture_data)
|
137
|
+
self.find_by_uuid(capture_data["uuid"])
|
138
|
+
end
|
139
|
+
```
|
140
|
+
|
141
|
+
#### Disabling User Creation
|
142
|
+
|
143
|
+
By default the gem will create a user if the user doesn't exist in the system. If you want to disable the creation of new users, and only allow current users to log in, you can disable automatic account creation in your `config/intitializers/devise.rb` initializer.
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
Devise.setup do |config|
|
147
|
+
config.capturable_auto_create_account = false
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
## Example Application
|
152
|
+
|
153
|
+
I've made an example application that demonstrates how to integrate this gem with Rails. You can find it here.
|
154
|
+
|
155
|
+
[Rails Capturable Example](https://github.com/runemadsen/capture_example)
|
156
|
+
|
157
|
+
## Running the Tests
|
158
|
+
|
159
|
+
Run the tests with the `rspec` command.
|
160
|
+
|
161
|
+
## TODO
|
162
|
+
|
163
|
+
* Ability to add the Janrain script to the asset pipeline. However, it relies on an 'id', which makes it harder.
|
164
|
+
* Remove normal Devise sign_in routes? Or leave it up to the user?
|
165
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'devise_capturable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "devise_capturable"
|
8
|
+
gem.version = Devise::Capturable::VERSION
|
9
|
+
gem.authors = ["Rune Skjoldborg Madsen"]
|
10
|
+
gem.email = ["rune@runemadsen.com"]
|
11
|
+
gem.description = %q{Devise::Capturable is a gem that makes it possible to use the Janrain Engage user registration widget, while preserving your Devise authentication setup with your custom user model.}
|
12
|
+
gem.summary = %q{Janrain Capture for Devise}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency("httparty")
|
21
|
+
gem.add_development_dependency("devise")
|
22
|
+
gem.add_development_dependency("rspec")
|
23
|
+
gem.add_development_dependency("rails")
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
function afterJanrainLogin(result)
|
2
|
+
{
|
3
|
+
// create form
|
4
|
+
var form = $('<form accept-charset="UTF-8" action="/users/sign_in" method="post" id="capturable-inject-form"></form>')
|
5
|
+
|
6
|
+
// create hidden div in form
|
7
|
+
var hidden_els = $('<div style="margin:0;padding:0;display:inline"></div>')
|
8
|
+
|
9
|
+
// add utf
|
10
|
+
hidden_els.append('<input name="utf8" type="hidden" value="✓">')
|
11
|
+
|
12
|
+
// grab forgery token
|
13
|
+
var token_name = $("meta[name='csrf-param']").attr('content')
|
14
|
+
var token_val = $("meta[name='csrf-token']").attr('content')
|
15
|
+
if(token_name && token_val)
|
16
|
+
{
|
17
|
+
hidden_els.prepend('<input name="'+token_name +'" type="hidden" value="'+token_val+'">')
|
18
|
+
}
|
19
|
+
|
20
|
+
// append hidden els to form
|
21
|
+
form.append(hidden_els)
|
22
|
+
|
23
|
+
// add oauth code to form
|
24
|
+
form.append('<input id="authorization-code" name="code" type="hidden" value="'+result.authorizationCode+'">')
|
25
|
+
|
26
|
+
janrain.capture.ui.modal.close();
|
27
|
+
|
28
|
+
$('body').append(form);
|
29
|
+
form.submit()
|
30
|
+
}
|
31
|
+
|
32
|
+
function janrainCaptureWidgetOnLoad() {
|
33
|
+
|
34
|
+
// these settings will always be the same
|
35
|
+
janrain.settings.capture.flowName = 'signIn';
|
36
|
+
janrain.settings.capture.responseType = 'code';
|
37
|
+
|
38
|
+
// these settings are never used but crashes the widget if not present
|
39
|
+
janrain.settings.capture.redirectUri = 'http://stupidsettings.com';
|
40
|
+
|
41
|
+
// go go widget go
|
42
|
+
janrain.capture.ui.start();
|
43
|
+
janrain.events.onCaptureLoginSuccess.addHandler(afterJanrainLogin);
|
44
|
+
janrain.events.onCaptureRegistrationSuccess.addHandler(afterJanrainLogin);
|
45
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Capturable
|
5
|
+
|
6
|
+
class API
|
7
|
+
|
8
|
+
include HTTParty
|
9
|
+
format :json
|
10
|
+
#debug_output $stderr
|
11
|
+
|
12
|
+
def self.token(code)
|
13
|
+
post("#{Devise.capturable_server}/oauth/token", :query => {
|
14
|
+
code: code,
|
15
|
+
redirect_uri: "http://stupidsettings.com",
|
16
|
+
grant_type: 'authorization_code',
|
17
|
+
client_id: Devise.capturable_client_id,
|
18
|
+
client_secret: Devise.capturable_client_secret,
|
19
|
+
})
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.entity(token)
|
23
|
+
post("#{Devise.capturable_server}/entity", headers: { 'Authorization' => "OAuth #{token}" })
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Devise
|
2
|
+
|
3
|
+
module Models
|
4
|
+
|
5
|
+
module Capturable
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.class_eval do
|
9
|
+
extend ClassMethods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# This is called from strategy and is used to fill a user model before saving
|
14
|
+
# It defaults to just setting the uuid, but you can override this in your user model
|
15
|
+
def set_capturable_params(capture_data)
|
16
|
+
self.email = capture_data["email"]
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
|
21
|
+
# Configuration params accessible within +Devise.setup+ procedure (in initalizer).
|
22
|
+
#
|
23
|
+
# Devise.setup do |config|
|
24
|
+
# config.capturable_auto_create_account = true
|
25
|
+
# end
|
26
|
+
::Devise::Models.config(self, :capturable_auto_create_account)
|
27
|
+
|
28
|
+
def capturable_auto_create_account?
|
29
|
+
self.capturable_auto_create_account
|
30
|
+
end
|
31
|
+
|
32
|
+
# This is called from strategy and is used to find a user when returning from janrain
|
33
|
+
# It defaults to find_by_uuid, but you can override this in your user model
|
34
|
+
def find_with_capturable_params(capture_data)
|
35
|
+
self.find_by_email(capture_data["email"])
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
def valid_for_capturable(resource, attributes)
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'devise_capturable/api'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
|
5
|
+
module Capturable
|
6
|
+
|
7
|
+
module Strategies
|
8
|
+
|
9
|
+
class Capturable < ::Devise::Strategies::Base
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
valid_controller? && valid_params? && mapping.to.respond_to?(:find_with_capturable_params) && mapping.to.method_defined?(:set_capturable_params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def authenticate!
|
16
|
+
klass = mapping.to
|
17
|
+
|
18
|
+
begin
|
19
|
+
|
20
|
+
token = Devise::Capturable::API.token(params[:code])
|
21
|
+
fail!(:capturable_invalid) unless token['stat'] == 'ok'
|
22
|
+
|
23
|
+
entity = Devise::Capturable::API.entity(token['access_token'])
|
24
|
+
user = klass.find_with_capturable_params(entity["result"])
|
25
|
+
|
26
|
+
unless klass.capturable_auto_create_account?
|
27
|
+
fail!(:capturable_invalid)
|
28
|
+
return
|
29
|
+
end
|
30
|
+
|
31
|
+
user ||= klass.new
|
32
|
+
user.set_capturable_params(entity["result"])
|
33
|
+
user.save(:validate => false)
|
34
|
+
success!(user)
|
35
|
+
rescue Exception => e
|
36
|
+
fail!(:capturable_invalid)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def valid_controller?
|
43
|
+
params[:controller].to_s =~ /sessions/
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid_params?
|
47
|
+
params[:code].present?
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
unless defined?(ActionView)
|
2
|
+
require 'action_view'
|
3
|
+
end
|
4
|
+
|
5
|
+
module Devise
|
6
|
+
module Capturable
|
7
|
+
module Helpers
|
8
|
+
|
9
|
+
include ActionView::Helpers::UrlHelper
|
10
|
+
|
11
|
+
def link_to_capturable(link_text, options={})
|
12
|
+
options = { :id => "capture_signin_link", :class => "capture_modal_open" }.merge(options)
|
13
|
+
link_to(link_text, "#", options)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
::ActionView::Base.send :include, Devise::Capturable::Helpers
|
@@ -0,0 +1,28 @@
|
|
1
|
+
unless defined?(Devise)
|
2
|
+
require 'devise'
|
3
|
+
end
|
4
|
+
|
5
|
+
require 'devise_capturable/model'
|
6
|
+
require 'devise_capturable/strategy'
|
7
|
+
Warden::Strategies.add(:capturable, Devise::Capturable::Strategies::Capturable)
|
8
|
+
require 'devise_capturable/view_helpers'
|
9
|
+
|
10
|
+
module Devise
|
11
|
+
mattr_accessor :capturable_server
|
12
|
+
mattr_accessor :capturable_client_id
|
13
|
+
mattr_accessor :capturable_client_secret
|
14
|
+
mattr_accessor :capturable_auto_create_account
|
15
|
+
@@capturable_auto_create_account = true
|
16
|
+
end
|
17
|
+
|
18
|
+
I18n.load_path.unshift File.join(File.dirname(__FILE__), *%w[devise_capturable locales en.yml])
|
19
|
+
Devise.add_module(:capturable, :strategy => true, :controller => :sessions, :model => 'devise_capturable/model')
|
20
|
+
|
21
|
+
module Devise
|
22
|
+
module Capturable
|
23
|
+
module Rails
|
24
|
+
class Engine < ::Rails::Engine
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'devise_capturable'
|
data/spec/api_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'devise_capturable', 'api')
|
3
|
+
|
4
|
+
describe 'Devise::Capturable::API' do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
Devise.stub(:capturable_server).and_return("https://something.dev.janraincapture.com")
|
8
|
+
Devise.stub(:capturable_client_id).and_return("thisis")
|
9
|
+
Devise.stub(:capturable_client_secret).and_return("atest")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should get token from code" do
|
13
|
+
Devise::Capturable::API.should_receive(:post).with("https://something.dev.janraincapture.com/oauth/token", :query => {
|
14
|
+
code: "abcdef",
|
15
|
+
redirect_uri: "http://stupidsettings.com",
|
16
|
+
grant_type: 'authorization_code',
|
17
|
+
client_id: "thisis",
|
18
|
+
client_secret: "atest",
|
19
|
+
}).and_return({"yeah" => "Yeah"})
|
20
|
+
Devise::Capturable::API.token("abcdef")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should get entity from token" do
|
24
|
+
Devise::Capturable::API.should_receive(:post).with("https://something.dev.janraincapture.com/entity", :headers => {
|
25
|
+
'Authorization' => "OAuth abcdef" }).and_return({"yeah" => "Yeah"})
|
26
|
+
Devise::Capturable::API.entity("abcdef")
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "active_support/all"
|
3
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'devise_capturable', 'strategy')
|
4
|
+
|
5
|
+
class User
|
6
|
+
end
|
7
|
+
|
8
|
+
PARAMS = { :code => "abcdefghijklmn" }
|
9
|
+
TOKEN = {"access_token" => "abcdefg", "stat" => "ok"}
|
10
|
+
ENTITY = {"result" => { "uuid" => "1234", "email" => "some@email.com" }}
|
11
|
+
|
12
|
+
describe 'Devise::Capturable' do
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
@strategy = Devise::Capturable::Strategies::Capturable.new
|
16
|
+
@mapping = mock(:mapping)
|
17
|
+
@mapping.should_receive(:to).and_return(User)
|
18
|
+
@strategy.should_receive(:mapping).and_return(@mapping)
|
19
|
+
@strategy.should_receive(:params).at_least(1).and_return(PARAMS)
|
20
|
+
@user = User.new
|
21
|
+
@user.stub(:set_capturable_params)
|
22
|
+
Devise::Capturable::API.stub(:token).and_return(TOKEN)
|
23
|
+
Devise::Capturable::API.stub(:entity).and_return(ENTITY)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should authenticate and set capturable params if a user exists in database" do
|
27
|
+
User.stub(:capturable_auto_create_account?).and_return(true)
|
28
|
+
@user.stub(:save).and_return(true)
|
29
|
+
User.should_receive(:find_with_capturable_params).with(ENTITY["result"]).and_return(@user)
|
30
|
+
@user.should_receive(:set_capturable_params).with(ENTITY["result"]).and_return(true)
|
31
|
+
@strategy.should_receive(:"success!").with(@user).and_return(true)
|
32
|
+
lambda { @strategy.authenticate! }.should_not raise_error
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'when no user exists in database' do
|
36
|
+
|
37
|
+
before(:each) do
|
38
|
+
User.should_receive(:find_with_capturable_params).and_return(nil)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should fail unless capturable_auto_create_account" do
|
42
|
+
User.should_receive(:"capturable_auto_create_account?").and_return(false)
|
43
|
+
@strategy.should_receive(:"fail!").with(:capturable_invalid).and_return(true)
|
44
|
+
lambda { @strategy.authenticate! }.should_not raise_error
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should create a new user and success if capturable_auto_create_account" do
|
48
|
+
User.should_receive(:"capturable_auto_create_account?").and_return(true)
|
49
|
+
User.should_receive(:new).and_return(@user)
|
50
|
+
@user.should_receive(:"set_capturable_params").with(ENTITY["result"]).and_return(true)
|
51
|
+
@user.should_receive(:save).with({ :validate => false }).and_return(true)
|
52
|
+
@strategy.should_receive(:"success!").with(@user).and_return(true)
|
53
|
+
lambda { @strategy.authenticate! }.should_not raise_error
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'devise_capturable', 'view_helpers')
|
3
|
+
|
4
|
+
describe 'View Helpers' do
|
5
|
+
|
6
|
+
it "should generate correct link" do
|
7
|
+
class Testing
|
8
|
+
include Devise::Capturable::Helpers
|
9
|
+
end
|
10
|
+
test = Testing.new
|
11
|
+
link = test.link_to_capturable("Login")
|
12
|
+
link.should == '<a href="#" class="capture_modal_open" id="capture_signin_link">Login</a>'
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: devise_capturable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rune Skjoldborg Madsen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: devise
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rails
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Devise::Capturable is a gem that makes it possible to use the Janrain
|
79
|
+
Engage user registration widget, while preserving your Devise authentication setup
|
80
|
+
with your custom user model.
|
81
|
+
email:
|
82
|
+
- rune@runemadsen.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .gitignore
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- devise_capturable.gemspec
|
93
|
+
- lib/assets/javascripts/devise_capturable.js
|
94
|
+
- lib/devise_capturable.rb
|
95
|
+
- lib/devise_capturable/api.rb
|
96
|
+
- lib/devise_capturable/locales/en.yml
|
97
|
+
- lib/devise_capturable/model.rb
|
98
|
+
- lib/devise_capturable/strategy.rb
|
99
|
+
- lib/devise_capturable/version.rb
|
100
|
+
- lib/devise_capturable/view_helpers.rb
|
101
|
+
- rails/init.rb
|
102
|
+
- spec/api_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/strategy_spec.rb
|
105
|
+
- spec/view_helpers_spec.rb
|
106
|
+
homepage: ''
|
107
|
+
licenses: []
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 1.8.24
|
127
|
+
signing_key:
|
128
|
+
specification_version: 3
|
129
|
+
summary: Janrain Capture for Devise
|
130
|
+
test_files:
|
131
|
+
- spec/api_spec.rb
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
- spec/strategy_spec.rb
|
134
|
+
- spec/view_helpers_spec.rb
|
135
|
+
has_rdoc:
|