dailycred 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-dailycred.gemspec
4
+ gemspec
5
+
6
+ gem 'omniauth'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Hank Stoever
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,76 @@
1
+ # Dailycred
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ And then execute:
8
+
9
+ $ bundle
10
+
11
+ Or install it yourself as:
12
+
13
+ $ gem install omniauth-dailycred
14
+
15
+ ## Usage
16
+
17
+ bash
18
+
19
+ rails g dailycred YOUR_CLIENT_ID YOUR_SECRET_KEY
20
+ rake db:migrate
21
+
22
+ This will generate everything you need to get going with authentication, including a user model, session controller, omniauth initializer, javascript tracking code, and many helper variables. You will You can locate your API keys at [dailycred](https://www.dailycred.com/admin/settings/keys)
23
+
24
+ ##### Authentication
25
+
26
+ Use the `:authenticate` helper to require a user to be signed in:
27
+
28
+ before_filter :authenticate
29
+
30
+ The current user object can be located with `current_user`:
31
+
32
+ # in posts_controller
33
+
34
+ @posts = currrent_user.posts.all
35
+
36
+ ##### Using only with Omniauth
37
+
38
+ If you already have omniauth set up and only want to use Dailycred as another OAuth provider, just add this line to your omniauth initializer file
39
+
40
+ provider :dailycred, 'CLIENT_ID', 'SECRET_KEY'
41
+
42
+ ##### Events
43
+
44
+ To fire an event to be logged in Dailycred:
45
+
46
+ #in your controller
47
+
48
+ before_filter :dailycred
49
+
50
+ def create
51
+ ...
52
+
53
+ # after successfully saving the model:
54
+ @dailycred.event(current_user.uid, 'Created Post', @post.name)
55
+
56
+ end
57
+
58
+ ## SSL Error
59
+
60
+ You may get an error such as the following:
61
+
62
+ Faraday::Error::ConnectionFailed (SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed):
63
+
64
+ If that is the case, consider following fixes explained [here](https://github.com/technoweenie/faraday/wiki/Setting-up-SSL-certificates) or [here](http://martinottenwaelter.fr/2010/12/ruby19-and-the-ssl-error).
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
73
+
74
+ omniauth-dailycred
75
+
76
+ OmniAuth adapter for dailycred using their OAuth2 Strategy
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/dailycred.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-=
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Hank Stoever"]
5
+ gem.email = ["hstove@gmail.com"]
6
+ gem.description = %q{descript}
7
+ gem.summary = %q{summary}
8
+ gem.homepage = "https://www.dailycred.com"
9
+ gem.add_dependency("omniauth")
10
+ gem.add_dependency("omniauth-oauth2")
11
+
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "dailycred"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = "0.1.0"
18
+ end
data/lib/dailycred.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "omniauth-dailycred/version"
2
+ require "omniauth/strategies/dailycred"
3
+ require "middleware/middleware"
4
+
5
+ class Dailycred
6
+
7
+ attr_accessor :client_id, :secret_key
8
+
9
+ def initialize(client_id, secret_key="")
10
+ @client_id = client_id
11
+ @secret_key = secret_key
12
+ end
13
+
14
+ URL = "https://www.dailycred.com"
15
+
16
+ def event(user_id, key, val="")
17
+ connection = Faraday::Connection.new Dailycred::URL, :ssl => { :ca_file => "/opt/local/share/curl/curl-ca-bundle.crt" }
18
+ opts = {
19
+ :client_id => @client_id,
20
+ :client_secret => @secret_key,
21
+ :key => key,
22
+ :valuestring => val,
23
+ :user_id => user_id
24
+ }
25
+ connection.post "/admin/api/customevent.json", opts
26
+ end
27
+
28
+ end
29
+
30
+ module Omniauth
31
+ module Dailycred
32
+ # Your code goes here...
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ Description:
2
+ Generates a user model and sessions controller for authentication with omniauth and dailycred. Also includes migration file and initializer.
3
+
4
+ Usage:
5
+ This generator takes two arguments: client_id and secret_key. If you do not specify your Dailycred API keys they will default to "YOUR_CLIENT_ID" and "YOUR_SECRET_KEY", respectively. You can later specify them at /config/initializers/omniauth.rb
6
+
7
+ Examples:
8
+
9
+ rails g dailycred
10
+
11
+ creates all files without your client_id and secret_key pre-inserted.
12
+
13
+ rails g dailycred client-id-here secret-key-here
14
+
15
+ creates all files with client_id "client-id-here" and secret_key "secret-key-here"
16
+
@@ -0,0 +1,76 @@
1
+ class DailycredGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ argument :client_id, :type => :string, :default => 'YOUR_CLIENT_ID', :banner => 'dailycred_client_id'
5
+ argument :secret_key, :type => :string, :default => 'YOUR_SECRET_KEY', :banner => 'dailycred_secret_key'
6
+
7
+ APP_NAME = Rails.application.class.parent.name
8
+
9
+ APP_ROUTES_LINES =<<-EOS
10
+ match '/auth/:provider/callback' => 'sessions#create'
11
+ match "/logout" => "sessions#destroy", :as => :logout
12
+ match "/auth" => "sessions#info", :as => :auth
13
+ EOS
14
+
15
+ APP_CONTROLLER_LINES =<<-EOS
16
+ helper_method :current_user, :login_path, :dailycred
17
+
18
+ private
19
+
20
+ def current_user
21
+ begin
22
+ @current_user || User.find(session[:user_id]) if session[:user_id]
23
+ rescue
24
+ nil
25
+ end
26
+ 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 signin_path
37
+ "/auth/dailycred?action=signup"
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
+ EOS
45
+
46
+ 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
+ # copy initializer
62
+ template "omniauth.rb", "config/initializers/omniauth.rb"
63
+ # session_controller
64
+ copy_file "sessions_controller.rb", "app/controllers/sessions_controller.rb"
65
+ # application controller
66
+ inject_into_class "app/controllers/application_controller.rb", ApplicationController, APP_CONTROLLER_LINES
67
+ # add user_model
68
+ copy_file "user.rb", "app/models/user.rb"
69
+ # session_controller
70
+ 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
+ # config/routes
74
+ inject_into_file "config/routes.rb", APP_ROUTES_LINES, :after => "#{APP_NAME}::Application.routes.draw do\n"
75
+ end
76
+ end
@@ -0,0 +1,7 @@
1
+ <% if current_user %>
2
+ <p>Hello <%= current_user.email %> <br>
3
+ <%= link_to 'Logout', logout_path %>
4
+ </p>
5
+ <% else %>
6
+ <%= link_to 'Login In', login_path %> or <%= link_to 'Sign Up', signup_path %>
7
+ <% end %>
@@ -0,0 +1,20 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.string :provider, null: false
5
+ t.string :uid, null: false
6
+ t.string :email, null: false, uniq: true
7
+ t.integer :created, :limit => 8
8
+ t.string :username
9
+ t.boolean :verified
10
+ t.boolean :admin
11
+ t.string :referred_by
12
+
13
+ t.timestamps
14
+ end
15
+ end
16
+
17
+ def self.down
18
+ drop_table :users
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ Rails.configuration.DAILYCRED_CLIENT_ID = "<%= client_id %>"
2
+ Rails.configuration.DAILYCRED_SECRET_KEY = "<%= secret_key %>"
3
+
4
+ dc_id = Rails.configuration.DAILYCRED_CLIENT_ID
5
+ dc_secret = Rails.configuration.DAILYCRED_SECRET_KEY
6
+
7
+ Rails.application.config.middleware.use OmniAuth::Builder do
8
+ provider :dailycred, dc_id, dc_secret
9
+ #if you get an error like this:
10
+ # => SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
11
+
12
+ #use this for OS X
13
+ #provider :dailycred, dc_id, dc_secret, {:client_options => {:ssl => {:ca_file => '/opt/local/share/curl/curl-ca-bundle.crt'}}}
14
+
15
+ #or this for ubuntu
16
+ #provider :dailycred, dc_id, dc_secret, {:client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}}}
17
+
18
+ #if you still get the SSL error, look here for help: http://martinottenwaelter.fr/2010/12/ruby19-and-the-ssl-error
19
+ end
20
+
21
+ Rails.application.config.middleware.use "Dailycred::Middleware", dc_id
@@ -0,0 +1,25 @@
1
+ class SessionsController < ApplicationController
2
+ before_filter :authenticate, :only => [:destroy]
3
+ before_filter :current_user
4
+
5
+ def create
6
+ @user = User.find_by_provider_and_uid(auth_hash['provider'], auth_hash['uid']) || User.create_with_omniauth(auth_hash)
7
+ session[:user_id] = @user.id
8
+ redirect_to auth_path
9
+ end
10
+
11
+ def destroy
12
+ session[:user_id] = nil
13
+ redirect_to auth_path
14
+ end
15
+
16
+ def info
17
+
18
+ end
19
+
20
+ private
21
+ def auth_hash
22
+ request.env['omniauth.auth']
23
+ end
24
+ end
25
+
@@ -0,0 +1,25 @@
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
+
25
+ end
@@ -0,0 +1,57 @@
1
+ class Dailycred
2
+ class Middleware
3
+ attr_accessor :client_id
4
+
5
+ def initialize(app, client_id)
6
+ @app = app
7
+ @client_id = client_id
8
+ end
9
+
10
+ def call(env)
11
+ @env = env
12
+ @status, @headers, @response = @app.call(env)
13
+
14
+ if @headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
15
+ body = ""
16
+ @response.each { |part| body << part }
17
+ index = body.rindex("</body>")
18
+ if index
19
+ body.insert(index, render_dailycred_scripts)
20
+ @headers["Content-Length"] = body.length.to_s
21
+ @response = [body]
22
+ end
23
+ end
24
+
25
+ [@status, @headers, @response]
26
+ end
27
+
28
+ private
29
+
30
+ def render_dailycred_scripts
31
+ <<-EOT
32
+ <!-- dailycred -->
33
+ <script type="text/javascript">
34
+ (function() {
35
+ var dc, dlh, home, id, page, referrer, title, url;
36
+ window.dc_opts = {
37
+ clientId: "#{@client_id}",
38
+ home: "https://www.dailycred.com"
39
+ };
40
+ id = dc_opts.clientId;
41
+ home = "https://www.dailycred.com";
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;
48
+ dc.src = url;
49
+ document.body.appendChild(dc);
50
+ }).call(this);
51
+ </script>
52
+ <!-- end dailycred -->
53
+ EOT
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,62 @@
1
+ require 'omniauth-oauth2'
2
+ require 'faraday'
3
+ require 'net/https'
4
+ require 'json'
5
+
6
+ module OmniAuth
7
+ module Strategies
8
+ class Dailycred < OmniAuth::Strategies::OAuth2
9
+ option :client_options, {
10
+ :site => 'https://www.dailycred.com',
11
+ :authorize_url => '/oauth/authorize',
12
+ :token_url => '/oauth/access_token'
13
+ }
14
+
15
+ ATTRIBUTES = ["email", "id", "username", "created", "verified", "admin", "referred_by", "tags", "referred"]
16
+ AUTH_PARAMS = ["action"]
17
+
18
+ option :authorize_options, OmniAuth::Strategies::Dailycred::AUTH_PARAMS
19
+
20
+ uid { user['id'] }
21
+
22
+ info do
23
+ infos = {}
24
+ OmniAuth::Strategies::Dailycred::ATTRIBUTES.each do |attribute|
25
+ infos[attribute] = user[attribute]
26
+ end
27
+ infos
28
+ end
29
+
30
+ alias :old_request_phase :request_phase
31
+
32
+ def request_phase
33
+ OmniAuth::Strategies::Dailycred::AUTH_PARAMS.each do |param|
34
+ val = session['omniauth.params'][param]
35
+ if val && !val.empty?
36
+ options[:authorize_params] ||= {}
37
+ options[:authorize_params].merge!(param => val)
38
+ end
39
+ end
40
+ old_request_phase
41
+ end
42
+
43
+ private
44
+
45
+ def user
46
+ return @duser if !@duser.nil?
47
+ connection = Faraday::Connection.new 'https://www.dailycred.com', :ssl => {
48
+ :ca_file => "/opt/local/share/curl/curl-ca-bundle.crt"
49
+ }
50
+ response = connection.get("/graph/me.json?access_token=#{access_token.token}")
51
+ json = JSON.parse(response.body)
52
+ @duser = {}
53
+ OmniAuth::Strategies::Dailycred::ATTRIBUTES.each do |attr|
54
+ @duser[attr] = json[attr]
55
+ end
56
+
57
+ @duser
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,5 @@
1
+ module Omniauth
2
+ module Dailycred
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::Dailycred do
4
+ subject do
5
+ OmniAuth::Strategies::Dailycred.new(nil, @options || {})
6
+ end
7
+
8
+
9
+ describe '#client' do
10
+ it 'should have the correct dwolla site' do
11
+ subject.client.site.should eq("https://auth.dailycred.com")
12
+ end
13
+ it 'should have the correct authorization url' do
14
+ subject.client.options[:authorize_url].should eq("https://auth.dailycred.com/oauth/authorize")
15
+ end
16
+
17
+ it 'should have the correct token url' do
18
+ subject.client.options[:token_url].should eq('https://auth.dailycred.com/oauth/tokeninfo')
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'rspec'
6
+ require 'rack/test'
7
+ require 'omniauth'
8
+ require 'dailycred'
9
+
10
+ Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f }
11
+
12
+ RSpec.configure do |config|
13
+ config.include Rack::Test::Methods
14
+ config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
15
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dailycred
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hank Stoever
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth
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: omniauth-oauth2
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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
+ description: descript
47
+ email:
48
+ - hstove@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - dailycred.gemspec
59
+ - lib/dailycred.rb
60
+ - lib/generators/USAGE
61
+ - lib/generators/dailycred_generator.rb
62
+ - lib/generators/templates/info.html.erb
63
+ - lib/generators/templates/migration_create_user.rb
64
+ - lib/generators/templates/omniauth.rb
65
+ - lib/generators/templates/sessions_controller.rb
66
+ - lib/generators/templates/user.rb
67
+ - lib/middleware/middleware.rb
68
+ - lib/omniauth-dailycred/version.rb
69
+ - lib/omniauth/strategies/dailycred.rb
70
+ - spec/omniauth/strategies/dailycred_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: https://www.dailycred.com
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.24
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: summary
96
+ test_files:
97
+ - spec/omniauth/strategies/dailycred_spec.rb
98
+ - spec/spec_helper.rb