aavkontakte-rails3 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +40 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +93 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/aavkontakte-rails3.gemspec +68 -0
- data/init.rb +1 -0
- data/lib/aavkontakte-rails3.rb +15 -0
- data/lib/vkontakte.js +71 -0
- data/lib/vkontakte/auth_success.rb +12 -0
- data/lib/vkontakte/authentication.rb +92 -0
- data/lib/vkontakte/helper.rb +27 -0
- data/lib/vkontakte/session.rb +85 -0
- metadata +117 -0
data/.document
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionmailer (2.3.11)
|
5
|
+
actionpack (= 2.3.11)
|
6
|
+
actionpack (2.3.11)
|
7
|
+
activesupport (= 2.3.11)
|
8
|
+
rack (~> 1.1.0)
|
9
|
+
activerecord (2.3.11)
|
10
|
+
activesupport (= 2.3.11)
|
11
|
+
activeresource (2.3.11)
|
12
|
+
activesupport (= 2.3.11)
|
13
|
+
activesupport (2.3.11)
|
14
|
+
authlogic (2.1.6)
|
15
|
+
activesupport
|
16
|
+
git (1.2.5)
|
17
|
+
jeweler (1.5.2)
|
18
|
+
bundler (~> 1.0.0)
|
19
|
+
git (>= 1.2.5)
|
20
|
+
rake
|
21
|
+
rack (1.1.2)
|
22
|
+
rails (2.3.11)
|
23
|
+
actionmailer (= 2.3.11)
|
24
|
+
actionpack (= 2.3.11)
|
25
|
+
activerecord (= 2.3.11)
|
26
|
+
activeresource (= 2.3.11)
|
27
|
+
activesupport (= 2.3.11)
|
28
|
+
rake (>= 0.8.3)
|
29
|
+
rake (0.8.7)
|
30
|
+
rcov (0.9.9)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
authlogic (~> 2.1.6)
|
37
|
+
bundler (~> 1.0.0)
|
38
|
+
jeweler (~> 1.5.2)
|
39
|
+
rails (~> 2.3.11)
|
40
|
+
rcov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Dmitry Vorotilin
|
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.rdoc
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
= aavkontakte-rails3
|
2
|
+
|
3
|
+
This is aavkontakte-rails3 - yet another authlogic vkontakte authorization gem. It provides vkontakte authorization to authlogic.
|
4
|
+
This gem is tested with authlogic and authlogic-rpx on my project. Please report me about bugs.
|
5
|
+
Vkontakte uses domain protection for url address of your site. But I noticed that:
|
6
|
+
http://your-really-domain:3000 after closing popup window is works too. You must add your domain in /etc/hosts
|
7
|
+
|
8
|
+
== Install
|
9
|
+
|
10
|
+
gem install aavkontakte-rails3
|
11
|
+
|
12
|
+
* Environment
|
13
|
+
|
14
|
+
In environment.rb:
|
15
|
+
|
16
|
+
config.gem 'aavkontakte-rails3'
|
17
|
+
|
18
|
+
* User model
|
19
|
+
|
20
|
+
Add following line:
|
21
|
+
|
22
|
+
acts_as_authentic do |c|
|
23
|
+
c.vkontakte_enabled = { :vk_app_id => "id", :vk_app_password => "password" }
|
24
|
+
c.vkontakte_merge_enabled true # mapping user data from vkontakte to new user account, true by default
|
25
|
+
end
|
26
|
+
|
27
|
+
* UserSession model:
|
28
|
+
|
29
|
+
By default "vkontakte_merge_enabled" uses "map_vkontakte_data" method:
|
30
|
+
|
31
|
+
private
|
32
|
+
def map_vkontakte_data
|
33
|
+
self.attempted_record.send("#{klass.login_field}=", @vkontakte_data[:user][:nickname]) if self.attempted_record.send(klass.login_field).blank?
|
34
|
+
self.attempted_record.send("first_name=", @vkontakte_data[:user][:first_name]) if @vkontakte_data[:user][:first_name]
|
35
|
+
self.attempted_record.send("last_name=", @vkontakte_data[:user][:last_name]) if @vkontakte_data[:user][:last_name]
|
36
|
+
end
|
37
|
+
|
38
|
+
If you set option to true, you need override it in user_session model.
|
39
|
+
|
40
|
+
* Migrations
|
41
|
+
|
42
|
+
Create migration "script/generate migration add_vkontakte_id_to_user vk_id:string":
|
43
|
+
|
44
|
+
class AddVkontakteIdToUser < ActiveRecord::Migration
|
45
|
+
def self.up
|
46
|
+
add_column :users, :vk_id, :string
|
47
|
+
# You must remove any database constraints for fields:
|
48
|
+
# change_column :users, :crypted_password, :string, :null => true
|
49
|
+
# change_column :users, :password_salt, :string, :null => true
|
50
|
+
# change_column :users, :login, :string, :null => true
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.down
|
54
|
+
remove_column :users, :vk_id
|
55
|
+
# change_column :users, :crypted_password, :string, :null => false
|
56
|
+
# change_column :users, :password_salt, :string, :null => false
|
57
|
+
# change_column :users, :login, :string, :null => false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
* Layout
|
63
|
+
|
64
|
+
In the body section:
|
65
|
+
|
66
|
+
<%= init_vkontakte %>
|
67
|
+
|
68
|
+
* Javascripts
|
69
|
+
|
70
|
+
After first start your application, it's copying vkontakte.js to your javascripts directory or if it doesn't exist.
|
71
|
+
|
72
|
+
* Link
|
73
|
+
|
74
|
+
Put link somewhere, where you want:
|
75
|
+
|
76
|
+
<%= vkontakte_login_link_to "Login with vkontakte" %>
|
77
|
+
|
78
|
+
By default it uses action create of user_sessions controller. Parameters are identical link_to without block.
|
79
|
+
|
80
|
+
== Contributing to aavkontakte-rails3
|
81
|
+
|
82
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
83
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
84
|
+
* Fork the project
|
85
|
+
* Start a feature/bugfix branch
|
86
|
+
* Commit and push until you are happy with your contribution
|
87
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
88
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
89
|
+
|
90
|
+
== Copyright
|
91
|
+
|
92
|
+
Copyright (c) 2011 Dmitry Vorotilin. See LICENSE.txt for
|
93
|
+
further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "aavkontakte-rails3"
|
16
|
+
gem.rubyforge_project = "aavkontakte-rails3"
|
17
|
+
gem.homepage = "http://github.com/blimp666/aavkontakte-rails3"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = %Q{Yet Another Authlogic Vkontakte Authorization}
|
20
|
+
gem.description = %Q{Vkontakte authorization for authlogic with ruby 1.9 and rails 3.0}
|
21
|
+
gem.email = "mydeeptown@gmail.com"
|
22
|
+
gem.authors = ["Dmitry Vorotilin"]
|
23
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
24
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
25
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
26
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
require 'rake/testtask'
|
31
|
+
Rake::TestTask.new(:test) do |test|
|
32
|
+
test.libs << 'lib' << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'rcov/rcovtask'
|
38
|
+
Rcov::RcovTask.new do |test|
|
39
|
+
test.libs << 'test'
|
40
|
+
test.pattern = 'test/**/test_*.rb'
|
41
|
+
test.verbose = true
|
42
|
+
end
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "aavkontakte-rails3 #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.3
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{aavkontakte-rails3}
|
8
|
+
s.version = "0.1.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Dmitry Vorotilin"]
|
12
|
+
s.date = %q{2011-05-18}
|
13
|
+
s.description = %q{Vkontakte authorization for authlogic}
|
14
|
+
s.email = %q{mydeeptown@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"aavkontakte-rails3.gemspec",
|
28
|
+
"init.rb",
|
29
|
+
"lib/aavkontakte-rails3.rb",
|
30
|
+
"lib/vkontakte.js",
|
31
|
+
"lib/vkontakte/authentication.rb",
|
32
|
+
"lib/vkontakte/helper.rb",
|
33
|
+
"lib/vkontakte/session.rb",
|
34
|
+
"lib/vkontakte/auth_success.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/blimp666/aavkontakte-rails3}
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubyforge_project = %q{aavkontakte-rails3}
|
40
|
+
s.rubygems_version = %q{1.3.7}
|
41
|
+
s.summary = %q{Yet Another Authlogic Vkontakte Authorization for Rails3}
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
|
49
|
+
s.add_runtime_dependency(%q<authlogic>, [">= 3.0.0"])
|
50
|
+
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
51
|
+
s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
|
52
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rails>, [">= 3.0.0"])
|
55
|
+
s.add_dependency(%q<authlogic>, [">= 3.0.0"])
|
56
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
57
|
+
s.add_dependency(%q<jeweler>, [">= 1.5.2"])
|
58
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<rails>, [">= 3.0.0"])
|
62
|
+
s.add_dependency(%q<authlogic>, [">= 3.0.0"])
|
63
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, [">= 1.5.2"])
|
65
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "aavkontakte"
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "digest/md5"
|
2
|
+
require "vkontakte/authentication"
|
3
|
+
require "vkontakte/session"
|
4
|
+
require "vkontakte/helper"
|
5
|
+
require "vkontakte/auth_success"
|
6
|
+
|
7
|
+
# TODO: may be rake task..
|
8
|
+
unless File.exists?("#{Rails.root}/public/assets/vkontakte.js")
|
9
|
+
# require "ftools"
|
10
|
+
# File.copy("#{File.dirname(__FILE__)}/vkontakte.js", "#{Rails.root}/public/assets")
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Base.send(:include, VkontakteAuthentication::ActsAsAuthentic)
|
14
|
+
Authlogic::Session::Base.send(:include, VkontakteAuthentication::Session)
|
15
|
+
ActionController::Base.helper VkontakteAuthentication::Helper
|
data/lib/vkontakte.js
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
function vkInit(id) {
|
2
|
+
window.vkAsyncInit = function() {
|
3
|
+
VK.init({
|
4
|
+
apiId: id,
|
5
|
+
nameTransportPath: '/xd_receiver.html'
|
6
|
+
});
|
7
|
+
};
|
8
|
+
|
9
|
+
setTimeout(function() {
|
10
|
+
var el = document.createElement("script");
|
11
|
+
el.type = "text/javascript";
|
12
|
+
el.src = "http://vkontakte.ru/js/api/openapi.js";
|
13
|
+
el.async = true;
|
14
|
+
document.getElementById("vk_api_transport").appendChild(el);
|
15
|
+
}, 0);
|
16
|
+
}
|
17
|
+
|
18
|
+
function vkLogin(options) {
|
19
|
+
VK.Auth.login(function(response) {
|
20
|
+
if(response.session) {
|
21
|
+
/* User is logged in */
|
22
|
+
// need ajax request? $.post(url, response, "script" );
|
23
|
+
response.user_session = response.session;
|
24
|
+
delete response.session;
|
25
|
+
post(options['url'], response, options['authenticity_token'], options['session_key'], options['session_id']);
|
26
|
+
if (response.settings){ /* Selected user access settings */ }
|
27
|
+
}
|
28
|
+
else { /* User pressed the Cancel button */ }
|
29
|
+
});
|
30
|
+
return false;
|
31
|
+
}
|
32
|
+
|
33
|
+
function post(url, params, token, session_key, session_id) {
|
34
|
+
var method = "post";
|
35
|
+
|
36
|
+
var form = document.createElement("form");
|
37
|
+
form.setAttribute("method", method);
|
38
|
+
form.setAttribute("action", url);
|
39
|
+
|
40
|
+
var authField = document.createElement("input");
|
41
|
+
authField.setAttribute("type", "hidden");
|
42
|
+
authField.setAttribute("name", "authenticity_token");
|
43
|
+
authField.setAttribute("value", token);
|
44
|
+
form.appendChild(authField);
|
45
|
+
|
46
|
+
var sessionField = document.createElement("input");
|
47
|
+
sessionField.setAttribute("type", "hidden");
|
48
|
+
sessionField.setAttribute("name", session_key);
|
49
|
+
sessionField.setAttribute("value", session_id);
|
50
|
+
form.appendChild(sessionField);
|
51
|
+
|
52
|
+
// recursively adds nested inputs to form
|
53
|
+
var add_inputs = function (form, object, parent_name) {
|
54
|
+
parent_name = parent_name || "";
|
55
|
+
for (var key in object) {
|
56
|
+
var hiddenField = document.createElement("input");
|
57
|
+
hiddenField.setAttribute("type", "hidden");
|
58
|
+
if (typeof(object[key]) == "object") add_inputs(form, object[key], parent_name ? parent_name + "[" + key + "]" : key);
|
59
|
+
else {
|
60
|
+
hiddenField.setAttribute("name", parent_name ? parent_name + "[" + key + "]" : key);
|
61
|
+
hiddenField.setAttribute("value", object[key]);
|
62
|
+
}
|
63
|
+
form.appendChild(hiddenField);
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
add_inputs(form, params);
|
68
|
+
|
69
|
+
document.body.appendChild(form); // Not entirely sure if this is necessary
|
70
|
+
form.submit();
|
71
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module VkontakteAuthentication
|
2
|
+
|
3
|
+
def self.auth_success?(vk_app_password, cookie, request_mid)
|
4
|
+
auth_data = CGI::parse(cookie)
|
5
|
+
auth_data.update(auth_data){|key| auth_data[key][0]}
|
6
|
+
result = "expire=%smid=%ssecret=%ssid=%s%s" % [ auth_data['expire'], auth_data['mid'], auth_data['secret'], auth_data['sid'], vk_app_password]
|
7
|
+
Digest::MD5.hexdigest(result).to_s == auth_data['sig'].to_s and
|
8
|
+
request_mid.to_i == auth_data['mid'].to_i
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module VkontakteAuthentication
|
2
|
+
module ActsAsAuthentic
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
extend Config
|
6
|
+
if defined? AuthlogicRpx
|
7
|
+
remove_acts_as_authentic_module AuthlogicRpx::ActsAsAuthentic::Methods
|
8
|
+
add_acts_as_authentic_module Methods, :prepend
|
9
|
+
add_acts_as_authentic_module AuthlogicRpx::ActsAsAuthentic::Methods
|
10
|
+
else
|
11
|
+
add_acts_as_authentic_module Methods, :prepend
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Config
|
17
|
+
def vkontakte_enabled(vk_app_data = {})
|
18
|
+
value = vk_app_data.present? && vk_app_data[:vk_app_id] && vk_app_data[:vk_app_password] ? true : false
|
19
|
+
if vkontakte_enabled_value(value)
|
20
|
+
vk_app_id vk_app_data[:vk_app_id]
|
21
|
+
vk_app_password vk_app_data[:vk_app_password]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
alias_method :vkontakte_enabled=, :vkontakte_enabled
|
25
|
+
|
26
|
+
def vkontakte_enabled_value(value = nil)
|
27
|
+
rw_config(:vkontakte_enabled, value, false)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Auto registration
|
31
|
+
def vkontakte_auto_registration(value = true)
|
32
|
+
rw_config(:vkontakte_auto_registration, value, true)
|
33
|
+
end
|
34
|
+
alias_method :vkontakte_auto_registration=, :vkontakte_auto_registration
|
35
|
+
|
36
|
+
def vkontakte_auto_registration_value(value = nil)
|
37
|
+
rw_config(:vkontakte_auto_registration, value, true)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Auto merge
|
41
|
+
def vkontakte_merge_enabled(value = true)
|
42
|
+
rw_config(:vkontakte_merge_enabled, value, true)
|
43
|
+
end
|
44
|
+
alias_method :vkontakte_merge_enabled=, :vkontakte_merge_enabled
|
45
|
+
|
46
|
+
def vkontakte_merge_enabled_value(value = nil)
|
47
|
+
rw_config(:vkontakte_merge_enabled, value, true)
|
48
|
+
end
|
49
|
+
|
50
|
+
def vk_app_id(value = nil)
|
51
|
+
rw_config(:vk_app_id, value)
|
52
|
+
ActiveRecord::Base.send(:rw_config, :vk_app_id, value)
|
53
|
+
end
|
54
|
+
|
55
|
+
def vk_app_password(value = nil)
|
56
|
+
rw_config(:vk_app_password, value)
|
57
|
+
end
|
58
|
+
|
59
|
+
def vk_app_cookie
|
60
|
+
rw_config(:vk_app_cookie, nil) || rw_config(:vk_app_cookie, "vk_app_#{vk_app_id}") if vk_app_id
|
61
|
+
end
|
62
|
+
|
63
|
+
def vk_id_field(value = nil)
|
64
|
+
rw_config(:vk_id_field, value, :vk_id)
|
65
|
+
end
|
66
|
+
alias_method :vk_id_field=, :vk_id_field
|
67
|
+
end
|
68
|
+
|
69
|
+
module Methods
|
70
|
+
def self.included(klass)
|
71
|
+
klass.class_eval do
|
72
|
+
validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_not_vkontakte?)
|
73
|
+
validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_not_vkontakte?)
|
74
|
+
validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_not_vkontakte?)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def using_vkontakte?
|
79
|
+
authenticating_with_vkontakte?
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
def validate_password_not_vkontakte?
|
84
|
+
!authenticating_with_vkontakte? && (defined?(AuthlogicRpx) ? !using_rpx? : true) && require_password?
|
85
|
+
end
|
86
|
+
|
87
|
+
def authenticating_with_vkontakte?
|
88
|
+
vk_id.present?
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module VkontakteAuthentication
|
2
|
+
module Helper
|
3
|
+
def init_vkontakte
|
4
|
+
vkontakte_div + vkontakte_init if vk_app_id
|
5
|
+
end
|
6
|
+
|
7
|
+
def vkontakte_login_link_to(name, url = user_session_path, html_options = {})
|
8
|
+
authenticity_token = protect_against_forgery? ? form_authenticity_token : ''
|
9
|
+
options = "{ url: '#{url}', authenticity_token: '#{authenticity_token}', session_key: '#{request.session_options[:key]}', session_id: '#{request.session_options[:id]}' }"
|
10
|
+
html_options.merge!(:onclick => "vkLogin(#{options});") if vk_app_id
|
11
|
+
link_to name, "#", html_options
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def vkontakte_div
|
16
|
+
content_tag(:div, "", :id => "vk_api_transport")
|
17
|
+
end
|
18
|
+
|
19
|
+
def vkontakte_init
|
20
|
+
javascript_include_tag("vkontakte") + javascript_tag("vkInit(#{vk_app_id});")
|
21
|
+
end
|
22
|
+
|
23
|
+
def vk_app_id
|
24
|
+
ActiveRecord::Base.send(:rw_config, :vk_app_id, nil)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module VkontakteAuthentication
|
2
|
+
module Session
|
3
|
+
def self.included(klass)
|
4
|
+
klass.class_eval do
|
5
|
+
include InstanceMethods
|
6
|
+
validate :validate_by_vk_cookie, :if => :authenticating_with_vkontakte?
|
7
|
+
before_destroy :destroy_vkontakte_cookies
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class NotInitializedError < StandardError
|
12
|
+
end
|
13
|
+
|
14
|
+
module InstanceMethods
|
15
|
+
def new_registration=(value)
|
16
|
+
@new_registration = value
|
17
|
+
end
|
18
|
+
|
19
|
+
def new_registration?
|
20
|
+
@new_registration.presence
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def authenticating_with_vkontakte?
|
25
|
+
record_class.vkontakte_enabled_value && controller.cookies[record_class.vk_app_cookie].present?
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def validate_by_vk_cookie
|
30
|
+
@vkontakte_data = controller.params[:user_session] if controller.params[:user_session]
|
31
|
+
if VkontakteAuthentication.auth_success?(record_class.vk_app_password,
|
32
|
+
controller.cookies[record_class.vk_app_cookie],
|
33
|
+
@vkontakte_data[:mid])
|
34
|
+
raise(NotInitializedError, "You must define vk_id column in your User model") unless record_class.attribute_names.include?(vk_id_field.to_s)
|
35
|
+
if @vkontakte_data
|
36
|
+
self.attempted_record = klass.where(vk_id_field => @vkontakte_data[:mid].to_i).first
|
37
|
+
if self.attempted_record.blank?
|
38
|
+
# creating a new account
|
39
|
+
self.new_registration = true
|
40
|
+
self.attempted_record = record_class.new
|
41
|
+
self.attempted_record.send "#{vk_id_field}=", @vkontakte_data[:mid].to_i
|
42
|
+
self.attempted_record.send :persistence_token=, Authlogic::Random.hex_token if self.attempted_record.respond_to? :persistence_token=
|
43
|
+
map_vkontakte_data if record_class.vkontakte_merge_enabled_value
|
44
|
+
self.attempted_record.save_without_session_maintenance
|
45
|
+
elsif !(record_class.vkontakte_auto_registration_value)
|
46
|
+
self.attempted_record = record_class.new
|
47
|
+
end
|
48
|
+
end
|
49
|
+
return true
|
50
|
+
else
|
51
|
+
errors.add(vk_id_field, "Authentication failed. Please try again.")
|
52
|
+
return false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def map_vkontakte_data
|
57
|
+
self.attempted_record.send("#{klass.login_field}=", @vkontakte_data[:user][:nickname]) if self.attempted_record.send(klass.login_field).blank?
|
58
|
+
self.attempted_record.send("first_name=", @vkontakte_data[:user][:first_name]) if @vkontakte_data[:user][:first_name]
|
59
|
+
self.attempted_record.send("last_name=", @vkontakte_data[:user][:last_name]) if @vkontakte_data[:user][:last_name]
|
60
|
+
end
|
61
|
+
|
62
|
+
def find_by_vk_id_method
|
63
|
+
self.class.find_by_vk_id_method
|
64
|
+
end
|
65
|
+
|
66
|
+
def vk_id_field
|
67
|
+
record_class.vk_id_field
|
68
|
+
end
|
69
|
+
|
70
|
+
def record_class
|
71
|
+
self.class.klass
|
72
|
+
end
|
73
|
+
|
74
|
+
def delete_cookie(key)
|
75
|
+
return unless key
|
76
|
+
domain = controller.request.domain
|
77
|
+
[".#{domain}", "#{domain}"].each { |d| controller.cookies.delete(key, :domain => d) }
|
78
|
+
end
|
79
|
+
|
80
|
+
def destroy_vkontakte_cookies
|
81
|
+
delete_cookie(record_class.vk_app_cookie)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aavkontakte-rails3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dmitry Vorotilin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-05-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &74084560 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *74084560
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: authlogic
|
27
|
+
requirement: &74084140 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *74084140
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &74083680 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *74083680
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: &74083210 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.5.2
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *74083210
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rcov
|
60
|
+
requirement: &74082940 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *74082940
|
69
|
+
description: Vkontakte authorization for authlogic
|
70
|
+
email: mydeeptown@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files:
|
74
|
+
- LICENSE.txt
|
75
|
+
- README.rdoc
|
76
|
+
files:
|
77
|
+
- .document
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.rdoc
|
82
|
+
- Rakefile
|
83
|
+
- VERSION
|
84
|
+
- aavkontakte-rails3.gemspec
|
85
|
+
- init.rb
|
86
|
+
- lib/aavkontakte-rails3.rb
|
87
|
+
- lib/vkontakte.js
|
88
|
+
- lib/vkontakte/authentication.rb
|
89
|
+
- lib/vkontakte/helper.rb
|
90
|
+
- lib/vkontakte/session.rb
|
91
|
+
- lib/vkontakte/auth_success.rb
|
92
|
+
homepage: http://github.com/blimp666/aavkontakte-rails3
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project: aavkontakte-rails3
|
113
|
+
rubygems_version: 1.8.16
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: Yet Another Authlogic Vkontakte Authorization for Rails3
|
117
|
+
test_files: []
|