authlogic_vkontakte 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +11 -0
- data/README.rdoc +50 -0
- data/Rakefile +14 -0
- data/authlogic_vkontakte.gemspec +30 -0
- data/init.rb +3 -0
- data/js/vkontakte.js +36 -0
- data/lib/profile_loader.rb +42 -0
- data/lib/settings.rb +21 -0
- data/lib/tasks/vkontakte.rake +22 -0
- data/lib/vkontakte_authentication.rb +94 -0
- data/lib/vkontakte_helper.rb +20 -0
- metadata +88 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Bind vkontakte authentication to your authlogic project.
|
2
|
+
== Install
|
3
|
+
gem install authlogic_vkontakte
|
4
|
+
|
5
|
+
== Setup
|
6
|
+
Run rake task to init resources (javascripts and other)
|
7
|
+
rake vkontakte:init
|
8
|
+
|
9
|
+
Edit vkontakte.yml file in your project directory
|
10
|
+
vk_app_id: <YOUR_APP_ID>
|
11
|
+
vk_app_password: <YOUR_PASSWORD>
|
12
|
+
|
13
|
+
Create migration like this one:
|
14
|
+
class AddVkontakteIdToUser < ActiveRecord::Migration
|
15
|
+
def self.up
|
16
|
+
add_column :users, :vk_id, :string
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.down
|
20
|
+
remove_column :users, :vk_id
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
Add "vontakte_authentication" to your User model
|
25
|
+
class User < ActiveRecord::Base
|
26
|
+
vkontakte_authentication
|
27
|
+
end
|
28
|
+
And a little requirement now - jQuery (see vkontakte.js) for posting to "create" action of UserSessionController. But
|
29
|
+
you can use Ajax.Request if you prefer Prototype. And so, TODO is to create version independent of the javascript framework.
|
30
|
+
|
31
|
+
== Usage
|
32
|
+
To include javascript file in layout:
|
33
|
+
<%= vkontakte_javascript_include_tag %>
|
34
|
+
|
35
|
+
Creating "vk_api_transport" 'div' and initializing OpenAPI:
|
36
|
+
<%= vkontakte_init_tag %>
|
37
|
+
|
38
|
+
To add Vkontakte button to your page, simply write this in view:
|
39
|
+
<%= vk_login_button %>
|
40
|
+
|
41
|
+
or, in haml
|
42
|
+
= vk_login_button
|
43
|
+
In your controller (UserSession):
|
44
|
+
def create
|
45
|
+
@user_session = UserSession.new(params[:status] ? cookies : params[:user_session])
|
46
|
+
if @user_session.save
|
47
|
+
..
|
48
|
+
...and you're in Vkontakte!
|
49
|
+
|
50
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rake"
|
3
|
+
require "echoe"
|
4
|
+
|
5
|
+
Echoe.new("authlogic_vkontakte", "0.1.0") do |p|
|
6
|
+
p.description= "Bind vkontakte authentication to your authlogic project"
|
7
|
+
p.url= "http://github.com/GarPit/authlogic_vkontakte"
|
8
|
+
p.author= "Igor Petrov"
|
9
|
+
p.email= "garik.piton@gmail.com"
|
10
|
+
p.ignore_pattern= ["tmp/*", "script/*", ".idea/*"]
|
11
|
+
p.development_dependencies= []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{authlogic_vkontakte}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Igor Petrov"]
|
9
|
+
s.date = %q{2010-10-15}
|
10
|
+
s.description = %q{Bind vkontakte authentication to your authlogic project}
|
11
|
+
s.email = %q{garik.piton@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/profile_loader.rb", "lib/settings.rb", "lib/tasks/vkontakte.rake", "lib/vkontakte_authentication.rb", "lib/vkontakte_helper.rb"]
|
13
|
+
s.files = ["README.rdoc", "Rakefile", "authlogic_vkontakte.gemspec", "init.rb", "js/vkontakte.js", "lib/profile_loader.rb", "lib/settings.rb", "lib/tasks/vkontakte.rake", "lib/vkontakte_authentication.rb", "lib/vkontakte_helper.rb", "Manifest"]
|
14
|
+
s.homepage = %q{http://github.com/GarPit/authlogic_vkontakte}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Authlogic_vkontakte", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{authlogic_vkontakte}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{Bind vkontakte authentication to your authlogic project}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/init.rb
ADDED
data/js/vkontakte.js
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
function init(id)
|
2
|
+
{
|
3
|
+
window.vkAsyncInit = function() {
|
4
|
+
VK.init({
|
5
|
+
apiId: id,
|
6
|
+
nameTransportPath: '/xd_receiver.html'
|
7
|
+
});
|
8
|
+
VK.UI.button('vk_login');
|
9
|
+
};
|
10
|
+
(function() {
|
11
|
+
var el = document.createElement("script");
|
12
|
+
el.type = "text/javascript";
|
13
|
+
el.charset = "windows-1251";
|
14
|
+
el.src = "http://vkontakte.ru/js/api/openapi.js";
|
15
|
+
el.async = true;
|
16
|
+
document.getElementById("vk_api_transport").appendChild(el);
|
17
|
+
}());
|
18
|
+
}
|
19
|
+
|
20
|
+
|
21
|
+
function doLogin() {
|
22
|
+
VK.Auth.login(
|
23
|
+
loginInternal,
|
24
|
+
VK.access.FRIENDS | VK.access.WIKI
|
25
|
+
);
|
26
|
+
|
27
|
+
}
|
28
|
+
function doLogout() {
|
29
|
+
VK.Auth.logout(logoutOpenAPI);
|
30
|
+
}
|
31
|
+
|
32
|
+
function loginInternal(response)
|
33
|
+
{
|
34
|
+
$.post("/user_session", response, "script" );
|
35
|
+
return false;
|
36
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module VkontakteAuthentication
|
2
|
+
|
3
|
+
class YmlLoadError < StandardError; end
|
4
|
+
class NoSettingsError < StandardError; end
|
5
|
+
|
6
|
+
class ProfileLoader
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@vkontakte_yml = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def vkontakte_yml_defined?
|
13
|
+
vkontakte_file && File.exist?(vkontakte_file)
|
14
|
+
end
|
15
|
+
|
16
|
+
def param(key)
|
17
|
+
vkontakte_yml[key.to_s]
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def vkontakte_yml
|
23
|
+
return @vkontakte_yml if @vkontakte_yml
|
24
|
+
unless vkontakte_yml_defined?
|
25
|
+
raise(NoSettingsError,"vkontakte.yml was not found.\n")
|
26
|
+
end
|
27
|
+
|
28
|
+
require "yaml"
|
29
|
+
|
30
|
+
begin
|
31
|
+
@vkontakte_yml = YAML.load_file(vkontakte_file)
|
32
|
+
rescue StandardError => e
|
33
|
+
raise(YmlLoadError,"vkontakte.yml was found, but could not be parsed.\n")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def vkontakte_file
|
38
|
+
@vkontakte_file ||= Dir.glob('{,.config/,config/}vkontakte{.yml,.yaml}').first
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
data/lib/settings.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module VkontakteAuthentication
|
2
|
+
|
3
|
+
module Settings
|
4
|
+
def profile_loader
|
5
|
+
@profile_loader ||= ProfileLoader.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def vk_app_id
|
9
|
+
@vk_app_id ||= profile_loader.param(:vk_app_id)
|
10
|
+
end
|
11
|
+
|
12
|
+
def vk_app_password
|
13
|
+
@vk_app_password ||= profile_loader.param(:vk_app_password)
|
14
|
+
end
|
15
|
+
|
16
|
+
def vk_app_cookie
|
17
|
+
'vk_app_' + vk_app_id.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
namespace :vkontakte do
|
2
|
+
desc "Init resources for plugin"
|
3
|
+
task :init => [:yml, :javascripts]
|
4
|
+
|
5
|
+
desc "Copy vkontakte.yml to current rails application"
|
6
|
+
task :yml do
|
7
|
+
puts "Copying YAML file..."
|
8
|
+
project_dir = RAILS_ROOT
|
9
|
+
yml = File.join(File.dirname(__FILE__), '..', '..', 'vkontakte.yml')
|
10
|
+
FileUtils.cp(yml, project_dir)
|
11
|
+
puts "vkontakte.yml copied successfully!"
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Copy javascript to current rails application"
|
15
|
+
task :javascripts do
|
16
|
+
puts "Copying javascript files..."
|
17
|
+
project_dir = RAILS_ROOT + '/public/javascripts/'
|
18
|
+
scripts = Dir[File.join(File.dirname(__FILE__), '..', '..', '/js/', '*.js')]
|
19
|
+
FileUtils.cp(scripts, project_dir)
|
20
|
+
puts "Files copied successfully!"
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require "profile_loader"
|
2
|
+
require "settings"
|
3
|
+
require "md5"
|
4
|
+
|
5
|
+
module VkontakteAuthentication
|
6
|
+
|
7
|
+
class NotInitializedError < StandardError;
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
include Settings
|
12
|
+
|
13
|
+
def find_by_vk_id_method(value = nil)
|
14
|
+
rw_config(:find_by_vk_id_method, value, "find_by_vk_id")
|
15
|
+
end
|
16
|
+
alias_method :find_by_vk_id_method=, :find_by_vk_id_method
|
17
|
+
|
18
|
+
def vk_id_field(value = nil)
|
19
|
+
rw_config(:vk_id_field, value, :vk_id)
|
20
|
+
end
|
21
|
+
alias_method :vk_id_field=, :vk_id_field
|
22
|
+
|
23
|
+
def vkontakte_authentication
|
24
|
+
raise(NotInitializedError, "create and initialize vkontakte.yml") unless profile_loader.vkontakte_yml_defined? && vk_app_id && vk_app_password
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def credentials=(value)
|
34
|
+
super
|
35
|
+
cookies = value.is_a?(Array) ? value.first : value
|
36
|
+
if cookies.is_a?(ActionController::CookieJar) && cookies[vk_app_cookie]
|
37
|
+
@vk_cookies = CGI::parse(cookies[vk_app_cookie])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def authenticating_with_vkontakte?
|
42
|
+
@vk_cookies
|
43
|
+
end
|
44
|
+
|
45
|
+
def validate_by_vk_cookie
|
46
|
+
result = "expire=%smid=%ssecret=%ssid=%s%s" % [@vk_cookies['expire'], @vk_cookies['mid'], @vk_cookies['secret'], @vk_cookies['sid'], self.class.vk_app_password]
|
47
|
+
if MD5.md5(result).to_s == @vk_cookies['sig'].to_s
|
48
|
+
raise(NotInitializedError, "you must define vk_id column in your User model") unless record_class.respond_to? find_by_vk_id_method
|
49
|
+
mid_cookie = @vk_cookies['mid'].first
|
50
|
+
possible_record = search_for_record(find_by_vk_id_method, mid_cookie)
|
51
|
+
if possible_record.nil?
|
52
|
+
possible_record = record_class.new(vk_id_field => mid_cookie)
|
53
|
+
possible_record.send :persistence_token=, Authlogic::Random.hex_token if possible_record.respond_to? :persistence_token=
|
54
|
+
possible_record.send :save, false
|
55
|
+
end
|
56
|
+
self.attempted_record = possible_record
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def vk_app_cookie
|
61
|
+
self.class.vk_app_cookie
|
62
|
+
end
|
63
|
+
|
64
|
+
def find_by_vk_id_method
|
65
|
+
self.class.find_by_vk_id_method
|
66
|
+
end
|
67
|
+
|
68
|
+
def vk_id_field
|
69
|
+
self.class.vk_id_field
|
70
|
+
end
|
71
|
+
|
72
|
+
def record_class
|
73
|
+
self.class.klass
|
74
|
+
end
|
75
|
+
|
76
|
+
def destroy_vkontakte_cookies
|
77
|
+
controller.cookies.delete vk_app_cookie
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
ActiveRecord::Base.class_eval do
|
85
|
+
extend VkontakteAuthentication::ClassMethods
|
86
|
+
attr_accessible vk_id_field
|
87
|
+
end
|
88
|
+
|
89
|
+
Authlogic::Session::Base.class_eval do
|
90
|
+
include VkontakteAuthentication::InstanceMethods
|
91
|
+
extend VkontakteAuthentication::ClassMethods
|
92
|
+
after_destroy :destroy_vkontakte_cookies
|
93
|
+
validate :validate_by_vk_cookie, :if => :authenticating_with_vkontakte?
|
94
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "settings"
|
2
|
+
|
3
|
+
module VkontakteAuthentication
|
4
|
+
module Helper
|
5
|
+
include Settings
|
6
|
+
|
7
|
+
def vkontakte_init_tag
|
8
|
+
content_tag(:div, "", :id => "vk_api_transport") +
|
9
|
+
content_tag(:script, "init(#{vk_app_id});")
|
10
|
+
end
|
11
|
+
|
12
|
+
def vkontakte_javascript_include_tag
|
13
|
+
javascript_include_tag "vkontakte"
|
14
|
+
end
|
15
|
+
|
16
|
+
def vk_login_button
|
17
|
+
content_tag(:div, "", {:id => "vk_login", :onclick => "doLogin(); return false;"})
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: authlogic_vkontakte
|
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
|
+
- Igor Petrov
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-15 00:00:00 +04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Bind vkontakte authentication to your authlogic project
|
23
|
+
email: garik.piton@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
- lib/profile_loader.rb
|
31
|
+
- lib/settings.rb
|
32
|
+
- lib/tasks/vkontakte.rake
|
33
|
+
- lib/vkontakte_authentication.rb
|
34
|
+
- lib/vkontakte_helper.rb
|
35
|
+
files:
|
36
|
+
- README.rdoc
|
37
|
+
- Rakefile
|
38
|
+
- authlogic_vkontakte.gemspec
|
39
|
+
- init.rb
|
40
|
+
- js/vkontakte.js
|
41
|
+
- lib/profile_loader.rb
|
42
|
+
- lib/settings.rb
|
43
|
+
- lib/tasks/vkontakte.rake
|
44
|
+
- lib/vkontakte_authentication.rb
|
45
|
+
- lib/vkontakte_helper.rb
|
46
|
+
- Manifest
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://github.com/GarPit/authlogic_vkontakte
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --line-numbers
|
54
|
+
- --inline-source
|
55
|
+
- --title
|
56
|
+
- Authlogic_vkontakte
|
57
|
+
- --main
|
58
|
+
- README.rdoc
|
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: 11
|
76
|
+
segments:
|
77
|
+
- 1
|
78
|
+
- 2
|
79
|
+
version: "1.2"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project: authlogic_vkontakte
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Bind vkontakte authentication to your authlogic project
|
87
|
+
test_files: []
|
88
|
+
|