Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring #64

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bc8324e
Add authenticate_admin.call to valid_doorkeeper_token?
texpert Aug 9, 2016
3074ad2
Do not run Doorkeper's authorization method if oauth2 scope is set to…
texpert Aug 12, 2016
a7c6ee1
Updated Travis matrix to test with Grape 0.17.0 version instead of 0.…
texpert Aug 31, 2016
f7b694c
Made resourse_owner available in the endpoint and passed to Doorkeepe…
texpert Sep 30, 2016
647c89e
Locked ruby version to minimal 2.3 - taking advantage of new syntax e…
texpert Sep 30, 2016
c6a0964
Fixed configuration. template and Gemfile.
texpert Sep 30, 2016
6c4b523
Removed obsolete methods and enriched some checks of auth and scope k…
texpert Sep 30, 2016
987c02f
Travis didn't found Grape 0.16, so - 0.16.2 will be minimal version.
texpert Sep 30, 2016
a9bf80d
Let Doorkeeper handle token authorization and auth error handling - i…
texpert Oct 3, 2016
94c6f30
Version bumped to 1.1.0.
texpert Oct 5, 2016
7c1bde1
Fixed the test failures.
texpert Oct 6, 2016
6bd923d
Fix to use string interpolation, not concatenation in helper methods.
texpert Oct 6, 2016
b53c325
Fix raising WinBouncer::Errors to ArgumentError - leftover from refac…
texpert Oct 6, 2016
32e28a3
Self-configure with :default AuthStrategy if configuration missing.
texpert Oct 6, 2016
6a511c4
Inject inline defied scopes into route' description's :security hash.
texpert Oct 5, 2016
35dba09
Set Doorkeeper default scopes if no scopes specified inline.
texpert Oct 11, 2016
9843ce7
Refactored auth_strategies to helpers ad scope fetching methods.
texpert Oct 17, 2016
9ecfe47
Minimal Ruby version reverted to 2.2.2
texpert Oct 17, 2016
8fc2d2a
Trying to extend build matrix to lower Grape versions.
texpert Oct 18, 2016
13ca882
Grape support restricted to 0.15 - 0.18.
texpert Oct 18, 2016
fb73913
Doorkeeper 3.1.0 is not compatible with Rails >= 5.0.0
texpert Oct 18, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.2
Exclude:
- 'vendor/**/*'
- 'spec/fixtures/**/*'
Expand Down
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.2.2
36 changes: 20 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@ before_install:
language: ruby
cache: bundler
rvm:
- 2.1.0
- 2.2.2
- 2.2.5
- 2.3.0
- 2.3.1
env:

- rails=4.2.6 grape=0.15.0 doorkeeper=3.1.0
- rails=4.2.6 grape=0.15.0 doorkeeper=4.0.0
- rails=4.2.6 grape=0.15.0 doorkeeper=4.1.0
- rails=4.2.6 grape=0.15.0 doorkeeper=4.2.0
- rails=4.2.6 grape=0.16.2 doorkeeper=3.1.0
- rails=5.0.0 grape=0.16.2 doorkeeper=4.0.0
- rails=5.0.0 grape=0.16.2 doorkeeper=4.1.0
- rails=4.2.7 grape=0.15.0 doorkeeper=3.1.0
- rails=4.2.7 grape=0.15.0 doorkeeper=4.0.0
- rails=4.2.7 grape=0.15.0 doorkeeper=4.1.0
- rails=4.2.7 grape=0.15.0 doorkeeper=4.2.0
- rails=5.0.0 grape=0.15.0 doorkeeper=4.0.0
- rails=5.0.0 grape=0.15.0 doorkeeper=4.1.0
- rails=5.0.0 grape=0.15.0 doorkeeper=4.2.0
- rails=4.2.7 grape=0.16.2 doorkeeper=3.1.0
- rails=4.2.7 grape=0.16.2 doorkeeper=4.0.0
- rails=4.2.6 grape=0.16.2 doorkeeper=4.1.0
- rails=4.2.6 grape=0.16.2 doorkeeper=4.2.0
- rails=5.0.0 grape=0.16.2 doorkeeper=4.2.0
- rails=4.2.7 grape=0.17.0 doorkeeper=3.1.0
- rails=5.0.0 grape=0.17.0 doorkeeper=4.0.0
- rails=5.0.0 grape=0.17.0 doorkeeper=4.1.0
- rails=5.0.0 grape=0.17.0 doorkeeper=4.2.0
- rails=4.2.7 grape=0.18.0 doorkeeper=3.1.0
- rails=5.0.0 grape=0.18.0 doorkeeper=4.0.0
- rails=5.0.0 grape=0.18.0 doorkeeper=4.1.0
- rails=5.0.0 grape=0.18.0 doorkeeper=4.2.0

