g5_authenticatable_api 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +24 -0
  3. data/.rspec +2 -0
  4. data/CHANGELOG.md +18 -0
  5. data/Gemfile +38 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +286 -0
  8. data/Rakefile +1 -0
  9. data/circle.yml +4 -0
  10. data/g5_authenticatable_api.gemspec +23 -0
  11. data/lib/g5_authenticatable_api/helpers/grape.rb +27 -0
  12. data/lib/g5_authenticatable_api/helpers/rails.rb +26 -0
  13. data/lib/g5_authenticatable_api/railtie.rb +11 -0
  14. data/lib/g5_authenticatable_api/token_validator.rb +65 -0
  15. data/lib/g5_authenticatable_api/version.rb +3 -0
  16. data/lib/g5_authenticatable_api.rb +9 -0
  17. data/spec/dummy/README.rdoc +28 -0
  18. data/spec/dummy/Rakefile +6 -0
  19. data/spec/dummy/app/api/hello_api.rb +13 -0
  20. data/spec/dummy/app/assets/images/.keep +0 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  22. data/spec/dummy/app/assets/javascripts/articles.js +2 -0
  23. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  24. data/spec/dummy/app/assets/stylesheets/articles.css +4 -0
  25. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  26. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  27. data/spec/dummy/app/controllers/rails_api/articles_controller.rb +11 -0
  28. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  29. data/spec/dummy/app/helpers/articles_helper.rb +2 -0
  30. data/spec/dummy/app/mailers/.keep +0 -0
  31. data/spec/dummy/app/models/.keep +0 -0
  32. data/spec/dummy/app/models/article.rb +2 -0
  33. data/spec/dummy/app/models/concerns/.keep +0 -0
  34. data/spec/dummy/app/models/user.rb +3 -0
  35. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  36. data/spec/dummy/bin/bundle +3 -0
  37. data/spec/dummy/bin/rails +4 -0
  38. data/spec/dummy/bin/rake +4 -0
  39. data/spec/dummy/config/application.rb +30 -0
  40. data/spec/dummy/config/boot.rb +10 -0
  41. data/spec/dummy/config/database.yml.ci +6 -0
  42. data/spec/dummy/config/database.yml.sample +50 -0
  43. data/spec/dummy/config/environment.rb +5 -0
  44. data/spec/dummy/config/environments/development.rb +29 -0
  45. data/spec/dummy/config/environments/production.rb +80 -0
  46. data/spec/dummy/config/environments/test.rb +36 -0
  47. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  48. data/spec/dummy/config/initializers/devise.rb +254 -0
  49. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  50. data/spec/dummy/config/initializers/inflections.rb +16 -0
  51. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  52. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  53. data/spec/dummy/config/initializers/session_store.rb +3 -0
  54. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  55. data/spec/dummy/config/locales/devise.en.yml +59 -0
  56. data/spec/dummy/config/locales/en.yml +23 -0
  57. data/spec/dummy/config/routes.rb +63 -0
  58. data/spec/dummy/config.ru +4 -0
  59. data/spec/dummy/db/migrate/20140217124048_devise_create_users.rb +15 -0
  60. data/spec/dummy/db/migrate/20140223194521_create_articles.rb +11 -0
  61. data/spec/dummy/db/schema.rb +39 -0
  62. data/spec/dummy/db/seeds.rb +7 -0
  63. data/spec/dummy/lib/assets/.keep +0 -0
  64. data/spec/dummy/lib/tasks/.keep +0 -0
  65. data/spec/dummy/log/.keep +0 -0
  66. data/spec/dummy/public/404.html +58 -0
  67. data/spec/dummy/public/422.html +58 -0
  68. data/spec/dummy/public/500.html +57 -0
  69. data/spec/dummy/public/favicon.ico +0 -0
  70. data/spec/dummy/public/robots.txt +5 -0
  71. data/spec/dummy/vendor/assets/javascripts/.keep +0 -0
  72. data/spec/dummy/vendor/assets/stylesheets/.keep +0 -0
  73. data/spec/factories/user.rb +8 -0
  74. data/spec/lib/g5_authenticatable_api/token_validator_spec.rb +199 -0
  75. data/spec/lib/g5_authenticatable_api/version_spec.rb +7 -0
  76. data/spec/requests/grape_api_spec.rb +21 -0
  77. data/spec/requests/rails_api_spec.rb +13 -0
  78. data/spec/spec_helper.rb +49 -0
  79. data/spec/support/factory_girl.rb +5 -0
  80. data/spec/support/shared_contexts/invalid_access_token.rb +24 -0
  81. data/spec/support/shared_contexts/valid_access_token.rb +7 -0
  82. data/spec/support/shared_examples/token_authenticatable_api.rb +106 -0
  83. data/spec/support/shared_examples/warden_authenticatable_api.rb +31 -0
  84. data/spec/support/warden.rb +4 -0
  85. metadata +223 -0
