tiny_passport 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +7 -0
  2. data/app/ables/tiny_passport/account_validate_able.rb +24 -0
  3. data/app/ables/tiny_passport/aggrement_term_able.rb +28 -0
  4. data/app/ables/tiny_passport/formable.rb +17 -0
  5. data/app/ables/tiny_passport/query_able.rb +25 -0
  6. data/app/controllers/tiny_passport/application_controller.rb +1 -0
  7. data/app/displays/tiny_passport/account_display.rb +25 -0
  8. data/app/displays/tiny_passport/oauth_login_display.rb +21 -0
  9. data/app/displays/tiny_passport/oauth_token_display.rb +25 -0
  10. data/app/errors/tiny_passport/alert_authorized_error.rb +6 -0
  11. data/app/errors/tiny_passport/unauthorized_error.rb +6 -0
  12. data/app/forms/tiny_passport/account_active_form.rb +83 -0
  13. data/app/forms/tiny_passport/account_form.rb +6 -0
  14. data/app/forms/tiny_passport/confirmation_token_form.rb +58 -0
  15. data/app/forms/tiny_passport/oauth/bind_connect_form.rb +65 -0
  16. data/app/forms/tiny_passport/oauth/bind_disconnect_form.rb +42 -0
  17. data/app/forms/tiny_passport/oauth/callback_form.rb +165 -0
  18. data/app/forms/tiny_passport/oauth/login_connect_form.rb +63 -0
  19. data/app/forms/tiny_passport/registration_form.rb +172 -0
  20. data/app/forms/tiny_passport/remember_token_auth_form.rb +32 -0
  21. data/app/forms/tiny_passport/reset_password_form.rb +95 -0
  22. data/app/forms/tiny_passport/reset_password_token_form.rb +55 -0
  23. data/app/forms/tiny_passport/session_form.rb +116 -0
  24. data/app/forms/tiny_passport/update_password_form.rb +50 -0
  25. data/app/forms/tiny_passport/welcome_email_form.rb +47 -0
  26. data/app/helpers/tiny_passport/application_helper.rb +1 -0
  27. data/app/mailers/tiny_passport/registration_mailer.rb +23 -0
  28. data/app/mailers/tiny_passport/reset_password_mailer.rb +16 -0
  29. data/app/models/concerns/tiny_passport/account_authenticatable.rb +24 -0
  30. data/app/models/tiny_passport/account.rb +41 -0
  31. data/app/models/tiny_passport/avatar.rb +6 -0
  32. data/app/models/tiny_passport/login_email.rb +89 -0
  33. data/app/models/tiny_passport/login_record.rb +69 -0
  34. data/app/models/tiny_passport/oauth_bind.rb +9 -0
  35. data/app/models/tiny_passport/oauth_info.rb +8 -0
  36. data/app/models/tiny_passport/oauth_login.rb +28 -0
  37. data/app/models/tiny_passport/oauth_token.rb +57 -0
  38. data/app/queries/tiny_passport/account_query.rb +45 -0
  39. data/app/services/tiny_passport/account_service.rb +43 -0
  40. data/app/services/tiny_passport/email_send_service.rb +32 -0
  41. data/app/services/tiny_passport/oauth_service.rb +11 -0
  42. data/app/views/layouts/tiny_passport/mailer.html.erb +7 -0
  43. data/app/views/tiny_passport/registration_mailer/send_confirmation_token.html.erb +5 -0
  44. data/app/views/tiny_passport/registration_mailer/welcome.html.erb +5 -0
  45. data/app/views/tiny_passport/reset_password_mailer/send_token.html.erb +5 -0
  46. data/config/locales/en/forms.en.yml +81 -0
  47. data/config/locales/zh-CN/forms.zh-CN.yml +123 -0
  48. data/config/routes.rb +1 -0
  49. data/db/migrate/20131105103853_create_tiny_passport_accounts.rb +23 -0
  50. data/db/migrate/20131105104415_create_tiny_passport_login_records.rb +20 -0
  51. data/db/migrate/20131105104559_create_tiny_passport_oauth_tokens.rb +27 -0
  52. data/db/migrate/20131105104632_create_tiny_passport_oauth_logins.rb +15 -0
  53. data/db/migrate/20131105104702_create_tiny_passport_oauth_binds.rb +16 -0
  54. data/db/migrate/20131105104738_create_tiny_passport_oauth_infos.rb +13 -0
  55. data/db/migrate/20131105104847_create_tiny_passport_avatars.rb +20 -0
  56. data/db/migrate/20131107031935_create_tiny_passport_login_emails.rb +25 -0
  57. data/lib/omniauth/strategies/qq_connect.rb +114 -0
  58. data/lib/tiny_passport/config.rb +72 -0
  59. data/lib/tiny_passport/engine.rb +16 -0
  60. data/lib/tiny_passport/http_authable.rb +50 -0
  61. data/lib/tiny_passport/http_authcenter.rb +138 -0
  62. data/lib/tiny_passport/mailerable.rb +20 -0
  63. data/lib/tiny_passport/omniauth/strategable.rb +21 -0
  64. data/lib/tiny_passport/version.rb +2 -1
  65. data/lib/tiny_passport.rb +13 -0
  66. data/test/dummy/app/controllers/application_controller.rb +1 -0
  67. data/test/dummy/app/helpers/application_helper.rb +1 -0
  68. data/test/dummy/config/application.rb +1 -0
  69. data/test/dummy/config/boot.rb +1 -0
  70. data/test/dummy/config/environment.rb +1 -0
  71. data/test/dummy/config/environments/development.rb +1 -0
  72. data/test/dummy/config/environments/production.rb +1 -0
  73. data/test/dummy/config/environments/test.rb +1 -0
  74. data/test/dummy/config/initializers/backtrace_silencers.rb +1 -0
  75. data/test/dummy/config/initializers/filter_parameter_logging.rb +1 -0
  76. data/test/dummy/config/initializers/inflections.rb +1 -0
  77. data/test/dummy/config/initializers/mime_types.rb +1 -0
  78. data/test/dummy/config/initializers/secret_token.rb +1 -0
  79. data/test/dummy/config/initializers/session_store.rb +1 -0
  80. data/test/dummy/config/initializers/wrap_parameters.rb +1 -0
  81. data/test/dummy/config/routes.rb +1 -0
  82. data/test/dummy/db/schema.rb +138 -0
  83. data/test/dummy/db/test.sqlite3 +0 -0
  84. data/test/dummy/log/test.log +15986 -0
  85. data/test/dummy/tmp/cache/271/2A1/tiny_cache%2Fmodels%2FTinyPassport%3A%3AAccount%2F1%2F1 +0 -0
  86. data/test/dummy/tmp/cache/EDF/4C1/tiny_cache%2Fmodel_uniq_keys%2FTinyPassport%3A%3ALoginEmail%2F%5B%5B%22account_id%22%2C+1%5D%5D +1 -0
  87. data/test/fixtures/tiny_passport/avatars.yml +23 -0
  88. data/test/fixtures/tiny_passport/login_emails.yml +23 -0
  89. data/test/fixtures/tiny_passport/login_records.yml +17 -0
  90. data/test/fixtures/tiny_passport/oauth_binds.yml +13 -0
  91. data/test/fixtures/tiny_passport/oauth_infos.yml +9 -0
  92. data/test/fixtures/tiny_passport/oauth_logins.yml +11 -0
  93. data/test/fixtures/tiny_passport/oauth_tokens.yml +21 -0
  94. data/test/fixtures/tiny_passport/users.yml +13 -0
  95. data/test/forms/tiny_passport/oauth/callback_form_test.rb +23 -0
  96. data/test/forms/tiny_passport/registration_form_test.rb +112 -0
  97. data/test/forms/tiny_passport/session_form_test.rb +139 -0
  98. data/test/forms/tiny_passport/update_password_form_test.rb +20 -0
  99. data/test/integration/navigation_test.rb +1 -0
  100. data/test/mailers/tiny_passport/registration_mailer_test.rb +10 -0
  101. data/test/mailers/tiny_passport/reset_password_mailer_test.rb +10 -0
  102. data/test/models/concerns/tiny_passport/configable_test.rb +46 -0
  103. data/test/models/tiny_passport/account_test.rb +8 -0
  104. data/test/models/tiny_passport/avatar_test.rb +10 -0
  105. data/test/models/tiny_passport/login_email_test.rb +10 -0
  106. data/test/models/tiny_passport/login_record_test.rb +10 -0
  107. data/test/models/tiny_passport/oauth_bind_test.rb +10 -0
  108. data/test/models/tiny_passport/oauth_info_test.rb +10 -0
  109. data/test/models/tiny_passport/oauth_login_test.rb +10 -0
  110. data/test/models/tiny_passport/oauth_token_test.rb +10 -0
  111. data/test/support/helpers.rb +66 -0
  112. data/test/test_helper.rb +24 -0
  113. data/test/tiny_passport_test.rb +1 -0
  114. metadata +257 -69
