authlogic-connect-mailru 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Alexey Noskov
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.
File without changes
@@ -0,0 +1,5 @@
1
+ require File.dirname(__FILE__) + "/authlogic_connect_mailru/mailru_client"
2
+ require File.dirname(__FILE__) + "/authlogic_connect_mailru/mailru_token"
3
+ require File.dirname(__FILE__) + "/authlogic_connect_mailru/mailru_session"
4
+
5
+ Authlogic::Session::Base.send(:include, MailruSession)
@@ -0,0 +1,54 @@
1
+ class MailruClient
2
+
3
+ ENDPOINT = 'http://www.appsmail.ru/platform/api'
4
+
5
+ def initialize( token )
6
+ @token = token
7
+ end
8
+
9
+ def respond_to?( m, *args )
10
+ super || api?( m )
11
+ end
12
+
13
+ def method_missing( m, *args, &block )
14
+ call_api( m.to_s.split('_').join('.'), *args )
15
+ end
16
+
17
+ def api?( method )
18
+ method =~ /^[a-z]+_[a-zA-Z]+$/
19
+ end
20
+
21
+ def call_api( method, params = {} )
22
+ JSON.parse(Net::HTTP.get_response(prepare_uri method, params).body)
23
+ end
24
+
25
+ def prepare_uri( method, params )
26
+ params_to_sign = {
27
+ :method => method,
28
+ :app_id => @token.class.app_id,
29
+ :secure => '1',
30
+ :format => 'json'
31
+ }.merge(convert_params params)
32
+
33
+ if @token.exp < Time.now.to_i
34
+ params_to_sign[:session_key] = @token.session_key
35
+ else
36
+ params_to_sign[:uid] = @token.key
37
+ end
38
+
39
+ URI.parse("#{ENDPOINT}?#{params_to_sign.to_query}&sig=#{@token.class.sign params_to_sign}")
40
+ end
41
+
42
+ def convert_params( params )
43
+ params.inject({}) do |hash, keyval|
44
+ hash[keyval.first] = case value = keyval.last
45
+ when Array then
46
+ value.join(',')
47
+ else
48
+ value.to_s
49
+ end
50
+ hash
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,68 @@
1
+ module MailruSession
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+ include InstanceMethods
6
+
7
+ validate :validate_by_mailru, :if => :authenticating_with_mailru?
8
+ end
9
+ end
10
+
11
+ module InstanceMethods
12
+
13
+ def authenticating_with_mailru?
14
+ mailru_credentials
15
+ end
16
+
17
+ def validate_by_mailru
18
+ return if attempted_record
19
+
20
+ if mailru_credentials[:sig] == MailruToken.sign( mailru_credentials )
21
+ if token = MailruToken.find_by_key( mailru_credentials[:oid], :include => :user ) # Try to find token by user id
22
+ token.token = mailru_token_value
23
+ token.save!
24
+
25
+ self.attempted_record = token.user
26
+ elsif auto_register?
27
+ self.attempted_record = klass.new
28
+ self.attempted_record.access_tokens << MailruToken.new( :key => mailru_credentials[:oid], :token => mailru_token_value )
29
+ self.attempted_record.save
30
+ else
31
+ Rails.logger.warn "Could not find user in our database, have you registered with your mailru account?"
32
+
33
+ errors.add(:user, "Could not find user in our database, have you registered with your mailru account?")
34
+ end
35
+ else
36
+ Rails.logger.warn "Wrong MailRu credentials signature"
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def mailru_credentials
43
+ return @mailru_credentials if defined? @mailru_credentials
44
+
45
+ values = auth_controller.cookies['mrc'] && CGI::parse( auth_controller.cookies['mrc'] )
46
+
47
+ if values
48
+ values.symbolize_keys!
49
+ values.each do |k,v|
50
+ values[k] = v.first
51
+ end
52
+
53
+ if values[:app_id] != MailruToken.app_id
54
+ Rails.logger.warn "Mailru AppId differ in cookies (#{values[:app_id]}) and config (#{MailruToken.app_id})"
55
+ values = nil
56
+ end
57
+ end
58
+
59
+ @mailru_credentials = values
60
+ end
61
+
62
+ def mailru_token_value
63
+ [ mailru_credentials[:session_key], mailru_credentials[:exp] ].join('|')
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,39 @@
1
+ class MailruToken < AccessToken
2
+
3
+ class << self
4
+
5
+ def app_id
6
+ credentials[:app_id]
7
+ end
8
+
9
+ def secret
10
+ credentials[:secret]
11
+ end
12
+
13
+ def sign( params )
14
+ Digest::MD5.hexdigest( params.reject{|k,v| k.to_s == 'sig'}.sort{|a, b| a.first.to_s <=> b.first.to_s}.collect{|key, value| "#{key}=#{value}"}.join + secret )
15
+ end
16
+
17
+ end
18
+
19
+ def client
20
+ @client ||= MailruClient.new self
21
+ end
22
+
23
+ def session_key
24
+ parse_token unless @session_key
25
+ @session_key
26
+ end
27
+
28
+ def exp
29
+ parse_token unless @exp
30
+ @exp.to_i
31
+ end
32
+
33
+ private
34
+
35
+ def parse_token
36
+ @session_key, @exp = token.split '|'
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authlogic-connect-mailru
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Alexey Noskov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-21 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: authlogic-connect
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Extension to authlogic-connect for Mail.ru authentication support
36
+ email:
37
+ - alexey.noskov@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - README.rdoc
44
+ - MIT-LICENSE
45
+ files:
46
+ - lib/authlogic-connect-mailru.rb
47
+ - lib/authlogic_connect_mailru/mailru_client.rb
48
+ - lib/authlogic_connect_mailru/mailru_session.rb
49
+ - lib/authlogic_connect_mailru/mailru_token.rb
50
+ - MIT-LICENSE
51
+ - README.rdoc
52
+ has_rdoc: true
53
+ homepage: http://github.com/alno/authlogic-connect-mailru
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 23
76
+ segments:
77
+ - 1
78
+ - 3
79
+ - 6
80
+ version: 1.3.6
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Extension to authlogic-connect for Mail.ru authentication support
88
+ test_files: []
89
+