grape-doorkeeper 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a4ac0769056de817dc467e2b4be02afc7b3f765
4
+ data.tar.gz: 636b183afb2133cb038b82be20eadab3b9fa6445
5
+ SHA512:
6
+ metadata.gz: 222c2a6103a496cac88a9b8c33b283418e46746939327d9a7f053d848bc57fe1099c213002851f52170ee3ac66150aba9a2bcd20da20172a71aba625d7eeb05c
7
+ data.tar.gz: 36cb04ff3de380c333823f8d91fb4c0094b34f44ea89f3c5141651017fdcec48cfe0bd45a21801cd7a6c6f9c99d74ac2f1bdaa0e7a174c834571bbe7685bee4f
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+
7
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Alexey Shcherbakov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ grape-doorkeeper
2
+ ================
3
+
4
+ [![Code Climate](https://codeclimate.com/github/fuCtor/grape-doorkeeper.png)](https://codeclimate.com/github/fuCtor/grape-doorkeeper)
5
+
6
+ Integration Grape with Doorkeeper
7
+
8
+ ```ruby
9
+ class API < Grape::API
10
+ helpers do
11
+ def current_token; env['api.token'] end
12
+ def current_user; @current_user ||= Acl::User.find(current_token.resource_owner_id) if current_token end
13
+ end
14
+
15
+ doorkeeper_for :all, scopes: [:read]
16
+
17
+ get :hello do
18
+ {:hello => current_user.email }
19
+ end
20
+
21
+ get :by, protected: false do
22
+ {:by => 1 }
23
+ end
24
+
25
+ # Use a different scope for this endpoint.
26
+ get :bar, scopes: [:write] do
27
+ {:bar=> 1 }
28
+ end
29
+
30
+ resource :statuses do
31
+ get :count do
32
+ {:count => 0 }
33
+ end
34
+
35
+ end
36
+ end
37
+ ```
@@ -0,0 +1,28 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "grape-doorkeeper/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "grape-doorkeeper"
6
+ s.version = GrapeDoorkeeper::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Alexey Shcherbakov"]
9
+ s.email = ["schalexey@gmail.com"]
10
+ s.homepage = "https://github.com/intridea/grape"
11
+ s.summary = %q{Gem for auth in grape via doorkeeper.}
12
+ s.description = %q{Gem for auth in grape via doorkeeper.}
13
+ s.license = "MIT"
14
+
15
+ s.rubyforge_project = "grape-doorkeeper"
16
+
17
+ s.add_runtime_dependency 'grape', '~> 0.6'
18
+ s.add_runtime_dependency 'doorkeeper', '~> 0'
19
+
20
+ s.add_development_dependency 'rack-test', '~> 0'
21
+ s.add_development_dependency 'rspec', '~> 2.9'
22
+ s.add_development_dependency 'bundler', '~> 0'
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
+ s.require_paths = ["lib"]
28
+ end
@@ -0,0 +1,6 @@
1
+ require 'grape'
2
+ require 'doorkeeper'
3
+ require 'grape-doorkeeper/oauth2'
4
+
5
+ module GrapeDoorkeeper
6
+ end
@@ -0,0 +1,73 @@
1
+ require 'grape/api'
2
+ require 'doorkeeper/doorkeeper_for'
3
+ module GrapeDoorkeeper
4
+ # OAuth 2.0 authorization for Grape APIs.
5
+
6
+ class Middleware < Grape::Middleware::Auth::OAuth2
7
+
8
+ def protected_endpoint?
9
+ endpoint = env['api.endpoint']
10
+ return endpoint.options[:route_options][:protected] if endpoint.options[:route_options].key?(:protected)
11
+
12
+ filter_options = options[:doorkeeper].filter_options
13
+ return true if filter_options.blank? #protect all routes
14
+
15
+ action = endpoint.namespace.split('/')[1] || endpoint.options[:path].first
16
+
17
+ if filter_options[:only]
18
+ return filter_options[:only].include?( action.to_sym )
19
+ elsif filter_options[:except]
20
+ return !filter_options[:except].include?( action.to_sym )
21
+ end
22
+
23
+ false
24
+ end
25
+
26
+ def verify_token(token_string)
27
+ return unless protected_endpoint?
28
+ token = Doorkeeper::AccessToken.authenticate(token_string)
29
+ doorkeeper = options[:doorkeeper]
30
+ if env['api.endpoint'].options[:route_options].key?(:scopes)
31
+ doorkeeper = Doorkeeper::DoorkeeperForBuilder.create_doorkeeper_for(:all, scopes: env['api.endpoint'].options[:route_options][:scopes])
32
+ end
33
+
34
+ if token
35
+ if !token.accessible?
36
+ error_out(401, 'expired_token')
37
+ else
38
+ if token.includes_scope?(doorkeeper.scopes)
39
+ env['api.token'] = token
40
+ else
41
+ error_out(403, 'insufficient_scope')
42
+ end
43
+ end
44
+ else
45
+ error_out(401, 'invalid_token')
46
+ end
47
+ end
48
+
49
+ def error_out(status, error)
50
+ scopes = options[:doorkeeper].instance_variable_get(:@scopes)
51
+
52
+ throw :error,
53
+ message: {error: error},
54
+ status: status,
55
+ headers: {
56
+ 'Content-Type' => 'application/json',
57
+ 'X-Accepted-OAuth-Scopes' => scopes,
58
+ 'WWW-Authenticate' => "OAuth realm='#{options[:realm]}', error='#{error}'"
59
+ }.reject { |k,v| v.nil? }
60
+ end
61
+
62
+ end
63
+
64
+ module OAuth2
65
+ def doorkeeper_for *args
66
+ doorkeeper_for = Doorkeeper::DoorkeeperForBuilder.create_doorkeeper_for(*args)
67
+ use GrapeDoorkeeper::Middleware, doorkeeper: doorkeeper_for
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ Grape::API.extend GrapeDoorkeeper::OAuth2
@@ -0,0 +1,3 @@
1
+ module GrapeDoorkeeper
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-doorkeeper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Shcherbakov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: doorkeeper
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Gem for auth in grape via doorkeeper.
84
+ email:
85
+ - schalexey@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - grape-doorkeeper.gemspec
95
+ - lib/grape-doorkeeper.rb
96
+ - lib/grape-doorkeeper/oauth2.rb
97
+ - lib/grape-doorkeeper/version.rb
98
+ homepage: https://github.com/intridea/grape
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project: grape-doorkeeper
118
+ rubygems_version: 2.2.2
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Gem for auth in grape via doorkeeper.
122
+ test_files: []