clickfunnels_auth 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c7d40b4437b17d3e713cb64d26871233bc05331
4
- data.tar.gz: e46057daa4cbc4df9fa92bc31f2ba2acf94a2057
3
+ metadata.gz: 81a0905a61b72e0495c63c333198902966d4516d
4
+ data.tar.gz: 41058a5b3a2f4730154f8a248d395a0542b4bd1d
5
5
  SHA512:
6
- metadata.gz: ed2561f4268f9b15c6f6cd889a55f91277ce6ec7ff57983c4476c0ea4f3ad74d4a9054e87f58582b6c9ae44f8a35412a593bcf9d1dbacf7bd0b1c1ec48533b03
7
- data.tar.gz: e44c4309e439c4050cb5f6e94172bb3b4b395c3838907524e2e598afe58a5dd72df5bfb06fb611ff507e6eb54cc460ae22b5cb612c49111c982dda5830993044
6
+ metadata.gz: 708d811d908a4817e64c10678bc0b8286e2b098fb2a3438e279b5f3f6082167230bf94390c1e8dcbe012e2ffe97da0f021988b53ba15948ca592f3f99672761f
7
+ data.tar.gz: 40127c474e94845ec8e22ea041ba2c6f34c8efb55592d4944643c1f4fe03eb18a086ac2eebd7452a729afc2dc083e0ebea8c8c3d790729c3e960b4577dfcbeef
data/README.md CHANGED
@@ -1,12 +1,11 @@
1
1
  # ClickfunnelsAuth
2
2
 
3
3
  A Rails engine that makes it easy to delegate authentication for a Rails site to
