sinatra_auth_oauthed 0.0.1

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: 353b360d0464204e9c6dff56f78882898bdcc30d
4
+ data.tar.gz: ba37ca3332426e2afe16f4dc8f86e15a98a7a411
5
+ SHA512:
6
+ metadata.gz: cd95a837e02574e1ec23a2d9996772a6226ca5e9a673e779356d4cc51c653a054e0eb26a2380ac8e84b196f721896add1dacba15fdea4b577db30733c8909e52
7
+ data.tar.gz: b433beb0a42b3edbe59d4663d99e7ba3b06459d097b940a20ee3243c12905eb51dd09488ca987f9dd280e0470b24398e95bd884f0ac23cdbc4b38b6b1b105482
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ coverage
2
+ .bundle
3
+ pkg
4
+ .DS_Store
5
+ Gemfile.lock
6
+ vendor/gems
7
+ *.gem
8
+ .rbenv-version
9
+ bin/
10
+ tags
11
+ .ruby-*
12
+ .sass-cache
13
+ *.swp
14
+ *.swo
15
+ .ruby-*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Documentation:
2
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ ruby '2.2.2'
3
+
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ group :red_green_refactor, halt_on_fail: true do
2
+ guard :rspec, cmd: 'bundle exec rspec' do
3
+ watch(%r{^spec/.+_spec\.rb$})
4
+ watch(%r{^config/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
5
+ end
6
+
7
+ guard :rubocop, all_on_start: false do
8
+ watch(/.+\.rb$/)
9
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
10
+ end
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2015 Seth Herr
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ ### A [Sinatra](http://www.sinatrarb.com/) template for [Grape-Doorkeep applications](https://github.com/sethherr/grape-doorkeeper)
2
+
3
+ A template application for building mini web sites/services authenticated with OAuth2.
4
+
5
+ - No database required
6
+ - [warden-oauth](https://github.com/Zensaburou/warden-oauthed) for authentication
7
+ - [Includes rails assets for bower package management](#rails-assets)
8
+
9
+ [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)
10
+
11
+ ## Using it:
12
+
13
+ 1. Fork or clone this repository
14
+ 2. Update the values in [.env](.env) with [your values](#env-values)
15
+ 3. Go to the directory this is contained in the terminal
16
+ 4. Use `rerun rackup` to start the server
17
+ 5. The above command will print out something like `WEBrick::HTTPServer#start: pid=62447 port=9292`. Go to localhost:{that_port_number} - e.g. [http://localhost:9292](http://localhost:9292).
18
+ 6. Go there to the above url to see the app! :sparkles:
19
+
20
+ ## Env Values
21
+
22
+ The `.env` file references the app you are using. You will need to create an app on the provider you're using.
23
+
24
+ The callback url for this sinatra template is `localhost:9292/auth/oauthed/callback` - change localhost:9292 to reflect your local conditions - e.g., if you're running on a different port, or if you deploy this to something with a domain name. You will have to add this to the application on the provider.
25
+
26
+ After you've created an application on the provider, add the client id and the client secret of it to the `.env` file.
27
+
28
+ For the `APPLICATION_SCOPES_REQUESTED`, separate scopes with whitespace
29
+ For `USER_ATTRIBUTES`, separate values with whitespace
30
+
31
+ ## Directories / files
32
+
33
+ *The directory structure of this app is set up to loosely resemble rails*
34
+
35
+ - `assets`
36
+ - javascript, coffeescript, css, scss - compiled, minified, concatenated.
37
+ - `views`
38
+ - haml, erb or html files. `layout.haml` is the layout file that wraps everything else up.
39
+ - `public`
40
+ - files in here are served directly from the base url
41
+ - `config/routes.rb`
42
+ - the routes for the app
43
+
44
+
45
+ ## Rails Assets
46
+
47
+ This template includes [rails-assets](https://rails-assets.org/), which makes it easy to use [bower](http://bower.io/) packages.
48
+
49
+ **View [the list of bower packages](http://bower.io/search/)**
50
+
51
+ Add bower packages to the Gemfile by putting them in the Bower packages block in this format:
52
+
53
+ gem 'rails-assets-BOWER_PACKAGE_NAME'`.
54
+
55
+ Since Javascript packages change quickly, it's a particularly good idea to lock the packages with a loose version after you `bundle install`. e.g. for the jQuery version currently installed,
56
+
57
+ `gem 'rails-assets-jquery', '~> 2.1.4'`
58
+
59
+ *(which means >= 2.1.4 and < 2.2.0).*
60
+
61
+ So you can safely update your packages with `bundle update`
62
+
63
+ =======
64
+
65
+ Many thanks to [sinatra_auth_github](https://github.com/atmos/sinatra_auth_github) :shipit: from which this draws inspiration.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ task default: [:spec]
2
+ require 'rspec/core/rake_task'
3
+ require 'dotenv/tasks'
4
+
5
+ desc 'Run specs'
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.pattern = 'spec/**/*_spec.rb'
8
+ end
data/config.ru ADDED
@@ -0,0 +1,9 @@
1
+ ENV['RACK_ENV'] ||= 'development'
2
+ require 'bundler'
3
+ require 'bundler/setup'
4
+
5
+ $LOAD_PATH << File.dirname(__FILE__) + '/lib'
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'sinatra_auth_oauthed'))
7
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec', 'app'))
8
+
9
+ run Example::App
@@ -0,0 +1,111 @@
1
+ require 'sinatra/base'
2
+ require 'warden-oauthed'
3
+
4
+ module Sinatra
5
+ module Auth
6
+ module Oauthed
7
+ class BadAuthentication < Sinatra::Base
8
+ enable :raise_errors
9
+ disable :show_exceptions
10
+
11
+ helpers do
12
+ def unauthorized_template
13
+ @unauthenticated_template ||= File.read(File.join(File.dirname(__FILE__), 'views', '401.html'))
14
+ end
15
+ end
16
+
17
+ get '/unauthenticated' do
18
+ status 403
19
+ unauthorized_template
20
+ end
21
+ end
22
+
23
+ module Helpers
24
+ def warden
25
+ env['warden']
26
+ end
27
+
28
+ def authenticate!(*args)
29
+ warden.authenticate!(*args)
30
+ end
31
+
32
+ def authenticated?(*args)
33
+ warden.authenticated?(*args)
34
+ end
35
+
36
+ def logout!
37
+ warden.logout
38
+ end
39
+
40
+ # The authenticated user object
41
+ def oauthed_user
42
+ warden.user
43
+ end
44
+
45
+ # Send a API GET request to the path defined in .env
46
+ #
47
+ # path - the path on api.github.com to hit
48
+ #
49
+ # Returns a rest client response object
50
+ #
51
+ # Examples
52
+ # oauthed_raw_request("/user")
53
+ # # => RestClient::Response
54
+ def oauthed_raw_request(path)
55
+ oauthed_user.oauthed_raw_request(path)
56
+ end
57
+
58
+ # Send a API GET request to the path defined in .env and parse the response body
59
+ #
60
+ # path - the path on api.github.com to hit
61
+ #
62
+ # Returns a parsed JSON response
63
+ #
64
+ # Examples
65
+ # oauthed_request("/user")
66
+ # # => { 'login' => 'atmos', ... }
67
+ def oauthed_request(path)
68
+ oauthed_user.oauthed_request(path)
69
+ end
70
+
71
+ def _relative_url_for(path)
72
+ request.script_name + path
73
+ end
74
+ end
75
+
76
+ def self.registered(app)
77
+ app.use Warden::Manager do |manager|
78
+ manager.default_strategies :oauthed
79
+ manager.failure_app = BadAuthentication
80
+
81
+ manager[:oauthed_client_id] = ENV['APPLICATION_CLIENT_ID']
82
+ manager[:oauthed_secret] = ENV['APPLICATION_CLIENT_SECRET']
83
+ manager[:oauthed_scopes] = ENV['APPLICATION_SCOPES_REQUESTED']
84
+ manager[:oauthed_oauth_domain] = ENV['OAUTH_BASE_URL']
85
+ manager[:oauthed_callback_url] = '/auth/oauthed/callback'
86
+ end
87
+
88
+ # Sign cookie sessions in with AS::Verifier
89
+ ENV['WARDEN_OAUTHED_VERIFIER_SECRET'] ||= ENV['OAUTHED_VERIFIER_SECRET']
90
+
91
+ unless ENV['WARDEN_OAUTHED_VERIFIER_SECRET']
92
+ warn 'No WARDEN_OAUTHED_VERIFIER_SECRET environmental variable found.'
93
+ warn 'Your sessions are likely being stored insecurely.'
94
+ end
95
+
96
+ app.helpers Helpers
97
+
98
+ app.get '/auth/oauthed/callback' do
99
+ if params['error']
100
+ redirect '/unauthenticated'
101
+ else
102
+ authenticate!
103
+ redirect '/'
104
+ return_to = session.delete('return_to') || _relative_url_for('/')
105
+ redirect return_to
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,7 @@
1
+ module Sinatra
2
+ module Auth
3
+ module Oauthed
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ <html>YOU CAN't HAVES permission </html>
@@ -0,0 +1 @@
1
+ require 'sinatra/auth/oauthed'
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+ require 'sinatra/auth/oauthed/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'sinatra_auth_oauthed'
7
+ s.version = Sinatra::Auth::Oauthed::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Seth Herr', 'David Jaress']
10
+ s.homepage = 'http://github.com/zensaburou/sinatra_auth_oauthed'
11
+ s.summary = 'A sinatra extension for easy oauth integration with oauth providers'
12
+ s.license = 'MIT'
13
+ s.description = s.summary
14
+
15
+ s.add_dependency 'sinatra', '~>1.0'
16
+ s.add_dependency 'warden-oauthed', '~>0.0.0'
17
+
18
+ s.add_development_dependency 'rake', '~> 10.4', '>=10.4.2'
19
+ s.add_development_dependency 'rspec', '~>2.4', '>=2.4.0'
20
+ s.add_development_dependency 'shotgun', '~> 0.9', '>=0.9.1'
21
+ s.add_development_dependency 'randexp', '~>0.1.5'
22
+ s.add_development_dependency 'rack-test', '~>0.5.3'
23
+ s.add_development_dependency 'addressable', '~>2.3', '>=2.3.8'
24
+
25
+ s.files = `git ls-files`.split("\n")
26
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
27
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
28
+ s.require_paths = ['lib']
29
+ end
data/spec/app.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Example
2
+ class App < Sinatra::Base
3
+ enable :sessions
4
+ register Sinatra::Auth::Oauthed
5
+
6
+ get '/logout' do
7
+ logout!
8
+ 'Peace!'
9
+ end
10
+
11
+ get '/' do
12
+ authenticate!
13
+ @user = oauthed_user
14
+ "authenticated: #{@user.email}"
15
+ end
16
+ end
17
+ end
data/spec/app_spec.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Example::App do
4
+ def app
5
+ @app ||= Example::App
6
+ end
7
+
8
+ describe "GET '/'" do
9
+ it 'redirects to redirect uri when requesting a url requiring authentication' do
10
+ response = get '/'
11
+
12
+ uri = Addressable::URI.parse(response.headers['Location'])
13
+
14
+ uri.scheme.should eql('http')
15
+ uri.host.should eql('localhost')
16
+
17
+ params = uri.query_values
18
+ expect(params['response_type']).to eq 'code'
19
+ expect(params['scope']).to eq 'public'
20
+ expect(params['client_id']).to match(/\w{20}/)
21
+ expect(params['redirect_uri']).to eq 'http://example.org/auth/oauthed/callback'
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ ENV['APPLICATION_CLIENT_ID'] = 'dddc188f84e348ddae69f17dd63e758ded4cc24abed435a53db9525db2cd7af45f0'
2
+ ENV['APPLICATION_CLIENT_SECRET'] = 'e35b8e02901a3ca1b5b1e0c4cf3e1e6dbd1855de4684bd95175721428816ae96'
3
+ ENV['APPLICATION_SCOPES_REQUESTED'] = 'public'
4
+ ENV['OAUTH_BASE_URL'] = 'http://localhost:3000'
5
+ ENV['OAUTHED_VERIFIER_SECRET'] = 'MyStu9GTLJX7vhF0LKQ9dQ-cjjTwMFWTkHV6VqE7ipBpwdwwamVdJx1AXkCuOu4g'
6
+ ENV['USER_ATTRIBUTES'] = 'id full_name email'
7
+ ENV['RACK_ENV'] = 'test'
8
+
9
+ require 'bundler'
10
+ require 'rubygems' unless defined?(Gem)
11
+ Bundler.require(:default, ENV['RACK_ENV'].to_sym)
12
+ require 'addressable/uri'
13
+ require 'pp'
14
+ require 'rack/test'
15
+ require 'sinatra/auth/oauthed'
16
+ require 'app'
17
+
18
+ RSpec.configure do |conf|
19
+ conf.include Rack::Test::Methods
20
+ end
metadata ADDED
@@ -0,0 +1,200 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra_auth_oauthed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Seth Herr
8
+ - David Jaress
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-10-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: warden-oauthed
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.0.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.0.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.4'
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 10.4.2
52
+ type: :development
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - "~>"
57
+ - !ruby/object:Gem::Version
58
+ version: '10.4'
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 10.4.2
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.4'
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.4.0
72
+ type: :development
73
+ prerelease: false
74
+ version_requirements: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - "~>"
77
+ - !ruby/object:Gem::Version
78
+ version: '2.4'
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.4.0
82
+ - !ruby/object:Gem::Dependency
83
+ name: shotgun
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.9'
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 0.9.1
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '0.9'
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 0.9.1
102
+ - !ruby/object:Gem::Dependency
103
+ name: randexp
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: 0.1.5
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: 0.1.5
116
+ - !ruby/object:Gem::Dependency
117
+ name: rack-test
118
+ requirement: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: 0.5.3
123
+ type: :development
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: 0.5.3
130
+ - !ruby/object:Gem::Dependency
131
+ name: addressable
132
+ requirement: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - "~>"
135
+ - !ruby/object:Gem::Version
136
+ version: '2.3'
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: 2.3.8
140
+ type: :development
141
+ prerelease: false
142
+ version_requirements: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: '2.3'
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: 2.3.8
150
+ description: A sinatra extension for easy oauth integration with oauth providers
151
+ email:
152
+ executables: []
153
+ extensions: []
154
+ extra_rdoc_files: []
155
+ files:
156
+ - ".gitignore"
157
+ - ".rspec"
158
+ - ".rubocop.yml"
159
+ - Gemfile
160
+ - Guardfile
161
+ - LICENSE
162
+ - README.md
163
+ - Rakefile
164
+ - config.ru
165
+ - lib/sinatra/auth/oauthed.rb
166
+ - lib/sinatra/auth/oauthed/version.rb
167
+ - lib/sinatra/auth/oauthed/views/401.html
168
+ - lib/sinatra_auth_oauthed.rb
169
+ - sinatra_auth_oauthed.gemspec
170
+ - spec/app.rb
171
+ - spec/app_spec.rb
172
+ - spec/spec_helper.rb
173
+ homepage: http://github.com/zensaburou/sinatra_auth_oauthed
174
+ licenses:
175
+ - MIT
176
+ metadata: {}
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ requirements: []
192
+ rubyforge_project:
193
+ rubygems_version: 2.4.8
194
+ signing_key:
195
+ specification_version: 4
196
+ summary: A sinatra extension for easy oauth integration with oauth providers
197
+ test_files:
198
+ - spec/app.rb
199
+ - spec/app_spec.rb
200
+ - spec/spec_helper.rb