grape_devise_token_auth 0.1.0

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: 52925f8c667a9fb733ff96c62b0954d58d58d7cd
4
+ data.tar.gz: fa72880434bd05f866161c0c8da9bc9a0af3cb94
5
+ SHA512:
6
+ metadata.gz: da3ffa3915aa58fe11c467c9e15dee10cb1f78a09a8faa77a0c3fa5b4e8aaaaa2d3de24bc7c500a1a28b226e91279e5f5441b2b50a3d37be89ae74d56d76abba
7
+ data.tar.gz: 19b085008ec2b60507a3a968013b78c7ad0bcab3e5cf739cfcae49331c0a6cefe6e79a030a96f12a15f21b874b09fa47115e41420a8503427aa0aafb40f5630b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grape_devise_token_auth.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Michael Cordell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # GrapeDeviseTokenAuth
2
+
3
+ GrapeDeviseTokenAuth gem is a compatability layer between
4
+ [devise_token_auth][1] and [grape][2]. It is useful when mounting a grape API
5
+ in a rails application where [devise][3] (or `devise_token_auth` + `devise`)
6
+ is already present. It is reliant on `devise_token_auth` and `devise`,
7
+ therefore it is not suitable for grape where these are not present.
8
+
9
+ The majority of the hard work and credit goes to [Lyann Dylan
10
+ Hurley][4] and his fantistic [devise_token_auth][1] gem.
11
+ I merely have ported this to work well with grape.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'grape_devise_token_auth'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install grape_devise_token_auth
28
+
29
+ ## Usage
30
+
31
+ Place this line in an initializer in your rails app or at least somewhere before
32
+ the grape API will get loaded:
33
+
34
+ ```ruby
35
+ GrapeDeviseTokenAuth.setup!(true)
36
+ ```
37
+
38
+ Within the Grape API:
39
+
40
+ ```
41
+ class Posts < Grape::API
42
+ auth :grape_devise_token_auth, resource_class: :user
43
+
44
+ helpers GrapeDeviseTokenAuth::AuthHelpers
45
+
46
+ # ...
47
+ end
48
+ ```
49
+
50
+ including the helpers line allows you to use methods the `current_user` and
51
+ `authenticated?` within the API.
52
+
53
+ The resource class option allows you to specific the scope that will be
54
+ authenticated, this corresponds to your devise mapping.
55
+
56
+ All calls will now be authenticated in the above API via rack middleware.
57
+
58
+ ## Testing and Example
59
+
60
+ Currently I am using [this repo][5] to test this gem, eventually I plan on
61
+ migrating the tests into the `grape_devise_token_auth` repo. For now though, I
62
+ refer you to that repo for how to integrate with an existing `devise` and
63
+ `devise_token_auth` repo.
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it ( https://github.com/[my-github-username]/grape_devise_token_auth/fork )
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create a new Pull Request
78
+
79
+ [1]: https://github.com/lynndylanhurley/devise_token_auth
80
+ [2]: https://github.com/intridea/grape
81
+ [3]: https://github.com/plataformatec/devise
82
+ [4]: https://github.com/lynndylanhurley
83
+ [5]: https://github.com/mcordell/rails_grape_auth
84
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "grape_devise_token_auth"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grape_devise_token_auth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "grape_devise_token_auth"
8
+ spec.version = GrapeDeviseTokenAuth::VERSION
9
+ spec.authors = ["Michael Cordell"]
10
+ spec.email = ["mike@mikecordell.com"]
11
+
12
+ spec.summary = %q{Allows an existing devise_token_auth/rails project to authenticate a Grape API}
13
+ spec.homepage = "https://github.com/mcordell/grape_devise_token_auth"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.8"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_dependency 'grape', '> 0.9.0'
22
+ spec.add_dependency 'devise', '~> 3.3'
23
+ spec.add_dependency 'devise_token_auth', '~> 0.1.32.beta9'
24
+ end
@@ -0,0 +1,21 @@
1
+ module GrapeDeviseTokenAuth
2
+ module AuthHelpers
3
+ def self.included(_base)
4
+ Devise.mappings.keys.each do |mapping|
5
+ define_method("current_#{mapping}") do
6
+ warden.session_serializer.fetch(:user)
7
+ end
8
+ end
9
+ end
10
+
11
+ def warden
12
+ @warden ||= env['warden']
13
+ end
14
+
15
+ def authenticated?(scope = :user)
16
+ user_type = "current_#{scope}"
17
+ return false unless respond_to?(user_type)
18
+ !!send(user_type)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,143 @@
1
+ module GrapeDeviseTokenAuth
2
+ class Middleware
3
+ ACCESS_TOKEN_KEY = 'HTTP_ACCESS_TOKEN'
4
+ EXPIRY_KEY = 'HTTP_EXPIRY'
5
+ UID_KEY = 'HTTP_UID'
6
+ CLIENT_KEY = 'HTTP_CLIENT'
7
+
8
+ def initialize(app, resource_name)
9
+ @app = app
10
+ resource_class_from_mapping(resource_name)
11
+ end
12
+
13
+ def call(env)
14
+ setup(env)
15
+ user = authenticate_from_token
16
+ return unauthorized unless user
17
+ sign_in_user(user)
18
+ responses_with_auth_headers(*@app.call(env))
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :uid, :client_id, :token, :expiry, :user, :resource_class, :resource, :warden, :batch_request_buffer_throttle, :request_start
24
+
25
+ def setup(env)
26
+ @request_start = Time.now
27
+ @uid = env[UID_KEY]
28
+ @client_id = env[CLIENT_KEY] || 'default'
29
+ @token = env[ACCESS_TOKEN_KEY]
30
+ @expiry = env[EXPIRY_KEY]
31
+ @warden = env['warden']
32
+ end
33
+
34
+ def sign_in_user(user)
35
+ # user already logged in from devise:
36
+ return resource if resource
37
+ @resource = user
38
+ set_user_in_warden(:user, user)
39
+ end
40
+
41
+ #extracted and simplified from Devise
42
+ def set_user_in_warden(scope, resource)
43
+ scope = Devise::Mapping.find_scope!(scope)
44
+ warden.session_serializer.store(resource, scope)
45
+ end
46
+
47
+ def resource_from_existing_devise_user
48
+ warden_user = warden.user(resource_class.to_s.underscore.to_sym)
49
+ return unless warden_user && warden_user.tokens[client_id].nil?
50
+ @resource = warden_user
51
+ @resource.create_new_auth_token
52
+ end
53
+
54
+ def authenticate_from_token(mapping = nil)
55
+ resource_class_from_mapping(mapping)
56
+ return nil unless resource_class
57
+
58
+ resource_from_existing_devise_user
59
+ return resource if correct_resource_type_logged_in?
60
+
61
+ return nil unless token_request_valid?
62
+
63
+ user = resource_class.find_by_uid(uid)
64
+
65
+ return nil unless user && user.valid_token?(token, client_id)
66
+
67
+ user
68
+ end
69
+
70
+ def token_request_valid?
71
+ token && uid
72
+ end
73
+
74
+ def correct_resource_type_logged_in?
75
+ resource && resource.class == resource_class
76
+ end
77
+
78
+ def resource_class_from_mapping(m)
79
+ mapping = m ? Devise.mappings[m] : Devise.mappings.values.first
80
+ @resource_class = mapping.to
81
+ end
82
+
83
+ def valid?
84
+ keys_present? && !expired?
85
+ end
86
+
87
+ def keys_present?
88
+ uid.present? && client_id.present? && token.present?
89
+ end
90
+
91
+ def expired?
92
+ env[EXPIRY_KEY].to_i < Time.now.to_i
93
+ end
94
+
95
+ def responses_with_auth_headers(status, headers, response)
96
+ [
97
+ status,
98
+ headers.merge(auth_headers),
99
+ response
100
+ ]
101
+ end
102
+
103
+ def auth_headers
104
+ return {} unless resource && resource.valid? && client_id
105
+ auth_headers_from_resource
106
+ end
107
+
108
+ def auth_headers_from_resource
109
+ auth_headers = {}
110
+ resource.with_lock do
111
+ if !DeviseTokenAuth.change_headers_on_each_request
112
+ auth_headers = resource.extend_batch_buffer(token, client_id)
113
+ elsif batch_request?
114
+ resource.extend_batch_buffer(token, client_id)
115
+ # don't set any headers in a batch request
116
+ else
117
+ auth_headers = resource.create_new_auth_token(client_id)
118
+ end
119
+ end
120
+ auth_headers
121
+ end
122
+
123
+ def unauthorized
124
+ [401,
125
+ { 'Content-Type' => 'application/json'
126
+ },
127
+ []
128
+ ]
129
+ end
130
+
131
+ def batch_request?
132
+ @batch_request ||= resource.tokens[client_id] &&
133
+ resource.tokens[client_id]['updated_at'] &&
134
+ within_batch_request_window?
135
+ end
136
+
137
+ def within_batch_request_window?
138
+ end_of_window = Time.parse(resource.tokens[client_id]['updated_at']) +
139
+ DeviseTokenAuth.batch_request_buffer_throttle
140
+ request_start < end_of_window
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,3 @@
1
+ module GrapeDeviseTokenAuth
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'grape_devise_token_auth/version'
2
+ require 'grape_devise_token_auth/middleware'
3
+ require 'grape_devise_token_auth/auth_helpers'
4
+ require 'grape'
5
+
6
+ module GrapeDeviseTokenAuth
7
+ def self.setup!(middleware = false)
8
+ add_auth_strategy if middleware
9
+ end
10
+
11
+ def self.add_auth_strategy
12
+ Grape::Middleware::Auth::Strategies.add(
13
+ :grape_devise_token_auth,
14
+ GrapeDeviseTokenAuth::Middleware,
15
+ ->(options) { [options[:resource_class]] }
16
+ )
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape_devise_token_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Cordell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: grape
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: devise
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: devise_token_auth
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.1.32.beta9
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.1.32.beta9
83
+ description:
84
+ email:
85
+ - mike@mikecordell.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - grape_devise_token_auth.gemspec
100
+ - lib/grape_devise_token_auth.rb
101
+ - lib/grape_devise_token_auth/auth_helpers.rb
102
+ - lib/grape_devise_token_auth/middleware.rb
103
+ - lib/grape_devise_token_auth/version.rb
104
+ homepage: https://github.com/mcordell/grape_devise_token_auth
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.5
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Allows an existing devise_token_auth/rails project to authenticate a Grape
128
+ API
129
+ test_files: []