omniauth_configure 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ # OS X
2
+ .DS_Store
3
+
4
+ # Ruby
5
+ *.gem
6
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # OmnAuth Configure
2
+
3
+ Centralize OmniAuth strategy configurations on the server. This has a couple
4
+ advantages over storing configuration details (e.g. client secret and urls) in
5
+ source control or the environment for the user running the application server.
6
+
7
+ * Keeping sensitive information out of source code
8
+ * Keeping configurations where they belong (/etc)
9
+
10
+ ## Installation
11
+
12
+ 1. Add the `omniauth_configure` gem to the Gemfile
13
+
14
+ ## Congiguration
15
+
16
+ ```
17
+ # /etc/nubic/omniauth/local.yml
18
+
19
+ defaults:
20
+ nucats_membership:
21
+ site: http://membership-staging.nubic.northwestern.edu
22
+ authorize_url: /auth
23
+ token_url: /token
24
+ nucats_assist:
25
+ nucats_membership:
26
+ client_id: abc123
27
+ client_secret: def456
28
+ facebok:
29
+ client_id: asdf213
30
+ client_secret: jimbo
31
+ nitro:
32
+ nucats_membership:
33
+ client_id: xyz987
34
+ client_secret:ufw654
35
+ ```
36
+
37
+ ## Rack
38
+
39
+ ```
40
+ # server.ru
41
+
42
+ OmniauthConfigure.configure {
43
+ app :example
44
+ strategies :nucats_membership
45
+ central '/etc/nubic/omniauth/local.yml'
46
+ }
47
+
48
+ OmniauthConfigure::Rack.use_in(self)
49
+ ```
50
+
51
+ ## Rails
52
+
53
+ ```
54
+ # config/environments/development.rb
55
+
56
+ OmniAuthConfigure.configure {
57
+ app :example
58
+ strategies :nucats_membership
59
+ central '/etc/nubic/omniauth/local.yml'
60
+ }
61
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require File.join('bundler', 'gem_tasks')
3
+ require File.join('rspec', 'core', 'rake_task')
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require File.join('omniauth_configure', 'central_parameters')
3
+ require File.join('omniauth_configure', 'configuration')
4
+ require File.join('omniauth_configure', 'rack')
5
+ require File.join('omniauth_configure', 'railtie') if defined?(Rails)
6
+
7
+ module OmniAuthConfigure
8
+ class << self
9
+ attr_accessor :configuration
10
+ end
11
+
12
+ def self.configure(&block)
13
+ @configuration ||= OmniAuthConfigure::Configuration.new
14
+ @configuration.enhance(&block)
15
+ end
16
+ end
@@ -0,0 +1,54 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'yaml'
3
+
4
+ module OmniAuthConfigure
5
+ class CentralParameters
6
+ attr_writer :entries, :raw_values
7
+
8
+ def entries
9
+ @entries ||= {}
10
+ end
11
+
12
+ def raw_values
13
+ @raw_values ||= {}
14
+ end
15
+
16
+ def initialize(file_path)
17
+ @raw_values = YAML::load( File.open(file_path) )
18
+
19
+ @raw_values = nested_symbolize_keys!(deep_clone(raw_values))
20
+ end
21
+
22
+ def [](app, provider)
23
+ unless entries.key?(app)
24
+ entries[app] = {}
25
+ entries[app][provider] =
26
+ {}.merge((raw_values[:default] || {})[provider] || {}).
27
+ merge((raw_values[:defaults] || {})[provider] || {}).
28
+ merge((raw_values[app] || {})[provider] || {})
29
+ end
30
+ entries[app][provider]
31
+ end
32
+
33
+ #######
34
+ private
35
+
36
+ def deep_clone(src)
37
+ clone = { }
38
+ src.each_pair do |k, v|
39
+ clone[k] = v.is_a?(Hash) ? deep_clone(v) : v
40
+ end
41
+ clone
42
+ end
43
+
44
+ def nested_symbolize_keys!(target)
45
+ target.keys.each do |k|
46
+ v = target[k]
47
+ nested_symbolize_keys!(v) if v.respond_to?(:keys)
48
+ target.delete(k)
49
+ target[k.to_sym] = v
50
+ end
51
+ target
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,48 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module OmniAuthConfigure
3
+ class Configuration < Struct.new(:app, :central)
4
+ attr_reader :strategies
5
+
6
+ def initialize(&config)
7
+ self.enhance(&config) if config
8
+ end
9
+
10
+ def strategies=(*strategies)
11
+ @strategies ||= strategies
12
+ end
13
+
14
+ ##
15
+ # Updates the configuration via the {ConfiguratorLanguage DSL}.
16
+ #
17
+ # @return [Configuration] itself
18
+ def enhance(&additional_config)
19
+ Configurator.new(self, &additional_config)
20
+ self
21
+ end
22
+
23
+ def parameters_for(app, provider)
24
+ ::OmniAuthConfigure::CentralParameters.new(central)[app, provider]
25
+ end
26
+ end
27
+
28
+ module ConfiguratorLanguage
29
+ def method_missing(m, *args, &block)
30
+ if @config.respond_to?(:"#{m}=")
31
+ @config.send(:"#{m}=", *args)
32
+ else
33
+ super
34
+ end
35
+ end
36
+ end
37
+
38
+ ##
39
+ # @private
40
+ class Configurator
41
+ include ConfiguratorLanguage
42
+
43
+ def initialize(target, &block)
44
+ @config = target
45
+ instance_eval(&block)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,44 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module OmniAuthConfigure::Rack
3
+ def self.use_in(builder, configuration=nil, &block)
4
+ effective_configuration = configuration || OmniAuthConfigure.configuration
5
+ unless effective_configuration
6
+ fail "No configuration was provided. " <<
7
+ "Please set one or the other before calling use_in."
8
+ end
9
+
10
+ app = effective_configuration.app
11
+ klasses = effective_configuration.strategies
12
+
13
+ klasses.each do |klass|
14
+ if klass.is_a?(Class)
15
+ middleware = klass
16
+ else
17
+ begin
18
+ middleware = OmniAuth::Strategies.const_get("#{OmniAuth::Utils.camelize(klass.to_s)}")
19
+ rescue NameError
20
+ raise LoadError, "Could not find matching strategy for #{klass.inspect}. You may need to install an additional gem (such as omniauth-#{klass})."
21
+ end
22
+ end
23
+
24
+ p = effective_configuration.parameters_for(app, klass)
25
+
26
+ middleware.args [:client_id, :client_secret]
27
+
28
+ cid = p[:client_id]
29
+ cs = p[:client_secret]
30
+ s = p[:site]
31
+ au = p[:authorize_url]
32
+ tu = p[:token_url]
33
+
34
+ args = [cid, cs]
35
+ if s || au || tu
36
+ middleware.args [:client_id, :client_secret, :client_options]
37
+ args << {:site => s, :authorize_url => au, :token_url => tu }
38
+ end
39
+ args << {} # Last argument to provider strategy is empty hash
40
+
41
+ builder.use middleware, *args, &block
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module OmniAuthConfigure
3
+ class Railtie < Rails::Railtie
4
+ initializer 'OmniAuthConfigure::Rails middleware installation' do |app|
5
+ Rails.logger.debug "Installing OmniAuthConfigure rack middleware"
6
+ OmniAuthConfigure::Rack.use_in(app.middleware)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module OmniAuthConfigure
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "omniauth_configure/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{omniauth_configure}
7
+ s.version = OmniAuthConfigure::VERSION
8
+
9
+ s.authors = ['John Dzak']
10
+ s.email = %q{j-dzak@northwestern.edu}
11
+ s.description = %q{Allows centralized OmniAuth strategy configurations}
12
+ s.summary = %q{Allows centralized OmniAuth strategy configurations}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_paths = ["lib"]
16
+ s.add_runtime_dependency 'omniauth', '~> 1.2'
17
+ s.add_development_dependency 'rspec'
18
+ s.add_development_dependency 'rake'
19
+ end
20
+
@@ -0,0 +1,44 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe OmniAuthConfigure::Configuration do
5
+ def config_from(&block)
6
+ OmniAuthConfigure::Configuration.new(&block)
7
+ end
8
+
9
+ describe '#parameters_for' do
10
+ let(:config) do
11
+ config_from {
12
+ central File.expand_path("../test_configuration.yml", __FILE__)
13
+ }
14
+ end
15
+
16
+ let (:northwestern) do
17
+ config.parameters_for(:patient_tracker, :northwestern)
18
+ end
19
+
20
+ let (:facebook) do
21
+ config.parameters_for(:patient_tracker, :facebook)
22
+ end
23
+
24
+ it 'aquires the default parameters' do
25
+ expect(northwestern[:site]).to eq('http://northwestern.edu')
26
+ end
27
+
28
+ it 'aquires the parameters' do
29
+ expect(northwestern[:client_id]).to eq('c1980')
30
+ expect(facebook[:client_id]).to eq('c1995')
31
+ end
32
+
33
+ it 'aquires the overridden parameters' do
34
+ expect(northwestern[:token_url]).to eq('/override/token')
35
+ end
36
+ end
37
+
38
+ describe '#strategies' do
39
+ it 'stores strategies' do
40
+ c = config_from { strategies :northwestern, :facebook, :twitter }
41
+ expect(c.strategies).to eq([:northwestern, :facebook, :twitter])
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,73 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'omniauth'
4
+
5
+ describe OmniAuthConfigure::Rack do
6
+ describe '#use_in' do
7
+ let(:builder) { OmniAuthConfigure::Spec::MockBuilder.new }
8
+
9
+ it "fails with a useful message if there's no configuration" do
10
+ builder.reset!
11
+ OmniAuthConfigure.configuration = nil
12
+
13
+ lambda { OmniAuthConfigure::Rack.use_in(builder) }.
14
+ should raise_error(/Please set one or the other before calling use_in./)
15
+ end
16
+
17
+ it 'adds middleware' do
18
+ OmniAuthConfigure.configure {
19
+ app :patient_tracker
20
+ strategies :northwestern, :facebook
21
+ central File.expand_path("../test_configuration.yml", __FILE__)
22
+ }
23
+
24
+ OmniAuthConfigure::Rack.use_in(builder)
25
+ expect(builder.uses[0].first).to eq(OmniAuth::Strategies::Northwestern)
26
+ expect(builder.uses[0].first.args).to eq([:client_id, :client_secret, :client_options])
27
+
28
+ expect(builder.uses[1].first).to eq(OmniAuth::Strategies::Facebook)
29
+ expect(builder.uses[1].first.args).to eq([:client_id, :client_secret])
30
+ end
31
+ end
32
+ end
33
+
34
+ module OmniAuthConfigure
35
+ module Spec
36
+ ##
37
+ # Record only version of Rack::Builder taken
38
+ # from Aker
39
+ #
40
+ # @see https://github.com/NUBIC/aker
41
+ # Aker: a flexible security framework for Rack (and Rails)
42
+ class MockBuilder
43
+ def reset!
44
+ self.uses.clear
45
+ end
46
+
47
+ def use(cls, *params, &block)
48
+ self.uses << [cls, params, block]
49
+ end
50
+
51
+ def uses
52
+ @uses ||= []
53
+ end
54
+
55
+ def using?(klass, *params)
56
+ self.uses.detect { |cls, prms, block| cls == klass && params == prms }
57
+ end
58
+
59
+ alias :find_use_of :using?
60
+ end
61
+ end
62
+ end
63
+
64
+ module OmniAuth
65
+ module Strategies
66
+ class Northwestern
67
+ include OmniAuth::Strategy
68
+ end
69
+ class Facebook
70
+ include OmniAuth::Strategy
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,13 @@
1
+ defaults:
2
+ northwestern:
3
+ site: http://northwestern.edu
4
+ authorize_url: /oauth/auth
5
+ token_url: /oauth/token
6
+ patient_tracker:
7
+ northwestern:
8
+ client_id: c1980
9
+ client_secret: kareem
10
+ token_url: /override/token
11
+ facebook:
12
+ client_id: c1995
13
+ client_secret: seagal
@@ -0,0 +1,5 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require File.join('bundler', 'setup')
3
+ require 'rspec'
4
+
5
+ require 'omniauth_configure'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth_configure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Dzak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Allows centralized OmniAuth strategy configurations
63
+ email: j-dzak@northwestern.edu
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - Gemfile
70
+ - README.md
71
+ - Rakefile
72
+ - lib/omniauth_configure.rb
73
+ - lib/omniauth_configure/central_parameters.rb
74
+ - lib/omniauth_configure/configuration.rb
75
+ - lib/omniauth_configure/rack.rb
76
+ - lib/omniauth_configure/railtie.rb
77
+ - lib/omniauth_configure/version.rb
78
+ - omniauth_configure.gemspec
79
+ - spec/omniauth_configure/configuration_spec.rb
80
+ - spec/omniauth_configure/rack_spec.rb
81
+ - spec/omniauth_configure/test_configuration.yml
82
+ - spec/spec_helper.rb
83
+ homepage:
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ segments:
96
+ - 0
97
+ hash: -1431042663031854455
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: -1431042663031854455
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.25
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Allows centralized OmniAuth strategy configurations
113
+ test_files: []