dailycred 0.1.8 → 0.1.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/.gitignore +0 -4
  2. data/Gemfile +3 -15
  3. data/README.md +34 -190
  4. data/Rakefile +0 -41
  5. data/dailycred.gemspec +5 -7
  6. data/lib/dailycred.rb +86 -1
  7. data/lib/generators/dailycred_generator.rb +44 -92
  8. data/lib/generators/templates/info.html.erb +1 -1
  9. data/lib/generators/templates/migration_create_user.rb +10 -54
  10. data/lib/generators/templates/omniauth.rb +17 -6
  11. data/lib/generators/templates/sessions_controller.rb +2 -8
  12. data/lib/generators/templates/user.rb +23 -1
  13. data/lib/middleware/middleware.rb +57 -0
  14. data/lib/omniauth/strategies/dailycred.rb +62 -0
  15. data/lib/omniauth-dailycred/version.rb +5 -0
  16. data/spec/omniauth/strategies/dailycred_spec.rb +22 -0
  17. data/spec/{helper_spec.rb → spec_helper.rb} +3 -3
  18. metadata +23 -107
  19. checksums.yaml +0 -7
  20. data/.travis.yml +0 -3
  21. data/Guardfile +0 -15
  22. data/app/controllers/sessions_controller.rb +0 -31
  23. data/app/views/sessions/info.html.erb +0 -13
  24. data/config/routes.rb +0 -7
  25. data/docs/lib/dailycred/acts_as_dailycred.html +0 -42
  26. data/docs/lib/dailycred/client.html +0 -214
  27. data/docs/lib/dailycred/engine.html +0 -112
  28. data/docs/lib/dailycred/helper.html +0 -125
  29. data/docs/lib/dailycred/middleware.html +0 -42
  30. data/docs/lib/dailycred/tests_helper.html +0 -42
  31. data/docs/lib/dailycred/user.html +0 -133
  32. data/docs/lib/dailycred.html +0 -42
  33. data/docs/lib/generators/dailycred_generator.html +0 -42
  34. data/docs/lib/generators/templates/migration_create_user.html +0 -39
  35. data/docs/lib/generators/templates/omniauth.html +0 -71
  36. data/docs/lib/generators/templates/sessions_controller.html +0 -96
  37. data/docs/lib/generators/templates/user.html +0 -42
  38. data/docs/lib/middleware/middleware.html +0 -38
  39. data/docs/lib/omniauth/strategies/dailycred.html +0 -199
  40. data/docs/lib/omniauth-dailycred/version.html +0 -38
  41. data/docs/lib/user/user.html +0 -129
  42. data/dummy/.gitignore +0 -16
  43. data/dummy/Gemfile +0 -40
  44. data/dummy/README.rdoc +0 -28
  45. data/dummy/Rakefile +0 -6
  46. data/dummy/app/assets/images/rails.png +0 -0
  47. data/dummy/app/assets/javascripts/application.js +0 -16
  48. data/dummy/app/assets/stylesheets/application.css +0 -13
  49. data/dummy/app/controllers/application_controller.rb +0 -17
  50. data/dummy/app/controllers/concerns/.keep +0 -0
  51. data/dummy/app/helpers/application_helper.rb +0 -2
  52. data/dummy/app/mailers/.keep +0 -0
  53. data/dummy/app/models/.keep +0 -0
  54. data/dummy/app/models/concerns/.keep +0 -0
  55. data/dummy/app/models/user.rb +0 -3
  56. data/dummy/app/views/layouts/application.html.erb +0 -14
  57. data/dummy/bin/bundle +0 -3
  58. data/dummy/bin/rails +0 -4
  59. data/dummy/bin/rake +0 -4
  60. data/dummy/config/application.rb +0 -22
  61. data/dummy/config/boot.rb +0 -4
  62. data/dummy/config/database.yml +0 -25
  63. data/dummy/config/environment.rb +0 -5
  64. data/dummy/config/environments/development.rb +0 -27
  65. data/dummy/config/environments/production.rb +0 -80
  66. data/dummy/config/environments/test.rb +0 -36
  67. data/dummy/config/initializers/backtrace_silencers.rb +0 -7
  68. data/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  69. data/dummy/config/initializers/inflections.rb +0 -16
  70. data/dummy/config/initializers/mime_types.rb +0 -5
  71. data/dummy/config/initializers/omniauth.rb +0 -9
  72. data/dummy/config/initializers/secret_token.rb +0 -12
  73. data/dummy/config/initializers/session_store.rb +0 -3
  74. data/dummy/config/initializers/wrap_parameters.rb +0 -14
  75. data/dummy/config/locales/en.yml +0 -23
  76. data/dummy/config/routes.rb +0 -50
  77. data/dummy/config.ru +0 -4
  78. data/dummy/db/migrate/20130417131939_create_users.rb +0 -64
  79. data/dummy/db/schema.rb +0 -36
  80. data/dummy/db/seeds.rb +0 -7
  81. data/dummy/lib/assets/.keep +0 -0
  82. data/dummy/lib/tasks/.keep +0 -0
  83. data/dummy/log/.keep +0 -0
  84. data/dummy/public/404.html +0 -27
  85. data/dummy/public/422.html +0 -26
  86. data/dummy/public/500.html +0 -26
  87. data/dummy/public/favicon.ico +0 -0
  88. data/dummy/public/robots.txt +0 -5
  89. data/dummy/test/controllers/.keep +0 -0
  90. data/dummy/test/fixtures/.keep +0 -0
  91. data/dummy/test/helpers/.keep +0 -0
  92. data/dummy/test/integration/.keep +0 -0
  93. data/dummy/test/mailers/.keep +0 -0
  94. data/dummy/test/models/.keep +0 -0
  95. data/dummy/test/test_helper.rb +0 -15
  96. data/dummy/vendor/assets/javascripts/.keep +0 -0
  97. data/dummy/vendor/assets/stylesheets/.keep +0 -0
  98. data/lib/dailycred/acts_as_dailycred.rb +0 -74
  99. data/lib/dailycred/client.rb +0 -106
  100. data/lib/dailycred/engine.rb +0 -53
  101. data/lib/dailycred/helper.rb +0 -61
  102. data/lib/dailycred/middleware.rb +0 -70
  103. data/lib/dailycred/tests_helper.rb +0 -5
  104. data/lib/dailycred/user.rb +0 -63
  105. data/lib/dailycred/version.rb +0 -3
  106. data/lib/generators/templates/login.html.erb +0 -13
  107. data/script/download_info.rb +0 -11
  108. data/spec/support/dailycred_spec.rb +0 -72
  109. data/spec/support/shared_examples.rb +0 -40
  110. data/test/generator_test.rb +0 -83
  111. data/test/test_helper.rb +0 -6