@@ -0,0 +1,27 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class CreateTinyPassportOauthTokens < ActiveRecord::Migration
3
+ def change
4
+ create_table :tiny_passport_oauth_tokens do |t|
5
+ t.integer :strategy_type_value, :default => 0, :null => false
6
+ t.string :strategy_id, :default => '', :null => false
7
+ t.string :access_token, :default => '', :null => false
8
+ t.string :access_secret, :default => '', :null => false
9
+ t.datetime :expires_at
10
+ t.string :origin_avatar_url, :default => '', :null => false
11
+ t.string :name, :default => '', :null => false
12
+ t.string :nick_name, :default => '', :null => false
13
+ t.integer :avatar_id
14
+ t.integer :gender, :default => 0, :null => false # 1: 男 2: 女
15
+
16
+ t.timestamps
17
+ end
18
+
19
+ add_index :tiny_passport_oauth_tokens,
20
+ [:strategy_id, :strategy_type_value],
21
+ :name => "index_tiny_passport_oauth_tokens_on_strategy",
22
+ :unique => true
23
+
24
+ add_index :tiny_passport_oauth_tokens, [:name]
25
+ add_index :tiny_passport_oauth_tokens, [:nick_name]
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class CreateTinyPassportOauthLogins < ActiveRecord::Migration
3
+ def change
4
+ create_table :tiny_passport_oauth_logins do |t|
5
+ t.integer :oauth_token_id
6
+ t.integer :account_id
7
+ t.integer :status_value, :default => 0, :null => false
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :tiny_passport_oauth_logins, [:oauth_token_id], :unique => true
13
+ add_index :tiny_passport_oauth_logins, [:account_id]
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class CreateTinyPassportOauthBinds < ActiveRecord::Migration
3
+ def change
4
+ create_table :tiny_passport_oauth_binds do |t|
5
+ t.integer :oauth_token_id
6
+ t.integer :account_id
7
+ t.integer :status_value, :default => 0, :null => false
8
+ t.text :config_info_yaml
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :tiny_passport_oauth_binds, [:oauth_token_id]
14
+ add_index :tiny_passport_oauth_binds, [:account_id]
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class CreateTinyPassportOauthInfos < ActiveRecord::Migration
3
+ def change
4
+ create_table :tiny_passport_oauth_infos do |t|
5
+ t.integer :oauth_token_id
6
+ t.text :raw_info_yaml
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ add_index :tiny_passport_oauth_infos, [:oauth_token_id], :unique => true
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class CreateTinyPassportAvatars < ActiveRecord::Migration
3
+ def change
4
+ create_table :tiny_passport_avatars do |t|
5
+ t.string :file, :default => '', :null => false
6
+ t.string :file_content_type, :default => '', :null => false
7
+ t.integer :file_size, :default => 0, :null => false
8
+ t.string :file_name, :default => '', :null => false
9
+ t.string :origin_file_name, :default => '', :null => false
10
+ t.integer :status_value, :default => 0, :null => false
11
+ t.references :owner, :polymorphic => true
12
+ t.integer :account_id
13
+
14
+ t.timestamps
15
+ end
16
+
17
+ add_index :tiny_passport_avatars, [:owner_id, :owner_type], :name => "index_tiny_passport_avatars_on_owner"
18
+ add_index :tiny_passport_avatars, [:file]
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class CreateTinyPassportLoginEmails < ActiveRecord::Migration
3
+ def change
4
+ create_table :tiny_passport_login_emails do |t|
5
+ t.string :value, :default => '', :null => false
6
+ t.string :encrypted_confirmation_token, :default => '', :null => false
7
+ t.datetime :confirmed_at
8
+ t.datetime :confirmation_send_at
9
+
10
+ t.string :encrypted_reset_password_token, :default => '', :null => false
11
+ t.datetime :reset_password_token_send_at
12
+
13
+ t.integer :account_id
14
+ t.datetime :deleted_at
15
+ t.integer :status_value, :default => 0, :null => false
16
+
17
+ t.timestamps
18
+ end
19
+
20
+ add_index :tiny_passport_login_emails, [:value], :unique => true
21
+ add_index :tiny_passport_login_emails, [:encrypted_confirmation_token], :name => :index_tiny_passport_login_emails_confirmation_token
22
+ add_index :tiny_passport_login_emails, [:encrypted_reset_password_token], :name => :index_tiny_passport_login_emails_reset_password_token
23
+ add_index :tiny_passport_login_emails, [:account_id]
24
+ end
25
+ end
@@ -0,0 +1,114 @@
1
+ # encoding: utf-8
2
+ require 'omniauth/strategies/oauth2'
3
+ require "tiny_passport/omniauth/strategable"
4
+
5
+ module OmniAuth
6
+ module Strategies
7
+ class QQConnect < ::OmniAuth::Strategies::OAuth2
8
+ include ::TinyPassport::OmniAuth::Strategable
9
+
10
+ option :name, "qq_connect"
11
+
12
+ option :client_options, {
13
+ :site => 'https://graph.qq.com/oauth2.0/',
14
+ :authorize_url => '/oauth2.0/authorize',
15
+ :token_url => "/oauth2.0/token"
16
+ }
17
+
18
+ option :token_params, {
19
+ :state => 'foobar',
20
+ :parse => :query
21
+ }
22
+
23
+ option :authorize_options, [:scope]
24
+
25
+ uid do
26
+ @uid ||= begin
27
+ access_token.options[:mode] = :query
28
+ access_token.options[:param_name] = :access_token
29
+ # Response Example: "callback( {\"client_id\":\"11111\",\"openid\":\"000000FFFF\"} );\n"
30
+ response = access_token_get('/oauth2.0/me')
31
+ #TODO handle error case
32
+ matched = response.body.match(/"openid":"(?<openid>\w+)"/)
33
+ matched[:openid]
34
+ end
35
+ end
36
+
37
+ # {
38
+ # "provider": "qq_connect",
39
+ # "uid": "51173C8FCF304708D299C1EE00A7BB6A",
40
+ # "info": {
41
+ # "nick_name": "Happy",
42
+ # "name": "Happy",
43
+ # "avatar": "http://qzapp.qlogo.cn/qzapp/100587827/51173C8FCF304708D299C1EE00A7BB6A/100",
44
+ # "gender": 1
45
+ # },
46
+ # "credentials": {
47
+ # "token": "71A81CB1FCB726C791D985C5AA117338",
48
+ # "refresh_token": "A3F01B40632B2C39262FA5F02981BC0A",
49
+ # "expires_at": 1396189038,
50
+ # "expires": true
51
+ # },
52
+ # "extra": {
53
+ # "raw_info": {
54
+ # "ret": 0,
55
+ # "msg": "",
56
+ # "is_lost": 0,
57
+ # "nickname": "Happy",
58
+ # "gender": "男",
59
+ # "figureurl": "http://qzapp.qlogo.cn/qzapp/100587827/51173C8FCF304708D299C1EE00A7BB6A/30",
60
+ # "figureurl_1": "http://qzapp.qlogo.cn/qzapp/100587827/51173C8FCF304708D299C1EE00A7BB6A/50",
61
+ # "figureurl_2": "http://qzapp.qlogo.cn/qzapp/100587827/51173C8FCF304708D299C1EE00A7BB6A/100",
62
+ # "figureurl_qq_1": "http://q.qlogo.cn/qqapp/100587827/51173C8FCF304708D299C1EE00A7BB6A/40",
63
+ # "figureurl_qq_2": "http://q.qlogo.cn/qqapp/100587827/51173C8FCF304708D299C1EE00A7BB6A/100",
64
+ # "is_yellow_vip": "0",
65
+ # "vip": "0",
66
+ # "yellow_vip_level": "0",
67
+ # "level": "0",
68
+ # "is_yellow_year_vip": "0"
69
+ # }
70
+ # }
71
+ # }
72
+ info do
73
+ {
74
+ :nick_name => raw_info['nickname'],
75
+ :name => raw_info['nickname'],
76
+ :avatar => raw_info['figureurl_2'],
77
+ :gender => get_gender_value(raw_info)
78
+ }
79
+ end
80
+
81
+ def get_gender_value raw_info={}
82
+ case raw_info['gender']
83
+ when '男'
84
+ ::TinySupport::Const::MALE
85
+ when '女'
86
+ ::TinySupport::Const::FAMALE
87
+ else
88
+ 0
89
+ end
90
+ end
91
+
92
+ extra do
93
+ {
94
+ :raw_info => raw_info
95
+ }
96
+ end
97
+
98
+ def raw_info
99
+ @raw_info ||= begin
100
+ #TODO handle error case
101
+ #TODO make info request url configurable
102
+ client_request(:get, "https://graph.qq.com/user/get_user_info", :params => {
103
+ :format => :json,
104
+ :openid => uid,
105
+ :oauth_consumer_key => options[:client_id],
106
+ :access_token => access_token.token
107
+ }, :parse => :json).parsed
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ OmniAuth.config.add_camelization('qq_connect', 'QQConnect')
@@ -0,0 +1,72 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module TinyPassport
3
+ class << self
4
+ attr_writer :config
5
+
6
+ def config
7
+ @config ||= Config.new
8
+ end
9
+
10
+ def configure
11
+ yield self.config ||= Config.new
12
+ end
13
+ end
14
+
15
+ class Config
16
+ def omniauth_providers= providers={}
17
+ @omniauth_providers ||= providers
18
+ end
19
+
20
+ def omniauth_providers
21
+ @omniauth_providers ||= {}
22
+ end
23
+
24
+ def omniauth_providers_regexp
25
+ @omniauth_providers_regexp ||= ::Regexp.new(self.omniauth_providers.keys.join('|'))
26
+ end
27
+
28
+ def cookies_default_options= options={}
29
+ @cookies_default_options ||= options
30
+ end
31
+
32
+ def cookies_default_options
33
+ @cookies_default_options
34
+ end
35
+
36
+ def session_store_config= options={}
37
+ @session_store_config ||= options
38
+ end
39
+
40
+ def session_store_config
41
+ @session_store_config
42
+ end
43
+
44
+ def mailer_config= conf={}
45
+ @mailer_config ||= conf
46
+ end
47
+
48
+ def login_email_confirmation_token_expires_in
49
+ @login_email_confirmation_token_expires_in ||= 7.days
50
+ end
51
+
52
+ def login_email_reset_password_token_expires_in
53
+ @login_email_reset_password_token_expires_in ||= 7.days
54
+ end
55
+
56
+ def mailer_config
57
+ @mailer_config ||= {}
58
+ end
59
+
60
+ def email_regexp
61
+ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/
62
+ end
63
+
64
+ def host= host
65
+ @host ||= host
66
+ end
67
+
68
+ def host
69
+ @host
70
+ end
71
+ end
72
+ end
@@ -1,5 +1,21 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'tiny_cache'
3
+ require 'tiny_util'
4
+ require 'tiny_support'
5
+ require 'protected_attributes'
6
+
1
7
  module TinyPassport
