├── .gitignore ├── .rubocop.yml ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── Gemfile ├── MIT-LICENSE ├── README.md ├── ROADMAP.md ├── Rakefile ├── app ├── controllers │ └── knock │ │ ├── application_controller.rb │ │ └── auth_token_controller.rb └── model │ └── knock │ └── auth_token.rb ├── bin └── rails ├── config └── routes.rb ├── gemfiles ├── rails_5.gemfile └── rails_5.gemfile.lock ├── knock.gemspec ├── lib ├── knock.rb ├── knock │ ├── authenticable.rb │ ├── authenticable │ │ └── getter_name.rb │ ├── engine.rb │ ├── tokenizable.rb │ └── version.rb └── tasks │ └── knock_tasks.rake └── test ├── dummy ├── README.rdoc ├── Rakefile ├── app │ ├── assets │ │ ├── config │ │ │ └── manifest.js │ │ ├── images │ │ │ └── .keep │ │ ├── javascripts │ │ │ └── application.js │ │ └── stylesheets │ │ │ └── application.css │ ├── controllers │ │ ├── admin_protected_controller.rb │ │ ├── admin_token_controller.rb │ │ ├── application_controller.rb │ │ ├── composite_name_entity_protected_controller.rb │ │ ├── concerns │ │ │ └── .keep │ │ ├── current_users_controller.rb │ │ ├── custom_unauthorized_entity_controller.rb │ │ ├── guest_protected_controller.rb │ │ ├── protected_resources_controller.rb │ │ ├── v1 │ │ │ └── test_namespaced_controller.rb │ │ ├── vendor_protected_controller.rb │ │ └── vendor_token_controller.rb │ ├── helpers │ │ └── application_helper.rb │ ├── mailers │ │ └── .keep │ ├── models │ │ ├── .keep │ │ ├── admin.rb │ │ ├── composite_name_entity.rb │ │ ├── concerns │ │ │ └── .keep │ │ ├── guest.rb │ │ ├── user.rb │ │ ├── v1 │ │ │ └── user.rb │ │ └── vendor.rb │ └── views │ │ └── layouts │ │ └── application.html.erb ├── bin │ ├── bundle │ ├── rails │ ├── rake │ └── setup ├── config.ru ├── config │ ├── application.rb │ ├── boot.rb │ ├── database.yml │ ├── environment.rb │ ├── environments │ │ ├── development.rb │ │ ├── production.rb │ │ └── test.rb │ ├── initializers │ │ ├── assets.rb │ │ ├── backtrace_silencers.rb │ │ ├── cookies_serializer.rb │ │ ├── filter_parameter_logging.rb │ │ ├── inflections.rb │ │ ├── knock.rb │ │ ├── mime_types.rb │ │ ├── session_store.rb │ │ └── wrap_parameters.rb │ ├── locales │ │ └── en.yml │ ├── routes.rb │ └── secrets.yml ├── db │ ├── migrate │ │ ├── 20150713101607_create_users.rb │ │ ├── 20160519075733_create_admins.rb │ │ ├── 20160522051816_create_vendors.rb │ │ ├── 20160522181712_create_composite_name_entities.rb │ │ └── 20161127203222_create_v1_users.rb │ └── schema.rb ├── lib │ └── assets │ │ └── .keep ├── log │ └── .keep ├── public │ ├── 404.html │ ├── 422.html │ ├── 500.html │ └── favicon.ico └── test │ ├── controllers │ ├── admin_protected_controller_test.rb │ ├── admin_token_controller_test.rb │ ├── composite_name_entity_protected_controller_test.rb │ ├── current_users_controller_test.rb │ ├── custom_unauthorized_entity_controller_test.rb │ ├── guest_protected_controller_test.rb │ ├── protected_resources_controller_test.rb │ ├── v1 │ │ └── test_namespaced_controller_test.rb │ ├── vendor_protected_controller_test.rb │ └── vendor_token_controller_test.rb │ └── models │ ├── admin_test.rb │ ├── user_test.rb │ └── vendor_test.rb ├── fixtures ├── admins.yml ├── composite_name_entities.yml ├── users.yml ├── v1_users.yml └── vendors.yml ├── knock_test.rb ├── model └── knock │ └── auth_token_test.rb ├── support └── generators_test_helper.rb ├── test_helper.rb └── unit └── knock └── authenticable └── getter_name_test.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle/ 2 | log/*.log 3 | pkg/ 4 | test/tmp/ 5 | test/dummy/db/*.sqlite3 6 | test/dummy/db/*.sqlite3-journal 7 | test/dummy/log/*.log 8 | test/dummy/tmp/ 9 | test/dummy/.sass-cache 10 | coverage/ 11 | *.gem 12 | Gemfile.lock 13 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | Style/StringLiterals: 2 | EnforcedStyle: double_quotes 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.6 4 | before_script: 5 | - bundle exec rake db:migrate RAILS_ENV=test 6 | addons: 7 | code_climate: 8 | repo_token: 61c3ea804322306871a6b7e06a0a6435672cd29aea0c2b286e6b837234ba9d90 9 | deploy: 10 | provider: rubygems 11 | api_key: 12 | secure: WxGCqYToLKgYediVNmHUQ5SkJ3hfKIIu6K8b8Fvd4nb2v6p9tUo+Cz4xN/mTU1tUT5xF4D+P3Oek9Ih5GCL56s+i5C8T60v71azupcQoJpr9AlUxNVipJy+BaaNWb/yH2c3GWYtVJmG44s/nBSgG2WJLhkHSjHM+0CuUF0PknW8WUY7+zkZW3XsVh3bv9+O32uSYizdNt2IiccvqcjEKtmD+mZ5U5/Zwosy1ExmGXFXgFCKhK1oBu/ejqQdwRzY3LwquaTg9tEqR3Cl2dwDboXN1Ga5V7Q3+zhqCYPho33CtNr9ZhaeWS/fI3VcUa7O3fzS71JWSn97gqfqhkehXuGbptebLmLOgRjfe7RloH2VVc8NoqTFLYyvptD3522TG/kSH+etOVQgBpTGXf6TDD6FybH/ZQ0efFRzm00feAEmrbW+AkdSonTCVr/LYePnYWBPuBOZmOf2wuV0+84E5Jlw1wFRLG2JzRDWzScREIbZ8Q4ZvUihEKHIy8a76N6TBTRnAQv/FMfpx4wDXWibP3ji7De9HADjgkUOtk5qTKn8flvNGtJ+phBHyNEg0dFVVQdvyxUuhHWZV1SqKZOy4ITRSGzruufnvTrg09HKDDx/jImSrK3mT/64NVLmmnXMCz/CkLXLMcl6cohgyQCugucsNocy66AZYh8fTB3t0LQ4= 13 | gem: knock 14 | on: 15 | tags: true 16 | repo: nsarno/knock 17 | notifications: 18 | slack: 19 | secure: Z/aucu3B6NfMxVOky4FK43hlhrArG7JbkCj6MZvldRzPP5Q/z2GD+isFvvvJVIO92Tu9NkBlGaSYmTKeQH8L5j2oqGKapVfK7zCUuLYuerP96P4OsLywHfH1E9T7xr9lRr/e/oEFmojN4WUoCXCtOUNVLr3e5vGJxmHIwyDKuLZDaTdKmeO6dVBwonkuccgM11d1CQmI9s5IGtv3jtQZGsccVEPQGbCDoD4s4xI8aEcf4YsXdSJ5v8vg0R3XEvc+GTRPvKAdPlKkFHfWgVyu1An0g0hIfX7cvcUEOHiCbstefhAi8AkI9muDqfa2GStOAp5lSdyRs2/MJCr2K7jVNgXfzAg+RarKMpbM9gNAMxL4Qa4O6lwrng5ynL9TUX28uvA4ceLNTycjX/WQdR1EiQf6zS6N/DqxFrCzTE/2JEY4fnaKTt9bmpYMvF+yGDE5TnmrwtUehqCuN8ViSAOFspr6KP947Fu5Q0gDaZAvhiOoDuSYxe1D04f6N+hdaVtrFvPVHWAXp1rR6oQbVAFxJAoh9sAHpI1Fa/y/x1hWJkRf6DFeyRadJblGQsN6894UFlCL6x5RPvT70sZ8EOx6F1EMJpQIKqN7z2bmMkQmsA1RvQm5ZzGqxiwA8ZyyKpUtVYM31hcH1qwGKIUcGkgp0Sv+BHCjW8prnDEK5SARfgI= 20 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | This project adheres to [Semantic Versioning](http://semver.org/). 5 | 6 | ## [2.2.0] - Unreleased 7 | 8 | ### Fixed 9 | 10 | - Autoloading & Zeitwerk issues 11 | - Update dependency to JWT 2.2.1 12 | - Update dependency to Rails 5 13 | - Implement `#respond_to_missing?` in `Authenticable` module 14 | 15 | ### Removed 16 | 17 | - Generators 18 | 19 | ## [2.1.1] - 2017-02-11 20 | 21 | ### Fixed 22 | 23 | - Stop trying to retrieve user from empty payload when no token is given 24 | 25 | ## [2.1] - 2017-01-31 26 | 27 | ### Fixed 28 | 29 | - Parsing of token controller to handle namespaces correctly 30 | 31 | ### Added 32 | 33 | - Configurable default validations by adding `verify_options` parameter to AuthToken initializer 34 | 35 | ## [2.0] - 2016-10-23 36 | 37 | ### Added 38 | 39 | - Configurable unauthorized response by overriding `Authenticable#unauthorized_entity` 40 | 41 | ### Removed 42 | 43 | - Deprecated features (see deprecated features in version 1.5) 44 | 45 | ## [1.5] - 2016-05-29 46 | 47 | ### Added 48 | 49 | - Exception configuration option `Knock.not_found_exception_class_name` 50 | - Multiple entity authentication (e.g. User, Admin, etc) 51 | - Possibility to have permanent tokens 52 | - Adding config options for exception class 53 | - Generator for token controller. E.g. `rails g knock:token_controller user` 54 | 55 | ### Changed 56 | 57 | - Deprecated `Authenticable#authenticate` in favor of `Authenticable#authenticate_user` 58 | - Deprecated use of `Knock.current_user_from_token` in favor of `User.from_token_payload` 59 | - Deprecated use of direct route to `AuthTokenController` in favor of generating a token controller 60 | - No need to mount the engine in `config/routes.rb` anymore 61 | 62 | ## [1.4.2] - 2016-01-29 63 | 64 | ### Fixed 65 | 66 | - Allow use of any or no prefix in authorization header. 67 | This fixes an unwanted breaking change introduced in `1.4.0` forcing the use 68 | of the `Bearer` prefix. 69 | 70 | ## [1.4.1] - 2016-01-08 71 | 72 | ### Fixed 73 | 74 | - Use lambda for audience verification 75 | 76 | ## [1.4.0] - 2016-01-02 77 | 78 | ### Changed 79 | 80 | - Allow use of rails versions above 4.2 81 | 82 | ### Added 83 | 84 | - Travis integration 85 | - Contribution guidelines 86 | - URL authentication 87 | - Allow use of different encoding algorithm 88 | - Expose `current_user` in the controllers without authenticating 89 | 90 | ### Fixed 91 | 92 | - Audience verification in token 93 | - Use lambda syntax compatible with older ruby versions 94 | - A few typos 95 | 96 | ## [1.3.0] - 2015-07-23 97 | 98 | ### Added 99 | 100 | - Configuration option for how the current_user is retrieved when signing in. 101 | - Configuration option for the handle attribute (email by default). 102 | 103 | ## [1.2.0] - 2015-07-16 104 | 105 | ### Added 106 | 107 | - Configuration option for how the current_user is retrieved when validating 108 | a token. (#1) 109 | 110 | ### Changed 111 | 112 | - Use "sub" claim to store the user id by default instead of "user_id". (#1) 113 | 114 | ### Fixed 115 | 116 | - Decode auth0_client_secret in default configuration for Auth0 117 | 118 | ## [1.1.0] - 2015-07-15 119 | 120 | ## [1.1.0.rc1] - 2015-07-15 121 | 122 | ### Added 123 | 124 | - `Knock.token_lifetime` configuration variable 125 | - `Knock.token_secret_signature_key` configuration variable 126 | - `Knock.token_audience` configuration variable 127 | - audience claim verification when decoding token 128 | - `Knock.setup` method for configuration in `knock.rb` initializer 129 | - generator for initializer (rails g knock:install) 130 | 131 | ## [1.0.0] - 2015-07-14 132 | 133 | ## [1.0.0.rc1] - 2015-07-14 134 | 135 | ### Added 136 | 137 | - `Knock::Authenticable` to secure endpoints with `before_action :authenticate` 138 | - `AuthToken` model provides JWT encapsulation 139 | - `AuthTokenController` provides out of the box sign in implementation 140 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Contribution is always highly appreciated. Here are a few guidelines to make it 4 | easier and faster for pull requests to get merged and issues to get fixed. 5 | 6 | ### Issues 7 | 8 | - Provide steps to reproduce the issue 9 | - Even better if possible, provide a failing test 10 | 11 | ### Pull Requests 12 | 13 | - Provide test for any code you add or change 14 | - Add your changes to the changelog in the `Unreleased` section 15 | 16 | Thanks ♥️ 17 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Declare your gem's dependencies in knock.gemspec. 4 | # Bundler will treat runtime dependencies like base dependencies, and 5 | # development dependencies will be added by default to the :development group. 6 | gemspec 7 | 8 | # Declare any dependencies that are still in development here instead of in 9 | # your gemspec. These might include edge Rails or gems from your path or 10 | # Git. Remember to move these dependencies to your gemspec before releasing 11 | # your gem to rubygems.org. 12 | 13 | # To use a debugger 14 | # gem 'byebug', group: [:development, :test] 15 | 16 | gem "simplecov", require: false, group: :test 17 | 18 | group :development do 19 | gem "bundler" 20 | gem "rake" 21 | end 22 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2015 Arnaud MESUREUR 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## DISCLAIMER 2 | 3 | This project is not being maintained and I don't recommend using it in its current form. 4 | As an alternative, I recommend using the [jwt](https://github.com/jwt/ruby-jwt) gem directly. 5 | 6 | # knock 7 | 8 | [](http://badge.fury.io/rb/knock) 9 | [](https://travis-ci.org/nsarno/knock) 10 | [](https://codeclimate.com/github/nsarno/knock) 11 | 12 | Seamless JWT authentication for Rails API 13 | 14 | ## Description 15 | 16 | Knock is an authentication solution for Rails API-only application based on JSON Web Tokens. 17 | 18 | ## Getting Started 19 | 20 | ### Installation 21 | 22 | Add this line to your application's Gemfile: 23 | 24 | ```ruby 25 | gem 'knock' 26 | ``` 27 | 28 | Then execute: 29 | 30 | $ bundle install 31 | 32 | ### Requirements 33 | 34 | Knock makes one assumption about your user model: 35 | 36 | It must have an `authenticate` method, similar to the one added by [has_secure_password](http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password). 37 | 38 | ```ruby 39 | class User < ActiveRecord::Base 40 | has_secure_password 41 | end 42 | ``` 43 | 44 | Using `has_secure_password` is recommended, but you don't have to as long as your user model implements an `authenticate` instance method with the same behavior. 45 | 46 | ### Usage 47 | 48 | Include the `Knock::Authenticable` module in your `ApplicationController` 49 | 50 | ```ruby 51 | class ApplicationController < ActionController::API 52 | include Knock::Authenticable 53 | end 54 | ``` 55 | 56 | You can now protect your resources by calling `authenticate_user` as a before_action 57 | inside your controllers: 58 | 59 | ```ruby 60 | class SecuredController < ApplicationController 61 | before_action :authenticate_user 62 | 63 | def index 64 | # etc... 65 | end 66 | 67 | # etc... 68 | end 69 | ``` 70 | 71 | You can access the current user in your controller with `current_user`. 72 | 73 | If no valid token is passed with the request, Knock will respond with: 74 | 75 | ``` 76 | head :unauthorized 77 | ``` 78 | 79 | You can modify this behaviour by overriding `unauthorized_entity` in your controller. 80 | 81 | You also have access directly to `current_user` which will try to authenticate or return `nil`: 82 | 83 | ```ruby 84 | def index 85 | if current_user 86 | # do something 87 | else 88 | # do something else 89 | end 90 | end 91 | ``` 92 | 93 | _Note: the `authenticate_user` method uses the `current_user` method. Overwriting `current_user` may cause unexpected behaviour._ 94 | 95 | You can do the exact same thing for any entity. E.g. for `Admin`, use `authenticate_admin` and `current_admin` instead. 96 | 97 | If you're using a namespaced model, Knock won't be able to infer it automatically from the method name. Instead you can use `authenticate_for` directly like this: 98 | 99 | ```ruby 100 | class ApplicationController < ActionController::Base 101 | include Knock::Authenticable 102 | 103 | private 104 | 105 | def authenticate_v1_user 106 | authenticate_for V1::User 107 | end 108 | end 109 | ``` 110 | 111 | ```ruby 112 | class SecuredController < ApplicationController 113 | before_action :authenticate_v1_user 114 | end 115 | ``` 116 | 117 | Then you get the current user by calling `current_v1_user` instead of `current_user`. 118 | 119 | ### Configuration 120 | 121 | #### In the entity model 122 | 123 | The entity model (e.g. `User`) can implement specific methods to provide 124 | customization over different parts of the authentication process. 125 | 126 | - **Find the entity when creating the token (when signing in)** 127 | 128 | By default, Knock tries to find the entity by email. If you want to modify this 129 | behaviour, implement within your entity model a class method `from_token_request` 130 | that takes the request in argument. 131 | 132 | E.g. 133 | 134 | ```ruby 135 | class User < ActiveRecord::Base 136 | def self.from_token_request request 137 | # Returns a valid user, `nil` or raise `Knock.not_found_exception_class_name` 138 | # e.g. 139 | # email = request.params["auth"] && request.params["auth"]["email"] 140 | # self.find_by email: email 141 | end 142 | end 143 | ``` 144 | 145 | - **Find the authenticated entity from the token payload (when authenticating a request)** 146 | 147 | By default, Knock assumes the payload as a subject (`sub`) claim containing the entity's id 148 | and calls `find` on the model. If you want to modify this behaviour, implement within 149 | your entity model a class method `from_token_payload` that takes the 150 | payload in argument. 151 | 152 | E.g. 153 | 154 | ```ruby 155 | class User < ActiveRecord::Base 156 | def self.from_token_payload payload 157 | # Returns a valid user, `nil` or raise 158 | # e.g. 159 | # self.find payload["sub"] 160 | end 161 | end 162 | ``` 163 | 164 | - **Modify the token payload** 165 | 166 | By default the token payload contains the entity's id inside the subject (`sub`) claim. 167 | If you want to modify this behaviour, implement within your entity model an instance method 168 | `to_token_payload` that returns a hash representing the payload. 169 | 170 | E.g. 171 | 172 | ```ruby 173 | class User < ActiveRecord::Base 174 | def to_token_payload 175 | # Returns the payload as a hash 176 | end 177 | end 178 | ``` 179 | 180 | - **Token Lifetime** 181 | 182 | By default the generated tokens will be valid, after generated, for 1 day. 183 | You can change it in the Knock configuration file (config/knock.rb), 184 | setting the desired lifetime: 185 | 186 | E.g. 187 | 188 | ```ruby 189 | Knock.token_lifetime = 3.hours 190 | ``` 191 | 192 | If you are generating tokens for more than one entity, you can pass 193 | each lifetime in a hash, using the entities class names as keys, like: 194 | 195 | E.g. 196 | 197 | ```ruby 198 | # How long before a token is expired. If nil is provided, 199 | # token will last forever. 200 | Knock.token_lifetime = { 201 | user: 1.day 202 | admin: 30.minutes 203 | } 204 | ``` 205 | 206 | #### In the initializer 207 | 208 | Read [lib/knock.rb](https://github.com/nsarno/knock/blob/master/lib/knock.rb) to learn about all the possible configuration options and their default values. 209 | 210 | You can create an initializer like in the example below: 211 | 212 | Inside `config/initializers/knock.rb` 213 | 214 | ```ruby 215 | Knock.setup do |config| 216 | config.token_lifetime = 1.hour 217 | 218 | # For Auth0 219 | config.token_audience = -> { Rails.application.secrets.auth0_client_id } 220 | config.token_secret_signature_key = -> { JWT.base64url_decode Rails.application.secrets.auth0_client_secret } 221 | end 222 | ``` 223 | 224 | ### Authenticating from a web or mobile application 225 | 226 | Example request to get a token from your API: 227 | 228 | ``` 229 | POST /user_token 230 | {"auth": {"email": "foo@bar.com", "password": "secret"}} 231 | ``` 232 | 233 | Example response from the API: 234 | 235 | ``` 236 | 201 Created 237 | {"jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9"} 238 | ``` 239 | 240 | To make an authenticated request to your API, you need to pass the token via the request header: 241 | 242 | ``` 243 | Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9 244 | GET /my_resources 245 | ``` 246 | 247 | Knock responds with a `404 Not Found` when the user cannot be found or the password is invalid. This is a security best practice to avoid giving away information about the existence or not of a particular user. 248 | 249 | **NB:** HTTPS should always be enabled when sending a password or token in your request. 250 | 251 | ### Authenticated tests 252 | 253 | To authenticate within your tests: 254 | 255 | 1. Create a valid token 256 | 2. Pass it in your request 257 | 258 | e.g. 259 | 260 | ```ruby 261 | class SecuredResourcesControllerTest < ActionDispatch::IntegrationTest 262 | def authenticated_header 263 | token = Knock::AuthToken.new(payload: { sub: users(:one).id }).token 264 | 265 | { 266 | 'Authorization': "Bearer #{token}" 267 | } 268 | end 269 | 270 | it 'responds successfully' do 271 | get secured_resources_url, headers: authenticated_header 272 | 273 | assert_response :success 274 | end 275 | end 276 | ``` 277 | 278 | #### Without ActiveRecord 279 | 280 | If no ActiveRecord is used, then you will need to specify what Exception will be used when the user is not found with the given credentials. 281 | 282 | ```ruby 283 | Knock.setup do |config| 284 | 285 | # Exception Class 286 | # --------------- 287 | # 288 | # Configure the Exception to be used (raised and rescued) for User Not Found. 289 | # note: change this if ActiveRecord is not being used. 290 | # 291 | # Default: 292 | config.not_found_exception_class_name = 'MyCustomException' 293 | end 294 | ``` 295 | 296 | ### Algorithms 297 | 298 | The JWT spec supports different kind of cryptographic signing algorithms. 299 | You can set `token_signature_algorithm` to use the one you want in the 300 | initializer or do nothing and use the default one (HS256). 301 | 302 | You can specify any of the algorithms supported by the 303 | [jwt](https://github.com/jwt/ruby-jwt) gem. 304 | 305 | If the algorithm you use requires a public key, you also need to set 306 | `token_public_key` in the initializer. 307 | 308 | ## CORS 309 | 310 | To enable cross-origin resource sharing, check out the [rack-cors](https://github.com/cyu/rack-cors) gem. 311 | 312 | ## Related links 313 | 314 | - [10 things you should know about tokens](https://auth0.com/blog/2014/01/27/ten-things-you-should-know-about-tokens-and-cookies/) 315 | 316 | ## Contributing 317 | 318 | 1. Fork it ( https://github.com/nsarno/knock/fork ) 319 | 2. Create your feature branch (`git checkout -b my-new-feature`) 320 | 3. Commit your changes (`git commit -am 'Add some feature'`) 321 | 4. Push to the branch (`git push origin my-new-feature`) 322 | 5. Create a new Pull Request 323 | 324 | ## License 325 | 326 | MIT 327 | -------------------------------------------------------------------------------- /ROADMAP.md: -------------------------------------------------------------------------------- 1 | ### Roadmap 2 | 3 | Keeping track here of changes considered for the future. 4 | 5 | - [ ] Support multiple token expiry times (#241) 6 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "rubygems" 2 | 3 | begin 4 | require 'bundler/setup' 5 | rescue LoadError 6 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks' 7 | end 8 | 9 | require 'rdoc/task' 10 | 11 | RDoc::Task.new(:rdoc) do |rdoc| 12 | rdoc.rdoc_dir = 'rdoc' 13 | rdoc.title = 'Knock' 14 | rdoc.options << '--line-numbers' 15 | rdoc.rdoc_files.include('README.rdoc') 16 | rdoc.rdoc_files.include('lib/**/*.rb') 17 | end 18 | 19 | APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__) 20 | load 'rails/tasks/engine.rake' 21 | 22 | 23 | load 'rails/tasks/statistics.rake' 24 | 25 | 26 | 27 | Bundler::GemHelper.install_tasks 28 | 29 | require 'rake/testtask' 30 | 31 | Rake::TestTask.new(:test) do |t| 32 | t.libs << 'lib' 33 | t.libs << 'test' 34 | t.pattern = 'test/**/*_test.rb' 35 | t.verbose = false 36 | end 37 | 38 | 39 | task default: :test 40 | -------------------------------------------------------------------------------- /app/controllers/knock/application_controller.rb: -------------------------------------------------------------------------------- 1 | module Knock 2 | class ApplicationController < ActionController::API 3 | rescue_from Knock.not_found_exception_class_name, with: :not_found 4 | 5 | private 6 | 7 | def not_found 8 | head :not_found 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /app/controllers/knock/auth_token_controller.rb: -------------------------------------------------------------------------------- 1 | require_dependency "knock/application_controller" 2 | 3 | module Knock 4 | class AuthTokenController < ApplicationController 5 | before_action :authenticate 6 | 7 | def create 8 | render json: auth_token, status: :created 9 | end 10 | 11 | private 12 | 13 | def authenticate 14 | unless entity.present? && entity.authenticate(auth_params[:password]) 15 | raise Knock.not_found_exception_class 16 | end 17 | end 18 | 19 | def auth_token 20 | if entity.respond_to? :to_token_payload 21 | AuthToken.new payload: entity.to_token_payload 22 | else 23 | AuthToken.new payload: { sub: entity.id } 24 | end 25 | end 26 | 27 | def entity 28 | @entity ||= 29 | if entity_class.respond_to? :from_token_request 30 | entity_class.from_token_request request 31 | else 32 | entity_class.find_by email: auth_params[:email] 33 | end 34 | end 35 | 36 | def entity_class 37 | entity_name.constantize 38 | end 39 | 40 | def entity_name 41 | self.class.name.scan(/\w+/).last.split('TokenController').first 42 | end 43 | 44 | def auth_params 45 | params.require(:auth).permit :email, :password 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /app/model/knock/auth_token.rb: -------------------------------------------------------------------------------- 1 | require "jwt" 2 | 3 | module Knock 4 | class AuthToken 5 | attr_reader :token 6 | attr_reader :payload 7 | attr_reader :entity_class_name 8 | 9 | def initialize(payload: {}, token: nil, verify_options: {}, entity_class_name: nil) 10 | @entity_class_name = entity_class_name 11 | if token.present? 12 | @payload, _ = JWT.decode token.to_s, decode_key, true, options.merge(verify_options) 13 | @token = token 14 | else 15 | @payload = claims.merge(payload) 16 | @token = JWT.encode @payload, 17 | secret_key, 18 | Knock.token_signature_algorithm 19 | end 20 | end 21 | 22 | def entity_for(entity_class) 23 | if entity_class.respond_to? :from_token_payload 24 | entity_class.from_token_payload @payload 25 | else 26 | entity_class.find @payload["sub"] 27 | end 28 | end 29 | 30 | def to_json(options = {}) 31 | { jwt: @token }.to_json 32 | end 33 | 34 | private 35 | 36 | def secret_key 37 | Knock.token_secret_signature_key.call 38 | end 39 | 40 | def decode_key 41 | Knock.token_public_key || secret_key 42 | end 43 | 44 | def options 45 | verify_claims.merge({ 46 | algorithm: Knock.token_signature_algorithm, 47 | }) 48 | end 49 | 50 | def claims 51 | _claims = {} 52 | _claims[:exp] = token_lifetime if verify_lifetime? 53 | _claims[:aud] = token_audience if verify_audience? 54 | _claims 55 | end 56 | 57 | def token_lifetime 58 | return unless verify_lifetime? 59 | 60 | if Knock.token_lifetime.is_a?(Hash) 61 | Knock.token_lifetime[entity_class_name].from_now.to_i 62 | else 63 | Knock.token_lifetime.from_now.to_i 64 | end 65 | end 66 | 67 | def verify_lifetime? 68 | !Knock.token_lifetime.nil? 69 | end 70 | 71 | def verify_claims 72 | { 73 | aud: token_audience, 74 | verify_aud: verify_audience?, 75 | verify_expiration: verify_lifetime?, 76 | } 77 | end 78 | 79 | def token_audience 80 | verify_audience? && Knock.token_audience.call 81 | end 82 | 83 | def verify_audience? 84 | Knock.token_audience.present? 85 | end 86 | end 87 | end 88 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application. 3 | 4 | ENGINE_ROOT = File.expand_path('../..', __FILE__) 5 | ENGINE_PATH = File.expand_path('../../lib/knock/engine', __FILE__) 6 | 7 | # Set up gems listed in the Gemfile. 8 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 9 | require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) 10 | 11 | require 'rails/all' 12 | require 'rails/engine/commands' 13 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Knock::Engine.routes.draw do 2 | post 'auth_token' => 'auth_token#create' 3 | end 4 | -------------------------------------------------------------------------------- /gemfiles/rails_5.gemfile: -------------------------------------------------------------------------------- 1 | # This file was generated by Appraisal 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "codeclimate-test-reporter", :group => :test, :require => nil 6 | gem "simplecov", :require => false, :group => :test 7 | gem "rails", "~> 5.0.0" 8 | 9 | group :development do 10 | gem "bundler" 11 | gem "rake" 12 | gem "appraisal" 13 | end 14 | 15 | gemspec :path => "../" 16 | -------------------------------------------------------------------------------- /gemfiles/rails_5.gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: ../ 3 | specs: 4 | knock (1.5) 5 | bcrypt (~> 3.1) 6 | jwt (~> 1.5) 7 | rails (>= 4.2) 8 | 9 | GEM 10 | remote: https://rubygems.org/ 11 | specs: 12 | actioncable (5.0.0.1) 13 | actionpack (= 5.0.0.1) 14 | nio4r (~> 1.2) 15 | websocket-driver (~> 0.6.1) 16 | actionmailer (5.0.0.1) 17 | actionpack (= 5.0.0.1) 18 | actionview (= 5.0.0.1) 19 | activejob (= 5.0.0.1) 20 | mail (~> 2.5, >= 2.5.4) 21 | rails-dom-testing (~> 2.0) 22 | actionpack (5.0.0.1) 23 | actionview (= 5.0.0.1) 24 | activesupport (= 5.0.0.1) 25 | rack (~> 2.0) 26 | rack-test (~> 0.6.3) 27 | rails-dom-testing (~> 2.0) 28 | rails-html-sanitizer (~> 1.0, >= 1.0.2) 29 | actionview (5.0.0.1) 30 | activesupport (= 5.0.0.1) 31 | builder (~> 3.1) 32 | erubis (~> 2.7.0) 33 | rails-dom-testing (~> 2.0) 34 | rails-html-sanitizer (~> 1.0, >= 1.0.2) 35 | activejob (5.0.0.1) 36 | activesupport (= 5.0.0.1) 37 | globalid (>= 0.3.6) 38 | activemodel (5.0.0.1) 39 | activesupport (= 5.0.0.1) 40 | activerecord (5.0.0.1) 41 | activemodel (= 5.0.0.1) 42 | activesupport (= 5.0.0.1) 43 | arel (~> 7.0) 44 | activesupport (5.0.0.1) 45 | concurrent-ruby (~> 1.0, >= 1.0.2) 46 | i18n (~> 0.7) 47 | minitest (~> 5.1) 48 | tzinfo (~> 1.1) 49 | appraisal (2.1.0) 50 | bundler 51 | rake 52 | thor (>= 0.14.0) 53 | arel (7.1.2) 54 | bcrypt (3.1.11) 55 | builder (3.2.2) 56 | codeclimate-test-reporter (0.6.0) 57 | simplecov (>= 0.7.1, < 1.0.0) 58 | concurrent-ruby (1.0.2) 59 | docile (1.1.5) 60 | erubis (2.7.0) 61 | globalid (0.3.7) 62 | activesupport (>= 4.1.0) 63 | i18n (0.7.0) 64 | json (2.0.2) 65 | jwt (1.5.6) 66 | loofah (2.0.3) 67 | nokogiri (>= 1.5.9) 68 | mail (2.6.4) 69 | mime-types (>= 1.16, < 4) 70 | method_source (0.8.2) 71 | mime-types (3.1) 72 | mime-types-data (~> 3.2015) 73 | mime-types-data (3.2016.0521) 74 | mini_portile2 (2.1.0) 75 | minitest (5.9.0) 76 | nio4r (1.2.1) 77 | nokogiri (1.6.8) 78 | mini_portile2 (~> 2.1.0) 79 | pkg-config (~> 1.1.7) 80 | pkg-config (1.1.7) 81 | rack (2.0.1) 82 | rack-test (0.6.3) 83 | rack (>= 1.0) 84 | rails (5.0.0.1) 85 | actioncable (= 5.0.0.1) 86 | actionmailer (= 5.0.0.1) 87 | actionpack (= 5.0.0.1) 88 | actionview (= 5.0.0.1) 89 | activejob (= 5.0.0.1) 90 | activemodel (= 5.0.0.1) 91 | activerecord (= 5.0.0.1) 92 | activesupport (= 5.0.0.1) 93 | bundler (>= 1.3.0, < 2.0) 94 | railties (= 5.0.0.1) 95 | sprockets-rails (>= 2.0.0) 96 | rails-dom-testing (2.0.1) 97 | activesupport (>= 4.2.0, < 6.0) 98 | nokogiri (~> 1.6.0) 99 | rails-html-sanitizer (1.0.3) 100 | loofah (~> 2.0) 101 | railties (5.0.0.1) 102 | actionpack (= 5.0.0.1) 103 | activesupport (= 5.0.0.1) 104 | method_source 105 | rake (>= 0.8.7) 106 | thor (>= 0.18.1, < 2.0) 107 | rake (11.3.0) 108 | simplecov (0.12.0) 109 | docile (~> 1.1.0) 110 | json (>= 1.8, < 3) 111 | simplecov-html (~> 0.10.0) 112 | simplecov-html (0.10.0) 113 | sprockets (3.7.0) 114 | concurrent-ruby (~> 1.0) 115 | rack (> 1, < 3) 116 | sprockets-rails (3.2.0) 117 | actionpack (>= 4.0) 118 | activesupport (>= 4.0) 119 | sprockets (>= 3.0.0) 120 | sqlite3 (1.3.11) 121 | thor (0.19.1) 122 | thread_safe (0.3.5) 123 | timecop (0.8.1) 124 | tzinfo (1.2.2) 125 | thread_safe (~> 0.1) 126 | websocket-driver (0.6.4) 127 | websocket-extensions (>= 0.1.0) 128 | websocket-extensions (0.1.2) 129 | 130 | PLATFORMS 131 | ruby 132 | 133 | DEPENDENCIES 134 | appraisal 135 | bundler 136 | codeclimate-test-reporter 137 | knock! 138 | rails (~> 5.0.0) 139 | rake 140 | simplecov 141 | sqlite3 (~> 1.3) 142 | timecop (~> 0.8.0) 143 | 144 | BUNDLED WITH 145 | 1.12.5 146 | -------------------------------------------------------------------------------- /knock.gemspec: -------------------------------------------------------------------------------- 1 | $:.push File.expand_path("../lib", __FILE__) 2 | 3 | # Maintain your gem's version: 4 | require "knock/version" 5 | 6 | # Describe your gem and declare its dependencies: 7 | Gem::Specification.new do |s| 8 | s.name = "knock" 9 | s.version = Knock::Version::VERSION 10 | s.authors = ["Arnaud MESUREUR", "Ghjuvan-Carlu BIANCHI"] 11 | s.email = ["arnaud.mesureur@gmail.com"] 12 | s.homepage = "https://github.com/nsarno/knock" 13 | s.summary = "Seamless JWT authentication for Rails API." 14 | s.description = "Authentication solution for Rails based on JWT" 15 | s.license = "MIT" 16 | 17 | s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] 18 | s.test_files = Dir["test/**/*"] 19 | 20 | s.add_dependency "rails", ">= 5" 21 | s.add_dependency "jwt", "~> 2.2.1" 22 | s.add_dependency "bcrypt", "~> 3.1" 23 | 24 | s.add_development_dependency "sqlite3", "~> 1.3" 25 | s.add_development_dependency "timecop", "~> 0.8.0" 26 | end 27 | -------------------------------------------------------------------------------- /lib/knock.rb: -------------------------------------------------------------------------------- 1 | require "knock/engine" 2 | 3 | module Knock 4 | # How long before a token is expired. If nil is provided, 5 | # token will last forever. 6 | mattr_accessor :token_lifetime 7 | self.token_lifetime = 1.day 8 | 9 | # Configure the audience claim to identify the recipients that the token 10 | # is intended for. 11 | mattr_accessor :token_audience 12 | self.token_audience = nil 13 | 14 | # Configure the algorithm used to encode the token 15 | mattr_accessor :token_signature_algorithm 16 | self.token_signature_algorithm = "HS256" 17 | 18 | # Configure the key used to sign tokens. 19 | mattr_accessor :token_secret_signature_key 20 | self.token_secret_signature_key = -> do 21 | if Rails.application.respond_to?(:secret_key_base) 22 | Rails.application.secret_key_base 23 | else 24 | Rails.application.secrets.secret_key_base 25 | end 26 | end 27 | 28 | # Configure the public key used to decode tokens, when required. 29 | mattr_accessor :token_public_key 30 | self.token_public_key = nil 31 | 32 | # Configure the exception to be used when user cannot be found. 33 | mattr_accessor :not_found_exception_class_name 34 | self.not_found_exception_class_name = "ActiveRecord::RecordNotFound" 35 | 36 | def self.not_found_exception_class 37 | not_found_exception_class_name.to_s.constantize 38 | end 39 | 40 | # Default way to setup Knock. Run `rails generate knock:install` to create 41 | # a fresh initializer with all configuration values. 42 | def self.setup 43 | yield self 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/knock/authenticable.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module Knock 4 | # Include this module in your controller to enable authentication 5 | # for your endpoint. 6 | # 7 | # e.g. 8 | # Calling `authenticate_user` will try to find a valid `User` based on 9 | # the token payload. 10 | module Authenticable 11 | def authenticate_for(entity_class) 12 | getter_name = GetterName.new(entity_class).cleared 13 | define_current_entity_getter(entity_class, getter_name) 14 | public_send(getter_name) 15 | end 16 | 17 | private 18 | 19 | def token 20 | params[:token] || token_from_request_headers 21 | end 22 | 23 | def method_missing(method, *args) 24 | prefix, entity_name = method.to_s.split("_", 2) 25 | case prefix 26 | when "authenticate" 27 | unauthorized_entity(entity_name) unless authenticate_entity(entity_name) 28 | when "current" 29 | authenticate_entity(entity_name) 30 | else 31 | super 32 | end 33 | end 34 | 35 | def respond_to_missing?(method, *) 36 | prefix, = method.to_s.split("_", 2) 37 | case prefix 38 | when "authenticate" 39 | true 40 | else 41 | super 42 | end 43 | end 44 | 45 | def authenticate_entity(entity_name) 46 | return unless token 47 | 48 | entity_class = entity_name.camelize.constantize 49 | send(:authenticate_for, entity_class) 50 | end 51 | 52 | def unauthorized_entity(_entity_name) 53 | head(:unauthorized) 54 | end 55 | 56 | def token_from_request_headers 57 | request.headers["Authorization"]&.split&.last 58 | end 59 | 60 | # Dynamically defines a method similar to the example below. 61 | # 62 | # def current_user 63 | # @_current_user ||= fetch_entity_from_token(User) 64 | # end 65 | def define_current_entity_getter(entity_class, getter_name) 66 | return if respond_to?(getter_name) 67 | 68 | memoization_var_name = "@_#{getter_name}" 69 | self.class.send(:define_method, getter_name) do 70 | unless instance_variable_defined?(memoization_var_name) 71 | current = fetch_entity_from_token(entity_class) 72 | instance_variable_set(memoization_var_name, current) 73 | end 74 | instance_variable_get(memoization_var_name) 75 | end 76 | end 77 | 78 | def fetch_entity_from_token(entity_class) 79 | Knock::AuthToken.new(token: token).entity_for(entity_class) 80 | rescue Knock.not_found_exception_class, JWT::DecodeError, JWT::EncodeError 81 | nil 82 | end 83 | end 84 | end 85 | -------------------------------------------------------------------------------- /lib/knock/authenticable/getter_name.rb: -------------------------------------------------------------------------------- 1 | module Knock 2 | module Authenticable 3 | class GetterName 4 | attr_reader :entity_class 5 | 6 | def initialize(entity_class) 7 | @entity_class = entity_class 8 | end 9 | 10 | def cleared 11 | "current_#{entity_class.to_s.gsub('::', '').underscore}" 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/knock/engine.rb: -------------------------------------------------------------------------------- 1 | module Knock 2 | class Engine < ::Rails::Engine 3 | paths.add "lib", eager_load: true 4 | isolate_namespace Knock 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /lib/knock/tokenizable.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | module Knock 3 | # Include this module in your entity class (e.g. User) 4 | # for token serialization and deserialization. 5 | module Tokenizable 6 | def self.included(base) 7 | base.extends ClassMethods 8 | end 9 | 10 | module ClassMethods 11 | def from_token_payload(payload) 12 | find(payload["sub"]) 13 | end 14 | 15 | def from_token(token) 16 | auth_token = AuthToken.new(token: token) 17 | from_token_payload(auth_token.payload) 18 | end 19 | end 20 | 21 | def to_token_payload 22 | { sub: @object.id } 23 | end 24 | 25 | def to_token 26 | AuthToken.new(payload: to_token_payload).token 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /lib/knock/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module Knock 4 | class Version 5 | VERSION = "2.2.0" 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/tasks/knock_tasks.rake: -------------------------------------------------------------------------------- 1 | # desc "Explaining what the task does" 2 | # task :knock do 3 | # # Task goes here 4 | # end 5 | -------------------------------------------------------------------------------- /test/dummy/README.rdoc: -------------------------------------------------------------------------------- 1 | == README 2 | 3 | This README would normally document whatever steps are necessary to get the 4 | application up and running. 5 | 6 | Things you may want to cover: 7 | 8 | * Ruby version 9 | 10 | * System dependencies 11 | 12 | * Configuration 13 | 14 | * Database creation 15 | 16 | * Database initialization 17 | 18 | * How to run the test suite 19 | 20 | * Services (job queues, cache servers, search engines, etc.) 21 | 22 | * Deployment instructions 23 | 24 | * ... 25 | 26 | 27 | Please feel free to use a different markup language if you do not plan to run 28 | rake doc:app. 29 | -------------------------------------------------------------------------------- /test/dummy/Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require File.expand_path('../config/application', __FILE__) 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /test/dummy/app/assets/config/manifest.js: -------------------------------------------------------------------------------- 1 | //= link_tree ../images 2 | //= link_directory ../javascripts .js 3 | //= link_directory ../stylesheets .css 4 | -------------------------------------------------------------------------------- /test/dummy/app/assets/images/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nsarno/knock/8e8b3e8d29eccb83a83ea2449e006250f025632a/test/dummy/app/assets/images/.keep -------------------------------------------------------------------------------- /test/dummy/app/assets/javascripts/application.js: -------------------------------------------------------------------------------- 1 | // This is a manifest file that'll be compiled into application.js, which will include all the files 2 | // listed below. 3 | // 4 | // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 5 | // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. 6 | // 7 | // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 8 | // compiled file. 9 | // 10 | // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details 11 | // about supported directives. 12 | // 13 | //= require_tree . 14 | -------------------------------------------------------------------------------- /test/dummy/app/assets/stylesheets/application.css: -------------------------------------------------------------------------------- 1 | /* 2 | * This is a manifest file that'll be compiled into application.css, which will include all the files 3 | * listed below. 4 | * 5 | * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, 6 | * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. 7 | * 8 | * You're free to add application-wide styles to this file and they'll appear at the bottom of the 9 | * compiled file so the styles you add here take precedence over styles defined in any styles 10 | * defined in the other CSS/SCSS files in this directory. It is generally better to create a new 11 | * file per style scope. 12 | * 13 | *= require_tree . 14 | *= require_self 15 | */ 16 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/admin_protected_controller.rb: -------------------------------------------------------------------------------- 1 | class AdminProtectedController < ApplicationController 2 | before_action :authenticate_admin 3 | 4 | def index 5 | head :ok 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/admin_token_controller.rb: -------------------------------------------------------------------------------- 1 | class AdminTokenController < Knock::AuthTokenController 2 | end 3 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | # Prevent CSRF attacks by raising an exception. 3 | # For APIs, you may want to use :null_session instead. 4 | protect_from_forgery with: :null_session 5 | 6 | include Knock::Authenticable 7 | end 8 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/composite_name_entity_protected_controller.rb: -------------------------------------------------------------------------------- 1 | class CompositeNameEntityProtectedController < ApplicationController 2 | before_action :authenticate_composite_name_entity 3 | 4 | def index 5 | head :ok 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nsarno/knock/8e8b3e8d29eccb83a83ea2449e006250f025632a/test/dummy/app/controllers/concerns/.keep -------------------------------------------------------------------------------- /test/dummy/app/controllers/current_users_controller.rb: -------------------------------------------------------------------------------- 1 | class CurrentUsersController < ApplicationController 2 | def show 3 | if current_user 4 | head :ok 5 | else 6 | head :not_found 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/custom_unauthorized_entity_controller.rb: -------------------------------------------------------------------------------- 1 | class CustomUnauthorizedEntityController < ApplicationController 2 | before_action :authenticate_user 3 | 4 | def index 5 | head :ok 6 | end 7 | 8 | private 9 | 10 | def unauthorized_entity(entity) 11 | head :not_found 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/guest_protected_controller.rb: -------------------------------------------------------------------------------- 1 | class GuestProtectedController < ApplicationController 2 | before_action :authenticate_guest 3 | 4 | def index 5 | head :ok 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/protected_resources_controller.rb: -------------------------------------------------------------------------------- 1 | class ProtectedResourcesController < ApplicationController 2 | before_action :authenticate_user 3 | 4 | def index 5 | head :ok 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/v1/test_namespaced_controller.rb: -------------------------------------------------------------------------------- 1 | module V1 2 | class TestNamespacedController < ApplicationController 3 | 4 | before_action :authenticate_v1_user 5 | 6 | def index 7 | head :ok 8 | end 9 | 10 | private 11 | 12 | def authenticate_v1_user 13 | authenticate_for V1::User 14 | end 15 | 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/vendor_protected_controller.rb: -------------------------------------------------------------------------------- 1 | class VendorProtectedController < ApplicationController 2 | before_action :authenticate_vendor, only: [:index] 3 | before_action :some_missing_method, only: [:show] 4 | 5 | def index 6 | head :ok 7 | end 8 | 9 | def show 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /test/dummy/app/controllers/vendor_token_controller.rb: -------------------------------------------------------------------------------- 1 | class VendorTokenController < Knock::AuthTokenController 2 | end 3 | -------------------------------------------------------------------------------- /test/dummy/app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /test/dummy/app/mailers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nsarno/knock/8e8b3e8d29eccb83a83ea2449e006250f025632a/test/dummy/app/mailers/.keep -------------------------------------------------------------------------------- /test/dummy/app/models/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nsarno/knock/8e8b3e8d29eccb83a83ea2449e006250f025632a/test/dummy/app/models/.keep -------------------------------------------------------------------------------- /test/dummy/app/models/admin.rb: -------------------------------------------------------------------------------- 1 | class Admin < ActiveRecord::Base 2 | has_secure_password 3 | 4 | def self.from_token_request request 5 | email = request.params["auth"] && request.params["auth"]["email"] 6 | self.find_by email: email 7 | end 8 | 9 | def self.from_token_payload payload 10 | self.find payload["sub"] 11 | end 12 | 13 | def to_token_payload 14 | {sub: id} 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /test/dummy/app/models/composite_name_entity.rb: -------------------------------------------------------------------------------- 1 | class CompositeNameEntity < ActiveRecord::Base 2 | has_secure_password 3 | end 4 | -------------------------------------------------------------------------------- /test/dummy/app/models/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nsarno/knock/8e8b3e8d29eccb83a83ea2449e006250f025632a/test/dummy/app/models/concerns/.keep -------------------------------------------------------------------------------- /test/dummy/app/models/guest.rb: -------------------------------------------------------------------------------- 1 | class Guest 2 | def self.from_token_payload _payload 3 | # This is to simulate the use of `find_or_create` 4 | # on an AR model, regardless of the payload content 5 | new 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/dummy/app/models/user.rb: -------------------------------------------------------------------------------- 1 | class User < ActiveRecord::Base 2 | has_secure_password 3 | end 4 | -------------------------------------------------------------------------------- /test/dummy/app/models/v1/user.rb: -------------------------------------------------------------------------------- 1 | module V1 2 | class User < ActiveRecord::Base 3 | has_secure_password 4 | end 5 | end -------------------------------------------------------------------------------- /test/dummy/app/models/vendor.rb: -------------------------------------------------------------------------------- 1 | class Vendor < ActiveRecord::Base 2 | has_secure_password 3 | end 4 | -------------------------------------------------------------------------------- /test/dummy/app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |You may have mistyped the address or the page may have moved.
63 |If you are the application owner check the logs for more information.
65 |Maybe you tried to change something you didn't have access to.
63 |If you are the application owner check the logs for more information.
65 |If you are the application owner check the logs for more information.
64 |