4
- [Clickfunnels](https://github.com/clickfunnels/clickfunnelsr).
5
- See the [Clickfunnels Donations](https://github.com/clickfunnels_donations)
6
- project for an example of using this gem.
4
+ [Clickfunnels Login](https://github.com/etison/clickfunnels-login).
5
+ See the [test app](https://github.com/etison/clickfunnels-login-test)
6
+ for an example of using this gem.
7
7
 
8
- This is based on the SoAuth projects. See [http://www.octolabs.com/so-auth](http://www.octolabs.com/so-auth)
9
- for more details.
8
+ This is based on the SoAuth projects. See [http://www.octolabs.com/so-auth](http://www.octolabs.com/so-auth) for more details.
10
9
 
11
10
  Usage
12
11
  ==============
@@ -75,21 +74,33 @@ Then be sure to run migrations.
75
74
  rake db:migrate; rake db:test:prepare
76
75
  ```
77
76
 
78
- ## Update `ApplicationController`
77
+ ## Modify your `User` model
79
78
 
80
- Change your `ApplicationController` to inherit from
81
- `ClickfunnelsAuth::ApplicationController`. The first line should look like this.
79
+ Add this line:
82
80
 
83
- ```ruby
84
- class ApplicationController < ClickfunnelsAuth::ApplicationController
81
+ ```
82
+ include ClickfunnelsAuth::UserHelper
83
+ ```
84
+
85
+ ## Generate migrations from this addon
86
+
87
+ ```
88
+ rails clickfunnels_auth_engine:install:migrations
89
+ ```
90
+
91
+ Then run migrations.
92
+
93
+ ```bash
94
+ rake db:migrate; rake db:test:prepare
85
95
  ```
86
96
 
87
97
  ## Protect some stuff in a controller
88
98
 
89
- Use a `before_filter` to protect some controller actions.
99
+ Include the helper and then use a `before_action` to protect some controller actions.
90
100
 
91
101
  ```ruby
92
- before_filter :login_required
102
+ include ClickfunnelsAuth::ControllerHelper
103
+ before_action :login_required
93
104
  ```
94
105
 
95
106
  ## OPTIONAL : Change the default port of your new project
@@ -127,3 +138,34 @@ unicorn -p 3001 -c ./config/unicorn.rb
127
138
  or whatever.
128
139
 
129
140
  This project rocks and uses MIT-LICENSE.
141
+
142
+
143
+ ## Publishing
144
+ We publish this gem on rubygems as it does not have anything private in it.
145
+
146
+ In general the steps in this RubyGems guild are quite good. https://guides.rubygems.org/publishing/
147
+
148
+ They are summarized in the following.
149
+
150
+ ### Credentials
151
+ You'll need a to get your email added as an owner to the `clickfunnels_auth` gem
152
+ on rubygems. Post a note to `product-ops` and somebody will be able to help.
153
+
154
+ ### Building the gem
155
+ Increment the gem version at `lib/rucksack-api/version.rb`, and then run `rake build`, which will create the package under `pkg`
156
+
157
+ ### Pushing the gem to rubygem
158
+ Run the following (with your new version) to push to github:
159
+
160
+ ```
161
+ gem push pkg/clickfunnels_auth-0.1.2.gem
162
+ ```
163
+
164
+ You should see something like:
165
+ ```
166
+ Pushing gem to https://rubygems.org...
167
+ Successfully registered gem: clickfunnels_auth (0.1.2)
168
+ ```
169
+
170
+ ### Tag the new version
171
+ Tagging is very simple. Just run git tag -a 0.1.2 -m "Version 0.1.2" and then git push --tags to push them up to GitHub.
@@ -1,65 +1,7 @@
1
1
  module ClickfunnelsAuth
2
2
  class ApplicationController < ActionController::Base
3
3
 
4
- protect_from_forgery
5
-
6
- #before_filter :check_cookie
7
- #def check_cookie
8
- #if !cookie_valid?
9
- #session[:user_id] = nil
10
- #end
11
- #end
12
-
13
- def cookie_valid?
14
- cookies[:clickfunnels_auth].present? && session[:user_id].present? && cookies[:clickfunnels_auth].to_s == session[:user_id].to_s
15
- end
16
-
17
- def login_required
18
- if !current_user
19
- not_authorized
20
- end
21
- end
22
-
23
- def not_authorized
24
- respond_to do |format|
25
- format.html{ auth_redirect }
26
- format.json{ head :unauthorized }
27
- end
28
- end
29
-
30
- def auth_redirect
31
- observable_redirect_to "/auth/clickfunnels?origin=#{request.protocol}#{request.host_with_port}#{request.fullpath}"
32
- end
33
-
34
- def current_user
35
- return nil unless session[:user_id]
36
- @current_user ||= User.find_by_id(session[:user_id])
37
- end
38
-
39
- def signed_in?
40
- current_user.present?
41
- end
42
-
43
- helper_method :signed_in?
44
- helper_method :current_user
45
-
46
-
47
-
48
-
49
- private
50
-
51
- # These two methods help with testing
52
- def integration_test?
53
- Rails.env.test? && defined?(Cucumber::Rails)
54
- end
55
-
56
- def observable_redirect_to(url)
57
- if integration_test?
58
- render :text => "If this wasn't an integration test, you'd be redirected to: #{url}"
59
- else
60
- redirect_to url
61
- end
62
- end
4
+ include ClickfunnelsAuth::ControllerHelper
63
5
 
64
6
  end
65
7
  end
@@ -1,9 +1,13 @@
1
1
  class ClickfunnelsAuth::UserSessionsController < ClickfunnelsAuth::ApplicationController
2
- before_filter :login_required, :only => [ :destroy ]
2
+ before_action :login_required, :only => [ :destroy ]
3
3
 
4
4
  # omniauth callback method
5
5
  def create
6
- omniauth = env['omniauth.auth']
6
+ omniauth = request.env['omniauth.auth']
7
+ puts "omniauth ============================================================="
8
+ pp omniauth
9
+ puts "======================================================================"
10
+ puts "expires_at = #{omniauth['credentials']['expires_at']}"
7
11
 
8
12
  user = User.find_by_id(omniauth['uid'])
9
13
  if not user
@@ -15,9 +19,23 @@ class ClickfunnelsAuth::UserSessionsController < ClickfunnelsAuth::ApplicationCo
15
19
  user.email = omniauth['info']['name'] if user.respond_to?(:name)
16
20
  user.save
17
21
 
22
+ user.access_tokens.destroy_all
23
+
24
+ user.access_tokens.create!({
25
+ token: omniauth['credentials']['token'],
26
+ refresh_token: omniauth['credentials']['refresh_token'],
27
+ expires_at: omniauth['credentials']['expires_at'] ? Time.at(omniauth['credentials']['expires_at']) : omniauth['credentials']['expires_at']
28
+ })
29
+
18
30
  session[:user_id] = user.id
31
+
32
+ if block_given?
33
+ yield omniauth
34
+ end
35
+
19
36
  flash[:notice] = "Successfully logged in"
20
- redirect_to request.env['omniauth.origin'] || root_path
37
+ #redirect_to request.env['omniauth.origin'] || root_path
38
+ redirect_to session['origin'] || root_path
21
39
  end
22
40
 
23
41
  # Omniauth failure callback
@@ -0,0 +1,28 @@
1
+ module FakeAuth
2
+ class UsersController < ApplicationController
3
+ def index
4
+ session[:user_id] = nil
5
+ @users = User.all.limit(20)
6
+ end
7
+
8
+ def become
9
+ @user = User.find params[:user_id]
10
+
11
+ @user.access_tokens.destroy_all
12
+
13
+ @user.access_tokens.create!({
14
+ token: 'a-fake-auth-token',
15
+ refresh_token: 'a-fake-auth-refresh-token',
16
+ expires_at: Time.now + 1.year
17
+ })
18
+
19
+ session[:user_id] = @user.id
20
+
21
+ redirect_to root_path
22
+ end
23
+
24
+ def unbecome
25
+ redirect_to '/fake_auth'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,50 @@
1
+ module ClickfunnelsAuth
2
+ class AccessToken < ActiveRecord::Base
3
+ self.table_name = "clickfunnels_auth_access_tokens"
4
+
5
+ belongs_to :user
6
+
7
+ def refresh!
8
+ new_token = oauth_token.refresh!
9
+ self.save_token_data(new_token)
10
+ end
11
+
12
+ def save_token_data(token)
13
+ self.update_attributes({
14
+ token: token.token,
15
+ refresh_token: token.refresh_token,
16
+ expires_at: Time.at(token.expires_at)
17
+ })
18
+ end
19
+
20
+ def validate_token!
21
+ user_data = oauth_token.get(ENV['AUTH_PROVIDER_ME_URL']).parsed
22
+ user_id = user_data['id']
23
+ puts "we got a user_id = #{user_id}"
24
+ rescue OAuth2::Error => e
25
+ puts "caught an error #{e}"
26
+ puts e.as_json
27
+ self.destroy
28
+ end
29
+
30
+ def expired?
31
+ oauth_token.expired?
32
+ end
33
+
34
+ protected
35
+
36
+ def oauth_token
37
+ OAuth2::AccessToken.from_hash(oauth_client, {
38
+ :token_type=>"bearer",
39
+ :access_token=>self.token,
40
+ :refresh_token=>self.refresh_token,
41
+ :expires_at=>self.expires_at
42
+ })
43
+ end
44
+
45
+ def oauth_client
46
+ # TODO : Is there some way we can retrieve this already configured, instead of creating a new one?
47
+ @oauth_client ||= OAuth2::Client.new(ENV['AUTH_PROVIDER_APPLICATION_ID'], ENV['AUTH_PROVIDER_SECRET'], {site: ENV['AUTH_PROVIDER_URL']})
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ <h1>Fake Auth</h1>
2
+
3
+ <% @users.each do |user| %>
4
+ <%= form_tag(fake_auth_become_user_path(user.id), method: "post") do %>
5
+ <%= hidden_field_tag(:user_id, user.id) %>
6
+ <%= submit_tag("Become #{user.email} (Id: #{user.id})") %>
7
+ <% end %>
8
+ <% end %>
data/config/routes.rb CHANGED
@@ -1,8 +1,18 @@
1
1
  Rails.application.routes.draw do
2
+
3
+ # This needs to come before the other logout link so that our fake_auth one
4
+ # will take precedence if that setting is activated.
5
+ if ENV['ENABLE_FAKE_AUTH'] == 'true'
6
+ get 'fake_auth' => 'fake_auth/users#index'
7
+ post 'fake_auth/:user_id/become' => 'fake_auth/users#become', :as => :fake_auth_become_user
8
+ post '/logout', :to => 'fake_auth/users#unbecome'
9
+ end
10
+
2
11
  # omniauth
3
12
  get '/auth/:provider/callback', :to => 'clickfunnels_auth/user_sessions#create'
4
13
  get '/auth/failure', :to => 'clickfunnels_auth/user_sessions#failure'
5
14
 
6
15
  # Custom logout
7
16
  post '/logout', :to => 'clickfunnels_auth/user_sessions#destroy'
17
+
8
18
  end
@@ -0,0 +1,13 @@
1
+ class CreateAccessTokens < ActiveRecord::Migration
2
+ def change
3
+ create_table :clickfunnels_auth_access_tokens do |t|
4
+ t.string :token
5
+ t.string :refresh_token
6
+ t.timestamp :expires_at
7
+ t.bigint :user_id
8
+
9
+ t.timestamps
10
+ end
11
+ add_index :access_tokens, :user_id
12
+ end
13
+ end
@@ -1,4 +1,6 @@
1
1
  require "clickfunnels_auth/engine"
2
+ require "clickfunnels_auth/controller_helper"
3
+ require "clickfunnels_auth/user_helper"
2
4
  require 'omniauth-oauth2'
3
5
  require 'omniauth/strategies/clickfunnels'
4
6
 
@@ -0,0 +1,109 @@
1
+ module ClickfunnelsAuth
2
+ module ControllerHelper
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ protect_from_forgery
8
+ # TODO : We need to have the mothership set the clickfunnels_login_user cookie
9
+ #before_action :check_cookie
10
+ helper_method :signed_in?
11
+ helper_method :current_user
12
+ end
13
+
14
+ def check_cookie
15
+ if !cookie_valid?
16
+ session[:user_id] = nil
17
+ end
18
+ end
19
+
20
+ def cookie_valid?
21
+ cookies[:clickfunnels_login_user].present? && session[:user_id].present? && cookies[:clickfunnels_login_user].to_s == session[:user_id].to_s
22
+ end
23
+
24
+ def login_required
25
+ if !current_user
26
+ not_authorized
27
+ end
28
+ end
29
+
30
+ def not_authorized
31
+ respond_to do |format|
32
+ format.html{ auth_redirect }
33
+ format.json{ head :unauthorized }
34
+ end
35
+ end
36
+
37
+ def is_token_older_than_current_login?(token)
38
+ return false
39
+ # TODO : We need to get the mothership setting this and the clickfunnels_login_user cookie
40
+ if !cookies[:clickfunnels_login_timestamp].present?
41
+ return true
42
+ end
43
+ return token.updated_at < Time.at(cookies[:clickfunnels_login_timestamp].to_i)
44
+ end
45
+
46
+ def auth_redirect
47
+ origin = "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
48
+ # Currently Doorkeeper has a bug when the redirct contains query params, so for now
49
+ # we'll put the origin in the session instead of the redirect url.
50
+ #observable_redirect_to "/auth/clickfunnels?origin=#{CGI.escape(origin)}"
51
+ session['origin'] = origin
52
+ if ENV['ENABLE_FAKE_AUTH'] == 'true'
53
+ observable_redirect_to "/fake_auth"
54
+ else
55
+ observable_redirect_to "/auth/clickfunnels"
56
+ end
57
+ end
58
+
59
+ def current_user
60
+ return nil unless session[:user_id]
61
+ @current_user ||= User.find_by_id(session[:user_id])
62
+ token = @current_user.access_tokens.first
63
+ puts "token = #{token}"
64
+ puts "token.expired? = #{token.try :expired?}"
65
+ if token.blank?
66
+ puts "*******************************************************"
67
+ puts "we had a user, but they did not have a token!"
68
+ puts "*******************************************************"
69
+ session[:user_id] = nil
70
+ return nil
71
+ elsif token.expired? || is_token_older_than_current_login?(token)
72
+ begin
73
+ puts "*******************************************************"
74
+ puts "aobut to refresh the token!"
75
+ puts "token.expired? : #{token.expired?}"
76
+ puts "is_token_older_than_current_login?(token) : #{is_token_older_than_current_login?(token)}"
77
+ puts "*******************************************************"
78
+ token.refresh!
79
+ rescue OAuth2::Error => e
80
+ puts "caught error #{e}"
81
+ token.destroy!
82
+ session[:user_id] = nil
83
+ return nil
84
+ end
85
+ end
86
+ return @current_user
87
+ end
88
+
89
+ def signed_in?
90
+ current_user.present?
91
+ end
92
+
93
+ private
94
+
95
+ # These two methods help with testing
96
+ def integration_test?
97
+ Rails.env.test? && defined?(Cucumber::Rails)
98
+ end
99
+
100
+ def observable_redirect_to(url)
101
+ if integration_test?
102
+ render :text => "If this wasn't an integration test, you'd be redirected to: #{url}"
103
+ else
104
+ redirect_to url
105
+ end
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,11 @@
1
+ module ClickfunnelsAuth
2
+ module UserHelper
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ has_many :access_tokens, class_name: 'ClickfunnelsAuth::AccessToken'
8
+ end
9
+
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module ClickfunnelsAuth
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -4,7 +4,7 @@ module OmniAuth
4
4
  class Clickfunnels < OmniAuth::Strategies::OAuth2
5
5
 
6
6
  CUSTOM_PROVIDER_URL = ENV['AUTH_PROVIDER_URL'] || "http://custom-provider-goes-here"
7
- CUSTOM_PROVIDER_ME_URL = ENV['AUTH_PROVIDER_ME_URL'] || "/oauth/me.json"
7
+ CUSTOM_PROVIDER_ME_URL = ENV['AUTH_PROVIDER_ME_URL'] || "/api/attributes/me.json"
8
8
 
9
9
  option :client_options, {
10
10
  :site => CUSTOM_PROVIDER_URL,
@@ -13,13 +13,16 @@ module OmniAuth
13
13
  }
14
14
 
15
15
  uid {
16
- raw_info['id']
16
+ raw_info['id']
17
17
  }
18
18
 
19
19
  info do
20
20
  {
21
21
  :email => raw_info['email'],
22
- :admin => raw_info['admin']
22
+ :admin => raw_info['admin'],
23
+ :member_level => raw_info['funnelflix_member_level'],
24
+ :internal_affiliate_id => raw_info['internal_affiliate_id'],
25
+ :accounts => raw_info['accounts']
23
26
  }
24
27
  end
25
28
 
@@ -35,6 +38,13 @@ module OmniAuth
35
38
  def raw_info
36
39
  @raw_info ||= access_token.get(CUSTOM_PROVIDER_ME_URL).parsed
37
40
  end
41
+
42
+ # Omniauth-oauth2 > 1.3 breaks the callback url with extra parameter options
43
+ # https://github.com/omniauth/omniauth-oauth2/issues/81
44
+ # and also https://github.com/omniauth/omniauth-oauth2/commit/26152673224aca5c3e918bcc83075dbb0659717f#commitcomment-13935631
45
+ def callback_url
46
+ full_host + script_name + callback_path
47
+ end
38
48
  end
39
49
  end
40
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clickfunnels_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Green
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-08 00:00:00.000000000 Z
11
+ date: 2021-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -28,28 +28,28 @@ dependencies:
28
28
  name: omniauth
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.3.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.3.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: omniauth-oauth2
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 1.4.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.4.0
55
55
  - !ruby/object:Gem::Dependency
@@ -120,10 +120,16 @@ files:
120
120
  - Rakefile
121
121
  - app/controllers/clickfunnels_auth/application_controller.rb
122
122
  - app/controllers/clickfunnels_auth/user_sessions_controller.rb
123
+ - app/controllers/fake_auth/users_controller.rb
124
+ - app/models/clickfunnels_auth/access_token.rb
125
+ - app/views/fake_auth/users/index.html.erb
123
126
  - config/cucumber.yml
124
127
  - config/routes.rb
128
+ - db/migrate/20180815183533_create_access_tokens.rb
125
129
  - lib/clickfunnels_auth.rb
130
+ - lib/clickfunnels_auth/controller_helper.rb
126
131
  - lib/clickfunnels_auth/engine.rb
132
+ - lib/clickfunnels_auth/user_helper.rb
127
133
  - lib/clickfunnels_auth/version.rb
128
134
  - lib/generators/clickfunnels_auth/install/USAGE
129
135
  - lib/generators/clickfunnels_auth/install/install_generator.rb
@@ -150,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
156
  version: '0'
151
157
  requirements: []
152
158
  rubyforge_project:
153
- rubygems_version: 2.4.5
159
+ rubygems_version: 2.6.10
154
160
  signing_key:
155
161
  specification_version: 4
156
162
  summary: A gem that allows a Rails app to be an OAuth client of Clickfunnels.