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.
- checksums.yaml +7 -0
- data/app/ables/tiny_passport/account_validate_able.rb +24 -0
- data/app/ables/tiny_passport/aggrement_term_able.rb +28 -0
- data/app/ables/tiny_passport/formable.rb +17 -0
- data/app/ables/tiny_passport/query_able.rb +25 -0
- data/app/controllers/tiny_passport/application_controller.rb +1 -0
- data/app/displays/tiny_passport/account_display.rb +25 -0
- data/app/displays/tiny_passport/oauth_login_display.rb +21 -0
- data/app/displays/tiny_passport/oauth_token_display.rb +25 -0
- data/app/errors/tiny_passport/alert_authorized_error.rb +6 -0
- data/app/errors/tiny_passport/unauthorized_error.rb +6 -0
- data/app/forms/tiny_passport/account_active_form.rb +83 -0
- data/app/forms/tiny_passport/account_form.rb +6 -0
- data/app/forms/tiny_passport/confirmation_token_form.rb +58 -0
- data/app/forms/tiny_passport/oauth/bind_connect_form.rb +65 -0
- data/app/forms/tiny_passport/oauth/bind_disconnect_form.rb +42 -0
- data/app/forms/tiny_passport/oauth/callback_form.rb +165 -0
- data/app/forms/tiny_passport/oauth/login_connect_form.rb +63 -0
- data/app/forms/tiny_passport/registration_form.rb +172 -0
- data/app/forms/tiny_passport/remember_token_auth_form.rb +32 -0
- data/app/forms/tiny_passport/reset_password_form.rb +95 -0
- data/app/forms/tiny_passport/reset_password_token_form.rb +55 -0
- data/app/forms/tiny_passport/session_form.rb +116 -0
- data/app/forms/tiny_passport/update_password_form.rb +50 -0
- data/app/forms/tiny_passport/welcome_email_form.rb +47 -0
- data/app/helpers/tiny_passport/application_helper.rb +1 -0
- data/app/mailers/tiny_passport/registration_mailer.rb +23 -0
- data/app/mailers/tiny_passport/reset_password_mailer.rb +16 -0
- data/app/models/concerns/tiny_passport/account_authenticatable.rb +24 -0
- data/app/models/tiny_passport/account.rb +41 -0
- data/app/models/tiny_passport/avatar.rb +6 -0
- data/app/models/tiny_passport/login_email.rb +89 -0
- data/app/models/tiny_passport/login_record.rb +69 -0
- data/app/models/tiny_passport/oauth_bind.rb +9 -0
- data/app/models/tiny_passport/oauth_info.rb +8 -0
- data/app/models/tiny_passport/oauth_login.rb +28 -0
- data/app/models/tiny_passport/oauth_token.rb +57 -0
- data/app/queries/tiny_passport/account_query.rb +45 -0
- data/app/services/tiny_passport/account_service.rb +43 -0
- data/app/services/tiny_passport/email_send_service.rb +32 -0
- data/app/services/tiny_passport/oauth_service.rb +11 -0
- data/app/views/layouts/tiny_passport/mailer.html.erb +7 -0
- data/app/views/tiny_passport/registration_mailer/send_confirmation_token.html.erb +5 -0
- data/app/views/tiny_passport/registration_mailer/welcome.html.erb +5 -0
- data/app/views/tiny_passport/reset_password_mailer/send_token.html.erb +5 -0
- data/config/locales/en/forms.en.yml +81 -0
- data/config/locales/zh-CN/forms.zh-CN.yml +123 -0
- data/config/routes.rb +1 -0
- data/db/migrate/20131105103853_create_tiny_passport_accounts.rb +23 -0
- data/db/migrate/20131105104415_create_tiny_passport_login_records.rb +20 -0
- data/db/migrate/20131105104559_create_tiny_passport_oauth_tokens.rb +27 -0
- data/db/migrate/20131105104632_create_tiny_passport_oauth_logins.rb +15 -0
- data/db/migrate/20131105104702_create_tiny_passport_oauth_binds.rb +16 -0
- data/db/migrate/20131105104738_create_tiny_passport_oauth_infos.rb +13 -0
- data/db/migrate/20131105104847_create_tiny_passport_avatars.rb +20 -0
- data/db/migrate/20131107031935_create_tiny_passport_login_emails.rb +25 -0
- data/lib/omniauth/strategies/qq_connect.rb +114 -0
- data/lib/tiny_passport/config.rb +72 -0
- data/lib/tiny_passport/engine.rb +16 -0
- data/lib/tiny_passport/http_authable.rb +50 -0
- data/lib/tiny_passport/http_authcenter.rb +138 -0
- data/lib/tiny_passport/mailerable.rb +20 -0
- data/lib/tiny_passport/omniauth/strategable.rb +21 -0
- data/lib/tiny_passport/version.rb +2 -1
- data/lib/tiny_passport.rb +13 -0
- data/test/dummy/app/controllers/application_controller.rb +1 -0
- data/test/dummy/app/helpers/application_helper.rb +1 -0
- data/test/dummy/config/application.rb +1 -0
- data/test/dummy/config/boot.rb +1 -0
- data/test/dummy/config/environment.rb +1 -0
- data/test/dummy/config/environments/development.rb +1 -0
- data/test/dummy/config/environments/production.rb +1 -0
- data/test/dummy/config/environments/test.rb +1 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +1 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +1 -0
- data/test/dummy/config/initializers/inflections.rb +1 -0
- data/test/dummy/config/initializers/mime_types.rb +1 -0
- data/test/dummy/config/initializers/secret_token.rb +1 -0
- data/test/dummy/config/initializers/session_store.rb +1 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +1 -0
- data/test/dummy/config/routes.rb +1 -0
- data/test/dummy/db/schema.rb +138 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +15986 -0
- data/test/dummy/tmp/cache/271/2A1/tiny_cache%2Fmodels%2FTinyPassport%3A%3AAccount%2F1%2F1 +0 -0
- 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
- data/test/fixtures/tiny_passport/avatars.yml +23 -0
- data/test/fixtures/tiny_passport/login_emails.yml +23 -0
- data/test/fixtures/tiny_passport/login_records.yml +17 -0
- data/test/fixtures/tiny_passport/oauth_binds.yml +13 -0
- data/test/fixtures/tiny_passport/oauth_infos.yml +9 -0
- data/test/fixtures/tiny_passport/oauth_logins.yml +11 -0
- data/test/fixtures/tiny_passport/oauth_tokens.yml +21 -0
- data/test/fixtures/tiny_passport/users.yml +13 -0
- data/test/forms/tiny_passport/oauth/callback_form_test.rb +23 -0
- data/test/forms/tiny_passport/registration_form_test.rb +112 -0
- data/test/forms/tiny_passport/session_form_test.rb +139 -0
- data/test/forms/tiny_passport/update_password_form_test.rb +20 -0
- data/test/integration/navigation_test.rb +1 -0
- data/test/mailers/tiny_passport/registration_mailer_test.rb +10 -0
- data/test/mailers/tiny_passport/reset_password_mailer_test.rb +10 -0
- data/test/models/concerns/tiny_passport/configable_test.rb +46 -0
- data/test/models/tiny_passport/account_test.rb +8 -0
- data/test/models/tiny_passport/avatar_test.rb +10 -0
- data/test/models/tiny_passport/login_email_test.rb +10 -0
- data/test/models/tiny_passport/login_record_test.rb +10 -0
- data/test/models/tiny_passport/oauth_bind_test.rb +10 -0
- data/test/models/tiny_passport/oauth_info_test.rb +10 -0
- data/test/models/tiny_passport/oauth_login_test.rb +10 -0
- data/test/models/tiny_passport/oauth_token_test.rb +10 -0
- data/test/support/helpers.rb +66 -0
- data/test/test_helper.rb +24 -0
- data/test/tiny_passport_test.rb +1 -0
- metadata +257 -69
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5a60dc3a5327a71a16196868c24983c92ca86334
|
4
|
+
data.tar.gz: 23ffb623bc434a7520c73097b28471ef8a12d3f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b6d331b8096cb4ed17de84e09941256e459441572f78a4af79288ab37c31d584c854ee751613bd61d7a1fa14c23c028510bf124fae3a1d14364db247e0fa53e1
|
7
|
+
data.tar.gz: e062ca5ca0319cbd64d83ce3dc569124db341a4110d66b32c0c7d5a620f949aa165652a942ce0e302d25545d34695d851c712796c0c6fff100ee128966e0069e
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module TinyPassport
|
2
|
+
module AccountValidateAble
|
3
|
+
extend ::ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def account_password_validate
|
7
|
+
validates_presence_of :password
|
8
|
+
validates_length_of :password, :within => 6..24, :allow_blank => true
|
9
|
+
validates_confirmation_of :password, :if => :password_confirmation_required?
|
10
|
+
end
|
11
|
+
|
12
|
+
def account_login_name_validate
|
13
|
+
validates_presence_of :login_name
|
14
|
+
validates_length_of :login_name, :within => 3..18, :allow_blank => true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# 如果两者都为不为空,则判断, 是否一致
|
19
|
+
def password_confirmation_required?
|
20
|
+
self.password.present? || self.password_confirmation.present?
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module TinyPassport
|
2
|
+
module AggrementTermAble
|
3
|
+
extend ::ActiveSupport::Concern
|
4
|
+
included do
|
5
|
+
attr_accessor :agreement_terms
|
6
|
+
|
7
|
+
validate do |r|
|
8
|
+
unless r.agreement_terms?
|
9
|
+
r.errors.add :agreement_terms, :not_agree
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize *args
|
16
|
+
@agreement_terms = true
|
17
|
+
super(*args)
|
18
|
+
end
|
19
|
+
|
20
|
+
def agreement_terms?
|
21
|
+
[true, 'true', '1'].include?(self.agreement_terms)
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyPassport
|
3
|
+
module Formable
|
4
|
+
extend ::ActiveSupport::Concern
|
5
|
+
include ::TinySupport::Formable
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
# 邮箱校验
|
9
|
+
def email_validates_of *fields
|
10
|
+
options = fields.extract_options!
|
11
|
+
|
12
|
+
self.validates_format_of *fields, {:with => ::TinyPassport.config.email_regexp}.merge(options)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyPassport
|
3
|
+
module QueryAble
|
4
|
+
extend ::ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
def query_normalize_options options={}
|
10
|
+
result = {}
|
11
|
+
|
12
|
+
if options.key?(:page)
|
13
|
+
result[:page] = (options[:page] || 1).to_i
|
14
|
+
result[:page] = 1 if result[:page] < 1
|
15
|
+
end
|
16
|
+
|
17
|
+
if options.key?(:per_page)
|
18
|
+
result[:per_page] = (options[:per_page] || 20).to_i
|
19
|
+
result[:per_page] = 1 if result[:per_page] < 1
|
20
|
+
end
|
21
|
+
|
22
|
+
result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module TinyPassport
|
2
|
+
class AccountDisplay < ::TinySupport::ActiveRecord::DisplayBase
|
3
|
+
def basic
|
4
|
+
result = {}
|
5
|
+
|
6
|
+
result[:login_email?] = _origin_record.login_email?
|
7
|
+
result[:register_way] = _origin_record.register_way_display
|
8
|
+
result[:login_name] = _origin_record.login_name
|
9
|
+
result[:login_records_count] = _origin_record.login_records_count
|
10
|
+
result[:last_login_at] = _origin_record.last_login_at
|
11
|
+
result[:created_at] = _origin_record.created_at
|
12
|
+
|
13
|
+
if result[:login_email?]
|
14
|
+
login_email = _origin_record.login_email
|
15
|
+
result[:login_email] = {
|
16
|
+
:value => login_email.value,
|
17
|
+
:status => login_email.status_display
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
result
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module TinyPassport
|
2
|
+
class OauthLoginDisplay < ::TinySupport::ActiveRecord::DisplayBase
|
3
|
+
def basic
|
4
|
+
result = {}
|
5
|
+
|
6
|
+
result.merge!(
|
7
|
+
:crated_at => _origin_record.created_at,
|
8
|
+
:updated_at => _origin_record.updated_at,
|
9
|
+
:status => _origin_record.status_display,
|
10
|
+
:oauth_token => _origin_record.oauth_token?
|
11
|
+
)
|
12
|
+
|
13
|
+
if oauth_token = _origin_record.oauth_token
|
14
|
+
result.merge!(oauth_token.display.basic)
|
15
|
+
end
|
16
|
+
|
17
|
+
result
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module TinyPassport
|
2
|
+
class OauthTokenDisplay < ::TinySupport::ActiveRecord::DisplayBase
|
3
|
+
def basic
|
4
|
+
result = {}
|
5
|
+
|
6
|
+
result.merge!(
|
7
|
+
:nick_name => _origin_record.nick_name,
|
8
|
+
:name => _origin_record.name,
|
9
|
+
:access_token => _origin_record.access_token,
|
10
|
+
:access_secret => _origin_record.access_secret,
|
11
|
+
:expires_at => _origin_record.expires_at,
|
12
|
+
:strategy => {
|
13
|
+
:type => _origin_record.strategy_type_display,
|
14
|
+
:id => _origin_record.strategy_id,
|
15
|
+
:name => _origin_record.strategy_name
|
16
|
+
},
|
17
|
+
:avatar => {
|
18
|
+
},
|
19
|
+
:origin_avatar_url => _origin_record.origin_avatar_url
|
20
|
+
)
|
21
|
+
|
22
|
+
result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyPassport
|
3
|
+
class AccountActiveForm
|
4
|
+
include ::TinyPassport::Formable
|
5
|
+
|
6
|
+
attr_reader :active_way, # 激活方式
|
7
|
+
:confirmation_token, # 激活码
|
8
|
+
:value, # account的值
|
9
|
+
:ip # 当前IP
|
10
|
+
|
11
|
+
attr_reader :account, :login_record
|
12
|
+
|
13
|
+
validates_presence_of :confirmation_token
|
14
|
+
validates_presence_of :value
|
15
|
+
validates_inclusion_of :active_way, :in => ['login_email']
|
16
|
+
|
17
|
+
self.support_strip_values!(:confirmation_token)
|
18
|
+
|
19
|
+
def login_record?
|
20
|
+
!!@login_record
|
21
|
+
end
|
22
|
+
|
23
|
+
def submit params={}
|
24
|
+
@active_way = params[:active_way]
|
25
|
+
@confirmation_token = params[:confirmation_token]
|
26
|
+
@value = params[:value]
|
27
|
+
@ip = params[:ip]
|
28
|
+
@auto_login = params[:auto_login]
|
29
|
+
|
30
|
+
if self.valid? && self.success?
|
31
|
+
case self.active_way
|
32
|
+
when 'login_email'
|
33
|
+
# _login_email = ::TinyPassport::LoginEmail.authenticate_from_confirmation_token(self.confirmation_token)
|
34
|
+
_login_email = ::TinyPassport::LoginEmail.find_by_value(self.value)
|
35
|
+
|
36
|
+
if _login_email
|
37
|
+
case _login_email.status_key
|
38
|
+
when 'active'
|
39
|
+
self.errors.add :confirmation_token, :already_active # 已经激活了
|
40
|
+
when 'block'
|
41
|
+
self.errors.add :confirmation_token, :already_block # 已经被锁定了
|
42
|
+
when 'deleted'
|
43
|
+
self.errors.add :confirmation_token, :already_deleted # 已经被注销
|
44
|
+
else
|
45
|
+
if _login_email.confirmation_token_expires?
|
46
|
+
self.errors.add :confirmation_token, :expired
|
47
|
+
else
|
48
|
+
if _login_email.valid_confirmation_token?(self.confirmation_token)
|
49
|
+
::ActiveRecord::Base.transaction do
|
50
|
+
if _login_email.status_active_verify!
|
51
|
+
@account = _login_email.account
|
52
|
+
|
53
|
+
if @auto_login
|
54
|
+
@login_record = ::TinyPassport::LoginRecord.new
|
55
|
+
@login_record.ip = self.ip
|
56
|
+
@login_record.last_remembered_ip = self.ip
|
57
|
+
@login_record.remembered = true
|
58
|
+
@login_record.account = @account
|
59
|
+
@login_record.way_key = 'after_login_email_actived'
|
60
|
+
@login_record.save!
|
61
|
+
end
|
62
|
+
|
63
|
+
return true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
else
|
67
|
+
self.errors.add :confirmation_token, :not_matched
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
else
|
72
|
+
self.errors.add :confirmation_token, :not_found # 没有匹配的
|
73
|
+
end
|
74
|
+
else
|
75
|
+
self.errors.add :base, :invalid # 不可用
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
false
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyPassport
|
3
|
+
# 请求激活的表单
|
4
|
+
class ConfirmationTokenForm
|
5
|
+
include ::TinyPassport::Formable
|
6
|
+
|
7
|
+
attr_accessor :login_email_value
|
8
|
+
attr_accessor :active_way # 激活方式
|
9
|
+
|
10
|
+
validates_inclusion_of :active_way, :in => ['login_email']
|
11
|
+
self.email_validates_of :login_email_value, :if => :login_email_required?
|
12
|
+
|
13
|
+
# 是否需要邮箱
|
14
|
+
def login_email_required?
|
15
|
+
self.active_way == 'login_email'
|
16
|
+
end
|
17
|
+
|
18
|
+
def login_email
|
19
|
+
@login_email ||= ::TinyPassport::LoginEmail.where(:value => self.login_email_value).first
|
20
|
+
end
|
21
|
+
|
22
|
+
def submit params={}
|
23
|
+
self.active_way = 'login_email'
|
24
|
+
self.login_email_value = params[:login_email_value]
|
25
|
+
|
26
|
+
if self.valid? && self.success?
|
27
|
+
case self.active_way
|
28
|
+
when 'login_email'
|
29
|
+
if self.login_email
|
30
|
+
case self.login_email.status_key
|
31
|
+
when 'active'
|
32
|
+
self.errors.add :login_email_value, :already_active # 已经激活了
|
33
|
+
when 'block'
|
34
|
+
self.errors.add :login_email_value, :already_block # 已经被锁定了
|
35
|
+
when 'deleted'
|
36
|
+
self.errors.add :login_email_value, :already_deleted # 已经被注销
|
37
|
+
else
|
38
|
+
self.login_email.generate_encrypted_confirmation_token
|
39
|
+
self.login_email.confirmation_send_at = ::Time.now
|
40
|
+
|
41
|
+
if self.login_email.save!
|
42
|
+
# 发送邮件
|
43
|
+
!!(::TinyPassport::RegistrationMailer.send_confirmation_token(self.login_email).deliver) and return true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
else
|
47
|
+
self.errors.add :login_email_value, :not_found # 没有找到
|
48
|
+
end
|
49
|
+
else
|
50
|
+
# TODO: other method
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
false
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyPassport
|
3
|
+
module Oauth
|
4
|
+
# Oauth 分享的绑定
|
5
|
+
class BindConnectForm
|
6
|
+
include ::TinyPassport::Formable
|
7
|
+
|
8
|
+
attr_reader :account_id, :oauth_token_id
|
9
|
+
attr_reader :oauth_login
|
10
|
+
|
11
|
+
validate :verify_params
|
12
|
+
|
13
|
+
validates_presence_of :account_id, :oauth_token_id
|
14
|
+
|
15
|
+
# 提交
|
16
|
+
def submit params={}
|
17
|
+
@account_id = params[:account_id]
|
18
|
+
@oauth_token_id = params[:oauth_token_id]
|
19
|
+
|
20
|
+
if self.valid? && self.success?
|
21
|
+
::ActiveRecord::Base.transaction do
|
22
|
+
self.oauth_bind.save!
|
23
|
+
end
|
24
|
+
else
|
25
|
+
false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def account
|
30
|
+
@account ||= ::TinyPassport::Account.find_by_id(@account_id)
|
31
|
+
end
|
32
|
+
|
33
|
+
# 不唯一
|
34
|
+
def oauth_bind
|
35
|
+
@oauth_bind ||= ::TinyPassport::OauthBind.where(
|
36
|
+
:oauth_token_id => self.oauth_token_id,
|
37
|
+
:account_id => self.account_id
|
38
|
+
).first_or_initialize
|
39
|
+
end
|
40
|
+
|
41
|
+
def oauth_token
|
42
|
+
@oauth_token ||= ::TinyPassport::OauthToken.find_by_id(@oauth_token_id)
|
43
|
+
end
|
44
|
+
|
45
|
+
# 校验参数
|
46
|
+
def verify_params
|
47
|
+
if self.account
|
48
|
+
else
|
49
|
+
self.errors.add :account_id, :not_found
|
50
|
+
end
|
51
|
+
|
52
|
+
if self.oauth_token
|
53
|
+
else
|
54
|
+
self.errors.add :oauth_token_id, :not_found
|
55
|
+
end
|
56
|
+
|
57
|
+
if self.oauth_token.new_record?
|
58
|
+
else
|
59
|
+
self.errors.add :oauth_token_id, :already_connect
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyPassport
|
3
|
+
module Oauth
|
4
|
+
# Oauth 分享的绑定的断开
|
5
|
+
class BindDisconnectForm
|
6
|
+
include ::TinyPassport::Formable
|
7
|
+
|
8
|
+
attr_reader :oauth_bind_id
|
9
|
+
|
10
|
+
validate :verify_params
|
11
|
+
|
12
|
+
validates_presence_of :oauth_bind_id
|
13
|
+
|
14
|
+
# 提交
|
15
|
+
def submit params={}
|
16
|
+
@oauth_bind_id = params[:oauth_bind_id]
|
17
|
+
|
18
|
+
if self.valid? && self.success?
|
19
|
+
::ActiveRecord::Base.transaction do
|
20
|
+
self.oauth_bind.destroy!
|
21
|
+
end
|
22
|
+
else
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# 不唯一
|
28
|
+
def oauth_bind
|
29
|
+
@oauth_bind ||= ::TinyPassport::OauthBind.where(:id => self.oauth_bind_id).first
|
30
|
+
end
|
31
|
+
|
32
|
+
# 校验参数
|
33
|
+
def verify_params
|
34
|
+
if self.oauth_bind
|
35
|
+
else
|
36
|
+
self.errors.add :oauth_bind_id, :not_found
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,165 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyPassport
|
3
|
+
module Oauth
|
4
|
+
class CallbackForm
|
5
|
+
include ::TinyPassport::Formable
|
6
|
+
|
7
|
+
attr_reader :info_params, :usage
|
8
|
+
# 结果状态
|
9
|
+
attr_reader :result_status
|
10
|
+
attr_reader :login_record
|
11
|
+
attr_reader :ip
|
12
|
+
|
13
|
+
validates_inclusion_of :usage, :in => ['login', 'bind']
|
14
|
+
validate :verify_info_params
|
15
|
+
|
16
|
+
def submit usage, options={}, info_params={}
|
17
|
+
@usage ||= usage
|
18
|
+
@info_params ||= (info_params || {})
|
19
|
+
@ip = options[:ip]
|
20
|
+
|
21
|
+
if self.valid? && self.success?
|
22
|
+
case self.usage
|
23
|
+
when 'login'
|
24
|
+
apply_call_when_login!
|
25
|
+
when 'bind'
|
26
|
+
apply_call_when_bind!
|
27
|
+
end
|
28
|
+
|
29
|
+
true
|
30
|
+
else
|
31
|
+
|
32
|
+
false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def apply_call_when_login!
|
37
|
+
save_objects do
|
38
|
+
# 如果已经绑定过,则将其绑定上
|
39
|
+
if self.oauth_token.login_account?
|
40
|
+
@login_account = self.oauth_token.login_account
|
41
|
+
|
42
|
+
# 有绑定的账户
|
43
|
+
@login_record = ::TinyPassport::LoginRecord.new
|
44
|
+
@login_record.ip = self.ip
|
45
|
+
@login_record.last_remembered_ip = self.ip
|
46
|
+
@login_record.remembered = true
|
47
|
+
@login_record.account = @login_account
|
48
|
+
@login_record.way_key = 'oauth_qq_connect'
|
49
|
+
@login_record.save!
|
50
|
+
|
51
|
+
@result_status = 'already'
|
52
|
+
else
|
53
|
+
@result_status = 'noregister'
|
54
|
+
true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def apply_call_when_bind!
|
60
|
+
save_objects do
|
61
|
+
|
62
|
+
end
|
63
|
+
# TODO: add apply_call_when_bind!
|
64
|
+
end
|
65
|
+
|
66
|
+
# 保存相应的对象
|
67
|
+
def save_objects
|
68
|
+
::ActiveRecord::Base.transaction do
|
69
|
+
assign_oauth_token_attrs
|
70
|
+
assign_oauth_info_attrs
|
71
|
+
|
72
|
+
self.oauth_token.save!
|
73
|
+
self.oauth_info.save!
|
74
|
+
if block_given?
|
75
|
+
yield
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def assign_oauth_info_attrs
|
81
|
+
self.oauth_info.oauth_token = self.oauth_token
|
82
|
+
self.oauth_info.raw_info_yaml = self.params_raw_info.to_yaml
|
83
|
+
end
|
84
|
+
|
85
|
+
def assign_oauth_token_attrs
|
86
|
+
# 名称
|
87
|
+
self.oauth_token.name = self.params_info['name'].to_s
|
88
|
+
|
89
|
+
# 昵称
|
90
|
+
self.oauth_token.nick_name = self.params_info['nick_name'].to_s
|
91
|
+
|
92
|
+
# 原始头像
|
93
|
+
self.oauth_token.origin_avatar_url = self.params_info['avatar'].to_s
|
94
|
+
|
95
|
+
# access_token
|
96
|
+
self.oauth_token.access_token = self.params_credentials['token']
|
97
|
+
|
98
|
+
# gender
|
99
|
+
self.oauth_token.gender = self.params_info['gender']
|
100
|
+
|
101
|
+
if params_credentials['expires_at'].present?
|
102
|
+
# 过期时间
|
103
|
+
self.oauth_token.expires_at = ::Time.at(self.params_credentials['expires_at'].to_i)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def oauth_token
|
108
|
+
@oauth_token ||= (
|
109
|
+
::TinyPassport::OauthToken.find_or_initialize_by(
|
110
|
+
:strategy_id => self.params_uid,
|
111
|
+
:strategy_type_value => self.params_provider_value
|
112
|
+
)
|
113
|
+
)
|
114
|
+
end
|
115
|
+
|
116
|
+
# 长字段
|
117
|
+
def oauth_info
|
118
|
+
@oauth_info ||= (
|
119
|
+
self.oauth_token.oauth_info || ::TinyPassport::OauthInfo.new(
|
120
|
+
:oauth_token => self.oauth_token
|
121
|
+
)
|
122
|
+
)
|
123
|
+
end
|
124
|
+
|
125
|
+
def params_uid
|
126
|
+
self.info_params['uid']
|
127
|
+
end
|
128
|
+
|
129
|
+
def params_provider_value
|
130
|
+
self.params_provider_config.value
|
131
|
+
end
|
132
|
+
|
133
|
+
def params_provider_config
|
134
|
+
::TinyPassport::OauthToken.strategy_type_config.find_by_key(
|
135
|
+
self.info_params['provider'], false
|
136
|
+
)
|
137
|
+
end
|
138
|
+
|
139
|
+
def params_credentials
|
140
|
+
self.info_params['credentials'] || {}
|
141
|
+
end
|
142
|
+
|
143
|
+
def params_info
|
144
|
+
self.info_params['info'] || {}
|
145
|
+
end
|
146
|
+
|
147
|
+
def params_raw_info
|
148
|
+
(self.info_params['extra'] || {})['raw_info'] || {}
|
149
|
+
end
|
150
|
+
|
151
|
+
protected
|
152
|
+
|
153
|
+
def verify_info_params
|
154
|
+
if self.params_uid.blank?
|
155
|
+
self.errors.add :info_params, :uid_must_present
|
156
|
+
end
|
157
|
+
|
158
|
+
if self.params_provider_config.nil?
|
159
|
+
self.errors.add :info_params, :strategy_type_invalid
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyPassport
|
3
|
+
module Oauth
|
4
|
+
# Oauth 登录的绑定
|
5
|
+
class LoginConnectForm
|
6
|
+
include ::TinyPassport::Formable
|
7
|
+
|
8
|
+
attr_accessor :account_id, :oauth_token_id
|
9
|
+
attr_reader :oauth_login
|
10
|
+
|
11
|
+
validates_presence_of :account_id, :oauth_token_id
|
12
|
+
validate :verify_params
|
13
|
+
|
14
|
+
# 提交
|
15
|
+
def submit params={}
|
16
|
+
@account_id = params[:account_id]
|
17
|
+
@oauth_token_id = params[:oauth_token_id]
|
18
|
+
|
19
|
+
if self.valid? && self.success?
|
20
|
+
::ActiveRecord::Base.transaction do
|
21
|
+
# 唯一的
|
22
|
+
@oauth_login = ::TinyPassport::OauthLogin.new
|
23
|
+
|
24
|
+
@oauth_login.account = self.account
|
25
|
+
@oauth_login.oauth_token = self.oauth_token
|
26
|
+
|
27
|
+
@oauth_login.save!
|
28
|
+
end
|
29
|
+
else
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def account
|
35
|
+
return @account if defined?(@account)
|
36
|
+
@account ||= ::TinyPassport::Account.find_by_id(self.account_id)
|
37
|
+
end
|
38
|
+
|
39
|
+
def oauth_token
|
40
|
+
return @oauth_token if defined?(@oauth_token)
|
41
|
+
@oauth_token ||= ::TinyPassport::OauthToken.find_by_id(self.oauth_token_id)
|
42
|
+
end
|
43
|
+
|
44
|
+
# 校验参数
|
45
|
+
def verify_params
|
46
|
+
if self.account
|
47
|
+
else
|
48
|
+
self.errors.add :account_id, :not_found
|
49
|
+
end
|
50
|
+
|
51
|
+
if self.oauth_token
|
52
|
+
if self.oauth_token.oauth_login?
|
53
|
+
# 已经绑定过了
|
54
|
+
self.errors.add :oauth_token_id, :already_connect
|
55
|
+
end
|
56
|
+
else
|
57
|
+
self.errors.add :oauth_token_id, :not_found
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|