data/.gitignore CHANGED
@@ -15,7 +15,3 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- localtest/
19
- twooter/
20
- examples/tester/
21
- examples/
data/Gemfile CHANGED
@@ -1,18 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ # Specify your gem's dependencies in omniauth-dailycred.gemspec
3
4
  gemspec
4
- gem 'rspec'
5
- gem 'rails', '~> 3.2.12'
6
- gem 'simplecov'
7
- gem 'ruby_gntp'
8
- gem 'growl'
9
- gem 'spork'
10
- gem 'guard'
11
- gem 'guard-test'
12
- gem 'guard-rspec'
13
- gem "mocha", '0.12.1', :require => false
14
- gem 'fl-rocco'
15
- gem 'redcarpet', '~> 1.17'
16
- gem 'markdown'
17
- gem 'maruku'
18
- gem 'faraday'
5
+
6
+ gem 'omniauth'
data/README.md CHANGED
@@ -1,223 +1,67 @@
1
- ## Ruby on Rails
1
+ # Dailycred
2
2
 
3
- The Dailycred ruby gem is everything you need to get off the ground running with robust authentication. It includes an [omniauth](https://github.com/intridea/omniauth) provider and a generator to create necessary models and controllers. The generated authentication structure is inspired by [nifty-generators](https://github.com/ryanb/nifty-generators).
3
+ ## Installation
4
4
 
5
- If you'd rather only communicate with Dailycred through OAuth and provide your own authentication controllers and models, you can use the [omniauth_dailycred](https://github.com/dailycred/omniauth_dailycred) gem.
5
+ Add this line to your application's Gemfile:
6
6
 
7
- ##Installation
7
+ And then execute:
8
8
 
9
- To get started using Dailycred with Ruby on Rails, the first thing you need to do is add the dailycred gem to your gemfile:
9
+ $ bundle
10
10
 
11
- gem 'dailycred'
11
+ Or install it yourself as:
12
12
 
13
- Make sure you've signed up for Dailycred, and head over to your [settings](https://www.dailycred.com/admin/settings) page to get your API keys. Once you've done that, head back to the command line and run:
13
+ $ gem install omniauth-dailycred
14
14
 
15
- bundle
16
- rails g dailycred
17
- rake db:migrate
18
-
19
- Thats it! You've successfully added authentication to your app, and you can start signing up users. Run `rails s` to start your
20
- server, and point your browser to [http://localhost:3000/auth](http://localhost:3000/auth) and you'll see a pre-built page with links to sign up.
21
-
22
- Here's what the dailycred gem generates for you:
23
-
24
- * A few helper methods to `/app/controllers/application_controller.rb`.
25
- * An initializer file at `/config/initializers/omniauth.rb` which configures your dailycred API keys.
26
- * `/app/models/user.rb`, the User model.
27
- * A migration file to create the user table in your database.
28
-
29
-
30
- While this is enough to get off the ground running with user authentication, this setup is meant to be lightweight and flexible, so feel free to tinker with
31
- any of the generated code to better match your needs.
32
-
33
- ## Authenticating
15
+ ## Usage
34
16
 
35
- ### login_path
36
-
37
- A helper for linking to the authentication url.
38
-
39
- <%= link_to 'sign up', login_path %>
40
- # => <a href="/auth/dailycred">sign up</a>
41
-
42
- ### logout_path
43
-
44
- To logout a user, simply send them to `/auth/logout`.
45
-
46
- <%= link_to 'logout', logout_path %>
47
- # => <a href="/auth/logout">logout</a>
17
+ bash
48
18
 
49
- ### authenticate
50
-
51
- To protect a controller method from unauthorized users, use the 'authorize' helper method as a `before_filter`.
52
-
53
- #before_filter :authenticate, :except => [:index] #don't authenticate some
54
- #before_filter :authenticate, :only => [:create, :new] #only authenticate some
55
- before_filter :authenticate #all methods
56
-
57
- ## Social Connect
58
-
59
- To use a social sign-in service instead of email, and password, use `connect_path.`
60
-
61
- <%= link_to 'sign in with facebook', connect_path(:identity_provider => :facebook) %>
62
-
63
- The `identity_provider` can be one of `facebook`, `google`, `twitter`, `disqus`, or `instagram`.
64
-
65
- After a user has social connected, their social data is serialized into individual fields in the user model. The serialized object is the exact same as what the social provider's graph response returns. For example:
66
-
67
- p current_user.facebook
68
- # =>
69
- {
70
- "video_upload_limits" => {
71
- "length" => 1200.0,
72
- "size" => 1073741824.0
73
- },
74
- "locale" => "en_US",
75
- "link" => "http://www.facebook.com/joe.smith",
76
- "updated_time" => "2012-09-27T19:04:38+0000",
77
- "currency" => {
78
- "user_currency" => "USD",
79
- "currency_exchange" => 10.0,
80
- "currency_exchange_inverse" => 0.1,
81
- "currency_offset" => 100.0
82
- },
83
- "picture" => {
84
- "data" => {
85
- "url" => "http://profile.ak.fbcdn.net/hprofile-ak-ash4/370570_1039690812_2022945351_q.jpg",
86
- "is_silhouette" => false
87
- }
88
- },
89
- "id" => "1092609812",
90
- "third_party_id" => "cBLDKnqlfYlReV7Jo4yRAFB1a4I",
91
- "first_name" => "Joe",
92
- "username" => "jsmitty",
93
- "bio" => "shred the gnar.",
94
- "email" => "jsmitty@dailycred.com",
95
- "verified" => true,
96
- "name" => "Joe Smith",
97
- "last_name" => "Stoever",
98
- "gender" => "male",
99
- "access_token" =>"AAAFHsZAi9ddUBAKPMOKPDrmJlclwCoVHCfwflF5ZCyLZC70SOo0MPvj62lhHZAnV6jk8DEfBSjLtfcyC7Bx25a9CLphzoayv3EtvbE2tAQZDZD"
100
- }
19
+ rails g dailycred YOUR_CLIENT_ID YOUR_SECRET_KEY
20
+ rake db:migrate
101
21
 
102
- You can also connect additional social accounts to an existing user:
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)
103
23
 
104
- <%= link_to 'connect with facebook', connect_user(:facebook) %>
24
+ ##### Authentication
105
25
 
106
- `connect_user` defaults to connecting the `current_user`, but you can explicitly connect any user:
26
+ Use the `:authenticate` helper to require a user to be signed in:
107
27
 
108
- <%= link_to 'connect with google', connect_user(:google, @user) %>
28
+ before_filter :authenticate
109
29
 
110
- ---
30
+ The current user object can be located with `current_user`:
111
31
 
112
- ##Helpers
32
+ # in posts_controller
113
33
 
114
- There are a few other helper methods available:
34
+ @posts = currrent_user.posts.all
115
35
 
116
- ### current_user
36
+ ##### Using only with Omniauth
117
37
 
118
- Returns the current logged in user or nil. Example usage:
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
119
39
 
120
- if !current_user.nil?
121
- redirect_to :controller => 'welcome', :action => 'thanks'
122
- end
40
+ provider :dailycred, 'CLIENT_ID', 'SECRET_KEY'
123
41
 
124
- ### dailycred
42
+ ##### Events
125
43
 
126
- A helper for instantiating a dailycred client instance. Use as a `before_filter` to load a @dailycred instance variable, or just use it as a helper method. Example usage:
44
+ To fire an event to be logged in Dailycred:
127
45
 
128
- As a before filter:
46
+ #in your controller
129
47
 
130
48
  before_filter :dailycred
131
49
 
132
- def index
133
- @dailycred.event(current_user.uid, "New Task", @task.name)
134
- end
50
+ def create
51
+ ...
135
52
 
136
- or just as a helper
53
+ # after successfully saving the model:
54
+ @dailycred.event(current_user.uid, 'Created Post', @post.name)
137
55
 
138
- def index
139
- dailycred.event(current_user.uid, "New Task", @task.name)
140
56
  end
141
57
 
142
- ### Tagging a User
143
-
144
- Dailycred provides the ability to 'tag' users, whether for reference in analytics or any other reason. Note that this is a very simple 'tagging' system, and not something you should use for dynamic tagging situations in your application.
145
-
146
- @user.tag 'awesome'
147
- @user.untag 'awesome'
148
-
149
- ### Firing Events
150
-
151
- You can also fire events tied to a specific user - this is helpful for goal tracking and tying actions to a specific user in analytics. We already fire many events for when a user signs up, resets a password, and much more, but you can also use the event system for something more specific for your application.
152
-
153
- # user#fire_event(key, value)
154
- @user.fire_event 'task added', @task.name
155
-
156
- ### Building Referral URLs
157
-
158
- To easily build referral URLs to track sharing amongst your users, use `referral_link(my_site)`, where *my_site* is the url that you wish referred users to be sent to.
159
-
160
- current_user.referral_link("http://www.mysite.com")
161
-
162
- ### Testing Controllers
163
-
164
- Testing controllers that have the `authenticate` before filter is easy:
165
-
166
- # with mocha
167
- @controller.stubs(:current_user).returns(@user)
168
-
169
- See `dummy/test/functional/post_controller_test.rb` for an example.
170
-
171
- ### Dailycred API
172
-
173
- For reference, have a look at the [annotated source code.](https://www.dailycred.com/public/docs/ruby/lib/dailycred.html)
174
-
175
- For all API calls, you must first initalize a Dailycred client:
176
-
177
- @dailycred = Dailycred.new "YOUR_CLIENT_ID", "your_secret_key", opts
178
-
179
- Where opts is an optional hash for passing options. After initializing your client, you can create events as well as tag and untag users:
180
-
181
- @dailycred.event(current_user.uid, "New Task", @task.name) # user_id, key, value
182
- @dailycred.tag(current_user.uid, "Failed Checkout") # user_id, key
183
- @dailycred.untag(current_user.uid, "Failed Checkout") # user_id, key
184
-
185
-
186
- ### Persona Login
187
-
188
- 1. Set your `persona audience` in your [dailycred identity provider settings](https://www.dailycred.com/admin/settings/identity-providers). This will be `http://{your-url}/auth/dailycred/callback`.
189
- 2. Make sure you have configured your `callback url` in your [dailycred app settings](https://www.dailycred.com/admin/settings)
190
- 3. In `config/omniauth.rb`, configure your middleware options to include your persona audience:
191
-
192
- Rails.configuration.DAILYCRED_OPTIONS = {
193
- middleware: {
194
- persona_audience = "http://{your-url}/auth/dailycred/callback"
195
- }
196
- }
197
-
198
- 4. In your javascript, call `personaLogin()`.
199
-
200
- $(document).ready(function(){
201
- $('.persona-login').click(function(){
202
- personaLogin();
203
- })
204
- })
205
-
206
- 5. Your user will be redirected to your [persona.org](https://persona.org) to login. If successful, your user will be redirected to your OAuth callback URL and be logged in.
207
-
208
- For more details, visit our [persona documentation](https://www.dailycred.com/docs/persona)
209
-
58
+ ## SSL Error
210
59
 
211
- ### Configuration
60
+ You may get an error such as the following:
212
61
 
213
- To specify where users should be redirected after authentication actions, setup configure an `after_auth` property on a `Rails.configuration.DAILYCRED_OPTIONS` variable. Example:
62
+ Faraday::Error::ConnectionFailed (SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed):
214
63
 
215
- # configure where users should be redirected after authentication
216
- #
217
- # Rails.configuration.DAILYCRED_OPTIONS = {
218
- # :after_auth => '/hello', #after login
219
- # :after_unauth => '/goodbye' #after logout
220
- # }
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).
221
65
 
222
66
  ## Contributing
223
67
 
@@ -227,6 +71,6 @@ To specify where users should be redirected after authentication actions, setup
227
71
  4. Push to the branch (`git push origin my-new-feature`)
228
72
  5. Create new Pull Request
229
73
 
230
- ![](https://www.dailycred.com/dc.gif?client_id=dailycred&title=rails_repo "dailycred")
74
+ omniauth-dailycred
231
75
 
232
- [![Build Status](https://secure.travis-ci.org/dailycred/dailycred.png?branch=master)](https://travis-ci.org/dailycred/dailycred)
76
+ OmniAuth adapter for dailycred using their OAuth2 Strategy
data/Rakefile CHANGED
@@ -1,43 +1,2 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
- require 'rspec/core/rake_task'
4
-
5
- desc "Run specs"
6
- RSpec::Core::RakeTask.new do |t|
7
- # t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
8
- # Put spec opts in a file named .rspec in root
9
- end
10
-
11
- desc "run travis"
12
- task :travis do
13
- ["rake spec","ruby test/test_helper.rb"].each do |cmd|
14
- puts "Starting to run #{cmd}..."
15
- system("export DISPLAY=:99.0 && bundle exec #{cmd}")
16
- raise "#{cmd} failed!" unless $?.exitstatus == 0
17
- end
18
- end
19
-
20
- task :default => :travis
21
-
22
- desc 'docs'
23
- task :docs do
24
- begin
25
- require 'rocco'
26
- require 'rocco/tasks'
27
- require 'fileutils'
28
- require 'maruku'
29
- Rocco::make 'docs/'
30
- FileUtils.cp_r "docs/lib/", "/Users/hank/java/dailycred/public/docs/ruby/", :verbose => true
31
- md = ""
32
- File.open("README.md", "r") do |infile|
33
- while (line = infile.gets)
34
- md += line
35
- end
36
- end
37
- doc = Maruku.new(md)
38
- File.open("/Users/hank/java/dailycred/app/views/tags/docs/rubyonrails.html", 'w') {|f| f.write doc.to_html}
39
- rescue LoadError
40
- warn "#$! -- rocco tasks not loaded."
41
- task :rocco
42
- end
43
- end
data/dailycred.gemspec CHANGED
@@ -1,20 +1,18 @@
1
1
  # -*- encoding: utf-8 -*-=
2
- require File.expand_path('../lib/dailycred/version', __FILE__)
3
2
 
4
3
  Gem::Specification.new do |gem|
5
4
  gem.authors = ["Hank Stoever"]
6
5
  gem.email = ["hstove@gmail.com"]
7
- gem.description = %q{A Ruby on Rails engine for authentication with Dailycred.}
8
- gem.summary = %q{}
6
+ gem.description = %q{descript}
7
+ gem.summary = %q{summary}
9
8
  gem.homepage = "https://www.dailycred.com"
10
- gem.add_dependency("omniauth_dailycred")
11
- gem.add_dependency("faraday")
12
-
9
+ gem.add_dependency("omniauth")
10
+ gem.add_dependency("omniauth-oauth2")
13
11
 
14
12
  gem.files = `git ls-files`.split("\n")
15
13
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
14
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
15
  gem.name = "dailycred"
18
16
  gem.require_paths = ["lib"]
19
- gem.version = Dailycred::VERSION
17
+ gem.version = "0.1.21"
20
18
  end
data/lib/dailycred.rb CHANGED
@@ -1 +1,86 @@
1
- Dir[File.expand_path('../dailycred/*', __FILE__)].each { |f| require f }
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, :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
+ p opts
60
+ response = get_conn.post url, opts
61
+ p response.body
62
+ end
63
+
64
+ def ssl_opts
65
+ opts = {}
66
+ p @options
67
+ p " ^^^^ @options "
68
+ if @options[:client_options] && @options[:client_options][:ssl]
69
+ opts[:ssl] = @options[:client_options][:ssl]
70
+ end
71
+ p opts
72
+ opts
73
+ end
74
+
75
+ def base_opts
76
+ {
77
+ :client_id => @client_id,
78
+ :client_secret => @secret_key
79
+ }
80
+ end
81
+
82
+ def get_conn
83
+ p ssl_opts
84
+ Faraday::Connection.new Dailycred::URL, ssl_opts
85
+ end
86
+ end
@@ -1,26 +1,22 @@
1
- require 'rails/generators'
2
- require 'pp'
3
- require 'json'
4
- require 'faraday'
5
1
  class DailycredGenerator < Rails::Generators::Base
6
2
  source_root File.expand_path('../templates', __FILE__)
7
3
 
8
- CLIENT_ID_DEFAULT = 'YOUR_CLIENT_ID'
9
- CLIENT_SECRET_DEFAULT = 'YOUR_SECRET_KEY'
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'
10
6
 
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'
7
+ APP_NAME = Rails.application.class.parent.name
13
8
 
14
9
  APP_ROUTES_LINES =<<-EOS
15
- mount Dailycred::Engine => '/auth', :as => 'dailycred_engine'
10
+ match '/auth/:provider/callback' => 'sessions#create'
11
+ match "/logout" => "sessions#destroy", :as => :logout
12
+ match "/auth" => "sessions#info", :as => :auth
16
13
  EOS
17
14
 
18
15
  APP_CONTROLLER_LINES =<<-EOS
19
- helper_method :current_user
16
+ helper_method :current_user, :login_path, :dailycred, :signup_path
20
17
 
21
18
  private
22
19
 
23
- # helper method for getting the current signed in user
24
20
  def current_user
25
21
  begin
26
22
  @current_user || User.find(session[:user_id]) if session[:user_id]
@@ -28,97 +24,53 @@ class DailycredGenerator < Rails::Generators::Base
28
24
  nil
29
25
  end
30
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 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, config.dc_client_opts)
43
+ end
31
44
  EOS
32
45
 
33
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
34
61
  # copy initializer
35
62
  template "omniauth.rb", "config/initializers/omniauth.rb"
36
- # get client info from login if they didnt specify info
37
- puts "Please manually configure your API keys in config/initializers/omniauth.rb"
63
+ # session_controller
64
+ copy_file "sessions_controller.rb", "app/controllers/sessions_controller.rb"
38
65
  # application controller
39
- insert_into_file "app/controllers/application_controller.rb", APP_CONTROLLER_LINES, :after => /class ApplicationController\n|class ApplicationController .*\n/
66
+ inject_into_class "app/controllers/application_controller.rb", ApplicationController, APP_CONTROLLER_LINES
40
67
  # add user_model
41
68
  copy_file "user.rb", "app/models/user.rb"
42
69
  # session_controller
43
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"
44
73
  # config/routes
45
- inject_into_file "config/routes.rb", APP_ROUTES_LINES, :after => ".draw do\n"
46
- end
47
-
48
- private
49
-
50
- def get_info first=true
51
- if first
52
- puts "Please insert your dailycred credentials. You must sign up for a free account at "+
53
- "http://www.dailycred.com. This is to automatically configure your api keys. If you wish to skip, enter 'n' as your email."
54
- else
55
- puts "Invalid email and password. Try again or type 'n' to skip."
56
- end
57
- #ssl opts
58
- # $stderr.puts 'getting input'
59
- input = get_input
60
- email, password = input[0], input[1]
61
- # $stderr.puts 'got input'
62
- return if email == "n"
63
- ssl_opts = {}
64
- if File.exists?('/etc/ssl/certs')
65
- ssl_opts = { :ca_path => '/etc/ssl/certs'}
66
- end
67
- if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
68
- ssl_opts = { :ca_file => '/opt/local/share/curl/curl-ca-bundle.crt' }
69
- end
70
- # url = "https://www.dailycred.com"
71
- # url = "http://localhost:9000"
72
- # staging server for a very short time
73
- url = "https://www.dailycred.com"
74
- connection = Faraday::Connection.new url, :ssl => ssl_opts
75
- params = {
76
- :login => email,
77
- :pass => password,
78
- :client_id => "dailycred"
79
- }
80
- response = connection.post("user/api/signin.json", params)
81
- json = JSON.parse(response.body)
82
- if !json["worked"]
83
- # wrong password
84
- p ''
85
- get_info false
86
- end
87
- access_token = json["access_token"]
88
- response = connection.post("graph/clientinfo.json", :access_token => access_token)
89
- json = JSON.parse(response.body)
90
- if !json["worked"]
91
- p "There was an error retrieving your account information. Please manually configure your API keys in config/initializers/omniauth.rb"
92
- return
93
- end
94
- @client_id = json["clientId"]
95
- @secret_key = json["clientSecret"]
96
- gsub_file("config/initializers/omniauth.rb", /YOUR_CLIENT_ID/, @client_id) if @client_id
97
- gsub_file("config/initializers/omniauth.rb", /YOUR_SECRET_KEY/, @secret_key) if @secret_key
98
- end
99
-
100
- def get_input
101
- puts ''
102
- print "Email:"
103
- email = gets.chomp
104
- return email, nil if email == "n"
105
- stty_settings = %x[stty -g]
106
- print 'Password: '
107
- begin
108
- %x[stty -echo]
109
- password = gets.chomp
110
- ensure
111
- %x[stty #{stty_settings}]
112
- end
113
- p ''
114
- return email, password
74
+ inject_into_file "config/routes.rb", APP_ROUTES_LINES, :after => "#{APP_NAME}::Application.routes.draw do\n"
115
75
  end
116
-
117
- private
118
-
119
- def show obj
120
- $stderr.puts obj
121
- end
122
-
123
-
124
76
  end
@@ -3,5 +3,5 @@
3
3
  <%= link_to 'Logout', logout_path %>
4
4
  </p>
5
5
  <% else %>
6
- <%= link_to 'Login In', login_path(:action => 'signin') %> or <%= link_to 'Sign Up', login_path() %>
6
+ <%= link_to 'Login In', login_path %> or <%= link_to 'Sign Up', signup_path %>
7
7
  <% end %>