2
8
  class Engine < ::Rails::Engine
3
9
  isolate_namespace TinyPassport
10
+
11
+ initializer "tiny_passport.add_middleware" do |app|
12
+ app.middleware.use ::OmniAuth::Builder do
13
+ ::TinyPassport.config.omniauth_providers.each do |provider_name, provider_config|
14
+ require "omniauth/strategies/#{provider_name}"
15
+
16
+ provider provider_name, *provider_config
17
+ end
18
+ end
19
+ end
4
20
  end
5
21
  end
@@ -0,0 +1,50 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module TinyPassport
3
+ module HttpAuthable
4
+ extend ::ActiveSupport::Concern
5
+
6
+ included do
7
+ helper_method :authcenter, :current_account
8
+
9
+ after_filter :sync_authcenter_frontend_signed_status
10
+ end
11
+
12
+ #
13
+ # authcenter.create_session(login_record)
14
+ # authcenter.clear_session
15
+ #
16
+ def authcenter
17
+ @authcenter ||= ::TinyPassport::HttpAuthcenter.new(
18
+ :request => request,
19
+ :response => response,
20
+ :cookies => cookies,
21
+ :session => session,
22
+ :url_authcenter => url_authcenter
23
+ )
24
+ end
25
+
26
+ def current_account
27
+ self.authcenter.current_account
28
+ end
29
+
30
+ def authcenter_account_required!
31
+ unless authcenter.account_signed_in?
32
+ raise ::TinyPassport::UnauthorizedError.new
33
+ end
34
+ end
35
+
36
+ # 用户不能登录,如果登录了,提示退出
37
+ def authcenter_account_no_required!
38
+ if authcenter.account_signed_in?
39
+ raise ::TinyPassport::AlertAuthorizedError.new
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def sync_authcenter_frontend_signed_status
46
+ self.authcenter.sync_frontend_signed_status
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,138 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module TinyPassport
3
+ class HttpAuthcenter
4
+ SESSION_KEY = '_account_id'.freeze
5
+ REMEMBER_TOKEN_KEY = '_account_remember_token'.freeze
6
+ FRONTEND_SIGNED_STATUS_KEY = '_account_signed_status'
7
+ FRONTEND_SIGNED_STATUSES = ::TinyUtil::Config.new([
8
+ {:key => 'signed', :value => '1', :desc => 'signed'}
9
+ ])
10
+ RETURN_TO_KEY = 'return_to'
11
+
12
+ attr_reader :request, :response, :session, :cookies, :url_authcenter
13
+
14
+
15
+ def initialize attrs={}
16
+ @request = attrs[:request]
17
+ @response = attrs[:response]
18
+ @session = attrs[:session]
19
+ @cookies = attrs[:cookies]
20
+ @url_authcenter = attrs[:url_authcenter]
21
+ end
22
+
23
+ def domain
24
+ @domain ||= ::DomainName.new(request.host).domain
25
+ end
26
+
27
+ def return_to(url_to=nil)
28
+ request.params[RETURN_TO_KEY] ||
29
+ session.delete(RETURN_TO_KEY) ||
30
+ url_to ||
31
+ (request.url if request.get?) ||
32
+ url_authcenter.root_url
33
+ end
34
+
35
+ def create_session login_record
36
+ if login_record.remembered
37
+ cookies_default_options = ::TinyPassport.config.cookies_default_options
38
+
39
+ cookies[REMEMBER_TOKEN_KEY] = cookies_default_options.merge(
40
+ :value => login_record.remember_token,
41
+ :expires => login_record.remember_expires_at,
42
+ :path => '/',
43
+ :domain => self.domain
44
+ )
45
+ end
46
+
47
+ _save_account_id login_record.account_id
48
+ end
49
+
50
+ def clear_oauth_session
51
+ session.delete "authcenter.oauth.usage"
52
+ session.delete "authcenter.oauth.token_id"
53
+ end
54
+
55
+ def clear_session
56
+ _remember_token = cookies.delete REMEMBER_TOKEN_KEY, :domain => self.domain
57
+
58
+ if _remember_token
59
+ if _login_record = ::TinyPassport::LoginRecord.authenticate_from_remember_token(_remember_token)
60
+ _login_record.remember_sign_out_at = Time.now
61
+ _login_record.save
62
+ end
63
+ end
64
+
65
+ cookies.delete FRONTEND_SIGNED_STATUS_KEY, :domain => self.domain
66
+ session.delete SESSION_KEY
67
+ end
68
+
69
+ def current_account
70
+ return @current_account if defined?(@current_account)
71
+
72
+ account_id = session[SESSION_KEY]
73
+
74
+ # @current_account = account_id ? TinyPassport::Account.find_by_id(account_id) : nil
75
+ @current_account = account_id ? TinyPassport::Account.tiny_cache_find_by(:id => account_id) : nil
76
+
77
+ unless @current_account
78
+ if _remember_token = cookies[REMEMBER_TOKEN_KEY]
79
+ _remember_token_form = ::TinyPassport::RememberTokenAuthForm.new
80
+
81
+ # 使用记住token登录
82
+ _remember_token_form.submit(
83
+ :remember_token => _remember_token,
84
+ :last_remembered_ip => request.remote_ip
85
+ )
86
+
87
+ if _remember_token_form.success?
88
+ _login_record = _remember_token_form.login_record
89
+ # 将account_id保存到session中
90
+ _save_account_id _login_record.account_id
91
+ # 设置当前account
92
+ @current_account = _login_record.account
93
+ end
94
+ end
95
+ end
96
+
97
+ unless @current_account
98
+ clear_session
99
+ end
100
+
101
+ @current_account
102
+ end
103
+
104
+ def current_account= account
105
+ @current_account = account
106
+ end
107
+
108
+ def account_signed_in?
109
+ !!(self.current_account)
110
+ end
111
+
112
+ def frontend_account_signed_in?
113
+ cookies[FRONTEND_SIGNED_STATUS_KEY] == FRONTEND_SIGNED_STATUSES.find_by_key('signed').value
114
+ end
115
+
116
+ # 同步前端的登录状态
117
+ def sync_frontend_signed_status
118
+ if self.account_signed_in?
119
+ cookies[FRONTEND_SIGNED_STATUS_KEY] = {
120
+ :value => FRONTEND_SIGNED_STATUSES.find_by_key('signed').value,
121
+ :expires => ::Time.now + ::TinyPassport.config.session_store_config[:expire_after].to_i,
122
+ :path => '/',
123
+ :domain => self.domain
124
+ }
125
+
126
+ else
127
+ cookies.delete FRONTEND_SIGNED_STATUS_KEY, :domain => self.domain
128
+ end
129
+ end
130
+
131
+ private
132
+
133
+ def _save_account_id account_id
134
+ session[SESSION_KEY] = account_id
135
+ end
136
+
137
+ end
138
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module TinyPassport
3
+ module Mailerable
4
+ extend ::ActiveSupport::Concern
5
+
6
+ included do
7
+ layout 'tiny_passport/mailer'
8
+
9
+ _mailer_config = ::TinyPassport.config.mailer_config
10
+
11
+ self.default(_mailer_config[:default]) if _mailer_config[:default]
12
+ self.default_options = _mailer_config[:default_options] if _mailer_config[:default_options]
13
+ self.default_url_options = _mailer_config[:default_url_options] if _mailer_config[:default_url_options]
14
+
15
+ if _mailer_config[:smtp_settings].is_a?(::Hash)
16
+ self.smtp_settings.merge!(_mailer_config[:smtp_settings])
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module TinyPassport
2
+ module OmniAuth
3
+ module Strategable
4
+ extend ::ActiveSupport::Concern
5
+
6
+ TIMEOUT = 10
7
+
8
+ def client_request *args
9
+ Timeout.timeout(TIMEOUT) do
10
+ client.request *args
11
+ end
12
+ end
13
+
14
+ def access_token_get *args
15
+ Timeout.timeout(TIMEOUT) do
16
+ access_token.get *args
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module TinyPassport
2
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
3
4
  end
