dailycred 0.1.21 → 0.1.22
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Rakefile +10 -0
- data/dailycred.gemspec +1 -1
- data/lib/dailycred.rb +14 -13
- data/lib/generators/dailycred_generator.rb +12 -1
- data/lib/generators/templates/login.html.erb +13 -0
- data/lib/generators/templates/migration_create_user.rb +5 -1
- data/lib/generators/templates/omniauth.rb +5 -1
- data/lib/generators/templates/sessions_controller.rb +5 -3
- data/lib/generators/templates/user.rb +8 -19
- data/lib/middleware/middleware.rb +18 -7
- data/lib/omniauth/strategies/dailycred.rb +26 -14
- data/lib/user/user.rb +63 -0
- data/localtest/.gitignore +15 -0
- data/localtest/Gemfile +39 -0
- data/localtest/README.rdoc +261 -0
- data/localtest/Rakefile +7 -0
- data/localtest/app/assets/images/rails.png +0 -0
- data/localtest/app/assets/javascripts/application.js +15 -0
- data/localtest/app/assets/stylesheets/application.css +13 -0
- data/localtest/app/controllers/application_controller.rb +42 -0
- data/localtest/app/controllers/sessions_controller.rb +27 -0
- data/localtest/app/helpers/application_helper.rb +2 -0
- data/localtest/app/mailers/.gitkeep +0 -0
- data/localtest/app/models/.gitkeep +0 -0
- data/localtest/app/models/user.rb +14 -0
- data/localtest/app/views/layouts/application.html.erb +14 -0
- data/localtest/app/views/sessions/info +9 -0
- data/localtest/config/application.rb +62 -0
- data/localtest/config/boot.rb +6 -0
- data/localtest/config/database.yml +25 -0
- data/localtest/config/environment.rb +5 -0
- data/localtest/config/environments/development.rb +37 -0
- data/localtest/config/environments/production.rb +67 -0
- data/localtest/config/environments/test.rb +37 -0
- data/localtest/config/initializers/backtrace_silencers.rb +7 -0
- data/localtest/config/initializers/inflections.rb +15 -0
- data/localtest/config/initializers/mime_types.rb +5 -0
- data/localtest/config/initializers/omniauth.rb +20 -0
- data/localtest/config/initializers/secret_token.rb +7 -0
- data/localtest/config/initializers/session_store.rb +8 -0
- data/localtest/config/initializers/wrap_parameters.rb +14 -0
- data/localtest/config/locales/en.yml +5 -0
- data/localtest/config/routes.rb +61 -0
- data/localtest/config.ru +4 -0
- data/localtest/db/migrate/20120906153457_create_users.rb +21 -0
- data/localtest/db/migrate/20120906234358_alter_column_from_users.rb +6 -0
- data/localtest/db/migrate/20120907004912_add_column_to_users.rb +5 -0
- data/localtest/db/migrate/20120907014514_add_tags_to_users.rb +6 -0
- data/localtest/db/schema.rb +33 -0
- data/localtest/db/seeds.rb +7 -0
- data/localtest/lib/assets/.gitkeep +0 -0
- data/localtest/lib/tasks/.gitkeep +0 -0
- data/localtest/log/.gitkeep +0 -0
- data/localtest/public/404.html +26 -0
- data/localtest/public/422.html +26 -0
- data/localtest/public/500.html +25 -0
- data/localtest/public/favicon.ico +0 -0
- data/localtest/public/robots.txt +5 -0
- data/localtest/script/rails +6 -0
- data/localtest/test/fixtures/.gitkeep +0 -0
- data/localtest/test/functional/.gitkeep +0 -0
- data/localtest/test/integration/.gitkeep +0 -0
- data/localtest/test/performance/browsing_test.rb +12 -0
- data/localtest/test/test_helper.rb +13 -0
- data/localtest/test/unit/.gitkeep +0 -0
- data/localtest/vendor/assets/javascripts/.gitkeep +0 -0
- data/localtest/vendor/assets/stylesheets/.gitkeep +0 -0
- data/localtest/vendor/plugins/.gitkeep +0 -0
- data/spec/{spec_helper.rb → helper_spec.rb} +2 -0
- data/spec/omniauth/strategies/dailycred_spec.rb +50 -10
- data/spec/support/dailycred_spec.rb +58 -0
- data/spec/support/shared_examples.rb +40 -0
- metadata +66 -4
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -1,2 +1,12 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
2
|
require "bundler/gem_tasks"
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
desc 'Default: run specs.'
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc "Run specs"
|
9
|
+
RSpec::Core::RakeTask.new do |t|
|
10
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
11
|
+
# Put spec opts in a file named .rspec in root
|
12
|
+
end
|
data/dailycred.gemspec
CHANGED
data/lib/dailycred.rb
CHANGED
@@ -4,7 +4,14 @@ require "middleware/middleware"
|
|
4
4
|
|
5
5
|
class Dailycred
|
6
6
|
|
7
|
-
attr_accessor :client_id, :secret_key, :options
|
7
|
+
attr_accessor :client_id, :secret_key, :options, :url
|
8
|
+
|
9
|
+
URL = "https://www.dailycred.com"
|
10
|
+
|
11
|
+
ROUTES = {
|
12
|
+
:signup => "/user/api/signup.json",
|
13
|
+
:login => "/user/api/signin.json"
|
14
|
+
}
|
8
15
|
|
9
16
|
# Initializes a dailycred object
|
10
17
|
# @param [String] client_id the client's daiycred client id
|
@@ -14,10 +21,10 @@ class Dailycred
|
|
14
21
|
@client_id = client_id
|
15
22
|
@secret_key = secret_key
|
16
23
|
@options = opts
|
24
|
+
opts[:client_options] ||= {}
|
25
|
+
@url = opts[:client_options][:site] || Dailycred::URL
|
17
26
|
end
|
18
27
|
|
19
|
-
URL = "https://www.dailycred.com"
|
20
|
-
|
21
28
|
# Generates a Dailycred event
|
22
29
|
# @param [String] user_id the user's dailycred user id
|
23
30
|
# @param [String] key the name of the event type
|
@@ -42,7 +49,7 @@ class Dailycred
|
|
42
49
|
post "/admin/api/user/tag.json", opts
|
43
50
|
end
|
44
51
|
|
45
|
-
# Untag a user in dailycred
|
52
|
+
# Untag a user in dailycred
|
46
53
|
# (see #tag)
|
47
54
|
def untag(user_id, tag)
|
48
55
|
opts = {
|
@@ -52,23 +59,18 @@ class Dailycred
|
|
52
59
|
post "/admin/api/user/untag.json", opts
|
53
60
|
end
|
54
61
|
|
55
|
-
private
|
56
|
-
|
57
62
|
def post(url, opts)
|
58
63
|
opts.merge! base_opts
|
59
|
-
p opts
|
60
64
|
response = get_conn.post url, opts
|
61
|
-
p response.body
|
62
65
|
end
|
63
66
|
|
67
|
+
private
|
68
|
+
|
64
69
|
def ssl_opts
|
65
70
|
opts = {}
|
66
|
-
p @options
|
67
|
-
p " ^^^^ @options "
|
68
71
|
if @options[:client_options] && @options[:client_options][:ssl]
|
69
72
|
opts[:ssl] = @options[:client_options][:ssl]
|
70
73
|
end
|
71
|
-
p opts
|
72
74
|
opts
|
73
75
|
end
|
74
76
|
|
@@ -80,7 +82,6 @@ class Dailycred
|
|
80
82
|
end
|
81
83
|
|
82
84
|
def get_conn
|
83
|
-
|
84
|
-
Faraday::Connection.new Dailycred::URL, ssl_opts
|
85
|
+
Faraday::Connection.new @url, ssl_opts
|
85
86
|
end
|
86
87
|
end
|
@@ -17,6 +17,7 @@ class DailycredGenerator < Rails::Generators::Base
|
|
17
17
|
|
18
18
|
private
|
19
19
|
|
20
|
+
# helper method for getting the current signed in user
|
20
21
|
def current_user
|
21
22
|
begin
|
22
23
|
@current_user || User.find(session[:user_id]) if session[:user_id]
|
@@ -25,21 +26,31 @@ class DailycredGenerator < Rails::Generators::Base
|
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
29
|
+
# use as a before_filter to only allow signed in users
|
30
|
+
# example:
|
31
|
+
# before_filter :authenticate
|
28
32
|
def authenticate
|
29
33
|
redirect_to auth_path unless current_user
|
30
34
|
end
|
31
35
|
|
36
|
+
# link to sign up
|
32
37
|
def signup_path
|
33
38
|
"/auth/dailycred"
|
34
39
|
end
|
35
40
|
|
41
|
+
# link to login
|
36
42
|
def login_path
|
37
43
|
"/auth/dailycred?action=signin"
|
38
44
|
end
|
39
45
|
|
46
|
+
# helper method for getting an instance of dailycred
|
47
|
+
# example:
|
48
|
+
# dailycred.tagUser "user_id", "tag"
|
49
|
+
#
|
50
|
+
# for more documentation, visit https://www.dailycred.com/api/ruby
|
40
51
|
def dailycred
|
41
52
|
config = Rails.configuration
|
42
|
-
@dailycred ||= Dailycred.new(config.DAILYCRED_CLIENT_ID, config.DAILYCRED_SECRET_KEY, config.
|
53
|
+
@dailycred ||= Dailycred.new(config.DAILYCRED_CLIENT_ID, config.DAILYCRED_SECRET_KEY, config.dc_options)
|
43
54
|
end
|
44
55
|
EOS
|
45
56
|
|
@@ -3,12 +3,16 @@ class CreateUsers < ActiveRecord::Migration
|
|
3
3
|
create_table :users do |t|
|
4
4
|
t.string :provider, null: false
|
5
5
|
t.string :uid, null: false
|
6
|
-
t.string :email
|
6
|
+
t.string :email
|
7
7
|
t.integer :created, :limit => 8
|
8
8
|
t.string :username
|
9
9
|
t.boolean :verified
|
10
10
|
t.boolean :admin
|
11
11
|
t.string :referred_by
|
12
|
+
t.string :token
|
13
|
+
t.text :facebook
|
14
|
+
t.text :tags
|
15
|
+
t.text :referred
|
12
16
|
|
13
17
|
t.timestamps
|
14
18
|
end
|
@@ -4,7 +4,11 @@ Rails.configuration.DAILYCRED_SECRET_KEY = "<%= secret_key %>"
|
|
4
4
|
dc_id = Rails.configuration.DAILYCRED_CLIENT_ID
|
5
5
|
dc_secret = Rails.configuration.DAILYCRED_SECRET_KEY
|
6
6
|
|
7
|
-
dc_options = { :client_options => {
|
7
|
+
dc_options = { :client_options => {
|
8
|
+
:site => "http://localhost:9000",
|
9
|
+
:authorize_url => '/oauth/authorize',
|
10
|
+
:token_url => '/oauth/access_token'
|
11
|
+
} }
|
8
12
|
|
9
13
|
if File.exists?('/etc/ssl/certs')
|
10
14
|
dc_options[:client_options][:ssl] = { :ca_path => '/etc/ssl/certs'}
|
@@ -2,22 +2,24 @@ 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
|
-
redirect_to
|
9
|
+
redirect_to "/auth"
|
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
18
|
def info
|
17
|
-
|
18
19
|
end
|
19
20
|
|
20
21
|
private
|
22
|
+
|
21
23
|
def auth_hash
|
22
24
|
request.env['omniauth.auth']
|
23
25
|
end
|
@@ -1,25 +1,14 @@
|
|
1
1
|
class User < ActiveRecord::Base
|
2
|
+
serialize :facebook, Hash
|
3
|
+
serialize :tags, Array
|
4
|
+
serialize :referred, Array
|
2
5
|
|
3
|
-
|
4
|
-
if model[:provider] == "dailycred"
|
5
|
-
create_with_dailycred model
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
private
|
6
|
+
attr_accessible :email, :id, :username, :created, :verified, :admin, :referred_by, :referred, :facebook, :tags, :provider, :uid, :token
|
10
7
|
|
11
|
-
def self.
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
8
|
+
def self.find_or_create_with_omniauth(model)
|
9
|
+
@user = User.find_by_provider_and_uid(model[:provider], model[:uid]) || User.new
|
10
|
+
@user.update_attributes model[:info]
|
11
|
+
@user
|
23
12
|
end
|
24
13
|
|
25
14
|
end
|
@@ -2,8 +2,12 @@ class 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[:triggers] ||= []
|
10
|
+
@app = app
|
7
11
|
@client_id = client_id
|
8
12
|
end
|
9
13
|
|
@@ -35,20 +39,27 @@ class Dailycred
|
|
35
39
|
var dc, dlh, home, id, page, referrer, title, url;
|
36
40
|
window.dc_opts = {
|
37
41
|
clientId: "#{@client_id}",
|
38
|
-
home: "
|
42
|
+
home: "#{@opts[:url]}"
|
39
43
|
};
|
40
44
|
id = dc_opts.clientId;
|
41
|
-
home = "https://www.dailycred.com";
|
45
|
+
home = window.dc_opts.home || "https://www.dailycred.com";
|
42
46
|
dlh = document.location.href;
|
43
|
-
page =
|
47
|
+
page = encodeURIComponent(dlh);
|
44
48
|
title = document.title ? document.title : "";
|
45
|
-
referrer = document.referrer ? document.referrer : "";
|
49
|
+
referrer = document.referrer ? encodeURIComponent(document.referrer) : "";
|
46
50
|
dc = document.createElement("img");
|
47
|
-
url = "" + home + "/dc.gif?
|
51
|
+
url = "" + home + "/dc.gif?url=" + page + "&title=" + title + "&client_id=" + window.dc_opts.clientId + "&referrer=" + referrer;
|
48
52
|
dc.src = url;
|
49
53
|
document.body.appendChild(dc);
|
50
54
|
}).call(this);
|
51
55
|
</script>
|
56
|
+
<script src="#{@opts[:url]}/public/js/dailycred.coffee"></script>
|
57
|
+
<script>
|
58
|
+
DC.init({
|
59
|
+
"showModal" : #{@opts[:modal]},
|
60
|
+
"triggers" : #{@opts[:triggers].to_s}
|
61
|
+
});
|
62
|
+
</script>
|
52
63
|
<!-- end dailycred -->
|
53
64
|
EOT
|
54
65
|
end
|
@@ -2,34 +2,39 @@ require 'omniauth-oauth2'
|
|
2
2
|
require 'faraday'
|
3
3
|
require 'net/https'
|
4
4
|
require 'json'
|
5
|
+
require 'pp'
|
5
6
|
|
6
7
|
module OmniAuth
|
7
8
|
module Strategies
|
8
9
|
class Dailycred < OmniAuth::Strategies::OAuth2
|
10
|
+
|
9
11
|
option :client_options, {
|
10
|
-
:site =>
|
12
|
+
:site => "https://www.dailycred.com",
|
11
13
|
:authorize_url => '/oauth/authorize',
|
12
14
|
:token_url => '/oauth/access_token'
|
13
15
|
}
|
14
16
|
|
15
|
-
ATTRIBUTES = ["email", "
|
17
|
+
ATTRIBUTES = ["email", "username", "created", "verified", "admin", "referred_by", "tags", "referred"]
|
16
18
|
AUTH_PARAMS = ["action"]
|
17
19
|
|
18
20
|
option :authorize_options, OmniAuth::Strategies::Dailycred::AUTH_PARAMS
|
19
|
-
|
21
|
+
|
20
22
|
uid { user['id'] }
|
21
|
-
|
23
|
+
|
22
24
|
info do
|
23
|
-
|
24
|
-
OmniAuth::Strategies::Dailycred::ATTRIBUTES.each do |attribute|
|
25
|
-
infos[attribute] = user[attribute]
|
26
|
-
end
|
27
|
-
infos
|
25
|
+
user
|
28
26
|
end
|
29
|
-
|
27
|
+
|
30
28
|
alias :old_request_phase :request_phase
|
31
29
|
|
32
|
-
|
30
|
+
|
31
|
+
def authorize_params
|
32
|
+
super.tap do |params|
|
33
|
+
params[:state] ||= {}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def request_phase
|
33
38
|
OmniAuth::Strategies::Dailycred::AUTH_PARAMS.each do |param|
|
34
39
|
val = session['omniauth.params'][param]
|
35
40
|
if val && !val.empty?
|
@@ -44,19 +49,26 @@ module OmniAuth
|
|
44
49
|
|
45
50
|
def user
|
46
51
|
return @duser if !@duser.nil?
|
47
|
-
connection = Faraday::Connection.new
|
52
|
+
connection = Faraday::Connection.new options.client_options[:site], :ssl => {
|
48
53
|
:ca_file => "/opt/local/share/curl/curl-ca-bundle.crt"
|
49
54
|
}
|
50
55
|
response = connection.get("/graph/me.json?access_token=#{access_token.token}")
|
51
56
|
json = JSON.parse(response.body)
|
52
|
-
|
57
|
+
pp json
|
58
|
+
@duser = {'token' => access_token.token}
|
59
|
+
@duser['provider'] = 'dailycred'
|
60
|
+
@duser['uid'] = json['id'] || json['user_id']
|
53
61
|
OmniAuth::Strategies::Dailycred::ATTRIBUTES.each do |attr|
|
54
62
|
@duser[attr] = json[attr]
|
55
63
|
end
|
64
|
+
if !json["FACEBOOK"].nil?
|
65
|
+
@duser['facebook'] = json["FACEBOOK"]["members"]
|
66
|
+
end
|
67
|
+
pp @duser
|
56
68
|
|
57
69
|
@duser
|
58
70
|
end
|
59
|
-
|
71
|
+
|
60
72
|
end
|
61
73
|
end
|
62
74
|
end
|
data/lib/user/user.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
class Dailycred
|
2
|
+
class Auth
|
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
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile ~/.gitignore_global
|
6
|
+
|
7
|
+
# Ignore bundler config
|
8
|
+
/.bundle
|
9
|
+
|
10
|
+
# Ignore the default SQLite database.
|
11
|
+
/db/*.sqlite3
|
12
|
+
|
13
|
+
# Ignore all logfiles and tempfiles.
|
14
|
+
/log/*.log
|
15
|
+
/tmp
|
data/localtest/Gemfile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '3.2.8'
|
4
|
+
|
5
|
+
# Bundle edge Rails instead:
|
6
|
+
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
7
|
+
|
8
|
+
gem 'sqlite3'
|
9
|
+
gem 'dailycred', :path => "../"
|
10
|
+
|
11
|
+
|
12
|
+
# Gems used only for assets and not required
|
13
|
+
# in production environments by default.
|
14
|
+
group :assets do
|
15
|
+
gem 'sass-rails', '~> 3.2.3'
|
16
|
+
gem 'coffee-rails', '~> 3.2.1'
|
17
|
+
|
18
|
+
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
|
19
|
+
# gem 'therubyracer', :platforms => :ruby
|
20
|
+
|
21
|
+
gem 'uglifier', '>= 1.0.3'
|
22
|
+
end
|
23
|
+
|
24
|
+
gem 'jquery-rails'
|
25
|
+
|
26
|
+
# To use ActiveModel has_secure_password
|
27
|
+
# gem 'bcrypt-ruby', '~> 3.0.0'
|
28
|
+
|
29
|
+
# To use Jbuilder templates for JSON
|
30
|
+
# gem 'jbuilder'
|
31
|
+
|
32
|
+
# Use unicorn as the app server
|
33
|
+
# gem 'unicorn'
|
34
|
+
|
35
|
+
# Deploy with Capistrano
|
36
|
+
# gem 'capistrano'
|
37
|
+
|
38
|
+
# To use debugger
|
39
|
+
# gem 'debugger'
|