addons:
code_climate:
Expand All @@ -27,11 +39,3 @@ notifications:
hipchat:
rooms:
secure: SUWenlDzlDbpryO1QzD+rN4MxIBpAAzwsFqnnkyRQ11thRVdvKuT2TUd+RlYImLXDNkvNjqmpXh7mihtcro9g8unR3nF1UKbuAPIv2kCklsio0jAnjVn7+h1l56hsa90Jy9t/YpKtoLx2QNWLz70n8VrtGJMAt53T6tZdgNUp58=
matrix:
exclude:
- rvm: 2.1.0
env: rails=5.0.0 grape=0.16.2 doorkeeper=4.0.0
- rvm: 2.1.0
env: rails=5.0.0 grape=0.16.2 doorkeeper=4.1.0
- rvm: 2.1.0
env: rails=5.0.0 grape=0.16.2 doorkeeper=4.2.0
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog

## Unreleased

## 1.1.0
* [#63](https://github.com/antek-drzewiecki/wine_bouncer/pull/65): Auth strategies could coexist being specified in an array. Grape support restricted to 0.15 - 0.18. Code refactoring. Swagger 2.0 auth strategy implementation started.

## 1.0.1
* [#65](https://github.com/antek-drzewiecki/wine_bouncer/pull/65): Support for Doorkeeper 4.1 and 4.2. Thanks @daveallie
Expand Down
8 changes: 3 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

source 'https://rubygems.org'

ENV['grape'] ||= '0.16.2'
ENV['rails'] ||= '5.0.0'
ENV['doorkeeper'] ||= '4.0.0'

ruby '>= 2.2.2' if ENV['rails'][0].to_i > 4
ENV['grape'] ||= '0.18.0'
ENV['rails'] ||= '5.0.0.1'
ENV['doorkeeper'] ||= '4.2.0'

gem 'rails', ENV['rails']

Expand Down
48 changes: 30 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ Table of Contents


## Requirements
- Ruby > 2.0
- Doorkeeper > 1.4.0 and < 4.3
- Grape > 0.10 and < 1.0
- Ruby >= 2.2.2
- Ruby on Rails >= 4.2.6 and <= 5.0.0.1
- Doorkeeper = 3.1.0 (only working with Rails 4.2.x) and >=4.0.0 <= 4.2.0 (working with Rails 4.2.x and 5.0.x)
- Grape >= 0.15.0 and < 0.18.0

Please submit pull requests and Travis env bumps for newer dependency versions.

Expand All @@ -39,7 +40,7 @@ Please submit pull requests and Travis env bumps for newer dependency versions.
Add this line to your application's Gemfile:

```ruby
gem 'wine_bouncer', '~> 1.0.1'
gem 'wine_bouncer', '~> 1.1.0'
```

And then execute:
Expand All @@ -66,7 +67,7 @@ This creates a rails initializer in your Rails app at `config/initializers/wine_

``` ruby
WineBouncer.configure do |config|
config.auth_strategy = :default
config.auth_strategy = %i(default)

config.define_resource_owner do
User.find(doorkeeper_access_token.resource_owner_id) if doorkeeper_access_token
Expand Down Expand Up @@ -136,6 +137,8 @@ Behaviour of the authentication can be customized by selecting an authentication
#### Default
The default strategy only authenticates endpoints which are annotated by the `oauth2` method. Un-annotated endpoints still can be accessed without authentication.

If the config.auth_strategy array is ommited from the initializer file or commented, the :default strategy will be set and used automatically.

#### Swagger

WineBouncer comes with a strategy that can be perfectly used with [grape-swagger](https://github.com/tim-vandecasteele/grape-swagger) with a syntax compliant with the [swagger spec](https://github.com/wordnik/swagger-spec/).
Expand All @@ -149,7 +152,7 @@ Create a rails initializer in your Rails app at `config/initializers/wine_bounce

``` ruby
WineBouncer.configure do |config|
config.auth_strategy = :swagger
config.auth_strategy = %i(swagger)

config.define_resource_owner do
User.find(doorkeeper_access_token.resource_owner_id) if doorkeeper_access_token
Expand Down Expand Up @@ -184,6 +187,27 @@ If the authorization method is not set, the end point is assumed to be __protect
To protect your endpoint with other scopes append the following method `oauth2 'first scope', 'second scope'`.


#### Swagger 2.0

This strategy is in a work-in-progress state. It fetches all the authorixation scopes from the Swagger route
description, but could not yet differentiate the oauth grant type, i.e. the Security Definitions defined in Swagger document could not
yet be accessed at runtime in the grape-swagger gem. So, currently, all the scopes would be fetched regardless of
the OAuth grant type.

_NOTE_: [Swagger-UI is supporting only implicit flow yet](https://github.com/swagger-api/swagger-ui/issues/2406#issuecomment-248651879)

The symbolized name of the Swagger 2.0 auth strategy is :swagger_2 - see the initializer example below:

``` ruby
WineBouncer.configure do |config|
config.auth_strategy = %i(swagger_2)

config.define_resource_owner do
User.find(doorkeeper_access_token.resource_owner_id) if doorkeeper_access_token
end
end
```

### Token information

WineBouncer comes with free extras! Methods for `resource_owner` and `doorkeeper_access_token` get included in your endpoints. You can use them to get the current resource owner, and the access_token object of doorkeeper.
Expand All @@ -208,18 +232,6 @@ end

You probably shouldn't, though.

## Exceptions and Exception handling

This gem raises the following exceptions which can be handled in your Grape API, see [Grape documentation](https://github.com/intridea/grape#exception-handling).

* `WineBouncer::Errors::OAuthUnauthorizedError`
when the request is unauthorized.
* `WineBouncer::Errors::OAuthForbiddenError`
when the token is found but scopes do not match.

Detailed doorkeeper error response can be found in the error's `response` attribute. You could use
it to compose the actual HTTP response to API users.

## Example/Template Application

A full working sample app (or starter template) can be found at [grape-doorkeeper on github](https://github.com/sethherr/grape-doorkeeper). It has one click deploy to Heroku and [a live example](https://grape-doorkeeper.herokuapp.com/).
Expand Down
15 changes: 15 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
Upgrading WineBouncer
=====================

## Upgrading to >= 1.1.0

Auth strategies should be written now as an array of symbols in the WineBouncer's initializer:

```
config.auth_strategy = %i(default) # Although this could be entirely skipped - if not specified, the auth strategy is automatically set to %i(default).
```

or, for using both :swagger and :protected strategies:

```
config.auth_strategy = %i(protected swagger)
```

_NOTE_: Doorkeeper 3.1.0 is not compatible with Rails >= 5.0.0 - use Doorkeeper >= 4.0.0

## Upgrading to >= 1.0

Expand Down
4 changes: 2 additions & 2 deletions lib/generators/templates/wine_bouncer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

WineBouncer.configure do |config|
config.auth_strategy = :default
config.auth_strategy = %i(default) # :protected, :swagger and :swagger_2 strategies currently implemented

config.define_resource_owner do
config.define_resource_owner do |doorkeeper_access_token|
User.find(doorkeeper_access_token.resource_owner_id) if doorkeeper_access_token
end
end
4 changes: 1 addition & 3 deletions lib/wine_bouncer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
require 'grape'
require 'doorkeeper'
require 'wine_bouncer/version'
require 'wine_bouncer/errors'
require 'wine_bouncer/configuration'
require 'wine_bouncer/oauth2'
require 'wine_bouncer/base_strategy'
require 'wine_bouncer/auth_methods/auth_methods'
require 'wine_bouncer/helpers'
require 'wine_bouncer/extension'

module WineBouncer
Expand Down
39 changes: 0 additions & 39 deletions lib/wine_bouncer/auth_methods/auth_methods.rb

This file was deleted.

27 changes: 0 additions & 27 deletions lib/wine_bouncer/auth_strategies/default.rb

This file was deleted.

43 changes: 0 additions & 43 deletions lib/wine_bouncer/auth_strategies/protected.rb

This file was deleted.

33 changes: 0 additions & 33 deletions lib/wine_bouncer/auth_strategies/swagger.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/wine_bouncer/base_strategy.rb

This file was deleted.

Loading