data/lib/tiny_passport.rb CHANGED
@@ -1,4 +1,17 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "omniauth"
3
+ require "tiny_passport/config"
1
4
  require "tiny_passport/engine"
5
+ require "tiny_passport/mailerable"
6
+ require "tiny_passport/http_authable"
7
+ require "tiny_passport/http_authcenter"
2
8
 
3
9
  module TinyPassport
4
10
  end
11
+
12
+ if defined?(ActionController::Base)
13
+ ActionController::Base.send :include, ::TinyPassport::HttpAuthable
14
+ end
15
+
16
+ I18n.load_path += Dir[Pathname.new(__FILE__).join('..', '..', 'config', 'locales', '**', '*.{rb,yml}').to_s]
17
+
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  class ApplicationController < ActionController::Base
2
3
  # Prevent CSRF attacks by raising an exception.
3
4
  # For APIs, you may want to use :null_session instead.
@@ -1,2 +1,3 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module ApplicationHelper
2
3
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require File.expand_path('../boot', __FILE__)
2
3
 
3
4
  require 'rails/all'
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Set up gems listed in the Gemfile.
2
3
  ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Load the Rails application.
2
3
  require File.expand_path('../application', __FILE__)
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  Dummy::Application.configure do
2
3
  # Settings specified here will take precedence over those in config/application.rb.
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  Dummy::Application.configure do
2
3
  # Settings specified here will take precedence over those in config/application.rb.
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  Dummy::Application.configure do
2
3
  # Settings specified here will take precedence over those in config/application.rb.
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # Configure sensitive parameters which will be filtered from the log file.
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # Add new inflection rules using the following format. Inflections
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # Add new mime types for use in respond_to blocks:
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  # Your secret key is used for verifying the integrity of signed cookies.
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  # Be sure to restart your server when you modify this file.
2
3
 
3
4
  Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'