oauth_service 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +7 -0
  4. data/Rakefile +34 -0
  5. data/app/controllers/concerns/oauth_service/google.rb +12 -0
  6. data/app/controllers/concerns/oauth_service/mailru.rb +29 -0
  7. data/app/controllers/concerns/oauth_service/yandex.rb +19 -0
  8. data/app/controllers/oauth_service/access_controller.rb +34 -0
  9. data/app/controllers/oauth_service/login_controller.rb +90 -0
  10. data/app/models/oauth_service/url.rb +9 -0
  11. data/app/models/oauth_service/user.rb +5 -0
  12. data/app/models/oauth_service/user_group.rb +5 -0
  13. data/app/models/oauth_service/users_group.rb +6 -0
  14. data/app/models/oauth_service/users_url.rb +6 -0
  15. data/config/routes.rb +6 -0
  16. data/lib/generators/oauth_service/controllers_generator.rb +15 -0
  17. data/lib/generators/oauth_service/install_generator.rb +15 -0
  18. data/lib/generators/oauth_service/migrations_generator.rb +20 -0
  19. data/lib/generators/oauth_service/models_generator.rb +17 -0
  20. data/lib/generators/templates/controllers/login_controller.rb +3 -0
  21. data/lib/generators/templates/migrations/create_tables.rb +45 -0
  22. data/lib/generators/templates/models/url.rb +2 -0
  23. data/lib/generators/templates/models/user.rb +3 -0
  24. data/lib/generators/templates/models/user_group.rb +3 -0
  25. data/lib/generators/templates/models/users_group.rb +3 -0
  26. data/lib/generators/templates/models/users_url.rb +2 -0
  27. data/lib/generators/templates/oauth_service.rb +12 -0
  28. data/lib/oauth_service/engine.rb +4 -0
  29. data/lib/oauth_service/provider.rb +85 -0
  30. data/lib/oauth_service/version.rb +3 -0
  31. data/lib/oauth_service.rb +29 -0
  32. data/lib/tasks/oauth_service_tasks.rake +4 -0
  33. data/test/dummy/README.rdoc +28 -0
  34. data/test/dummy/Rakefile +6 -0
  35. data/test/dummy/app/assets/javascripts/application.js +13 -0
  36. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  37. data/test/dummy/app/controllers/application_controller.rb +5 -0
  38. data/test/dummy/app/controllers/d_controller.rb +5 -0
  39. data/test/dummy/app/controllers/login_controller.rb +3 -0
  40. data/test/dummy/app/helpers/application_helper.rb +2 -0
  41. data/test/dummy/app/models/url.rb +2 -0
  42. data/test/dummy/app/models/user.rb +3 -0
  43. data/test/dummy/app/models/user_group.rb +3 -0
  44. data/test/dummy/app/models/users_group.rb +3 -0
  45. data/test/dummy/app/models/users_url.rb +2 -0
  46. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  47. data/test/dummy/app/views/login/index.html.erb +15 -0
  48. data/test/dummy/bin/bundle +3 -0
  49. data/test/dummy/bin/rails +4 -0
  50. data/test/dummy/bin/rake +4 -0
  51. data/test/dummy/bin/setup +29 -0
  52. data/test/dummy/config/application.rb +26 -0
  53. data/test/dummy/config/boot.rb +5 -0
  54. data/test/dummy/config/database.yml +25 -0
  55. data/test/dummy/config/environment.rb +5 -0
  56. data/test/dummy/config/environments/development.rb +41 -0
  57. data/test/dummy/config/environments/production.rb +79 -0
  58. data/test/dummy/config/environments/test.rb +42 -0
  59. data/test/dummy/config/initializers/assets.rb +11 -0
  60. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  61. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  62. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  63. data/test/dummy/config/initializers/inflections.rb +16 -0
  64. data/test/dummy/config/initializers/mime_types.rb +4 -0
  65. data/test/dummy/config/initializers/oauth_service.rb +12 -0
  66. data/test/dummy/config/initializers/session_store.rb +3 -0
  67. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  68. data/test/dummy/config/locales/en.yml +23 -0
  69. data/test/dummy/config/routes.rb +58 -0
  70. data/test/dummy/config/secrets.yml +22 -0
  71. data/test/dummy/config.ru +4 -0
  72. data/test/dummy/db/development.sqlite3 +0 -0
  73. data/test/dummy/db/migrate/20160514163909_create_tables.rb +45 -0
  74. data/test/dummy/db/schema.rb +45 -0
  75. data/test/dummy/log/development.log +17802 -0
  76. data/test/dummy/log/test.log +860 -0
  77. data/test/dummy/public/404.html +67 -0
  78. data/test/dummy/public/422.html +67 -0
  79. data/test/dummy/public/500.html +66 -0
  80. data/test/dummy/public/favicon.ico +0 -0
  81. data/test/dummy/test/fixtures/urls.yml +17 -0
  82. data/test/dummy/test/fixtures/user_groups.yml +7 -0
  83. data/test/dummy/test/fixtures/users.yml +9 -0
  84. data/test/dummy/test/fixtures/users_groups.yml +9 -0
  85. data/test/dummy/test/fixtures/users_urls.yml +9 -0
  86. data/test/dummy/test/models/url_test.rb +7 -0
  87. data/test/dummy/test/models/user_group_test.rb +7 -0
  88. data/test/dummy/test/models/user_test.rb +7 -0
  89. data/test/dummy/test/models/users_group_test.rb +7 -0
  90. data/test/dummy/test/models/users_url_test.rb +7 -0
  91. data/test/dummy/tmp/cache/assets/sprockets/v3.0/5L/5Lly_CA8DZvPhQV2jDQx-Y6P_y3Ygra9t5jfSlGhHDA.cache +2 -0
  92. data/test/dummy/tmp/cache/assets/sprockets/v3.0/6I/6Iapn2T9iTksiIpNjV38wlfVmi1jq4PD1Xh1Dr0fR0o.cache +0 -0
  93. data/test/dummy/tmp/cache/assets/sprockets/v3.0/CN/CN681ktqxLiK3U4MteA6Q4ZunlBxEgm2YFdmwxg3I6E.cache +0 -0
  94. data/test/dummy/tmp/cache/assets/sprockets/v3.0/Ch/Ch2bQFHkYziI9Erdkuj8uoPJyw0W2aA5prtYAqlccww.cache +1 -0
  95. data/test/dummy/tmp/cache/assets/sprockets/v3.0/DS/DSOLSc6A5RVSmvM415eEWAWG_AgOvZcLZOXQjsXyWQA.cache +2 -0
  96. data/test/dummy/tmp/cache/assets/sprockets/v3.0/Fr/FrVwswdDNGIkD24DY-aVGVj_ODmW3_o_Ji5khIzOlMI.cache +0 -0
  97. data/test/dummy/tmp/cache/assets/sprockets/v3.0/JY/JYpZExAhxpesd9z2s7dJupiDc-rDf6RBnmWH-HYpr2A.cache +1 -0
  98. data/test/dummy/tmp/cache/assets/sprockets/v3.0/J_/J_9ERjsyvsou7I7CNLORkwcBYyxCSdQRj9el7fbYNx4.cache +1 -0
  99. data/test/dummy/tmp/cache/assets/sprockets/v3.0/K9/K9ZheMi0hi4DNLzmDMRnv9A_lOVz33kNImc16Now42o.cache +1 -0
  100. data/test/dummy/tmp/cache/assets/sprockets/v3.0/LH/LHgjtAV8kdldaJ_dX0RCznzjmWYRuLdhU29fZCJ0VmU.cache +1 -0
  101. data/test/dummy/tmp/cache/assets/sprockets/v3.0/Nn/NnUCa7jNYx9HCmEB7E7WPWT00DwaM4IYICy1Ju1jjcs.cache +1 -0
  102. data/test/dummy/tmp/cache/assets/sprockets/v3.0/OI/OI6uxGcnsKavdWTtwDAasU3wPx8QXhzBgV0X2n1KjMQ.cache +2 -0
  103. data/test/dummy/tmp/cache/assets/sprockets/v3.0/Uo/Uon_Y1HqDVimN2TgfScHxbXPjDSsOsj6d7l8v4pJEO0.cache +0 -0
  104. data/test/dummy/tmp/cache/assets/sprockets/v3.0/b-/b-1gw2MCeaTZEWFQ_udDtgx4hJmooIz9uDVlHE97S2g.cache +1 -0
  105. data/test/dummy/tmp/cache/assets/sprockets/v3.0/bs/bsek6r8m5C0VziFPGuHOVymYEpIXo5gZyfU-8nPj01M.cache +0 -0
  106. data/test/dummy/tmp/cache/assets/sprockets/v3.0/gZ/gZp3uXMHuYQC4hzCr7bQfetKNdJAtbQmg3so2KpW1Dw.cache +2 -0
  107. data/test/dummy/tmp/cache/assets/sprockets/v3.0/hZ/hZi1k6tpxxCGYxRe7zY74ItcOI8gZrREOpGuA8JSpGg.cache +2 -0
  108. data/test/dummy/tmp/cache/assets/sprockets/v3.0/kz/kzdSvu57G4i6eTuarsZCAfbhbICnkRa0Xhi0b9ua6qk.cache +1 -0
  109. data/test/dummy/tmp/cache/assets/sprockets/v3.0/lz/lz4_WSikXa5YohqgYmqCvVmW_r_ncbvtgDX7bJoO03s.cache +0 -0
  110. data/test/dummy/tmp/cache/assets/sprockets/v3.0/pE/pEhaat2KBd5SrT7szC_8R1_6hK17FTpvoRFkmCRSD3M.cache +2 -0
  111. data/test/dummy/tmp/cache/assets/sprockets/v3.0/qS/qSORh00SA5qx5xcvNgMx-S-T737dJS6smxZEhRaf0J4.cache +0 -0
  112. data/test/dummy/tmp/cache/assets/sprockets/v3.0/qd/qdZCi8acwxpvAQAC8sNXRjkz73Pdy7yQpG7_rNCxcYs.cache +0 -0
  113. data/test/dummy/tmp/cache/assets/sprockets/v3.0/rC/rCO5-bHVJ6Y_GsPBmOPUL23pfjvc2Gw2zt_ODmZsygw.cache +1 -0
  114. data/test/dummy/tmp/cache/assets/sprockets/v3.0/uV/uVnTx3UxyufXVkCnR8AW30pR5GiIUTCMcOMEPWt38dY.cache +1 -0
  115. data/test/oauth_service_provider_test.rb +8 -0
  116. data/test/oauth_service_test.rb +7 -0
  117. data/test/test_helper.rb +19 -0
  118. metadata +273 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc4e4f436f6b04ef9ba3061db91a2d1c9e49649c
