oa-mailru_alexandrov 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Alexander Gorkunov, Igor Alexandrov
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,81 @@
1
+ = oa-mailru_alexandrov
2
+
3
+ Auhtorization with Mail.RU extension for OmniAuth
4
+ Based on Alexander Gorkunov oa-mailru. Added JQuery magic to Mail.Ru intialization script.
5
+
6
+ == Installation
7
+
8
+ gem install oa-mailru_alexandrov
9
+
10
+ Add to environment.rb
11
+
12
+ config.gem 'oa-mailru_alexandrov', :lib => 'oa-mailru'
13
+
14
+ Add to config/initializers/omniauth.rb:
15
+
16
+ #for rails3 projects
17
+ Rails.application.config.middleware.use OmniAuth::Builder do
18
+ provider :mail_ru_api, <Application ID>, <Private Key>
19
+ end
20
+ #for rails2 projects
21
+ ActionController::Dispatcher.middleware.use "OmniAuth::Builder" do
22
+ provider :mail_ru_api, <Application ID>, <Private Key>
23
+ end
24
+
25
+ If omniauth routes doesn't exist add them to config/routes.rb (e.g.):
26
+
27
+ #rails3
28
+ match '/auth/:provider/callback' => 'authentications#create'
29
+ #rails2
30
+ map.auth_callback '/auth/:provider/callback', :controller => :authentications :action => :create
31
+
32
+ Then use special helper method for generate mail.ru button:
33
+
34
+ <%= raw mailru_login_button %>
35
+
36
+ or if you want use custom button then use:
37
+
38
+ <%= raw mailru_login_button("<a href='#' onclick='mailruLogin.login();'>Mail.RU Login</a>") %>
39
+
40
+ After click on the button popup window with mail.ru auth-page will be shown and after user was signed in and added
41
+ permissions for the application then user-data will be transfered with POST-request to /auth/mailru/callback path.
42
+ In action with this path you can use params from request["omniauth.auth"] which body look like:
43
+
44
+ {
45
+ 'uid' => '1234567890', # user Id from mail.ru
46
+ 'provider' => 'mailru',
47
+ 'user_info' => {
48
+ 'nick' => 'igor.alexandrov',
49
+ 'email' => 'igor-alexandrov@inbox.ru'
50
+ 'name' => 'Igor Alexandrov',
51
+ 'first_name' => 'Igor',
52
+ 'last_name' => 'Alexandrov',
53
+ 'picture' => 'userpic url here'
54
+ 'country' => 'Russia',
55
+ 'town' => 'Tver'
56
+ },
57
+ 'extra' => {'user_hash' => 'some additional data here'}
58
+ }
59
+
60
+ == Debugging Information
61
+
62
+ Mail.RU API has some host name permission restrictions, so correct debugging session is available only on public server with
63
+ some host name (don't forget to put receiver.html to root directory of your site).
64
+
65
+ == Links
66
+
67
+ OmniAuth: http://github.com/intridea/omniauth
68
+
69
+ == Note on Patches/Pull Requests
70
+
71
+ * Fork the project.
72
+ * Make your feature addition or bug fix.
73
+ * Add tests for it. This is important so I don't break it in a
74
+ future version unintentionally.
75
+ * Commit, do not mess with rakefile, version, or history.
76
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
77
+ * Send me a pull request. Bonus points for topic branches.
78
+
79
+ == Copyright
80
+
81
+ Copyright (c) 2011 Alexander Gorkunov, Igor Alexandrov. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "oa-mailru_alexandrov"
8
+ gem.summary = %Q{OmniAuth extension for mail.ru authentication}
9
+ gem.description = %Q{OmniAuth extension for mail.ru authentication}
10
+ gem.email = "igor.alexandrov@gmail.com"
11
+ gem.homepage = "http://github.com/igor-alexandrov/oa-mailru"
12
+ gem.authors = ["Alexander Gorkunov", "Igor Alexandrov"]
13
+ # gem.rubyforge_project = "oa-mailru"
14
+ gem.add_dependency 'oa-core', '~>0.1.4'
15
+ gem.add_development_dependency "rspec", ">= 1.2.9"
16
+ gem.add_development_dependency "yard", ">= 0"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ Jeweler::RubyforgeTasks.new do |rubyforge|
21
+ rubyforge.doc_task = "yardoc"
22
+ end
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
25
+ end
26
+
27
+ require 'spec/rake/spectask'
28
+ Spec::Rake::SpecTask.new(:spec) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.spec_files = FileList['spec/**/*_spec.rb']
31
+ end
32
+
33
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
34
+ spec.libs << 'lib' << 'spec'
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :spec => :check_dependencies
40
+
41
+ task :default => :spec
42
+
43
+ begin
44
+ require 'yard'
45
+ YARD::Rake::YardocTask.new
46
+ rescue LoadError
47
+ task :yardoc do
48
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
49
+ end
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
data/lib/oa-mailru.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'omniauth/mailru'
2
+ if defined?(Rails)
3
+ ActionController::Base.helper OmniAuth::Strategies::MailRuApi::ViewHelper::PageHelper
4
+ end
@@ -0,0 +1,8 @@
1
+ require 'omniauth/core'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ autoload :MailRuApi, 'omniauth/strategies/mailru_api'
6
+ end
7
+ end
8
+
@@ -0,0 +1,121 @@
1
+ # coding: utf-8
2
+ module OmniAuth
3
+ module Strategies
4
+ class MailRuApi
5
+ class ViewHelper
6
+ module PageHelper
7
+ def mailru_login_page
8
+ mailru_header +
9
+ mailru_login_button +
10
+ mailru_footer
11
+ end
12
+
13
+ def mailru_header
14
+ <<-HEADER
15
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
16
+ <html xmlns="http://www.w3.org/1999/xhtml">
17
+ <head>
18
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
19
+ <title>Вход в Mail.RU</title>
20
+ </head>
21
+ <body>
22
+ HEADER
23
+ end
24
+
25
+ def mailru_login_button(control = nil)
26
+ unless control.present?
27
+ control = '<a class="mrc__connectButton">вход в mail.ru</a>'
28
+ init_control = "mailru.connect.initButton();"
29
+ end
30
+ <<-BUTTON
31
+ <script type="text/javascript" src="http://cdn.connect.mail.ru/js/loader.js"></script>
32
+ <script type="text/javascript">
33
+ mailruLogin = {
34
+ initialized: false,
35
+ initMailRuApi: function() {
36
+ mailru.connect.init('#{OmniAuth.config.mailru_app_id}', '#{OmniAuth.config.mailru_private_key}');
37
+ #{init_control}
38
+ mailru.events.listen(mailru.connect.events.login, function(session){
39
+ mailru.common.users.getInfo(function(result){
40
+ mailruLogin.redirectWithPost('#{OmniAuth.config.path_prefix}/mailru/callback', result[0]);
41
+ });
42
+ });
43
+ mailruLogin.initialized = true;
44
+ },
45
+ load: function() {
46
+ mailru.loader.require('api', function() {
47
+ mailruLogin.initMailRuApi();
48
+ });
49
+ },
50
+ login: function(){
51
+ if(!mailruLogin.initialized) mailruLogin.initMailRuApi();
52
+ mailru.connect.login();
53
+ },
54
+ redirectWithPost: function(url, data) {
55
+ data = data || {};
56
+ #{ if respond_to?(:form_authenticity_token)
57
+ "data['authenticity_token'] = '#{form_authenticity_token}'; var method = 'POST';"
58
+ else
59
+ "var method = 'GET';"
60
+ end
61
+ }
62
+ var form = document.createElement("form"), input;
63
+ form.setAttribute("action", url);
64
+ form.setAttribute("method", method);
65
+
66
+ for (var property in data) {
67
+ if (data.hasOwnProperty(property)) {
68
+ var value = data[property];
69
+ if(property == 'location') {
70
+ input = document.createElement("input");
71
+ input.setAttribute("type", "hidden");
72
+ input.setAttribute("name", 'country');
73
+ input.setAttribute("value", value['country']['name']);
74
+ form.appendChild(input);
75
+ input = document.createElement("input");
76
+ input.setAttribute("type", "hidden");
77
+ input.setAttribute("name", 'city');
78
+ input.setAttribute("value", value['city']['name']);
79
+ form.appendChild(input);
80
+ }
81
+ else if (value instanceof Array) {
82
+ for (var i = 0, l = value.length; i < l; i++) {
83
+ input = document.createElement("input");
84
+ input.setAttribute("type", "hidden");
85
+ input.setAttribute("name", property);
86
+ input.setAttribute("value", value[i]);
87
+ form.appendChild(input);
88
+ }
89
+ }
90
+ else {
91
+ input = document.createElement("input");
92
+ input.setAttribute("type", "hidden");
93
+ input.setAttribute("name", property);
94
+ input.setAttribute("value", value);
95
+ form.appendChild(input);
96
+ }
97
+ }
98
+ }
99
+ document.body.appendChild(form);
100
+ form.submit();
101
+ document.body.removeChild(form);
102
+ }
103
+ };
104
+ jQuery(document).ready(function(){
105
+ mailruLogin.load();
106
+ });
107
+ </script>
108
+ #{control}
109
+ BUTTON
110
+ end
111
+
112
+ def mailru_footer
113
+ <<-FOOTER
114
+ </body></html>
115
+ FOOTER
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,49 @@
1
+ require 'omniauth/mailru'
2
+ require 'omniauth/strategies/mailru_api/view_helper'
3
+
4
+ module OmniAuth
5
+ class Configuration
6
+ attr_accessor :mailru_app_id, :mailru_private_key
7
+ end
8
+ end
9
+
10
+ module OmniAuth
11
+ module Strategies
12
+ class MailRuApi
13
+ include OmniAuth::Strategy
14
+ include ViewHelper::PageHelper
15
+
16
+ def initialize(app, app_id, app_private_key, options = {})
17
+ OmniAuth.config.mailru_app_id = app_id
18
+ OmniAuth.config.mailru_private_key = app_private_key
19
+ @options = options
20
+ super(app, :mailru)
21
+ end
22
+
23
+ attr_reader :app_id
24
+ attr_reader :app_private_key
25
+
26
+ def request_phase
27
+ Rack::Response.new(mailru_login_page).finish
28
+ end
29
+
30
+ def auth_hash
31
+ OmniAuth::Utils.deep_merge(super(), {
32
+ 'uid' => request[:uid],
33
+ 'user_info' => {
34
+ 'nick' => request[:nick],
35
+ 'email' => request[:email],
36
+ 'name' => "#{request[:first_name]} #{request[:last_name]}",
37
+ 'first_name' => request[:first_name],
38
+ 'last_name' => request[:last_name],
39
+ 'picture' => request[:has_pic] ? request[:pic] : nil,
40
+ 'country' => request[:country],
41
+ 'town' => request[:city],
42
+ 'urls' => { 'Page' => request[:link] }
43
+ },
44
+ 'extra' => {'user_hash' => request}
45
+ })
46
+ end
47
+ end
48
+ end
49
+ end
Binary file
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "OaMailRu" do
4
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'oa-mailru'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oa-mailru_alexandrov
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - Alexander Gorkunov
14
+ - Igor Alexandrov
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-02-07 00:00:00 +03:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: oa-core
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 19
31
+ segments:
32
+ - 0
33
+ - 1
34
+ - 4
35
+ version: 0.1.4
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 13
47
+ segments:
48
+ - 1
49
+ - 2
50
+ - 9
51
+ version: 1.2.9
52
+ type: :development
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: yard
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ type: :development
67
+ version_requirements: *id003
68
+ description: OmniAuth extension for mail.ru authentication
69
+ email: igor.alexandrov@gmail.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files:
75
+ - LICENSE
76
+ - README.rdoc
77
+ files:
78
+ - LICENSE
79
+ - README.rdoc
80
+ - Rakefile
81
+ - VERSION
82
+ - lib/oa-mailru.rb
83
+ - lib/omniauth/mailru.rb
84
+ - lib/omniauth/strategies/mailru_api.rb
85
+ - lib/omniauth/strategies/mailru_api/view_helper.rb
86
+ - pkg/oa-mailru_alexandrov-0.0.4.gem
87
+ - spec/oa-mailru_spec.rb
88
+ - spec/spec.opts
89
+ - spec/spec_helper.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/igor-alexandrov/oa-mailru
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options: []
96
+
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
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.5.0
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: OmniAuth extension for mail.ru authentication
124
+ test_files:
125
+ - spec/oa-mailru_spec.rb
126
+ - spec/spec_helper.rb