proxy_auth 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bcfc463d0a3b896471a8dc09d9a873a459461bf8
4
+ data.tar.gz: 8bd9c92e225c13d8c75f30331ebe6d6732a8f033
5
+ SHA512:
6
+ metadata.gz: 6a2a859c16b9c79a37875ed0018f9cbcf8f76f8a3ce0325a39492a8d637c1f7d53e4c7961cd2bbefd800bf711d10ffa0bef4bb7ccba7a268e524475bf7780c42
7
+ data.tar.gz: b1afb72fcf889f4e4262ea23e76e9fa10ab0f44717e7f25837f1b94303ac98c3a90bc3bd25ff06e5b3107b7db320051ec4576e56e7bf6600b10cda3cb353ca61
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Fractal Soft
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.md ADDED
@@ -0,0 +1,43 @@
1
+ ## Installation
2
+
3
+ 1. In your **Gemfile** add line ```gem 'proxy_auth'```
4
+ 2. Run ```bundle install```
5
+ 3. Set config/secrets.yml:
6
+ ```
7
+ #!bash
8
+ proxy:
9
+ token: SECRET_TOKEN
10
+ url: http://HOST:PORT
11
+ service:
12
+ name: SERVICE_NAME
13
+ url: http://SERVICE_HOST:SERVICE_PORT
14
+ ```
15
+ 4. Initialize with command ```rails generate proxy_auth:install```
16
+ 5. Secure your application. In file app/controllers/application_controller.rb
17
+ ```
18
+ #!ruby
19
+ before_action :authenticate
20
+
21
+ protected
22
+
23
+ def authenticate
24
+ authenticate_token || render_unauthorized
25
+ end
26
+
27
+ def authenticate_token
28
+ authenticate_with_http_token do |token|
29
+ ProxyAuth::Token.instance.valid? token
30
+ end
31
+ end
32
+
33
+ def render_unauthorized
34
+ render json: 'Bad credentials', status: 401
35
+ end
36
+ ```
37
+ 6. Add management routes
38
+ ```
39
+ #!ruby
40
+ mount ProxyAuth::Engine => '/proxy'
41
+ ```
42
+ 7. Restart your Rails application.
43
+ 8. Rake task for reconnecting to proxy: ```rake proxy_auth:register_service```
data/Rakefile ADDED
@@ -0,0 +1,17 @@
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 = 'ProxyAuth'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,4 @@
1
+ module ProxyAuth
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,20 @@
1
+ module ProxyAuth
2
+ # management methods used for server maintenance
3
+ class RegisterServicesController < ApplicationController
4
+ before_action :authenticate
5
+
6
+ def create
7
+ ProxyAuth::Token.instance.register_service
8
+ head :ok
9
+ end
10
+
11
+ private
12
+
13
+ def authenticate
14
+ authenticate_with_http_basic do |user, password|
15
+ secrets = Rails.application.secrets
16
+ secrets.admin_login == user && secrets.admin_password == password
17
+ end
18
+ end
19
+ end
20
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ ProxyAuth::Engine.routes.draw do
2
+ resource :register_service, only: :create
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+
3
+ module ProxyAuth
4
+ # Proxy Auth Generator
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root(File.expand_path(__dir__ + '/templates'))
7
+
8
+ def copy_initializer
9
+ destination = "#{Rails.root}/config/initializers/proxy_auth.rb"
10
+ copy_file 'initializer.rb', destination
11
+ end
12
+ end
13
+ end
@@ -0,0 +1 @@
1
+ ProxyAuth::Token.instance
@@ -0,0 +1,52 @@
1
+ module ProxyAuth
2
+ # Connects to proxy and gets service token
3
+ class Connection
4
+ def service_token
5
+ register_service
6
+ @response && JSON.parse(@response.body)['token']
7
+ end
8
+
9
+ private
10
+
11
+ def register_service
12
+ @response = http.post('/services', params, headers)
13
+ Rails.logger.info "Service #{service_name} registered."
14
+ rescue Errno::ECONNREFUSED
15
+ Rails.logger.fatal 'Proxy connection refused.'
16
+ end
17
+
18
+ def registration_token
19
+ encoder = ActionController::HttpAuthentication::Token
20
+ token = Rails.application.secrets.proxy['token']
21
+ @registration_token ||= encoder.encode_credentials token
22
+ end
23
+
24
+ def headers
25
+ {
26
+ 'Accept' => 'application/json',
27
+ 'Content-Type' => 'application/json',
28
+ 'Authorization' => registration_token
29
+ }
30
+ end
31
+
32
+ def http
33
+ @http ||= Net::HTTP.new(uri.host, uri.port)
34
+ end
35
+
36
+ def params
37
+ { service: { name: service_name, host: service_url } }.to_json
38
+ end
39
+
40
+ def service_name
41
+ @service_name ||= Rails.application.secrets.service['name']
42
+ end
43
+
44
+ def service_url
45
+ @service_url ||= Rails.application.secrets.service['url']
46
+ end
47
+
48
+ def uri
49
+ @uri ||= URI.parse(Rails.application.secrets.proxy['url'])
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,6 @@
1
+ module ProxyAuth
2
+ # Engine class
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace ProxyAuth
5
+ end
6
+ end
@@ -0,0 +1,34 @@
1
+ require 'singleton'
2
+
3
+ module ProxyAuth
4
+ # Service token
5
+ class Token
6
+ include Singleton
7
+
8
+ attr_reader :token
9
+
10
+ def initialize
11
+ register_service
12
+ end
13
+
14
+ def register_service
15
+ Thread.new do
16
+ set_token
17
+ end
18
+ end
19
+
20
+ def valid?(token)
21
+ @token == token
22
+ end
23
+
24
+ private
25
+
26
+ def set_token
27
+ connection = Connection.new
28
+ 3.times do
29
+ @token = connection.service_token
30
+ @token && break || sleep(5)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ # Gem version
2
+ module ProxyAuth
3
+ VERSION = '0.0.2'
4
+ end
data/lib/proxy_auth.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'proxy_auth/connection'
2
+ require 'proxy_auth/token'
3
+ require 'proxy_auth/engine'
4
+ require 'generators/install_generator'
@@ -0,0 +1,18 @@
1
+ namespace :proxy_auth do
2
+ desc 'Connect to proxy'
3
+ task register_service: :environment do
4
+ secrets = Rails.application.secrets
5
+
6
+ url = secrets.service['url']
7
+ uri = URI.parse(url)
8
+ http = Net::HTTP.new(uri.host, uri.port)
9
+
10
+ login = secrets.admin_login
11
+ password = secrets.admin_password
12
+ encoder = ActionController::HttpAuthentication::Basic
13
+ authorization = encoder.encode_credentials(login, password)
14
+ headers = { 'Authorization' => authorization }
15
+
16
+ http.post('/proxy/register_service', nil, headers)
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxy_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Grzegorz Lisowski
8
+ - Alek Malaszkiewicz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-03-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec-rails
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.2'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.2'
42
+ description: Client to microservice proxy authentication
43
+ email:
44
+ - info@fractalsoft.org
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - app/controllers/proxy_auth/application_controller.rb
53
+ - app/controllers/proxy_auth/register_services_controller.rb
54
+ - config/routes.rb
55
+ - lib/generators/install_generator.rb
56
+ - lib/generators/templates/initializer.rb
57
+ - lib/proxy_auth.rb
58
+ - lib/proxy_auth/connection.rb
59
+ - lib/proxy_auth/engine.rb
60
+ - lib/proxy_auth/token.rb
61
+ - lib/proxy_auth/version.rb
62
+ - lib/tasks/proxy_auth.rake
63
+ homepage:
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.4.5
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Proxy authentication client
87
+ test_files: []