Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Regenerate gemspec for version 0.1.0

  • Loading branch information...
commit 3ee91bc645697528a48d1cc49fa0b2207829eed2 1 parent 06facdf
@route authored
View
2  .gitignore
@@ -40,3 +40,5 @@ pkg
#
# For vim:
#*.swp
+
+.idea
View
20 Gemfile.lock
@@ -0,0 +1,20 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ git (1.2.5)
+ jeweler (1.5.2)
+ bundler (~> 1.0.0)
+ git (>= 1.2.5)
+ rake
+ rake (0.8.7)
+ rcov (0.9.9)
+ shoulda (2.11.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (~> 1.0.0)
+ jeweler (~> 1.5.2)
+ rcov
+ shoulda
View
44 README.rdoc
@@ -1,6 +1,47 @@
= aavkontakte
-Description goes here.
+Another authlogic vkontakte authorization
+
+== Install
+
+gem install aavkontakte
+
+* Environment
+
+In environment.rb:
+
+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}'
+
+* Migrations
+
+TODO: migrations
+
+* Layout
+
+In the body section:
+<%= init_vkontakte %>
+
+* Javascripts
+
+After first start your application, it's copying vkontakte.js to your javascripts directory or if it doesn't exist.
+
+* 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])
+ ...
+
+
+That's all
== Contributing to aavkontakte
@@ -16,4 +57,3 @@ Description goes here.
Copyright (c) 2011 Dmitry Vorotilin. See LICENSE.txt for
further details.
-
View
4 Rakefile
@@ -15,8 +15,8 @@ Jeweler::Tasks.new do |gem|
gem.name = "aavkontakte"
gem.homepage = "http://github.com/deeptown/aavkontakte"
gem.license = "MIT"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{Another Authlogic Vkontakte}
+ gem.description = %Q{Another Authlogic Vkontakte}
gem.email = "mydeeptown@gmail.com"
gem.authors = ["Dmitry Vorotilin"]
# Include your dependencies below. Runtime dependencies are required when using your gem,
View
68 aavkontakte.gemspec
@@ -0,0 +1,68 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{aavkontakte}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Dmitry Vorotilin"]
+ s.date = %q{2011-01-30}
+ s.description = %q{Another Authlogic Vkontakte}
+ s.email = %q{mydeeptown@gmail.com}
+ s.extra_rdoc_files = [
+ "LICENSE.txt",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ "Gemfile",
+ "Gemfile.lock",
+ "LICENSE.txt",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "init.rb",
+ "lib/aavkontakte.rb",
+ "lib/vkontakte.js",
+ "lib/vkontakte/authentication.rb",
+ "lib/vkontakte/helper.rb",
+ "lib/vkontakte/session.rb",
+ "test/helper.rb",
+ "test/test_aavkontakte.rb"
+ ]
+ s.homepage = %q{http://github.com/deeptown/aavkontakte}
+ s.licenses = ["MIT"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{Another Authlogic Vkontakte}
+ s.test_files = [
+ "test/helper.rb",
+ "test/test_aavkontakte.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_development_dependency(%q<rcov>, [">= 0"])
+ else
+ s.add_dependency(%q<shoulda>, [">= 0"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_dependency(%q<rcov>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<shoulda>, [">= 0"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
+ s.add_dependency(%q<rcov>, [">= 0"])
+ end
+end
+
View
1  init.rb
@@ -0,0 +1 @@
+require "aavkontakte"
View
12 lib/aavkontakte.rb
@@ -0,0 +1,12 @@
+require "vkontakte/authentication"
+require "vkontakte/session"
+require "vkontakte/helper"
+
+unless File.exists?("#{RAILS_ROOT}/public/javascripts/vkontakte.js")
+ require "ftools"
+ File.copy("#{File.dirname(__FILE__)}/vkontakte.js", "#{RAILS_ROOT}/public/javascripts")
+end
+
+ActiveRecord::Base.send(:include, VkontakteAuthentication::ActsAsAuthentic)
+Authlogic::Session::Base.send(:include, VkontakteAuthentication::Session)
+ActionController::Base.helper VkontakteAuthentication::Helper
View
69 lib/vkontakte.js
@@ -0,0 +1,69 @@
+function vkInit(id) {
+ window.vkAsyncInit = function() {
+ VK.init({
+ apiId: id,
+ nameTransportPath: '/xd_receiver.html'
+ });
+ };
+
+ setTimeout(function() {
+ var el = document.createElement("script");
+ el.type = "text/javascript";
+ el.src = "http://vkontakte.ru/js/api/openapi.js";
+ el.async = true;
+ document.getElementById("vk_api_transport").appendChild(el);
+ }, 0);
+}
+
+function vkLogin(options) {
+ VK.Auth.login(function(response) {
+ if(response.session) {
+ /* User is logged in */
+ // need ajax request? $.post(url, response, "script" );
+ post(options['url'], response, options['authenticity_token'], options['session_name'], options['session_key']);
+ if (response.settings){ /* Selected user access settings */ }
+ }
+ else { /* User pressed the Cancel button */ }
+ }, VK.access.FRIENDS | VK.access.WIKI);
+ return false;
+}
+
+function post(url, params, token, session_name, session_key) {
+ var method = "post";
+
+ var form = document.createElement("form");
+ form.setAttribute("method", method);
+ form.setAttribute("action", url);
+
+ var authField = document.createElement("input");
+ authField.setAttribute("type", "hidden");
+ authField.setAttribute("name", "authenticity_token");
+ authField.setAttribute("value", token);
+ form.appendChild(authField);
+
+ var sessionField = document.createElement("input");
+ sessionField.setAttribute("type", "hidden");
+ sessionField.setAttribute("name", session_name);
+ sessionField.setAttribute("value", session_key);
+ form.appendChild(sessionField);
+
+ // recursively adds nested inputs to form
+ var add_inputs = function (form, object, parent_name) {
+ parent_name = parent_name || "";
+ for (var key in object) {
+ var hiddenField = document.createElement("input");
+ hiddenField.setAttribute("type", "hidden");
+ if (typeof(object[key]) == "object") add_inputs(form, object[key], parent_name ? parent_name + "[" + key + "]" : key);
+ else {
+ hiddenField.setAttribute("name", parent_name ? parent_name + "[" + key + "]" : key);
+ hiddenField.setAttribute("value", object[key]);
+ }
+ form.appendChild(hiddenField);
+ }
+ }
+
+ add_inputs(form, params);
+
+ document.body.appendChild(form); // Not entirely sure if this is necessary
+ form.submit();
+}
View
59 lib/vkontakte/authentication.rb
@@ -0,0 +1,59 @@
+module VkontakteAuthentication
+ module ActsAsAuthentic
+ def self.included(klass)
+ klass.class_eval do
+ extend Config
+ if defined? AuthlogicRpx::ActsAsAuthentic::Methods
+ remove_acts_as_authentic_module AuthlogicRpx::ActsAsAuthentic::Methods
+ add_acts_as_authentic_module Methods, :prepend
+ add_acts_as_authentic_module AuthlogicRpx::ActsAsAuthentic::Methods
+ else
+ add_acts_as_authentic_module Methods, :prepend
+ end
+ end
+ end
+
+ class NotInitializedError < StandardError
+ end
+
+ module Config
+ def vkontakte_enabled(vk_app_data = {})
+ value = vk_app_data.present? ? vk_app_data[:vk_app_id].present? && vk_app_data[:vk_app_password] : false
+ vkontakte_enabled_value(value)
+ if vkontakte_enabled_value
+ rw_config(:vk_app_id, vk_app_data[:vk_app_id])
+ rw_config(:vk_app_password, vk_app_data[:vk_app_password])
+ rw_config(:vk_app_cookie, "vk_app_#{vk_app_data[:vk_app_id]}")
+
+ message = "Set vk_app_id and vk_app_password in your environment."
+ raise NotInitializedError, message if vkontakte_enabled_value && VK_APP_ID.blank? && VK_APP_PASSWORD.blank? && VK_APP_COOKIE.blank?
+ end
+ end
+ alias_method :vkontakte_enabled=,:vkontakte_enabled
+
+ def vkontakte_enabled_value(value = nil)
+ rw_config(:vkontakte_enabled, value, false)
+ end
+ end
+
+ module Methods
+ def self.included(klass)
+ klass.class_eval do
+ validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_not_vkontakte?)
+ validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_not_vkontakte?)
+ validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_not_vkontakte?)
+ end
+ end
+
+ private
+ def validate_password_not_vkontakte?
+ !authenticating_with_vkontakte? && (defined? AuthlogicRpx::ActsAsAuthentic::Methods ? !using_rpx? : true) && require_password?
+ end
+
+ def authenticating_with_vkontakte?
+ vk_id.present?
+ end
+ end
+
+ end
+end
View
22 lib/vkontakte/helper.rb
@@ -0,0 +1,22 @@
+module VkontakteAuthentication
+ module Helper
+ def init_vkontakte
+ vkontakte_div + vkontakte_init
+ end
+
+ def vkontakte_login_link(name, url = user_sessions_path, html_options = {})
+ authenticity_token = protect_against_forgery? ? form_authenticity_token : ''
+ options = "{ url: '#{url}', authenticity_token: '#{authenticity_token}', session_name: '#{session_key_name}', session_key: '#{cookies[session_key_name]}' }"
+ link_to name, "#", html_options.merge(:onclick => "vkLogin(#{options});")
+ end
+
+ private
+ def vkontakte_div
+ content_tag(:div, "", :id => "vk_api_transport")
+ end
+
+ def vkontakte_init
+ javascript_include_tag("vkontakte") + javascript_tag("vkInit(#{VK_APP_ID});") if ActiveRecord::Base.vkontakte_enabled_value
+ end
+ end
+end
View
79 lib/vkontakte/session.rb
@@ -0,0 +1,79 @@
+require "md5"
+
+module VkontakteAuthentication
+ module Session
+
+ def self.included(klass)
+ klass.class_eval do
+ extend Config
+ include InstanceMethods
+
+ after_destroy :destroy_vkontakte_cookies
+ validate :validate_by_vk_cookie, :if => :authenticating_with_vkontakte?
+ end
+ end
+
+ class NotInitializedError < StandardError
+ end
+
+ module Config
+ def find_by_vk_id_method(value = nil)
+ rw_config(:find_by_vk_id_method, value, "find_by_vk_id")
+ end
+ alias_method :find_by_vk_id_method=, :find_by_vk_id_method
+
+ def vk_id_field(value = nil)
+ rw_config(:vk_id_field, value, :vk_id)
+ end
+ alias_method :vk_id_field=, :vk_id_field
+ end
+
+ module InstanceMethods
+ private
+ def credentials=(value)
+ super
+ cookies = value.is_a?(Array) ? value.first : value
+ if record_class.vkontakte_enabled_value && cookies && cookies[VK_APP_COOKIE]
+ @vk_cookies = CGI::parse(cookies[VK_APP_COOKIE])
+ end
+ end
+
+ def authenticating_with_vkontakte?
+ 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'], 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
+ possible_record = search_for_record(find_by_vk_id_method, mid_cookie)
+ if possible_record.nil?
+ possible_record = record_class.new
+ possible_record.send "#{vk_id_field}=", mid_cookie
+ possible_record.send :persistence_token=, Authlogic::Random.hex_token if possible_record.respond_to? :persistence_token=
+ possible_record.send :save, false
+ end
+ self.attempted_record = possible_record
+ end
+ end
+
+ def find_by_vk_id_method
+ self.class.find_by_vk_id_method
+ end
+
+ def vk_id_field
+ self.class.vk_id_field
+ end
+
+ def record_class
+ self.class.klass
+ end
+
+ def destroy_vkontakte_cookies
+ controller.cookies.delete VK_APP_COOKIE
+ end
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.