4
+ data.tar.gz: 39e7c45adb3f4a851ee20d40dfad135eb4e97046
5
+ SHA512:
6
+ metadata.gz: d119e5c4b4dd7fac49b8d020ef06bfa200212e35c72d0590093dc15b74f76f3eaa9eb3eeb933bb8c8fa4aa808f45fa739de87365f55d4352b646e1e1324cb671
7
+ data.tar.gz: 8e167caf502e999ae856fe346a02bf93fddbb3babfd277876ba086b8e6a67bcbdbcc75933c04e1155bbff2e000c786a7ff301dda7c6b00a188abc64a50d07a9a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 DCrow
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,7 @@
1
+ = OauthService
2
+
3
+ This project rocks and uses MIT-LICENSE.
4
+
5
+ Skeleton project for authentication.
6
+ Uses Google, Yandex, Mail_Ru oauth2 services to authenticate users.
7
+ Response is json.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'OauthService'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,12 @@
1
+ module OauthService
2
+ class Google < Provider
3
+ def get_info_headers(options = {})
4
+ { 'Authorization' => "Bearer #{options[:access_token]}" }
5
+ end
6
+
7
+ def get_info_params(options = {})
8
+ {}
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,29 @@
1
+ module OauthService
2
+ class MailRu < Provider
3
+ def initialize(name, downcase_name, auth_url, client_id, client_secret,
4
+ info_url, scopes, token_url)
5
+ super(name, downcase_name, auth_url, client_id, client_secret,
6
+ info_url, scopes, token_url)
7
+ @scopes = nil
8
+ end
9
+
10
+ def get_user_info(info)
11
+ info.first
12
+ end
13
+
14
+ def get_info_headers(options = {})
15
+ nil
16
+ end
17
+
18
+ def get_info_params(options = {})
19
+ query_params = {
20
+ 'app_id' => client_id,
21
+ 'method' => 'users.getInfo',
22
+ 'secure' => 1,
23
+ 'session_key' => options[:access_token]
24
+ }
25
+ query_params['sig'] = Digest::MD5.hexdigest(query_params.collect { |v| v.join('=') }.join + client_secret)
26
+ query_params
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ module OauthService
2
+ class Yandex < Provider
3
+ def get_user_info(info)
4
+ {
5
+ :email => info[:default_email],
6
+ :id => info[:id],
7
+ :name => info[:display_name]
8
+ }
9
+ end
10
+
11
+ def get_info_headers(options = {})
12
+ { 'Authorization' => "OAuth #{options[:access_token]}" }
13
+ end
14
+
15
+ def get_info_params(options = {})
16
+ {}
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ module OauthService
2
+ class AccessController < OauthService.parent_controller.constantize
3
+ before_filter :check_access
4
+
5
+ def get_user_id
6
+ if (api_code = request.headers["HTTP_API_CODE"]) && api_code!=""
7
+ user = ::User.find_by(api_code: api_code)
8
+ else
9
+ user = ::User.find_by(name: "guest")
10
+ end
11
+ user.id
12
+ end
13
+
14
+ def check_access
15
+ check_status = false
16
+ path = request.path
17
+ http_method = request.method.to_s
18
+ user_urls = ::Url.by_user get_user_id
19
+
20
+ check_status = user_urls.any? do |user_url|
21
+ path[Regexp.new(user_url.url_pattern)]==path &&
22
+ (user_url.http_method.nil? || http_method==user_url.http_method)
23
+ end
24
+
25
+ unless check_status
26
+ if request.headers["HTTP_API_CODE"]
27
+ render :json => {:success => false, :error => "Not authorized"}, :status => 401
28
+ else
29
+ render :json => {:success => false, :error => "Permission denied"}, :status => 403
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,90 @@
1
+ require "oauth_service"
2
+
3
+ module OauthService
4
+ class LoginController < AccessController
5
+ def oauth_callback
6
+ user_info = get_user_info(params[:provider_name], request.url, params[:code])
7
+
8
+ if user_info
9
+ user = ::User.find_by(name: user_info[:email])
10
+ api_code = generate_api_code
11
+ success = user.blank?
12
+
13
+ unless success
14
+ user.update_attributes(:api_code => api_code)
15
+ render :json => {:success => true, :api_code => api_code}, :status => 200
16
+ else
17
+ render :json => {:success => false, :error=> "No such user exists"}, :status => 404
18
+ end
19
+ end
20
+ end
21
+
22
+ def logout
23
+ user = ::User.find_by(api_code: api_code)
24
+ success = user.blank?
25
+ unless success
26
+ user.update_attributes(:api_code => nil)
27
+ render :json => {:success => true}, :status => 400
28
+ else
29
+ render :json => {:success => false, :error=> "No such user exists"}, :status => 400
30
+ end
31
+ end
32
+
33
+ private
34
+ def generate_api_code
35
+ SecureRandom.uuid
36
+ end
37
+
38
+ def get_user_info(provider_name, redirect_uri, code)
39
+ provider = OauthService::Provider::get_provider_by_name(provider_name)
40
+ token_uri = URI.parse(provider.token_url)
41
+ http_token = Net::HTTP.new(token_uri.host, token_uri.port)
42
+ http_token.use_ssl = true if token_uri.scheme == "https"
43
+
44
+ http_token.start do |http_token_request|
45
+ res = http_token_request.send_request("POST",
46
+ token_uri.request_uri,
47
+ URI.encode_www_form(provider.get_token_params(original_url: redirect_uri, code: code)),
48
+ { 'Content-Type' => "application/x-www-form-urlencoded" })
49
+
50
+ res_body = ActiveSupport::JSON.decode(res.body).symbolize_keys
51
+
52
+ if res.code!='200'
53
+ render :json => {
54
+ :success => false,
55
+ :error => res_body[:error],
56
+ :error_description => res_body[:error_description]
57
+ },
58
+ :status => res.code
59
+ return false
60
+ end
61
+
62
+ info_uri = URI.parse(provider.info_url + "?" +
63
+ URI.encode_www_form(provider.get_info_params(access_token: res_body[:access_token])))
64
+ http_info = Net::HTTP.new(info_uri.host, info_uri.port)
65
+ http_info.use_ssl = true if info_uri.scheme == "https"
66
+
67
+ http_info.start do |http_info_request|
68
+ res = http_info_request.send_request("GET",
69
+ info_uri.request_uri,
70
+ nil,
71
+ provider.get_info_headers(access_token: res_body[:access_token]))
72
+
73
+ res_body = ActiveSupport::JSON.decode(res.body).symbolize_keys
74
+
75
+ if res.code!='200'
76
+ render :json => {
77
+ :success => false,
78
+ :error => res_body[:error],
79
+ :error_description => res_body[:error_description]
80
+ },
81
+ :status => res.code
82
+ return false
83
+ end
84
+ provider.get_user_info(res_body)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
@@ -0,0 +1,9 @@
1
+ module OauthService
2
+ class Url < ActiveRecord::Base
3
+ has_many :users_url
4
+
5
+ def self.by_user(user_id)
6
+ includes(users_url: [users_group: [:user]]).where(:users_groups => {:user_id => user_id}).all
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module OauthService
2
+ class User < ActiveRecord::Base
3
+ has_many :users_group
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module OauthService
2
+ class UserGroup < ActiveRecord::Base
3
+ has_many :users_group
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module OauthService
2
+ class UsersGroup < ActiveRecord::Base
3
+ belongs_to :user
4
+ belongs_to :user_group
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module OauthService
2
+ class UsersUrl < ActiveRecord::Base
3
+ belongs_to :url
4
+ belongs_to :users_group
5
+ end
6
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "oauth_service"
2
+
3
+ Rails.application.routes.draw do
4
+ get "#{OauthService.redirect_uri}/:provider_name", to: "#{OauthService.login_controller[0..-11].downcase}#oauth_callback"
5
+ get "#{OauthService.redirect_uri}/logout", to: "#{OauthService.login_controller[0..-11].downcase}#logout"
6
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/generators/base'
2
+
3
+ module OauthService
4
+ module Generators
5
+ class ControllersGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../templates/controllers", __FILE__)
7
+
8
+ desc "Create inherited LoginController in your app/controllers folder."
9
+
10
+ def create_controllers
11
+ template "login_controller.rb", "app/controllers/login_controller.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require "rails/generators/base"
2
+
3
+ module OauthService
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../templates", __FILE__)
7
+
8
+ desc "Creates a OauthService initializer."
9
+
10
+ def copy_initializer
11
+ template "oauth_service.rb", "config/initializers/oauth_service.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module OauthService
4
+ module Generators
5
+ class MigrationsGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ source_root File.expand_path("../../templates/migrations", __FILE__)
8
+
9
+ desc "Create table migration in your db/migrate folder."
10
+
11
+ def create_migrations
12
+ migration_template "create_tables.rb", "db/migrate/create_tables.rb"
13
+ end
14
+
15
+ def self.next_migration_number(path)
16
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails/generators/base'
2
+
3
+ module OauthService
4
+ module Generators
5
+ class ModelsGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../templates/models", __FILE__)
7
+
8
+ desc "Create inherited Models in your app/models folder."
9
+
10
+ def create_models
11
+ MODELS.each do |model|
12
+ template "#{model}.rb", "app/models/#{model}.rb"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ class LoginController < OauthService::LoginController
2
+
3
+ end
@@ -0,0 +1,45 @@
1
+ class CreateTables < ActiveRecord::Migration
2
+ def self.up
3
+
4
+ create_table :users do |t|
5
+ t.string :name, nil: false, index: true
6
+ t.string :api_code
7
+ end
8
+
9
+ create_table :urls do |t|
10
+ t.string :url_pattern
11
+ t.string :name
12
+ t.string :http_method
13
+ end
14
+
15
+ create_table :user_groups do |t|
16
+ t.string :name, nil: false, index: true
17
+ end
18
+
19
+ create_table :users_groups do |t|
20
+ t.integer :user_group_id
21
+ t.integer :user_id
22
+ end
23
+
24
+ create_table :users_urls do |t|
25
+ t.integer :users_group_id
26
+ t.integer :url_id
27
+ end
28
+
29
+ User.create :name => "guest", :id => 1
30
+ Url.create :url_pattern => "^/login", :name => "Login page", :http_method => "GET", :id => 1
31
+ Url.create :url_pattern => "^/oauth/.*", :name => "AuthCallback page", :http_method => "GET", :id => 2
32
+ UserGroup.create :id => 1, :name => "Guest"
33
+ UsersGroup.create :id => 1, :user_id => 1, :user_group_id => 1
34
+ UsersUrl.create :id => 1, :url_id => 1, :users_group_id => 1
35
+ UsersUrl.create :id => 2, :url_id => 2, :users_group_id => 1
36
+ end
37
+
38
+ def self.down
39
+ drop_table :users_urls
40
+ drop_table :users_groups
41
+ drop_table :user_groups
42
+ drop_table :users
43
+ drop_table :urls
44
+ end
45
+ end
@@ -0,0 +1,2 @@
1
+ class Url < OauthService::Url
2
+ end
@@ -0,0 +1,3 @@
1
+ class User < OauthService::User
2
+ end
3
+
@@ -0,0 +1,3 @@
1
+ class UserGroup < OauthService::UserGroup
2
+ end
3
+
@@ -0,0 +1,3 @@
1
+ class UsersGroup < OauthService::UsersGroup
2
+ end
3
+
@@ -0,0 +1,2 @@
1
+ class UsersUrl < OauthService::UsersUrl
2
+ end
@@ -0,0 +1,12 @@
1
+ OauthService.setup do |config|
2
+ # The parent controller all OauthService controllers inherits from.
3
+ # config.parent_controller = "ApplicationController"
4
+
5
+ # The controller name where auth callback is redirected.
6
+ # Has to extend OauthService::LoginController.
7
+ # Change if default login controller is not LoginController.
8
+ # config.login_controller = "LoginController"
9
+
10
+ # The relative route where auth service callback is redirected.
11
+ # config.redirect_uri = "/oauth/"
12
+ end
@@ -0,0 +1,4 @@
1
+ module OauthService
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,85 @@
1
+ module OauthService
2
+ class Provider
3
+ AVAILABLE_PROVIDERS = ['YANDEX', 'GOOGLE', 'MAIL_RU']
4
+
5
+
6
+ attr_reader :name, :downcase_name, :auth_url, :client_id, :client_secret,
7
+ :info_url, :scopes, :token_url
8
+
9
+ def initialize(name, downcase_name, auth_url, client_id, client_secret,
10
+ info_url, scopes, token_url)
11
+ @name = name
12
+ @downcase_name = downcase_name
13
+ @auth_url = auth_url
14
+ @client_id = client_id
15
+ @client_secret = client_secret
16
+ @info_url = info_url
17
+ @scopes = scopes
18
+ @token_url = token_url
19
+ end
20
+
21
+ def get_redirect_uri(request_url)
22
+ redirect_uri = OauthService.redirect_uri
23
+
24
+ if redirect_uri[0..3]=="http"
25
+ redirect_uri + downcase_name
26
+ else
27
+ uri = URI.parse(request_url)
28
+ uri.path = redirect_uri + downcase_name
29
+ uri.query = nil
30
+
31
+ return uri.to_s
32
+ end
33
+ end
34
+
35
+ def get_token_params(options = {})
36
+ {
37
+ 'client_id' => client_id,
38
+ 'client_secret' => client_secret,
39
+ 'redirect_uri' => get_redirect_uri(options[:original_url]),
40
+ 'grant_type' => 'authorization_code',
41
+ 'code' => options[:code]
42
+ }
43
+ end
44
+
45
+ def get_user_info(info)
46
+ info
47
+ end
48
+
49
+ def get_info_headers(options = {})
50
+ raise "Headers for token request method is undefined"
51
+ end
52
+
53
+ def get_info_params(options = {})
54
+ raise "Paramaters for information request method is undefined"
55
+ end
56
+
57
+ def self.providers_data
58
+ @@providers_data ||= AVAILABLE_PROVIDERS.collect do |provider|
59
+ ("OauthService::Provider::#{provider.downcase.camelize}").constantize.new(
60
+ provider,
61
+ provider.downcase,
62
+ ENV["#{provider}_AUTH_URL"],
63
+ ENV["#{provider}_CLIENT_ID"],
64
+ ENV["#{provider}_CLIENT_SECRET"],
65
+ ENV["#{provider}_INFO_URL"],
66
+ ENV["#{provider}_SCOPES"],
67
+ ENV["#{provider}_TOKEN_URL"]
68
+ ) if ENV["#{provider}_AUTH_URL"] &&
69
+ ENV["#{provider}_CLIENT_ID"] &&
70
+ ENV["#{provider}_CLIENT_SECRET"] &&
71
+ ENV["#{provider}_INFO_URL"] &&
72
+ ENV["#{provider}_SCOPES"] &&
73
+ ENV["#{provider}_TOKEN_URL"]
74
+ end.compact
75
+ end
76
+
77
+ def self.get_provider_by_name(name)
78
+ res = providers_data.select do |provider|
79
+ provider.downcase_name == name.downcase
80
+ end
81
+
82
+ res ? res.first : nil
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module OauthService
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require "oauth_service/provider"
2
+ require "oauth_service/engine"
3
+ require "securerandom"
4
+ require "rails"
5
+
6
+ module OauthService
7
+ MODELS = ['url', 'users_url', 'user', 'user_group', 'users_group']
8
+
9
+ # The parent controller all OauthService controllers inherits from.
10
+ # Defaults to "ApplicationController".
11
+ mattr_accessor :parent_controller
12
+ @@parent_controller = "ApplicationController"
13
+
14
+ # The relative route where auth service callback is redirected.
15
+ # Defaults to "/oauth/".
16
+ mattr_accessor :redirect_uri
17
+ @@redirect_uri = "/oauth/"
18
+
19
+ # Name of the controller which inherits from OauthService::LoginController
20
+ # Defaults to "LoginController".
21
+ mattr_accessor :login_controller
22
+ @@login_controller = "LoginController"
23
+
24
+ # Default way to set up OauthService. Run rails generate oauth_service:install to create
25
+ # a fresh initializer with all configuration values.
26
+ def self.setup
27
+ yield self
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :oauth_service do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .