dailycred 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +14 -2
- data/Guardfile +15 -0
- data/README.md +159 -33
- data/Rakefile +30 -0
- data/app/controllers/sessions_controller.rb +31 -0
- data/app/views/sessions/info.html.erb +13 -0
- data/config/routes.rb +7 -0
- data/dailycred.gemspec +4 -3
- data/docs/lib/dailycred/acts_as_dailycred.html +42 -0
- data/docs/lib/dailycred/client.html +214 -0
- data/docs/lib/dailycred/engine.html +112 -0
- data/docs/lib/dailycred/helper.html +125 -0
- data/docs/lib/dailycred/middleware.html +42 -0
- data/docs/lib/dailycred/tests_helper.html +42 -0
- data/docs/lib/dailycred/user.html +133 -0
- data/docs/lib/dailycred.html +42 -0
- data/docs/lib/generators/dailycred_generator.html +42 -0
- data/docs/lib/generators/templates/migration_create_user.html +39 -0
- data/docs/lib/generators/templates/omniauth.html +71 -0
- data/docs/lib/generators/templates/sessions_controller.html +96 -0
- data/docs/lib/generators/templates/user.html +42 -0
- data/docs/lib/middleware/middleware.html +38 -0
- data/docs/lib/omniauth/strategies/dailycred.html +199 -0
- data/docs/lib/omniauth-dailycred/version.html +38 -0
- data/docs/lib/user/user.html +129 -0
- data/dummy/.gitignore +15 -0
- data/dummy/Gemfile +55 -0
- data/dummy/Guardfile +31 -0
- data/dummy/README.rdoc +261 -0
- data/dummy/Rakefile +7 -0
- data/dummy/app/assets/images/rails.png +0 -0
- data/dummy/app/assets/javascripts/application.js +15 -0
- data/dummy/app/assets/javascripts/posts.js.coffee +3 -0
- data/dummy/app/assets/stylesheets/application.css +13 -0
- data/dummy/app/assets/stylesheets/posts.css.scss +3 -0
- data/dummy/app/assets/stylesheets/scaffolds.css.scss +69 -0
- data/dummy/app/controllers/application_controller.rb +16 -0
- data/dummy/app/controllers/posts_controller.rb +85 -0
- data/dummy/app/helpers/application_helper.rb +2 -0
- data/dummy/app/helpers/posts_helper.rb +2 -0
- data/dummy/app/mailers/.gitkeep +0 -0
- data/dummy/app/models/.gitkeep +0 -0
- data/dummy/app/models/post.rb +3 -0
- data/dummy/app/models/user.rb +3 -0
- data/dummy/app/views/layouts/application.html.erb +14 -0
- data/dummy/app/views/posts/_form.html.erb +21 -0
- data/dummy/app/views/posts/edit.html.erb +6 -0
- data/dummy/app/views/posts/index.html.erb +23 -0
- data/dummy/app/views/posts/new.html.erb +5 -0
- data/dummy/app/views/posts/show.html.erb +10 -0
- data/dummy/config/application.rb +62 -0
- data/dummy/config/boot.rb +6 -0
- data/dummy/config/database.yml +25 -0
- data/dummy/config/environment.rb +5 -0
- data/dummy/config/environments/development.rb +37 -0
- data/dummy/config/environments/production.rb +67 -0
- data/dummy/config/environments/test.rb +37 -0
- data/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/dummy/config/initializers/inflections.rb +15 -0
- data/dummy/config/initializers/mime_types.rb +5 -0
- data/dummy/config/initializers/omniauth.rb +13 -0
- data/dummy/config/initializers/secret_token.rb +7 -0
- data/dummy/config/initializers/session_store.rb +8 -0
- data/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/dummy/config/locales/en.yml +5 -0
- data/dummy/config/routes.rb +61 -0
- data/dummy/config.ru +4 -0
- data/dummy/db/migrate/20120925172903_create_users.rb +68 -0
- data/dummy/db/migrate/20120926012555_create_posts.rb +9 -0
- data/dummy/db/migrate/20121002192037_update_users_2.rb +70 -0
- data/dummy/db/schema.rb +45 -0
- data/dummy/db/seeds.rb +7 -0
- data/dummy/lib/assets/.gitkeep +0 -0
- data/dummy/lib/tasks/.gitkeep +0 -0
- data/dummy/log/.gitkeep +0 -0
- data/dummy/public/404.html +26 -0
- data/dummy/public/422.html +26 -0
- data/dummy/public/500.html +25 -0
- data/dummy/public/favicon.ico +0 -0
- data/dummy/public/index.html +241 -0
- data/dummy/public/robots.txt +5 -0
- data/dummy/script/rails +6 -0
- data/dummy/test/fixtures/.gitkeep +0 -0
- data/dummy/test/fixtures/posts.yml +7 -0
- data/dummy/test/fixtures/users.yml +3 -0
- data/dummy/test/functional/.gitkeep +0 -0
- data/dummy/test/functional/posts_controller_test.rb +51 -0
- data/dummy/test/integration/.gitkeep +0 -0
- data/dummy/test/performance/browsing_test.rb +12 -0
- data/dummy/test/test_helper.rb +62 -0
- data/dummy/test/unit/.gitkeep +0 -0
- data/dummy/test/unit/helpers/posts_helper_test.rb +4 -0
- data/dummy/test/unit/post_test.rb +7 -0
- data/dummy/test/unit/user_test.rb +77 -0
- data/dummy/vendor/assets/javascripts/.gitkeep +0 -0
- data/dummy/vendor/assets/stylesheets/.gitkeep +0 -0
- data/dummy/vendor/plugins/.gitkeep +0 -0
- data/lib/dailycred/acts_as_dailycred.rb +87 -0
- data/lib/dailycred/client.rb +105 -0
- data/lib/dailycred/engine.rb +52 -0
- data/lib/dailycred/helper.rb +60 -0
- data/lib/{middleware → dailycred}/middleware.rb +25 -15
- data/lib/dailycred/tests_helper.rb +5 -0
- data/lib/dailycred/user.rb +63 -0
- data/lib/dailycred.rb +1 -79
- data/lib/generators/dailycred_generator.rb +94 -44
- data/lib/generators/templates/info.html.erb +1 -1
- data/lib/generators/templates/login.html.erb +13 -0
- data/lib/generators/templates/migration_create_user.rb +60 -10
- data/lib/generators/templates/omniauth.rb +6 -17
- data/lib/generators/templates/sessions_controller.rb +8 -2
- data/lib/generators/templates/user.rb +1 -23
- data/lib/omniauth/strategies/dailycred.rb +37 -20
- data/script/download_info.rb +11 -0
- data/spec/{spec_helper.rb → helper_spec.rb} +4 -2
- data/spec/omniauth/strategies/dailycred_spec.rb +52 -10
- data/spec/support/dailycred_spec.rb +72 -0
- data/spec/support/shared_examples.rb +40 -0
- data/test/generator_test.rb +85 -0
- data/test/test_helper.rb +6 -0
- metadata +116 -8
- data/lib/omniauth-dailycred/version.rb +0 -5
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rails'
|
2
|
+
module Dailycred
|
3
|
+
|
4
|
+
class Engine < Rails::Engine
|
5
|
+
|
6
|
+
initializer 'dailycred.setup', :after=>"dailycred" do |app|
|
7
|
+
conf = Rails.configuration# alias for configuration
|
8
|
+
if conf.respond_to? 'DAILYCRED_CLIENT_ID'
|
9
|
+
id = conf.DAILYCRED_CLIENT_ID
|
10
|
+
secret = conf.DAILYCRED_SECRET_KEY
|
11
|
+
|
12
|
+
# setup omniauth options, can be overridden. ex:
|
13
|
+
# Rails.application.DAILYCRED_OPTIONS = {
|
14
|
+
# :client_options => {
|
15
|
+
# :site => 'https://www.dailycred.com',
|
16
|
+
# :authorize_url => '/connect',
|
17
|
+
# :token_url => '/oauth/access_token'
|
18
|
+
# }
|
19
|
+
# }
|
20
|
+
if conf.respond_to? 'DAILYCRED_OPTIONS'
|
21
|
+
opts = conf.DAILYCRED_OPTIONS
|
22
|
+
opts[:client_options] ||= {}
|
23
|
+
opts[:client_options][:ssl] = {}
|
24
|
+
else
|
25
|
+
opts = {:client_options => {:ssl => {}}}
|
26
|
+
end
|
27
|
+
|
28
|
+
if File.exists?('/etc/ssl/certs')
|
29
|
+
opts[:client_options][:ssl][:ca_path] = '/etc/ssl/certs'
|
30
|
+
end
|
31
|
+
if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
|
32
|
+
opts[:client_options][:ssl][:ca_file] = '/opt/local/share/curl/curl-ca-bundle.crt'
|
33
|
+
end
|
34
|
+
|
35
|
+
conf.DAILYCRED_OPTIONS = opts
|
36
|
+
|
37
|
+
Rails.application.config.middleware.use OmniAuth::Builder do
|
38
|
+
provider :dailycred, id, secret, opts
|
39
|
+
end
|
40
|
+
|
41
|
+
Rails.application.config.middleware.use "Dailycred::Middleware", id
|
42
|
+
|
43
|
+
ActiveSupport.on_load(:action_controller) do
|
44
|
+
include Dailycred::Helpers
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Dailycred
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
# use as a before_filter to only allow signed in users
|
5
|
+
# example:
|
6
|
+
# before_filter :authenticate
|
7
|
+
def authenticate
|
8
|
+
redirect_to_unauth unless current_user
|
9
|
+
end
|
10
|
+
|
11
|
+
# helper method for getting an instance of dailycred
|
12
|
+
# example:
|
13
|
+
# dailycred.tagUser "user_id", "tag"
|
14
|
+
#
|
15
|
+
# for more documentation, visit https://www.dailycred.com/api/ruby
|
16
|
+
def dailycred
|
17
|
+
config = Rails.configuration
|
18
|
+
@dailycred ||= Dailycred::Client.new(config.DAILYCRED_CLIENT_ID, config.DAILYCRED_SECRET_KEY, config.DAILYCRED_OPTIONS)
|
19
|
+
end
|
20
|
+
|
21
|
+
# when making oauth calls, we may need to redirect to our oauth callback url
|
22
|
+
# make sure we have the correct state passed back and forth
|
23
|
+
def set_state
|
24
|
+
@state = session["omniauth.state"] = SecureRandom.hex(24)
|
25
|
+
end
|
26
|
+
|
27
|
+
def login_path(params={})
|
28
|
+
"/auth/dailycred"
|
29
|
+
end
|
30
|
+
|
31
|
+
def connect_path(params)
|
32
|
+
url = "#{request.protocol}#{request.host_with_port}/auth/dailycred"
|
33
|
+
p = []
|
34
|
+
params.each do |k,v|
|
35
|
+
p << "#{k}=#{v.to_s}"
|
36
|
+
end
|
37
|
+
url += "?" if p.size > 0
|
38
|
+
url += p.join("&")
|
39
|
+
end
|
40
|
+
|
41
|
+
def connect_user provider, user=nil
|
42
|
+
if user.nil?
|
43
|
+
user = current_user
|
44
|
+
end
|
45
|
+
connect_path(access_token: user.token, identity_provider: provider)
|
46
|
+
end
|
47
|
+
|
48
|
+
def redirect_to_auth opts={}
|
49
|
+
conf = Rails.configuration.DAILYCRED_OPTIONS
|
50
|
+
path = !conf[:after_auth].nil? ? conf[:after_auth] : dailycred_engine.auth_info_path
|
51
|
+
redirect_to path, opts
|
52
|
+
end
|
53
|
+
|
54
|
+
def redirect_to_unauth opts = {}
|
55
|
+
conf = Rails.configuration.DAILYCRED_OPTIONS
|
56
|
+
path = !conf[:after_unauth].nil? ? conf[:after_unauth] : dailycred_engine.auth_info_path
|
57
|
+
redirect_to path, opts
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -1,9 +1,14 @@
|
|
1
|
-
|
1
|
+
module Dailycred
|
2
2
|
class Middleware
|
3
3
|
attr_accessor :client_id
|
4
4
|
|
5
|
-
def initialize(app, client_id)
|
6
|
-
@
|
5
|
+
def initialize(app, client_id, opts={})
|
6
|
+
@opts = opts
|
7
|
+
@opts[:url] ||= "https://www.dailycred.com"
|
8
|
+
@opts[:modal] ||= false
|
9
|
+
@opts[:sdk] ||= false
|
10
|
+
@opts[:triggers] ||= []
|
11
|
+
@app = app
|
7
12
|
@client_id = client_id
|
8
13
|
end
|
9
14
|
|
@@ -28,29 +33,34 @@ class Dailycred
|
|
28
33
|
private
|
29
34
|
|
30
35
|
def render_dailycred_scripts
|
31
|
-
|
36
|
+
str =<<-EOT
|
32
37
|
<!-- dailycred -->
|
33
38
|
<script type="text/javascript">
|
34
39
|
(function() {
|
35
|
-
var dc,
|
40
|
+
var dc, url;
|
36
41
|
window.dc_opts = {
|
37
42
|
clientId: "#{@client_id}",
|
38
|
-
home: "
|
43
|
+
home: "#{@opts[:url]}"
|
39
44
|
};
|
40
|
-
|
41
|
-
|
42
|
-
dlh = document.location.href;
|
43
|
-
page = dlh.indexOf('#') > -1 ? dlh.substring(0, dlh.indexOf("#")) : dlh;
|
44
|
-
title = document.title ? document.title : "";
|
45
|
-
referrer = document.referrer ? document.referrer : "";
|
46
|
-
dc = document.createElement("img");
|
47
|
-
url = "" + home + "/dc.gif?page=" + page + "&title=" + title + "&client_id=" + window.dc_opts.clientId + "&referrer=" + referrer;
|
45
|
+
dc = document.createElement("script");
|
46
|
+
url = dc_opts.home + "/public/js/cred.coffee";
|
48
47
|
dc.src = url;
|
49
48
|
document.body.appendChild(dc);
|
50
49
|
}).call(this);
|
51
50
|
</script>
|
52
|
-
<!-- end dailycred -->
|
53
51
|
EOT
|
52
|
+
str2 =<<-EOT
|
53
|
+
<script src="https://s3.amazonaws.com/file.dailycred.com/js/dailycred.js"></script>
|
54
|
+
<script>
|
55
|
+
DC.init({
|
56
|
+
"showModal" : #{@opts[:modal]}
|
57
|
+
#{',"modal":{"triggers":'+@opts[:triggers].to_s+'}' if @opts[:modal]}
|
58
|
+
});
|
59
|
+
</script>
|
60
|
+
EOT
|
61
|
+
str += str2 if @opts[:sdk]
|
62
|
+
str += "\n<!-- end dailycred -->"
|
63
|
+
str
|
54
64
|
end
|
55
65
|
|
56
66
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Dailycred
|
2
|
+
class User
|
3
|
+
include ActiveModel::Validations
|
4
|
+
include ActiveModel::Serialization
|
5
|
+
|
6
|
+
validates_presence_of :email, :pass
|
7
|
+
|
8
|
+
attr_accessor :client, :email, :pass, :authorized
|
9
|
+
|
10
|
+
def initialize client, user = {}
|
11
|
+
self.client = client
|
12
|
+
self.authorized = false
|
13
|
+
user.each do |k,v|
|
14
|
+
self[k] = v if self.respond_to(k)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def login
|
19
|
+
if !self.valid?
|
20
|
+
#it didn't work already, return false
|
21
|
+
return false
|
22
|
+
end
|
23
|
+
|
24
|
+
response = JSON.parse client.login(self.to_hash)
|
25
|
+
err_parser response
|
26
|
+
|
27
|
+
return false if !self.valid?
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_hash
|
32
|
+
{
|
33
|
+
:email => self.email,
|
34
|
+
:pass => self.pass
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
#response is a hash, which is
|
41
|
+
#a json-parsed http response body
|
42
|
+
def err_parser response
|
43
|
+
if !response["worked"]
|
44
|
+
self.authorized = false
|
45
|
+
response["errors"].each do |err|
|
46
|
+
attrib = err["attribute"]
|
47
|
+
message = err["message"]
|
48
|
+
if attrib == "form"
|
49
|
+
self.errors.add_to_base message
|
50
|
+
else
|
51
|
+
if attrib == "user"
|
52
|
+
self.errors.add :email, message
|
53
|
+
elsif self.respond_to attrib
|
54
|
+
self.errors.add attrib, message
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/lib/dailycred.rb
CHANGED
@@ -1,80 +1,2 @@
|
|
1
|
-
require "omniauth-dailycred/version"
|
2
1
|
require "omniauth/strategies/dailycred"
|
3
|
-
require
|
4
|
-
|
5
|
-
class Dailycred
|
6
|
-
|
7
|
-
attr_accessor :client_id, :secret_key, :options
|
8
|
-
|
9
|
-
# Initializes a dailycred object
|
10
|
-
# @param [String] client_id the client's daiycred client id
|
11
|
-
# @param [String] secret_key the clients secret key
|
12
|
-
# @param [Hash] opts a hash of options
|
13
|
-
def initialize(client_id, secret_key="", opts={})
|
14
|
-
@client_id = client_id
|
15
|
-
@secret_key = secret_key
|
16
|
-
@options = opts
|
17
|
-
end
|
18
|
-
|
19
|
-
URL = "https://www.dailycred.com"
|
20
|
-
|
21
|
-
# Generates a Dailycred event
|
22
|
-
# @param [String] user_id the user's dailycred user id
|
23
|
-
# @param [String] key the name of the event type
|
24
|
-
# @param [String] val the value of the event (optional)
|
25
|
-
def event(user_id, key, val="")
|
26
|
-
opts = {
|
27
|
-
:key => key,
|
28
|
-
:valuestring => val,
|
29
|
-
:user_id => user_id
|
30
|
-
}
|
31
|
-
post "/admin/api/customevent.json", opts
|
32
|
-
end
|
33
|
-
|
34
|
-
# Tag a user in dailycred
|
35
|
-
# @param [String] user_id the user's dailycred user id
|
36
|
-
# @param [String] tag the desired tag to add
|
37
|
-
def tag(user_id, tag)
|
38
|
-
opts = {
|
39
|
-
:user_id => user_id,
|
40
|
-
:tag => tag
|
41
|
-
}
|
42
|
-
post "/admin/api/user/tag.json", opts
|
43
|
-
end
|
44
|
-
|
45
|
-
# Untag a user in dailycred
|
46
|
-
# (see #tag)
|
47
|
-
def untag(user_id, tag)
|
48
|
-
opts = {
|
49
|
-
:user_id => user_id,
|
50
|
-
:tag => tag
|
51
|
-
}
|
52
|
-
post "/admin/api/user/untag.json", opts
|
53
|
-
end
|
54
|
-
|
55
|
-
private
|
56
|
-
|
57
|
-
def post(url, opts)
|
58
|
-
opts.merge! base_opts
|
59
|
-
get_conn.post url, opts
|
60
|
-
end
|
61
|
-
|
62
|
-
def ssl_opts
|
63
|
-
opts = {}
|
64
|
-
if @options[:client_options] && @options[:client_options][:ssl]
|
65
|
-
opts[:ssl] = @options[:client_options][:ssl]
|
66
|
-
end
|
67
|
-
opts
|
68
|
-
end
|
69
|
-
|
70
|
-
def base_opts
|
71
|
-
{
|
72
|
-
:client_id => @client_id,
|
73
|
-
:client_secret => @client_secret
|
74
|
-
}
|
75
|
-
end
|
76
|
-
|
77
|
-
def get_conn
|
78
|
-
Faraday::Connection.new Dailycred::URL, ssl_opts
|
79
|
-
end
|
80
|
-
end
|
2
|
+
Dir[File.expand_path('../dailycred/*', __FILE__)].each { |f| require f }
|
@@ -1,22 +1,26 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'pp'
|
3
|
+
require 'json'
|
4
|
+
require 'faraday'
|
1
5
|
class DailycredGenerator < Rails::Generators::Base
|
2
6
|
source_root File.expand_path('../templates', __FILE__)
|
3
7
|
|
4
|
-
|
5
|
-
|
8
|
+
CLIENT_ID_DEFAULT = 'YOUR_CLIENT_ID'
|
9
|
+
CLIENT_SECRET_DEFAULT = 'YOUR_SECRET_KEY'
|
6
10
|
|
7
|
-
|
11
|
+
argument :client_id, :type => :string, :default => CLIENT_ID_DEFAULT, :banner => 'dailycred_client_id'
|
12
|
+
argument :secret_key, :type => :string, :default => CLIENT_SECRET_DEFAULT, :banner => 'dailycred_secret_key'
|
8
13
|
|
9
14
|
APP_ROUTES_LINES =<<-EOS
|
10
|
-
|
11
|
-
match "/logout" => "sessions#destroy", :as => :logout
|
12
|
-
match "/auth" => "sessions#info", :as => :auth
|
15
|
+
mount Dailycred::Engine => '/auth', :as => 'dailycred_engine'
|
13
16
|
EOS
|
14
17
|
|
15
18
|
APP_CONTROLLER_LINES =<<-EOS
|
16
|
-
helper_method :current_user
|
19
|
+
helper_method :current_user
|
17
20
|
|
18
21
|
private
|
19
22
|
|
23
|
+
# helper method for getting the current signed in user
|
20
24
|
def current_user
|
21
25
|
begin
|
22
26
|
@current_user || User.find(session[:user_id]) if session[:user_id]
|
@@ -24,53 +28,99 @@ class DailycredGenerator < Rails::Generators::Base
|
|
24
28
|
nil
|
25
29
|
end
|
26
30
|
end
|
27
|
-
|
28
|
-
def authenticate
|
29
|
-
redirect_to auth_path unless current_user
|
30
|
-
end
|
31
|
-
|
32
|
-
def signup_path
|
33
|
-
"/auth/dailycred"
|
34
|
-
end
|
35
|
-
|
36
|
-
def login_path
|
37
|
-
"/auth/dailycred?action=signin"
|
38
|
-
end
|
39
|
-
|
40
|
-
def dailycred
|
41
|
-
config = Rails.configuration
|
42
|
-
@dailycred ||= Dailycred.new(config.DAILYCRED_CLIENT_ID, config.DAILYCRED_SECRET_KEY)
|
43
|
-
end
|
44
31
|
EOS
|
45
32
|
|
46
33
|
def install
|
47
|
-
dailycred_ascii =<<-EOS
|
48
|
-
*****
|
49
|
-
*****
|
50
|
-
*****
|
51
|
-
*****
|
52
|
-
*****
|
53
|
-
***** Thanks for using dailycred!
|
54
|
-
*****
|
55
|
-
*****
|
56
|
-
*****
|
57
|
-
*****
|
58
|
-
*****
|
59
|
-
EOS
|
60
|
-
print dailycred_ascii
|
61
34
|
# copy initializer
|
62
35
|
template "omniauth.rb", "config/initializers/omniauth.rb"
|
63
|
-
#
|
64
|
-
|
36
|
+
# get client info from login if they didnt specify info
|
37
|
+
if @client_id == CLIENT_ID_DEFAULT
|
38
|
+
get_info
|
39
|
+
end
|
65
40
|
# application controller
|
66
|
-
|
41
|
+
insert_into_file "app/controllers/application_controller.rb", APP_CONTROLLER_LINES, :after => /class ApplicationController\n|class ApplicationController .*\n/
|
67
42
|
# add user_model
|
68
43
|
copy_file "user.rb", "app/models/user.rb"
|
69
44
|
# session_controller
|
70
45
|
copy_file "migration_create_user.rb", "db/migrate/#{Time.now.strftime('%Y%m%d%H%M%S')}_create_users.rb"
|
71
|
-
# auth page
|
72
|
-
copy_file "info.html.erb", "app/views/sessions/info"
|
73
46
|
# config/routes
|
74
|
-
inject_into_file "config/routes.rb", APP_ROUTES_LINES, :after => "
|
47
|
+
inject_into_file "config/routes.rb", APP_ROUTES_LINES, :after => ".draw do\n"
|
75
48
|
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def get_info first=true
|
53
|
+
if first
|
54
|
+
puts "Please insert your dailycred credentials. You must sign up for a free account at "+
|
55
|
+
"http://www.dailycred.com. This is to automatically configure your api keys. If you wish to skip, enter 'n' as your email."
|
56
|
+
else
|
57
|
+
puts "Invalid email and password. Try again or type 'n' to skip."
|
58
|
+
end
|
59
|
+
#ssl opts
|
60
|
+
# $stderr.puts 'getting input'
|
61
|
+
input = get_input
|
62
|
+
email, password = input[0], input[1]
|
63
|
+
# $stderr.puts 'got input'
|
64
|
+
return if email == "n"
|
65
|
+
ssl_opts = {}
|
66
|
+
if File.exists?('/etc/ssl/certs')
|
67
|
+
ssl_opts = { :ca_path => '/etc/ssl/certs'}
|
68
|
+
end
|
69
|
+
if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
|
70
|
+
ssl_opts = { :ca_file => '/opt/local/share/curl/curl-ca-bundle.crt' }
|
71
|
+
end
|
72
|
+
# url = "https://www.dailycred.com"
|
73
|
+
# url = "http://localhost:9000"
|
74
|
+
# staging server for a very short time
|
75
|
+
url = "http://ec2-72-44-40-55.compute-1.amazonaws.com:9000"
|
76
|
+
connection = Faraday::Connection.new url, :ssl => ssl_opts
|
77
|
+
params = {
|
78
|
+
:login => email,
|
79
|
+
:pass => password,
|
80
|
+
:client_id => "dailycred"
|
81
|
+
}
|
82
|
+
response = connection.post("user/api/signin.json", params)
|
83
|
+
json = JSON.parse(response.body)
|
84
|
+
if !json["worked"]
|
85
|
+
# wrong password
|
86
|
+
p ''
|
87
|
+
get_info false
|
88
|
+
end
|
89
|
+
access_token = json["access_token"]
|
90
|
+
response = connection.post("graph/clientinfo.json", :access_token => access_token)
|
91
|
+
json = JSON.parse(response.body)
|
92
|
+
if !json["worked"]
|
93
|
+
p "There was an error retrieving your account information. Please manually configure your API keys in config/initializers/omniauth.rb"
|
94
|
+
return
|
95
|
+
end
|
96
|
+
@client_id = json["clientId"]
|
97
|
+
@secret_key = json["clientSecret"]
|
98
|
+
gsub_file("config/initializers/omniauth.rb", /YOUR_CLIENT_ID/, @client_id) if @client_id
|
99
|
+
gsub_file("config/initializers/omniauth.rb", /YOUR_SECRET_KEY/, @secret_key) if @secret_key
|
100
|
+
end
|
101
|
+
|
102
|
+
def get_input
|
103
|
+
puts ''
|
104
|
+
print "Email:"
|
105
|
+
email = gets.chomp
|
106
|
+
return email, nil if email == "n"
|
107
|
+
stty_settings = %x[stty -g]
|
108
|
+
print 'Password: '
|
109
|
+
begin
|
110
|
+
%x[stty -echo]
|
111
|
+
password = gets.chomp
|
112
|
+
ensure
|
113
|
+
%x[stty #{stty_settings}]
|
114
|
+
end
|
115
|
+
p ''
|
116
|
+
return email, password
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def show obj
|
122
|
+
$stderr.puts obj
|
123
|
+
end
|
124
|
+
|
125
|
+
|
76
126
|
end
|
@@ -1,20 +1,70 @@
|
|
1
1
|
class CreateUsers < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
if !table_exists? :users
|
4
|
+
create_table :users do |t|
|
5
|
+
t.string :provider
|
6
|
+
t.string :uid
|
7
|
+
t.string :email
|
8
|
+
t.integer :created
|
9
|
+
t.string :username
|
10
|
+
t.boolean :verified
|
11
|
+
t.boolean :admin
|
12
|
+
t.string :referred_by
|
13
|
+
t.string :token
|
14
|
+
t.text :facebook
|
15
|
+
t.text :tags
|
16
|
+
t.text :referred
|
17
|
+
t.text :google
|
18
|
+
t.text :twitter
|
19
|
+
t.text :github
|
20
|
+
t.text :access_tokens
|
21
|
+
t.boolean :subscribed
|
22
|
+
t.string :display
|
12
23
|
|
13
|
-
|
24
|
+
t.timestamps
|
25
|
+
end
|
26
|
+
else
|
27
|
+
safe_column :users, :provider, :string
|
28
|
+
safe_column :users, :uid, :string
|
29
|
+
safe_column :users, :email, :string
|
30
|
+
safe_column :users, :created, :integer
|
31
|
+
safe_column :users, :username, :string
|
32
|
+
safe_column :users, :verified, :booleanied
|
33
|
+
safe_column :users, :admin, :boolean
|
34
|
+
safe_column :users, :referred, :string
|
35
|
+
safe_column :users, :token, :string
|
36
|
+
safe_column :users, :facebook, :text
|
37
|
+
safe_column :users, :tags, :text
|
38
|
+
safe_column :users, :referred, :text
|
39
|
+
safe_column :users, :google, :text
|
40
|
+
safe_column :users, :twitter, :text
|
41
|
+
safe_column :users, :github, :text
|
42
|
+
safe_column :users, :access_tokens, :text
|
43
|
+
safe_column :users, :display, :string
|
44
|
+
safe_column :users, :subscribed, :boolean
|
14
45
|
end
|
15
46
|
end
|
16
47
|
|
17
48
|
def self.down
|
18
49
|
drop_table :users
|
19
50
|
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def table_exists? table
|
55
|
+
ActiveRecord::Base.connection.table_exists?(table.to_s)
|
56
|
+
end
|
57
|
+
|
58
|
+
def column_exists?(table, column)
|
59
|
+
begin
|
60
|
+
ActiveRecord::Base.connection.columns(table.to_s).map(&:name).include?(column.to_s)
|
61
|
+
rescue NoMethodError #actual_columns is empty and it doesn't respond to the map method
|
62
|
+
false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def safe_column table, name, type
|
67
|
+
add_column table.to_sym, name.to_sym, type.to_sym unless column_exists? table, name
|
68
|
+
end
|
69
|
+
|
20
70
|
end
|
@@ -1,20 +1,9 @@
|
|
1
1
|
Rails.configuration.DAILYCRED_CLIENT_ID = "<%= client_id %>"
|
2
2
|
Rails.configuration.DAILYCRED_SECRET_KEY = "<%= secret_key %>"
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
dc_options[:client_options][:ssl] = { :ca_path => '/etc/ssl/certs'}
|
11
|
-
end
|
12
|
-
if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
|
13
|
-
dc_options[:client_options][:ssl] = { :ca_file => '/opt/local/share/curl/curl-ca-bundle.crt' }
|
14
|
-
end
|
15
|
-
|
16
|
-
Rails.application.config.middleware.use OmniAuth::Builder do
|
17
|
-
provider :dailycred, dc_id, dc_secret, dc_options
|
18
|
-
end
|
19
|
-
|
20
|
-
Rails.application.config.middleware.use "Dailycred::Middleware", dc_id
|
4
|
+
# configure where users should be redirected after authentication
|
5
|
+
#
|
6
|
+
# Rails.configuration.DAILYCRED_OPTIONS = {
|
7
|
+
# :after_auth => '/hello', #after login
|
8
|
+
# :after_unauth => '/goodbye' #after logout
|
9
|
+
# }
|
@@ -2,22 +2,28 @@ class SessionsController < ApplicationController
|
|
2
2
|
before_filter :authenticate, :only => [:destroy]
|
3
3
|
before_filter :current_user
|
4
4
|
|
5
|
+
# Callback Route for OAuth flow
|
5
6
|
def create
|
6
|
-
@user = User.
|
7
|
+
@user = User.find_or_create_with_omniauth auth_hash
|
7
8
|
session[:user_id] = @user.id
|
8
9
|
redirect_to auth_path
|
9
10
|
end
|
10
11
|
|
12
|
+
#GET /logout
|
11
13
|
def destroy
|
12
14
|
session[:user_id] = nil
|
13
15
|
redirect_to auth_path
|
14
16
|
end
|
15
17
|
|
16
|
-
def
|
18
|
+
def failure
|
19
|
+
redirect_to auth_path, notice: params[:message]
|
20
|
+
end
|
17
21
|
|
22
|
+
def info
|
18
23
|
end
|
19
24
|
|
20
25
|
private
|
26
|
+
|
21
27
|
def auth_hash
|
22
28
|
request.env['omniauth.auth']
|
23
29
|
end
|
@@ -1,25 +1,3 @@
|
|
1
1
|
class User < ActiveRecord::Base
|
2
|
-
|
3
|
-
def self.create_with_omniauth(model)
|
4
|
-
if model[:provider] == "dailycred"
|
5
|
-
create_with_dailycred model
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
private
|
10
|
-
|
11
|
-
def self.create_with_dailycred(model)
|
12
|
-
info = model[:info]
|
13
|
-
create! do |user|
|
14
|
-
user.provider = model[:provider]
|
15
|
-
user.uid = model[:uid]
|
16
|
-
user.email =info[:email]
|
17
|
-
user.username = info[:username]
|
18
|
-
user.created = info[:created]
|
19
|
-
user.verified = info[:verified]
|
20
|
-
user.admin = info[:admin]
|
21
|
-
user.referred_by = info[:referred_by]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
2
|
+
acts_as_dailycred
|
25
3
|
end
|