Permalink
Browse files

* deleted configuration

* fixed helper
* adds desc to readme
  • Loading branch information...
1 parent c80b22e commit 2c92dd37a8999670acb4e65df8b3fabb17878b54 Dmitry Vorotilin committed Jan 31, 2011
Showing with 60 additions and 46 deletions.
  1. +27 −14 README.rdoc
  2. +2 −1 lib/aavkontakte.rb
  3. +19 −2 lib/vkontakte/authentication.rb
  4. +0 −19 lib/vkontakte/configuration.rb
  5. +6 −2 lib/vkontakte/helper.rb
  6. +6 −8 lib/vkontakte/session.rb
View
@@ -1,6 +1,6 @@
= aavkontakte
-Another authlogic vkontakte authorization
+This is beta version of aavkontakte - another authlogic vkontakte authorization. It provides vkontakte authorization to authlogic.
== Install
@@ -10,20 +10,35 @@ gem install aavkontakte
In environment.rb:
-config.gem 'aavkontakte', :version => '=0.1.0'
+ config.gem 'aavkontakte', :version => '>=0.1.0'
-VK_APP_ID = 'vk_app_id'
-VK_APP_PASSWORD = 'vk_app_password'
-VK_APP_COOKIE = 'vk_app_#{VK_APP_ID}'
+* User model
+
+Add following line:
+
+ acts_as_authentic do |c|
+ c.vkontakte_enabled = { :vk_app_id => "id", :vk_app_password => "password" }
+ end
* Migrations
-TODO: migrations
+Create migration:
+
+ class AddVkontakteIdToUser < ActiveRecord::Migration
+ def self.up
+ add_column :users, :vk_id, :string
+ end
+
+ def self.down
+ remove_column :users, :vk_id
+ end
+ end
* Layout
In the body section:
-<%= init_vkontakte %>
+
+ <%= init_vkontakte %>
* Javascripts
@@ -32,16 +47,14 @@ After first start your application, it's copying vkontakte.js to your javascript
* Link
put link somewhere, where you want
-<%= vkontakte_login_link "Войти через vkontakte" %>
-
-* User session
-def create
- @user_session = UserSession.new(params[:status] ? cookies : params[:user_session])
- ...
+ <%= vkontakte_login_link "Login with vkontakte" %>
+* User session
-That's all
+ def create
+ @user_session = UserSession.new(params[:status] ? cookies : params[:user_session])
+ ...
== Contributing to aavkontakte
View
@@ -1,8 +1,9 @@
-require "vkontakte/configuration"
+require "md5"
require "vkontakte/authentication"
require "vkontakte/session"
require "vkontakte/helper"
+# TODO: may be rake task..
unless File.exists?("#{RAILS_ROOT}/public/javascripts/vkontakte.js")
require "ftools"
File.copy("#{File.dirname(__FILE__)}/vkontakte.js", "#{RAILS_ROOT}/public/javascripts")
@@ -2,7 +2,7 @@ module VkontakteAuthentication
module ActsAsAuthentic
def self.included(klass)
klass.class_eval do
- extend Configuration, Config
+ extend Config
if defined? AuthlogicRpx
remove_acts_as_authentic_module AuthlogicRpx::ActsAsAuthentic::Methods
add_acts_as_authentic_module Methods, :prepend
@@ -15,13 +15,30 @@ def self.included(klass)
module Config
def vkontakte_enabled(vk_app_data = {})
- value = true if vk_app_data.present? && vk_app_data[:vk_app_id] && vk_app_data[:vk_app_password]
+ value = vk_app_data.present? && vk_app_data[:vk_app_id] && vk_app_data[:vk_app_password] ? true : false
if vkontakte_enabled_value(value)
vk_app_id vk_app_data[:vk_app_id]
vk_app_password vk_app_data[:vk_app_password]
end
end
alias_method :vkontakte_enabled=, :vkontakte_enabled
+
+ def vkontakte_enabled_value(value = nil)
+ rw_config(:vkontakte_enabled, value, false)
+ end
+
+ def vk_app_id(value = nil)
+ rw_config(:vk_app_id, value)
+ ActiveRecord::Base.send(:rw_config, :vk_app_id, value)
+ end
+
+ def vk_app_password(value = nil)
+ rw_config(:vk_app_password, value)
+ end
+
+ def vk_app_cookie
+ rw_config(:vk_app_cookie, nil) || rw_config(:vk_app_cookie, "vk_app_#{vk_app_id}") if vk_app_id
+ end
end
module Methods
@@ -1,19 +0,0 @@
-module VkontakteAuthentication
- module Configuration
- def vkontakte_enabled_value(value = nil)
- rw_config(:vkontakte_enabled, value, false)
- end
-
- def vk_app_id(value = nil)
- rw_config(:vk_app_id, value)
- end
-
- def vk_app_password(value = nil)
- rw_config(:vk_app_password, value)
- end
-
- def vk_app_cookie
- rw_config(:vk_app_cookie, nil) || rw_config(:vk_app_cookie, "vk_app_#{vk_app_id}") if vk_app_id
- end
- end
-end
View
@@ -1,7 +1,7 @@
module VkontakteAuthentication
module Helper
def init_vkontakte
- vkontakte_div + vkontakte_init if User.vkontakte_enabled_value
+ vkontakte_div + vkontakte_init
end
def vkontakte_login_link(name, url = user_sessions_path, html_options = {})
@@ -16,7 +16,11 @@ def vkontakte_div
end
def vkontakte_init
- javascript_include_tag("vkontakte") + javascript_tag("vkInit(#{User.vk_app_id});")
+ javascript_include_tag("vkontakte") + javascript_tag("vkInit(#{vk_app_id});")
+ end
+
+ def vk_app_id
+ ActiveRecord::Base.send(:rw_config, :vk_app_id, nil)
end
end
end
View
@@ -1,10 +1,8 @@
-require "md5"
-
module VkontakteAuthentication
module Session
def self.included(klass)
klass.class_eval do
- extend Config, Configuration
+ extend Config
include InstanceMethods
after_destroy :destroy_vkontakte_cookies
validate :validate_by_vk_cookie, :if => :authenticating_with_vkontakte?
@@ -31,17 +29,17 @@ module InstanceMethods
def credentials=(value)
super
cookies = value.is_a?(Array) ? value.first : value
- if self.class.vkontakte_enabled_value && cookies && cookies[self.class.vk_app_cookie]
- @vk_cookies = CGI::parse(cookies[self.class.vk_app_cookie])
+ if record_class.vkontakte_enabled_value && cookies && cookies[record_class.vk_app_cookie]
+ @vk_cookies = CGI::parse(cookies[record_class.vk_app_cookie])
end
end
def authenticating_with_vkontakte?
- self.class.vkontakte_enabled_value && @vk_cookies
+ record_class.vkontakte_enabled_value && @vk_cookies
end
def validate_by_vk_cookie
- result = "expire=%smid=%ssecret=%ssid=%s%s" % [@vk_cookies['expire'], @vk_cookies['mid'], @vk_cookies['secret'], @vk_cookies['sid'], self.class.vk_app_password]
+ result = "expire=%smid=%ssecret=%ssid=%s%s" % [@vk_cookies['expire'], @vk_cookies['mid'], @vk_cookies['secret'], @vk_cookies['sid'], record_class.vk_app_password]
if MD5.md5(result).to_s == @vk_cookies['sig'].to_s
raise(NotInitializedError, "You must define vk_id column in your User model") unless record_class.respond_to? find_by_vk_id_method
mid_cookie = @vk_cookies['mid'].first
@@ -69,7 +67,7 @@ def record_class
end
def destroy_vkontakte_cookies
- controller.cookies.delete vk_app_cookie
+ controller.cookies.delete record_class.vk_app_cookie
end
end
end

0 comments on commit 2c92dd3

Please sign in to comment.