@@ -0,0 +1,59 @@
1
+ # Additional translations at https://github.com/plataformatec/devise/wiki/I18n
2
+
3
+ en:
4
+ devise:
5
+ confirmations:
6
+ confirmed: "Your account was successfully confirmed."
7
+ send_instructions: "You will receive an email with instructions about how to confirm your account in a few minutes."
8
+ send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes."
9
+ failure:
10
+ already_authenticated: "You are already signed in."
11
+ inactive: "Your account is not activated yet."
12
+ invalid: "Invalid email or password."
13
+ locked: "Your account is locked."
14
+ last_attempt: "You have one more attempt before your account will be locked."
15
+ not_found_in_database: "Invalid email or password."
16
+ timeout: "Your session expired. Please sign in again to continue."
17
+ unauthenticated: "You need to sign in or sign up before continuing."
18
+ unconfirmed: "You have to confirm your account before continuing."
19
+ mailer:
20
+ confirmation_instructions:
21
+ subject: "Confirmation instructions"
22
+ reset_password_instructions:
23
+ subject: "Reset password instructions"
24
+ unlock_instructions:
25
+ subject: "Unlock Instructions"
26
+ omniauth_callbacks:
27
+ failure: "Could not authenticate you from %{kind} because \"%{reason}\"."
28
+ success: "Successfully authenticated from %{kind} account."
29
+ passwords:
30
+ no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided."
31
+ send_instructions: "You will receive an email with instructions about how to reset your password in a few minutes."
32
+ send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
33
+ updated: "Your password was changed successfully. You are now signed in."
34
+ updated_not_active: "Your password was changed successfully."
35
+ registrations:
36
+ destroyed: "Bye! Your account was successfully cancelled. We hope to see you again soon."
37
+ signed_up: "Welcome! You have signed up successfully."
38
+ signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated."
39
+ signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked."
40
+ signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please open the link to activate your account."
41
+ update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and click on the confirm link to finalize confirming your new email address."
42
+ updated: "You updated your account successfully."
43
+ sessions:
44
+ signed_in: "Signed in successfully."
45
+ signed_out: "Signed out successfully."
46
+ unlocks:
47
+ send_instructions: "You will receive an email with instructions about how to unlock your account in a few minutes."
48
+ send_paranoid_instructions: "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
49
+ unlocked: "Your account has been unlocked successfully. Please sign in to continue."
50
+ errors:
51
+ messages:
52
+ already_confirmed: "was already confirmed, please try signing in"
53
+ confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one"
54
+ expired: "has expired, please request a new one"
55
+ not_found: "not found"
56
+ not_locked: "was not locked"
57
+ not_saved:
58
+ one: "1 error prohibited this %{resource} from being saved:"
59
+ other: "%{count} errors prohibited this %{resource} from being saved:"
@@ -0,0 +1,23 @@
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # To learn more, please read the Rails Internationalization guide
20
+ # available at http://guides.rubyonrails.org/i18n.html.
21
+
22
+ en:
23
+ hello: "Hello world"
@@ -0,0 +1,63 @@
1
+ Dummy::Application.routes.draw do
2
+ namespace :rails_api do
3
+ resources :articles, only: :index, defaults: {format: 'json'}
4
+ end
5
+
6
+ devise_for :users
7
+ mount HelloAPI => '/grape_api'
8
+
9
+ # The priority is based upon order of creation: first created -> highest priority.
10
+ # See how all your routes lay out with "rake routes".
11
+
12
+ # You can have the root of your site routed with "root"
13
+ root 'welcome#index'
14
+
15
+ # Example of regular route:
16
+ # get 'products/:id' => 'catalog#view'
17
+
18
+ # Example of named route that can be invoked with purchase_url(id: product.id)
19
+ # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
20
+
21
+ # Example resource route (maps HTTP verbs to controller actions automatically):
22
+ # resources :products
23
+
24
+ # Example resource route with options:
25
+ # resources :products do
26
+ # member do
27
+ # get 'short'
28
+ # post 'toggle'
29
+ # end
30
+ #
31
+ # collection do
32
+ # get 'sold'
33
+ # end
34
+ # end
35
+
36
+ # Example resource route with sub-resources:
37
+ # resources :products do
38
+ # resources :comments, :sales
39
+ # resource :seller
40
+ # end
41
+
42
+ # Example resource route with more complex sub-resources:
43
+ # resources :products do
44
+ # resources :comments
45
+ # resources :sales do
46
+ # get 'recent', on: :collection
47
+ # end
48
+ # end
49
+
50
+ # Example resource route with concerns:
51
+ # concern :toggleable do
52
+ # post 'toggle'
53
+ # end
54
+ # resources :posts, concerns: :toggleable
55
+ # resources :photos, concerns: :toggleable
56
+
57
+ # Example resource route within a namespace:
58
+ # namespace :admin do
59
+ # # Directs /admin/products/* to Admin::ProductsController
60
+ # # (app/controllers/admin/products_controller.rb)
61
+ # resources :products
62
+ # end
63
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,15 @@
1
+ class DeviseCreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table(:users) do |t|
4
+ t.string :email, null: false, default: ''
5
+ t.string :provider, null: false, default: 'g5'
6
+ t.string :uid, null: false
7
+ t.string :g5_access_token
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :users, :email, unique: true
13
+ add_index :users, [:provider, :uid], unique: true
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ class CreateArticles < ActiveRecord::Migration
2
+ def change
3
+ create_table :articles do |t|
4
+ t.string :title
5
+ t.text :body
6
+ t.string :tags
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20140223194521) do
15
+
16
+ # These are extensions that must be enabled in order to support this database
17
+ enable_extension "plpgsql"
18
+
19
+ create_table "articles", force: true do |t|
20
+ t.string "title"
21
+ t.text "body"
22
+ t.string "tags"
23
+ t.datetime "created_at"
24
+ t.datetime "updated_at"
25
+ end
26
+
27
+ create_table "users", force: true do |t|
28
+ t.string "email", default: "", null: false
29
+ t.string "provider", default: "g5", null: false
30
+ t.string "uid", null: false
31
+ t.string "g5_access_token"
32
+ t.datetime "created_at"
33
+ t.datetime "updated_at"
34
+ end
35
+
36
+ add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
37
+ add_index "users", ["provider", "uid"], name: "index_users_on_provider_and_uid", unique: true, using: :btree
38
+
39
+ end
@@ -0,0 +1,7 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
7
+ # Mayor.create(name: 'Emanuel', city: cities.first)
File without changes
File without changes
File without changes
@@ -0,0 +1,58 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/404.html -->
52
+ <div class="dialog">
53
+ <h1>The page you were looking for doesn't exist.</h1>
54
+ <p>You may have mistyped the address or the page may have moved.</p>
55
+ </div>
56
+ <p>If you are the application owner check the logs for more information.</p>
57
+ </body>
58
+ </html>
@@ -0,0 +1,58 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/422.html -->
52
+ <div class="dialog">
53
+ <h1>The change you wanted was rejected.</h1>
54
+ <p>Maybe you tried to change something you didn't have access to.</p>
55
+ </div>
56
+ <p>If you are the application owner check the logs for more information.</p>
57
+ </body>
58
+ </html>
@@ -0,0 +1,57 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/500.html -->
52
+ <div class="dialog">
53
+ <h1>We're sorry, but something went wrong.</h1>
54
+ </div>
55
+ <p>If you are the application owner check the logs for more information.</p>
56
+ </body>
57
+ </html>
File without changes
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-agent: *
5
+ # Disallow: /
File without changes
File without changes
@@ -0,0 +1,8 @@
1
+ FactoryGirl.define do
2
+ factory :user do
3
+ sequence(:email) { |n| "user.#{n}@test.host" }
4
+ provider 'g5'
5
+ sequence(:uid) { |n| "remote-user-#{n}" }
6
+ sequence(:g5_access_token) { |n| "token-abc123-#{n}" }
7
+ end
8
+ end
@@ -0,0 +1,199 @@
1
+ require 'spec_helper'
2
+
3
+ describe G5AuthenticatableApi::TokenValidator do
4
+ subject { validator }
5
+
6
+ let(:validator) { described_class.new(params, headers) }
7
+
8
+ let(:headers) {}
9
+ let(:params) { {'access_token' => token_value} }
10
+ let(:token_value) { 'abc123' }
11
+
12
+ describe '#access_token' do
13
+ subject(:access_token) { validator.access_token }
14
+
15
+ context 'with auth header' do
16
+ let(:headers) { {'Authorization' => "Bearer #{token_value}"} }
17
+ let(:params) {}
18
+
19
+ it 'should extract the token value from the header' do
20
+ expect(access_token).to eq(token_value)
21
+ end
22
+ end
23
+
24
+ context 'with auth param' do
25
+ let(:params) { {'access_token' => token_value} }
26
+ let(:headers) {}
27
+
28
+ it 'should extract the token value from the access_token parameter' do
29
+ expect(access_token).to eq(token_value)
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '#validate!' do
35
+ subject(:validate!) { validator.validate! }
36
+
37
+ context 'when token is valid' do
38
+ include_context 'valid access token'
39
+
40
+ it 'should initialize the auth client with the access token' do
41
+ validate!
42
+ expect(a_request(:get, 'auth.g5search.com/oauth/token/info').
43
+ with(headers: {'Authorization' => "Bearer #{token_value}"})).to have_been_made
44
+ end
45
+
46
+ it 'should not raise errors during validation' do
47
+ expect { validate! }.to_not raise_error
48
+ end
49
+
50
+ it 'should not set an error on the validator' do
51
+ validate!
52
+ expect(validator.error).to be_nil
53
+ end
54
+ end
55
+
56
+ context 'when token is invalid' do
57
+ include_context 'invalid access token'
58
+
59
+ it 'should re-raise the OAuth error' do
60
+ expect { validate! }.to raise_error(OAuth2::Error)
61
+ end
62
+
63
+ it 'should set the error on the validator' do
64
+ begin
65
+ validate!
66
+ rescue StandardError => validation_error
67
+ expect(validator.error).to eq(validation_error)
68
+ end
69
+ end
70
+ end
71
+
72
+ context 'when there is no token' do
73
+ let(:params) {}
74
+ let(:headers) {}
75
+
76
+ it 'should raise an error' do
77
+ expect { validate! }.to raise_error(RuntimeError)
78
+ end
79
+
80
+ it 'should set an error on the validator' do
81
+ begin
82
+ validate!
83
+ rescue RuntimeError => validation_error
84
+ expect(validator.error).to eq(validation_error)
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ describe '#valid?' do
91
+ subject(:valid?) { validator.valid? }
92
+
93
+ context 'when token is valid' do
94
+ include_context 'valid access token'
95
+
96
+ it 'should be valid' do
97
+ expect(validator).to be_valid
98
+ end
99
+
100
+ it 'should not set an error on the validator' do
101
+ valid?
102
+ expect(validator.error).to be_nil
103
+ end
104
+ end
105
+
106
+ context 'when token is invalid' do
107
+ include_context 'invalid access token'
108
+
109
+ it 'should not be valid' do
110
+ expect(validator).to_not be_valid
111
+ end
112
+
113
+ it 'should set an error on the validator' do
114
+ expect { valid? }.to change { validator.error }.from(nil).to(an_instance_of(OAuth2::Error))
115
+ end
116
+ end
117
+
118
+ context 'without token' do
119
+ let(:params) {}
120
+ let(:headers) {}
121
+
122
+ it 'should not be valid' do
123
+ expect(validator).to_not be_valid
124
+ end
125
+
126
+ it 'should set an error on the validator' do
127
+ expect { valid? }.to change { validator.error }.
128
+ from(nil).to(an_instance_of(RuntimeError))
129
+ end
130
+ end
131
+ end
132
+
133
+ describe '#auth_response_header' do
134
+ subject(:auth_response_header) { validator.auth_response_header }
135
+
136
+ let(:header_parts) { auth_response_header.match(auth_header_regex) }
137
+
138
+ context 'with invalid token error' do
139
+ include_context 'invalid access token'
140
+ before { validator.valid? }
141
+
142
+ let(:auth_header_regex) do
143
+ /Bearer error="(?<error>.+)",error_description="(?<error_description>.*)"/
144
+ end
145
+
146
+ it 'should be in the expected format' do
147
+ expect(auth_response_header).to match(auth_header_regex)
148
+ end
149
+
150
+ it 'should have the correct error code' do
151
+ expect(header_parts['error']).to eq(error_code)
152
+ end
153
+
154
+ it 'should have the correct error description' do
155
+ expect(header_parts['error_description']).to eq(error_description)
156
+ end
157
+ end
158
+
159
+ context 'with generic auth server error' do
160
+ include_context 'OAuth2 error'
161
+ before { validator.valid? }
162
+
163
+ let(:auth_header_regex) do
164
+ /Bearer error="(?<error>.+)"/
165
+ end
166
+
167
+ it 'should be in the expected format' do
168
+ expect(auth_response_header).to match(auth_header_regex)
169
+ end
170
+
171
+ it 'should have the default error code' do
172
+ expect(header_parts['error']).to eq('invalid_request')
173
+ end
174
+
175
+ it 'should not have an error description' do
176
+ expect(auth_response_header).to_not match(/error_description/)
177
+ end
178
+ end
179
+
180
+ context 'without token' do
181
+ let(:params) {}
182
+ let(:headers) {}
183
+ before { validator.valid? }
184
+
185
+ it 'should not include any error data' do
186
+ expect(auth_response_header).to eq('Bearer')
187
+ end
188
+ end
189
+
190
+ context 'with valid token' do
191
+ include_context 'valid access token'
192
+ before { validator.valid? }
193
+
194
+ it 'should be nil' do
195
+ expect(auth_response_header).to be_nil
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe G5AuthenticatableApi do
4
+ it 'should have a version' do
5
+ expect(G5AuthenticatableApi::VERSION).to be
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'a secure Grape API endpoint' do
4
+ let(:endpoint) { '/grape_api/hello' }
5
+ let(:params) {}
6
+ let(:headers) {}
7
+
8
+ describe 'GET request' do
9
+ subject(:api_call) { get endpoint, params, headers }
10
+
11
+ it_should_behave_like 'a warden authenticatable api'
12
+ it_should_behave_like 'a token authenticatable api'
13
+ end
14
+
15
+ describe 'POST request' do
16
+ subject(:api_call) { post endpoint, params, headers }
17
+
18
+ it_should_behave_like 'a warden authenticatable api'
19
+ it_should_behave_like 'a token authenticatable api'
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'A Secure Rails API endpoint' do
4
+ let(:params) {}
5
+ let(:headers) {}
6
+
7
+ describe 'GET request' do
8
+ subject(:api_call) { get '/rails_api/articles', params, headers }
9
+
10
+ it_should_behave_like 'a warden authenticatable api'
11
+ it_should_behave_like 'a token authenticatable api'
12
+ end
13
+ end