devise_yauth_token 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.rspec +1 -0
  2. data/Gemfile +3 -0
  3. data/Gemfile.lock +117 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README +13 -0
  6. data/Rakefile +2 -0
  7. data/VERSION +1 -0
  8. data/app/controllers/devise/yauth_sessions_controller.rb +27 -0
  9. data/app/controllers/devise/yauth_settings_controller.rb +24 -0
  10. data/app/views/yauth_sessions/new.html.haml +9 -0
  11. data/devise_yauth_token.gemspec +22 -0
  12. data/lib/.DS_Store +0 -0
  13. data/lib/account_ext.rb +42 -0
  14. data/lib/billing_client.rb +20 -0
  15. data/lib/devise_yauth_token.rb +97 -0
  16. data/lib/devise_yauth_token/devise_yauth_application_controller.rb +24 -0
  17. data/lib/devise_yauth_token/devise_yauth_token_helper.rb +46 -0
  18. data/lib/devise_yauth_token/engine.rb +9 -0
  19. data/lib/devise_yauth_token/fake_api_helper.rb +26 -0
  20. data/lib/devise_yauth_token/insque.rb +53 -0
  21. data/lib/devise_yauth_token/model.rb +33 -0
  22. data/lib/devise_yauth_token/rails.rb +8 -0
  23. data/lib/devise_yauth_token/routes.rb +23 -0
  24. data/lib/devise_yauth_token/strategy.rb +79 -0
  25. data/lib/devise_yauth_token/version.rb +3 -0
  26. data/lib/generators/billing_client/billing_client_generator.rb +13 -0
  27. data/lib/generators/billing_client/templates/billing.rb +2 -0
  28. data/lib/generators/billing_client/templates/billing_config.yml +6 -0
  29. data/lib/generators/devise_yauth_token/.DS_Store +0 -0
  30. data/lib/generators/devise_yauth_token/devise_yauth_token_generator.rb +29 -0
  31. data/lib/generators/devise_yauth_token/templates/accout.rb +2 -0
  32. data/lib/generators/devise_yauth_token/templates/accout_host.rb +2 -0
  33. data/lib/generators/devise_yauth_token/templates/add_columns_to_users.rb +25 -0
  34. data/lib/generators/devise_yauth_token/templates/create_account_hosts.rb +13 -0
  35. data/lib/generators/devise_yauth_token/templates/create_accounts.rb +18 -0
  36. data/lib/generators/devise_yauth_token/templates/eagle.rb +11 -0
  37. data/lib/generators/devise_yauth_token/templates/eagle.yml +27 -0
  38. data/lib/user_ext.rb +26 -0
  39. data/spec/billing_spec.rb +36 -0
  40. data/spec/devise_yauth_token_spec.rb +4 -0
  41. data/spec/spec_helper.rb +9 -0
  42. metadata +232 -0
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+ gemspec
3
+
@@ -0,0 +1,117 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ devise_yauth_token (2.0.6)
5
+ devise (>= 1.1.5)
6
+ json (>= 1.5.1)
7
+ nifty-generators (>= 0.4.4)
8
+ omniauth (>= 0.1.6)
9
+ rails (>= 3.0.0)
10
+ rest-client (>= 1.6.1)
11
+
12
+ GEM
13
+ remote: http://rubygems.org/
14
+ specs:
15
+ abstract (1.0.0)
16
+ actionmailer (3.0.9)
17
+ actionpack (= 3.0.9)
18
+ mail (~> 2.2.19)
19
+ actionpack (3.0.9)
20
+ activemodel (= 3.0.9)
21
+ activesupport (= 3.0.9)
22
+ builder (~> 2.1.2)
23
+ erubis (~> 2.6.6)
24
+ i18n (~> 0.5.0)
25
+ rack (~> 1.2.1)
26
+ rack-mount (~> 0.6.14)
27
+ rack-test (~> 0.5.7)
28
+ tzinfo (~> 0.3.23)
29
+ activemodel (3.0.9)
30
+ activesupport (= 3.0.9)
31
+ builder (~> 2.1.2)
32
+ i18n (~> 0.5.0)
33
+ activerecord (3.0.9)
34
+ activemodel (= 3.0.9)
35
+ activesupport (= 3.0.9)
36
+ arel (~> 2.0.10)
37
+ tzinfo (~> 0.3.23)
38
+ activeresource (3.0.9)
39
+ activemodel (= 3.0.9)
40
+ activesupport (= 3.0.9)
41
+ activesupport (3.0.9)
42
+ arel (2.0.10)
43
+ bcrypt-ruby (3.0.1)
44
+ builder (2.1.2)
45
+ devise (1.4.9)
46
+ bcrypt-ruby (~> 3.0)
47
+ orm_adapter (~> 0.0.3)
48
+ warden (~> 1.0.3)
49
+ diff-lcs (1.1.3)
50
+ erubis (2.6.6)
51
+ abstract (>= 1.0.0)
52
+ fakeweb (1.3.0)
53
+ fakeweb-matcher (1.2.2)
54
+ fakeweb (>= 1.2.5)
55
+ rspec (>= 1.2.0)
56
+ hashie (1.2.0)
57
+ i18n (0.5.0)
58
+ json (1.6.1)
59
+ mail (2.2.19)
60
+ activesupport (>= 2.3.6)
61
+ i18n (>= 0.4.0)
62
+ mime-types (~> 1.16)
63
+ treetop (~> 1.4.8)
64
+ mime-types (1.17.2)
65
+ nifty-generators (0.4.6)
66
+ omniauth (1.0.0)
67
+ hashie (~> 1.2)
68
+ rack
69
+ orm_adapter (0.0.5)
70
+ polyglot (0.3.3)
71
+ rack (1.2.4)
72
+ rack-mount (0.6.14)
73
+ rack (>= 1.0.0)
74
+ rack-test (0.5.7)
75
+ rack (>= 1.0)
76
+ rails (3.0.9)
77
+ actionmailer (= 3.0.9)
78
+ actionpack (= 3.0.9)
79
+ activerecord (= 3.0.9)
80
+ activeresource (= 3.0.9)
81
+ activesupport (= 3.0.9)
82
+ bundler (~> 1.0)
83
+ railties (= 3.0.9)
84
+ railties (3.0.9)
85
+ actionpack (= 3.0.9)
86
+ activesupport (= 3.0.9)
87
+ rake (>= 0.8.7)
88
+ rdoc (~> 3.4)
89
+ thor (~> 0.14.4)
90
+ rake (0.9.2.2)
91
+ rdoc (3.9.4)
92
+ rest-client (1.6.7)
93
+ mime-types (>= 1.16)
94
+ rspec (2.9.0)
95
+ rspec-core (~> 2.9.0)
96
+ rspec-expectations (~> 2.9.0)
97
+ rspec-mocks (~> 2.9.0)
98
+ rspec-core (2.9.0)
99
+ rspec-expectations (2.9.1)
100
+ diff-lcs (~> 1.1.3)
101
+ rspec-mocks (2.9.0)
102
+ thor (0.14.6)
103
+ treetop (1.4.10)
104
+ polyglot
105
+ polyglot (>= 0.3.1)
106
+ tzinfo (0.3.31)
107
+ warden (1.0.6)
108
+ rack (>= 1.0)
109
+
110
+ PLATFORMS
111
+ ruby
112
+
113
+ DEPENDENCIES
114
+ devise_yauth_token!
115
+ fakeweb (>= 1.3.0)
116
+ fakeweb-matcher (>= 1.2.2)
117
+ rspec (>= 2.9.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ YauthTokenAuthenticatable
2
+ =========================
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2010 [name of plugin creator], released under the MIT license
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.0.6
@@ -0,0 +1,27 @@
1
+ module Devise
2
+ class YauthSessionsController < SessionsController
3
+ prepend_before_filter :require_no_authentication, :only => [:new, :create]
4
+ #before_filter :authenticate_user!, :only => [:create]
5
+
6
+ def new
7
+ @erorr = false
8
+ respond_to do |format|
9
+ format.html { render "yauth_sessions/new", :layout => false }
10
+ format.json { render :json => to_json_api(nil, {:status => :sign_in_needed}, 401) }
11
+ format.js { render :js => "window.location = 'http://#{request.host}/users/sign_in/'" }
12
+ end
13
+ end
14
+
15
+ def error
16
+ @error = true
17
+ render "yauth_sessions/new", :layout => false
18
+ end
19
+
20
+ def create
21
+ resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#error")
22
+ set_flash_message(:notice, :signed_in) if is_navigational_format?
23
+ sign_in(resource_name, resource)
24
+ respond_with resource, :location => redirect_location(resource_name, resource)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module Devise
2
+ class YauthSettingsController < ApplicationController
3
+ skip_before_filter :authenticate_user!
4
+
5
+ def widget
6
+ redirect_to "http://#{current_account.yauth_host}/api/auth/widget.json?continue=#{params[:continue]}&callback=#{params[:callback]}"
7
+ end
8
+
9
+ def status
10
+ if params[:account_id]
11
+ a = Account.where(:yauth_account_id => params[:account_id]).first
12
+ if a.respond_to?(:status_parameters)
13
+ render :json => to_json_api(a.status_parameters)
14
+ else
15
+ render :json => to_json_api(:ok)
16
+ end
17
+ else
18
+ a = Account.first
19
+ u = User.first
20
+ render :text => :ok
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ !!!
2
+ %html
3
+ %head
4
+ %title
5
+ = javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
6
+ = yauth_auto_login("http://#{self.request.host}/users/sign_in") unless @error
7
+ %body
8
+ - if @error
9
+ Unknow Error
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{devise_yauth_token}
3
+ s.version = "2.0.6"
4
+ s.authors = ["andreev"]
5
+ s.email = ["d.g.andreev@gmail.com"]
6
+ s.summary = %q{Eagleplatform authorization center client module}
7
+ s.description = %q{Eagleplatform authorization center client module}
8
+ s.rdoc_options = ["--charset=UTF-8"]
9
+
10
+ s.files = `git ls-files`.split("\n")
11
+ s.require_paths = ["lib"]
12
+
13
+ s.add_dependency('devise', '>= 1.1.5' )
14
+ s.add_dependency('rails', '>= 3.0.0' )
15
+ s.add_dependency('nifty-generators', '>= 0.4.4' )
16
+ s.add_dependency('json', '>= 1.5.1')
17
+ s.add_dependency('omniauth', '>= 0.1.6')
18
+ s.add_dependency('rest-client', '>= 1.6.1')
19
+ s.add_development_dependency("rspec", ">= 2.9.0")
20
+ s.add_development_dependency("fakeweb", ">= 1.3.0")
21
+ s.add_development_dependency("fakeweb-matcher", ">= 1.2.2")
22
+ end
Binary file
@@ -0,0 +1,42 @@
1
+ module AccountExt
2
+ module ClassMethods
3
+ def get_by_code(code)
4
+ return nil if code.blank?
5
+ account = self.find_by_code(code)
6
+ if account.blank?
7
+ url = ::YAUTH_CONFIG["auth_url"]+"/api/auth/account_by_code/?code=#{code}"
8
+ result = RestClient.get(url) {|response, request, result| result }
9
+ result = JSON.parse(result.body) rescue {}
10
+ if result['status'] == 200
11
+ account = self.new
12
+ account.yauth_host = result['data']['account']['host']
13
+ account.admin = result['data']['account']['admin']
14
+ account.code = result['data']['account']['code']
15
+ account.name = result['data']['account']['name']
16
+ account.host = "#{code}.#{::YAUTH_CONFIG['application_host']}"
17
+ account.save(:validate => false)
18
+ end
19
+ end
20
+ account
21
+ end
22
+ end
23
+
24
+ def self.included(base)
25
+ base.extend(ClassMethods)
26
+ end
27
+
28
+ def status
29
+ yauth_account = Rails.cache.read("yauth_account_#{self.yauth_account_id}")
30
+ if yauth_account['is_paid']
31
+ { :status => :paid }
32
+ elsif yauth_account['locked_at']
33
+ { :status => :locked }
34
+ else
35
+ { :status => :trial, :days_left => 15 - (DateTime.now - DateTime.parse(yauth_account['created_at'])).to_i}
36
+ end
37
+ end
38
+
39
+ def yauth_account
40
+ Rails.cache.read("yauth_account_#{yauth_account_id}")
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ module BillingClient
2
+ ApiError = Class.new(StandardError)
3
+ ConfigError = Class.new(StandardError)
4
+
5
+ def self.check_code(account, product_code, code, reference)
6
+
7
+ raise ConfigError, "Config not found, run \"rails g billing_client\" first!" if ::BILLING_CONFIG.try(:[], 'host').blank?
8
+ uri = URI.join("http://#{account}.#{::BILLING_CONFIG['host']}", "/api/products/#{product_code}/can_use",).to_s
9
+
10
+ response = RestClient.get uri, :params => {:customer_code => code, :reference_object => reference}
11
+
12
+ response = JSON.parse(response, :symbolize_names => true)
13
+
14
+ if response[:status].to_s == "200"
15
+ true
16
+ else
17
+ raise ApiError, response[:errors].try(:join, ', ')
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,97 @@
1
+ require 'devise'
2
+ $: << File.expand_path("..", __FILE__)
3
+ require 'devise_yauth_token/strategy'
4
+ require 'devise_yauth_token/routes'
5
+ require 'devise_yauth_token/rails'
6
+ require 'devise_yauth_token/insque'
7
+ require 'devise_yauth_token/devise_yauth_token_helper'
8
+ require 'devise_yauth_token/devise_yauth_application_controller'
9
+
10
+ require 'billing_client'
11
+
12
+ module Devise
13
+ mattr_accessor :system_permissions
14
+ mattr_accessor :system_menu
15
+
16
+ def self.current_account(request_host)
17
+ return Account.where(:code => 'current').first if Rails.env == 'test'
18
+
19
+ account_host = AccountHost.find_by_host(request_host)
20
+ current_account ||= account_host.account if !account_host.blank?
21
+ return current_account if !current_account.blank?
22
+ url = ::YAUTH_CONFIG["auth_url"]+"/api/auth/account_by_host/?auth_host=#{request_host}"
23
+ result = RestClient.get(url) {|response, request, result| result }
24
+ result = JSON.parse(result.body) rescue {}
25
+ if result['status'] == 200
26
+ code = result['data']['account']['code']
27
+ current_account = Account.find_by_code(code) if !code.blank?
28
+ if current_account
29
+ account_host = current_account.account_hosts.build({:host => request_host})
30
+ account_host.save(:validate => false)
31
+ else
32
+ account = Account.new
33
+ account.yauth_host = result['data']['account']['host']
34
+ account.code = result['data']['account']['code']
35
+ account.name = result['data']['account']['name']
36
+ account.yauth_account_id = result['data']['account']['id']
37
+ account.account_hosts.build({:host => request_host})
38
+ account.save(:validate => false)
39
+ current_account = account
40
+ end
41
+ end
42
+ current_account
43
+ end
44
+ end
45
+
46
+
47
+ Devise.add_module(:yauth_token,
48
+ :strategy => true,
49
+ :model => 'devise_yauth_token/model',
50
+ :controller => :sessions,
51
+ :route => :yauth_token)
52
+
53
+
54
+ module ApplicationHelper
55
+ include DeviseYauthTokenHelper
56
+ end
57
+
58
+ class ActionController::Base
59
+ include DeviseYauthApplicationController
60
+ prepend_before_filter :guest_login
61
+ before_filter :set_account_time_zone
62
+ around_filter :sign_out_user_if_need
63
+
64
+ protected
65
+ def guest_login_hash guestlogin
66
+ require 'digest/md5'
67
+ Digest::MD5.hexdigest guestlogin.to_json
68
+ end
69
+
70
+ def guest_login
71
+ if params[:guestlogin].present? && cookies[:guest_login_hash] != guest_login_hash(params[:guestlogin])
72
+ cookies[:guest_login_hash] = guest_login_hash(params[:guestlogin])
73
+ continue = URI.parse request.url
74
+ continue.query = Rack::Utils.parse_nested_query(continue.query).delete_if {|k,v| k == 'guestlogin'}.to_param
75
+ redirect_to "http://#{current_account.yauth_host}/api/auth/guest_login?#{{:guestlogin => params[:guestlogin]}.to_param}&continue=#{continue.to_s}"
76
+ end
77
+ end
78
+
79
+ def after_sign_out_path_for(resource)
80
+ cookies[:guest_login_hash] = nil
81
+ "http://#{current_account.yauth_host}/users/sign_out"
82
+ end
83
+
84
+ def set_account_time_zone
85
+ Time.zone = current_account.yauth_account['timezone'] if current_account && current_account.yauth_account
86
+ end
87
+
88
+ def sign_out_user_if_need
89
+ if Rails.env != 'test' && !current_user.blank? &&
90
+ (current_user.need_sign_out || current_user.yauth_user.nil? || current_account.yauth_account.nil? || current_user.permissions.nil?)
91
+ current_user.clear_need_sign_out
92
+ sign_out(:user)
93
+ redirect_to session_path(:user)
94
+ end
95
+ yield
96
+ end
97
+ end
@@ -0,0 +1,24 @@
1
+ module DeviseYauthApplicationController
2
+ protected
3
+
4
+ def billing_client(product_code, billing_code, reference)
5
+ BillingClient::check_code(current_account.code, product_code, billing_code, reference)
6
+ end
7
+
8
+ def current_account
9
+ if params[:account]
10
+ @current_account ||= Account.find_by_code params[:account] rescue nil
11
+ end
12
+ @current_account ||= Devise.current_account(request.host)
13
+ end
14
+
15
+ def to_json_api(data=nil, errors=nil, status=200)
16
+ if data.is_a?(String)
17
+ data = JSON.parse(data) rescue data
18
+ end
19
+ if errors.is_a?(String)
20
+ errors = JSON.parse(errors) rescue errors
21
+ end
22
+ {:time => DateTime.now.to_s, :version => "3.0", :data => data, :errors => errors, :status => status}.to_json
23
+ end
24
+ end
@@ -0,0 +1,46 @@
1
+ module DeviseYauthTokenHelper
2
+ def current_account
3
+ @current_account ||= Devise.current_account(self.request.host)
4
+ end
5
+
6
+ def yauth_auto_login(auth_url = "")
7
+ unless user_signed_in?
8
+ %Q{<script type='text/javascript'>
9
+ function post_to_url(path, params, method) {
10
+ method = method || 'post'; // Set method to post by default, if not specified.
11
+ // The rest of this code assumes you are not using a library.
12
+ // It can be made less wordy if you use one.
13
+ var form = document.createElement('form');
14
+ form.setAttribute('method', method);
15
+ form.setAttribute('action', path);
16
+ for(var key in params) {
17
+ var hiddenField = document.createElement('input');
18
+ hiddenField.setAttribute('type', 'hidden');
19
+ hiddenField.setAttribute('name', key);
20
+ hiddenField.setAttribute('value', params[key]);
21
+ form.appendChild(hiddenField);
22
+ }
23
+ document.body.appendChild(form); // Not entirely sure if this is necessary
24
+ form.submit();
25
+ }
26
+ $(document).ready(function(){
27
+ var data = {};
28
+ $.getJSON('http://#{current_account.yauth_host}/api/auth/yauth_token.json?auth_host=#{self.request.host}&callback=?', data, function(jsonp){
29
+ if(jsonp.status == 200){
30
+ post_to_url('#{auth_url}', {'yauth_token': jsonp.data.token});
31
+ }
32
+ else if (jsonp.status == 401){
33
+ document.location.href = 'http://#{current_account.yauth_host}/users/sign_in?continue=#{self.request.url}';
34
+ }
35
+ else if (jsonp.status == 400){
36
+ document.location.href = 'http://#{current_account.yauth_host}/users/sign_out';
37
+ }
38
+ else if (jsonp.data.status == "locked") {
39
+ $.ajax({url:"/locked.html?a",success:function(d){$("body").html(d)}})
40
+ }
41
+ });
42
+ });
43
+ </script>}.html_safe
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,9 @@
1
+ #lib/authr/engine.rb
2
+ require "devise"
3
+ require "rails"
4
+
5
+ module Devise
6
+ class Engine < Rails::Engine
7
+ engine_name :devise_yauth_token
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ require "fakeweb"
2
+ module FakeApiHelper
3
+ def fake_api(*args, &block)
4
+ method = args[3].nil? ? :get : args[3]
5
+ FakeWeb.allow_net_connect = false
6
+ FakeWeb.register_uri method, %r|.*|,
7
+ :body => to_json_api(*args[0..2]).as_json
8
+ if block_given?
9
+ yield
10
+ FakeWeb.clean_registry
11
+ end
12
+ end
13
+
14
+ def to_json_api(data=nil, errors=nil, status=200)
15
+ if data.is_a?(String)
16
+ data = JSON.parse(data)
17
+ end
18
+
19
+ if errors.is_a?(String)
20
+ errors = JSON.parse(errors)
21
+ end
22
+ {:time => DateTime.now.to_s, :version => "3.0", :data => data, :errors => errors, :status => status}.to_json
23
+ end
24
+
25
+
26
+ end
@@ -0,0 +1,53 @@
1
+ module Insque
2
+ def self.yauth_user_sign_out msg
3
+ users = User.where(:yauth_user_id => msg['params']['id']) if msg['params']['id']
4
+ users = User.where(:yauth_account_id => msg['params']['account_id']) if msg['params']['account_id']
5
+ users.update_all :need_sign_out => true
6
+ end
7
+
8
+ def self.yauth_user_create msg
9
+ unless User.where(:yauth_user_id => msg['params']['id']).exists?
10
+ user = User.new
11
+ user.email = msg['params']['email']
12
+ user.omniauth_name = msg['params']['omniauth_name']
13
+ user.yauth_user_id = msg['params']['id']
14
+ user.yauth_account_id = msg['params']['account_id']
15
+ user.admin = msg['params']['admin']
16
+ user.account_id = Account.where(:yauth_account_id => user.yauth_account_id).first.id
17
+ user.save! :validate => false
18
+ end
19
+ end
20
+
21
+ def self.yauth_user_update msg
22
+ user = User.where(:yauth_user_id => msg['params']['id']).first
23
+ user.email = msg['params']['email']
24
+ user.omniauth_name = msg['params']['omniauth_name']
25
+ user.admin = msg['params']['admin']
26
+ user.save! :validate => false
27
+ end
28
+
29
+ def self.yauth_user_destroy msg
30
+ user = User.where(:yauth_user_id => msg['params']['id']).first
31
+ user.destroy
32
+ end
33
+
34
+ def self.yauth_account_create msg
35
+ unless Account.where(:yauth_account_id => msg['params']['id']).exists?
36
+ account = Account.new
37
+ account.yauth_host = msg['params']['host']
38
+ account.code = msg['params']['code']
39
+ account.name = msg['params']['name']
40
+ account.yauth_account_id = msg['params']['id']
41
+ account.save! :validate => false
42
+ end
43
+ end
44
+
45
+ def self.yauth_account_update msg
46
+ account = Account.where(:yauth_account_id => msg['params']['id']).first
47
+ account.yauth_host = msg['params']['host']
48
+ account.code = msg['params']['code']
49
+ account.name = msg['params']['name']
50
+ account.save! :validate => false
51
+ end
52
+ end
53
+
@@ -0,0 +1,33 @@
1
+ module Devise
2
+ module Models
3
+ # The TokenAuthenticatable module is responsible for generating an authentication token and
4
+ # validating the authenticity of the same while signing in.
5
+ #
6
+ # This module only provides a few helpers to help you manage the token. Creating and resetting
7
+ # the token is your responsibility.
8
+ #
9
+ # == Configuration:
10
+ #
11
+ # You can overwrite configuration values by setting in globally in Devise (+Devise.setup+),
12
+ # using devise method, or overwriting the respective instance method.
13
+ #
14
+ # +token_authentication_key+ - Defines name of the authentication token params key. E.g. /users/sign_in?some_key=...
15
+ #
16
+ module YauthToken
17
+ extend ActiveSupport::Concern
18
+
19
+ module ClassMethods
20
+
21
+ def find_for_yauth_authentication(value, account_id)
22
+ find_for_authentication({ :yauth_user_id => value, :account_id => account_id })
23
+ end
24
+
25
+ def password_required?
26
+ false
27
+ end
28
+
29
+ #::Devise::Models.config(self, :url)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ module Devise
2
+ module YauthToken
3
+ class Engine < ::Rails::Engine
4
+
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,23 @@
1
+ ActionDispatch::Routing::Mapper.class_eval do
2
+ protected
3
+
4
+ alias_method :devise_yauth_token, :devise_session
5
+ end
6
+
7
+
8
+ module DeviseYauthToken
9
+ module Routing #:nodoc:
10
+ module MapperExtensions
11
+ def yauth_settings
12
+ match '/api/yauth_settings/system_permissions', :to => 'devise/yauth_settings#system_permissions'
13
+ match '/api/yauth_settings/system_menu', :to => 'devise/yauth_settings#system_menu'
14
+ match '/api/yauth_settings/widget', :to => 'devise/yauth_settings#widget'
15
+ match '/api/yauth_users/sign_out', :to => 'devise/yauth_users#sign_out'
16
+ match '/status', :to => 'devise/yauth_settings#status'
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ ActionDispatch::Routing::Mapper.send :include, DeviseYauthToken::Routing::MapperExtensions
23
+
@@ -0,0 +1,79 @@
1
+ require 'devise/strategies/base'
2
+ require 'rest_client'
3
+
4
+
5
+ class Devise::Strategies::YauthToken < Devise::Strategies::Authenticatable
6
+
7
+ def current_account
8
+ @current_account ||= Devise.current_account(request.host)
9
+ end
10
+
11
+ def valid?
12
+ true
13
+ end
14
+
15
+ def authenticate!
16
+ @yauth_provider_error = nil
17
+ if token = params[:yauth_token]
18
+ result = RestClient.get("http://#{current_account.yauth_host}/api/auth/user.json?auth_host=#{request.host}&notrack=true&auth_token=#{token}"){|response, request, result| result }
19
+ result = JSON.parse(result.body) rescue {}
20
+ yauth_user_id = result['data']['user']['id'] if result['status'] == 200
21
+ if !yauth_user_id.blank? && !current_account.blank?
22
+ resource = mapping.to.find_for_yauth_authentication(yauth_user_id, current_account.id)
23
+ if resource.blank?
24
+ result['data']['user']['yauth_user_id'] = yauth_user_id
25
+ resource = mapping.to.new()
26
+ end
27
+ resource.email = result['data']['user']['email']
28
+ resource.omniauth_name = result['data']['user']['omniauth_name']
29
+ resource.yauth_user_id = result['data']['user']['id']
30
+ resource.yauth_account_id = result['data']['user']['account_id']
31
+ resource.admin = result['data']['user']['admin']
32
+ resource.need_sign_out = false
33
+ resource.account_id = current_account.id
34
+ resource.save(:validate => false)
35
+
36
+ current_account.code = result['data']['account']['code']
37
+ current_account.yauth_account_id = result['data']['account']['id']
38
+ current_account.name = result['data']['account']['name']
39
+ current_account.save(:validate => false)
40
+
41
+ Rails.logger.info "------------------#{result['data']}---------------------"
42
+ Rails.cache.write("yauth_user_#{result['data']['user']['id']}", result['data']['user'])
43
+ Rails.cache.write("yauth_account_#{result['data']['account']['id']}", result['data']['account'])
44
+ Rails.cache.write("user_permissions_#{resource.id}", result['data']['permissions'])
45
+ success!(resource)
46
+ else
47
+ @yauth_provider_error = I18n.t("devise.yauth_token.erorrs.token_invalid")
48
+ fail(:invalid_token)
49
+ end
50
+ else
51
+ @yauth_provider_error = I18n.t("devise.yauth_token.erorrs.token_blank")
52
+ fail(:invalid_token_blank)
53
+ end
54
+ end
55
+
56
+
57
+ def valid_request?
58
+ true
59
+ end
60
+
61
+ # Do not use remember_me behavior with token.
62
+ def remember_me?
63
+ true
64
+ end
65
+
66
+ # Try both scoped and non scoped keys.
67
+ def params_auth_hash
68
+ params[scope] || params
69
+ end
70
+
71
+ # Overwrite authentication keys to use token_authentication_key.
72
+ def authentication_keys
73
+ @authentication_keys ||= :yauth_token
74
+ end
75
+
76
+ end
77
+
78
+
79
+ Warden::Strategies.add(:yauth_token, Devise::Strategies::YauthToken)
@@ -0,0 +1,3 @@
1
+ module DeviseYauthToken
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+
3
+ class BillingClientGenerator < Rails::Generators::Base
4
+
5
+ def self.source_root
6
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
7
+ end
8
+
9
+ def create_initializer_file
10
+ template 'billing.rb', 'config/initializers/billing.rb'
11
+ template 'billing_config.yml', 'config/billing_config.yml'
12
+ end
13
+ end
@@ -0,0 +1,2 @@
1
+ config_file = File.join(Rails.root, 'config', 'billing_config.yml')
2
+ BILLING_CONFIG = YAML.load(IO.read(config_file))[Rails.env]
@@ -0,0 +1,6 @@
1
+ production:
2
+ host: "billing.test.eagleplatform.com"
3
+ development:
4
+ host: "billing.test.eagleplatform.com"
5
+ test:
6
+ host: "billing.test.eagleplatform.com"
@@ -0,0 +1,29 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+
5
+ class DeviseYauthTokenGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ def self.source_root
9
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
10
+ end
11
+
12
+
13
+ def self.next_migration_number(dirname)
14
+ #if ActiveRecord::Base.timestamped_migrations
15
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
16
+ "%.3d" % (current_migration_number(dirname) + 1)
17
+ #end
18
+ end
19
+
20
+ def create_migration_file
21
+ migration_template 'create_accounts.rb', 'db/migrate/create_accounts.rb'
22
+ migration_template 'create_account_hosts.rb', 'db/migrate/create_account_hosts.rb'
23
+ migration_template 'add_columns_to_users.rb', 'db/migrate/add_columns_to_users.rb'
24
+ template 'account.rb', 'app/models/account.rb'
25
+ template 'account_host.rb', 'app/models/account_host.rb'
26
+ template 'eagle.rb', 'config/initializers/eagle.rb'
27
+ template 'eagle.yml', 'config/eagle.yml'
28
+ end
29
+ end
@@ -0,0 +1,2 @@
1
+ class Account < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class AccountHost < ActiveRecord::Base
2
+ end
@@ -0,0 +1,25 @@
1
+ class AddColumnsToUsers < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :account_id, :integer
4
+ add_column :users, :omniauth_name, :string
5
+ add_column :users, :nick, :string
6
+
7
+ add_column :users, :yauth_account_id, :integer
8
+ add_column :users, :yauth_user_id, :integer
9
+ add_column :users, :admin, :boolean, :default => false
10
+
11
+ add_column :users, :need_sign_in, :boolean, :default => false
12
+ end
13
+
14
+ def self.down
15
+ remove_column :users, :account_id
16
+ remove_column :users, :omniauth_name
17
+ remove_column :users, :nick
18
+
19
+ remove_column :users, :yauth_account_id
20
+ remove_column :users, :yauth_user_id
21
+ remove_column :users, :admin
22
+
23
+ remove_column :users, :need_sign_in
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ class CreateAccountHosts < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :account_hosts do |t|
4
+ t.string :host
5
+ t.integer :account_id
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :account_hosts
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ class CreateAccounts < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :accounts do |t|
4
+ t.string :name
5
+ t.string :code
6
+ t.text :description
7
+ t.boolean :admin, :dafault => false
8
+ t.integer :yauth_account_id
9
+ t.string :host
10
+ t.string :yauth_host
11
+ t.timestamps
12
+ end
13
+ end
14
+
15
+ def self.down
16
+ drop_table :accounts
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ config_file = File.join(Rails.root, 'config', 'eagle.yml')
2
+ ::EAGLE_CONFIG = YAML.load(IO.read(config_file))[Rails.env]
3
+
4
+ Rails.application.config.to_prepare do
5
+ Account.class_eval do
6
+ include AccountExt
7
+ end
8
+ User.class_eval do
9
+ include UserExt
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ production:
2
+ media_host: 'media.eagleplatform.com'
3
+ streaming_host: 'streaming.eagleplatform.com'
4
+ billing_host: 'billing.eagleplatform.com'
5
+ yauth_host: 'auth.eagleplatform.com'
6
+ ystat_host: 'stat.eagleplatform.com'
7
+ balancer_host: 'panel.eaglecdn.com'
8
+ messenger_host: 'messenger.eagleplatform.com'
9
+ monitoring_host: 'monitoring.eagleplatform.com'
10
+ development:
11
+ media_host: 'media.test.eagleplatform.com'
12
+ streaming_host: 'streaming.test.eagleplatform.com'
13
+ billing_host: 'billing.test.eagleplatform.com'
14
+ yauth_host: 'auth.test.eagleplatform.com'
15
+ ystat_host: 'stat.test.eagleplatform.com'
16
+ balancer_host: 'panel.balancer.test.eagleplatform.com'
17
+ messenger_host: 'messenger.test.eagleplatform.com'
18
+ monitoring_host: 'monitoring.test.eagleplatform.com'
19
+ test:
20
+ media_host: 'media.test.eagleplatform.com'
21
+ streaming_host: 'streaming.test.eagleplatform.com'
22
+ billing_host: 'billing.test.eagleplatform.com'
23
+ yauth_host: 'auth.test.eagleplatform.com'
24
+ ystat_host: 'stat.test.eagleplatform.com'
25
+ balancer_host: 'panel.balancer.test.eagleplatform.com'
26
+ messenger_host: 'messenger.test.eagleplatform.com'
27
+ monitoring_host: 'monitoring.test.eagleplatform.com'
@@ -0,0 +1,26 @@
1
+ module UserExt
2
+ def clear_need_sign_out
3
+ self.need_sign_out = false
4
+ self.save(:validate => false)
5
+ end
6
+
7
+ def display_name
8
+ if omniauth_name.blank?
9
+ if email.blank?
10
+ 'anonymous'
11
+ else
12
+ email
13
+ end
14
+ else
15
+ omniauth_name
16
+ end
17
+ end
18
+
19
+ def permissions
20
+ Rails.cache.read("user_permissions_#{id}")
21
+ end
22
+
23
+ def yauth_user
24
+ Rails.cache.read("yauth_user_#{yauth_user_id}")
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'devise_yauth_token/fake_api_helper'
3
+ require 'fakeweb_matcher'
4
+
5
+ describe BillingClient do
6
+ include FakeApiHelper
7
+
8
+ let (:account_code) {"eagletest666"}
9
+ let (:product_code) {"someproductcode"}
10
+
11
+ ::BILLING_CONFIG = {'host' => 'somehost'}
12
+
13
+ it 'should send product code and auth code' do
14
+ fake_api true, nil, 200
15
+ BillingClient::check_code(account_code, product_code, "somecode", 666).should == true
16
+ FakeWeb.should have_requested(:get,
17
+ "http://#{account_code}.somehost/api/products/#{product_code}/can_use?customer_code=somecode&reference_object=666")
18
+ end
19
+
20
+ it 'should raise with invalid code' do
21
+ fake_api nil, ["Code doesn't found", "Some other Error"], 500
22
+ lambda {
23
+ BillingClient::check_code(account_code, product_code, "somecode", 666)
24
+ }.should raise_error(BillingClient::ApiError, "Code doesn't found, Some other Error")
25
+ end
26
+
27
+ it 'should raise without BILLING_CONFIG' do
28
+ ::BILLING_CONFIG.stub(:[]).and_return(nil)
29
+ fake_api true, nil, 200
30
+ lambda {
31
+ BillingClient::check_code(account_code, product_code, "somecode", 666)
32
+ }.should raise_error(BillingClient::ConfigError,
33
+ "Config not found, run \"rails g billing_client\" first!")
34
+ end
35
+
36
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+ describe DeviseYauthToken do
3
+ pending "write it"
4
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'rails/all'
5
+ require 'devise_yauth_token' # and any other gems you need
6
+
7
+ RSpec.configure do |config|
8
+ # some (optional) config here
9
+ end
metadata ADDED
@@ -0,0 +1,232 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_yauth_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - andreev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: devise
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.5
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: 1.1.5
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.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: 3.0.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: nifty-generators
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.4.4
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.4.4
62
+ - !ruby/object:Gem::Dependency
63
+ name: json
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.5.1
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.5.1
78
+ - !ruby/object:Gem::Dependency
79
+ name: omniauth
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 0.1.6
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 0.1.6
94
+ - !ruby/object:Gem::Dependency
95
+ name: rest-client
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.6.1
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 1.6.1
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 2.9.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 2.9.0
126
+ - !ruby/object:Gem::Dependency
127
+ name: fakeweb
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: 1.3.0
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: 1.3.0
142
+ - !ruby/object:Gem::Dependency
143
+ name: fakeweb-matcher
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: 1.2.2
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: 1.2.2
158
+ description: Eagleplatform authorization center client module
159
+ email:
160
+ - d.g.andreev@gmail.com
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files: []
164
+ files:
165
+ - .rspec
166
+ - Gemfile
167
+ - Gemfile.lock
168
+ - MIT-LICENSE
169
+ - README
170
+ - Rakefile
171
+ - VERSION
172
+ - app/controllers/devise/yauth_sessions_controller.rb
173
+ - app/controllers/devise/yauth_settings_controller.rb
174
+ - app/views/yauth_sessions/new.html.haml
175
+ - devise_yauth_token.gemspec
176
+ - lib/.DS_Store
177
+ - lib/account_ext.rb
178
+ - lib/billing_client.rb
179
+ - lib/devise_yauth_token.rb
180
+ - lib/devise_yauth_token/devise_yauth_application_controller.rb
181
+ - lib/devise_yauth_token/devise_yauth_token_helper.rb
182
+ - lib/devise_yauth_token/engine.rb
183
+ - lib/devise_yauth_token/fake_api_helper.rb
184
+ - lib/devise_yauth_token/insque.rb
185
+ - lib/devise_yauth_token/model.rb
186
+ - lib/devise_yauth_token/rails.rb
187
+ - lib/devise_yauth_token/routes.rb
188
+ - lib/devise_yauth_token/strategy.rb
189
+ - lib/devise_yauth_token/version.rb
190
+ - lib/generators/billing_client/billing_client_generator.rb
191
+ - lib/generators/billing_client/templates/billing.rb
192
+ - lib/generators/billing_client/templates/billing_config.yml
193
+ - lib/generators/devise_yauth_token/.DS_Store
194
+ - lib/generators/devise_yauth_token/devise_yauth_token_generator.rb
195
+ - lib/generators/devise_yauth_token/templates/accout.rb
196
+ - lib/generators/devise_yauth_token/templates/accout_host.rb
197
+ - lib/generators/devise_yauth_token/templates/add_columns_to_users.rb
198
+ - lib/generators/devise_yauth_token/templates/create_account_hosts.rb
199
+ - lib/generators/devise_yauth_token/templates/create_accounts.rb
200
+ - lib/generators/devise_yauth_token/templates/eagle.rb
201
+ - lib/generators/devise_yauth_token/templates/eagle.yml
202
+ - lib/user_ext.rb
203
+ - spec/billing_spec.rb
204
+ - spec/devise_yauth_token_spec.rb
205
+ - spec/spec_helper.rb
206
+ homepage:
207
+ licenses: []
208
+ post_install_message:
209
+ rdoc_options:
210
+ - --charset=UTF-8
211
+ require_paths:
212
+ - lib
213
+ required_ruby_version: !ruby/object:Gem::Requirement
214
+ none: false
215
+ requirements:
216
+ - - ! '>='
217
+ - !ruby/object:Gem::Version
218
+ version: '0'
219
+ required_rubygems_version: !ruby/object:Gem::Requirement
220
+ none: false
221
+ requirements:
222
+ - - ! '>='
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ requirements: []
226
+ rubyforge_project:
227
+ rubygems_version: 1.8.24
228
+ signing_key:
229
+ specification_version: 3
230
+ summary: Eagleplatform authorization center client module
231
+ test_files: []
232
+ has_rdoc: