omniauth-whiplash 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ddcdbdf5c216dcde0d9cd8e1fccf00de859be9f
4
- data.tar.gz: 64d0771c33f7dadb3b68d6be36785596f599cb1a
3
+ metadata.gz: f5bf177db66912eed1afa18035b59b3801edfa2a
4
+ data.tar.gz: 647973b4ad6f231446c470ff7f57703f2db1fdec
5
5
  SHA512:
6
- metadata.gz: 0fcc782373d6bd909a506e682c7a8c85f52136c78b2d2bff17e81f08c373327afc531000e1e036ba85f0b816718ec9102952eb7be9b916bdd6bd13bfdcdede55
7
- data.tar.gz: d0559dbccbf836fedcdd4e987aa30378643674e4b5c1612c3fc43a07c9029692ab63f2c019a1e1e9864cf240b4215e718cc25f9481e5fb1454af210e24884af5
6
+ metadata.gz: a8c04bddd93f89e43ca94bc641bed36a3a60c2f2ab20f27158a6002664ea075448cb4a55be08ba6ad8633315ad7c161630171620a35f3f0e4727ec9745323378
7
+ data.tar.gz: 34d068dcba1e6930a115d5bf0c9abe0a7b752e59f9ca99d417cb6517d2a130d5c10f085b1c4417b63775c86f3ff20d591c9b3d32bb6e5b19a2c640ebb5c0d31d
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Omniauth::Whiplash
2
2
 
3
- Whiplash OAuth2 Strategy for OmniAuth 1.0.
3
+ Whiplash OAuth2 Strategy for OmniAuth.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,32 +20,150 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- `OmniAuth::Strategies::Whiplash` is simply a Rack middleware. Read the OmniAuth 1.0 docs for detailed instructions.
23
+ `OmniAuth::Strategies::Whiplash` is simply a Rack middleware. Read the `omniauth-oauth2` docs for detailed instructions.
24
24
 
25
25
  Here's a quick example, adding the middleware to a Rails app in config/initializers/omniauth.rb:
26
26
 
27
27
  ```ruby
28
28
  Rails.application.config.middleware.use OmniAuth::Builder do
29
- provider :whiplash, ENV['WHILASH_CLIENT_ID'], ENV['WHIPLASH_CLIENT_SECRET']
29
+ provider :whiplash, ENV.fetch('WHIPLASH_CLIENT_ID'), ENV.fetch('WHIPLASH_CLIENT_SECRET'), scope: ENV.fetch('WHIPLASH_CLIENT_SCOPE')
30
30
  end
31
31
  ```
32
32
 
33
- ## Configuration
33
+ If you are using Devise, you can skip the above and instead include this to your Devise configuration in `initializers/devise.rb`:
34
34
 
35
- You can configure the scope, which you pass in to the provider method via a Hash:
35
+ ```ruby
36
+ config.omniauth :whiplash, ENV.fetch('WHIPLASH_CLIENT_ID'), ENV.fetch('WHIPLASH_CLIENT_SECRET'), scope: ENV.fetch('WHIPLASH_CLIENT_SCOPE')
37
+ ```
38
+
39
+ Please refer to the Whiplash API documentation for information regarding scopes.
40
+
41
+ ## Single Sign-On (SSO)
36
42
 
37
- `scope`: A comma-separated list of permissions you want to request from the user. See the Shopify API docs for a full list of available permissions.
38
- For example, to request read_products, read_orders and write_content permissions and display the authentication page:
43
+ There are a few steps to follow to get SSO configured via Oauth2. The solution below uses Devise. You don't have to use Devise, but it provides some of the OAuth2 legwork and so that is what we recommend.
44
+
45
+ *Note:* User accounts that are admin-level on Whiplash will automatically authorize your application. That means admins redirected to Whiplash for authentication will be immediately redirected back to your application if the permissions were configured correctly.
46
+
47
+ ### 1. Configure your application for Devise
48
+
49
+ Add to your `Gemfile`:
39
50
 
40
51
  ```ruby
41
- Rails.application.config.middleware.use OmniAuth::Builder do
42
- provider :whiplash, ENV['WHILASH_CLIENT_ID'], ENV['WHIPLASH_CLIENT_SECRET'], scope: 'read_orders write_orders read_items write_items read_web_hooks write_web_hooks read_customers read_user'
52
+ gem 'devise', '~> 4.3.0'
53
+ ```
54
+
55
+ Install Devise:
56
+
57
+ ```
58
+ rails generate devise:install
59
+ ```
60
+
61
+ Create a `User` model:
62
+
63
+ ```
64
+ rails generate devise User
65
+ ```
66
+
67
+ ### 2. Modify the user migration
68
+
69
+ You can remove some default Devise columns as we will not be using a standard Devise configuration.
70
+
71
+ Here is a sample migration that includes all the fields returned by the Whiplash OAuth endpoint:
72
+
73
+ ```ruby
74
+ class DeviseCreateUsers < ActiveRecord::Migration[5.1]
75
+ def change
76
+ enable_extension("citext")
77
+
78
+ create_table :users do |t|
79
+ t.citext :email, null: false, default: ""
80
+
81
+ t.string :provider
82
+ t.string :uid
83
+ t.string :first_name
84
+ t.string :last_name
85
+ t.string :role
86
+ t.string :whiplash_id
87
+
88
+ t.timestamps null: false
89
+ end
90
+
91
+ add_index :users, :email, unique: true
92
+ end
43
93
  end
44
94
  ```
45
95
 
46
- NOTE: The default scope is `read_user` and is required as part of the `scope` argument, if it's passed in.
96
+ *Please Note:* We are using the case-insensitive Postgres column type (`citext`) here. If you are using MySQL, you will want to switch this to `string` as that defaults to case-insensitive.
97
+
98
+ ### 3. Setup the User Model
99
+
100
+ You can add any additional validations or methods as per usual to the `User` model. This is just the base setup to get SSO working.
101
+
102
+ ```ruby
103
+ class User < ApplicationRecord
104
+
105
+ devise :omniauthable, omniauth_providers: [:whiplash]
106
+
107
+ def self.from_omniauth(omniauth_params)
108
+ User.find_or_create_by(email: omniauth_params.info['email']) do |u|
109
+ u.first_name = omniauth_params.info['first_name']
110
+ u.last_name = omniauth_params.info['last_name']
111
+ u.whiplash_id = omniauth_params.info['id']
112
+ u.role = omniauth_params.info['role']
113
+ end
114
+ end
115
+
116
+ end
117
+ ```
118
+
119
+ The `self.from_omniauth` method is called automatically when a user is signing in. It will create a `User` record for new users and return existing users for previously created ones.
120
+
121
+ ### 4. Setup the OmniAuth Endpoint
122
+
123
+ Create the controller in `controllers/users/omniauth_callbacks_controller.rb`:
124
+
125
+ ```ruby
126
+ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
127
+
128
+ skip_before_action :require_user
129
+
130
+ def whiplash
131
+ @user = User.from_omniauth(request.env["omniauth.auth"])
132
+
133
+ if @user.persisted?
134
+ sign_in_and_redirect(@user, event: :authentication)
135
+ end
136
+ end
137
+
138
+ def failure
139
+ redirect_to root_path
140
+ end
141
+
142
+ end
143
+ ```
144
+
145
+ *Please Note:* We have a `before_action` defined here called `require_user`. We have that defined in `ApplicationController` like so:
146
+
147
+ ```ruby
148
+ def require_user
149
+ unless current_user
150
+ redirect_to user_whiplash_omniauth_authorize_path
151
+ end
152
+ end
153
+ ```
154
+
155
+ Any controller you would like to place behind the SSO login, you can add the respective `before_action :require_user`.
156
+
157
+ Lastly, create the route in `routes.rb`
158
+
159
+ ```ruby
160
+ devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
161
+ ```
162
+
163
+ ### 5. There is no step 5!
164
+
165
+ You are done. Just make sure you have set the environment variables and Devise configuration degined in the Usage section.
47
166
 
48
- ALSO: The scope arguments should be passed in *separated by spaces, not commas*, as per above.
49
167
 
50
168
  ## Development
51
169
 
@@ -4,42 +4,28 @@ module OmniAuth
4
4
  option :name, :whiplash
5
5
 
6
6
  option :client_options, {
7
- site: "https://www.whiplashmerch.com",
8
- authorize_url: "oauth/authorize",
9
- request_token_url: "oauth/authorize",
10
- access_token_url: "oauth/token"
7
+ site: ENV['WHIPLASH_API_URL'] || "https://www.getwhiplash.com",
8
+ authorize_url: "/oauth/authorize"
11
9
  }
12
10
 
13
11
  uid { raw_info["id"] }
14
12
 
15
13
  info do
16
- { email: raw_info["email"], name: user_full_name }
17
- end
18
-
19
- extra do
20
- { raw_info: raw_info }
21
- end
22
-
23
- def callback_url
24
- options[:callback_url] || super
25
- end
26
-
27
- def user_full_name
28
- "#{raw_info['first_name']} #{raw_info['last_name']}".strip
14
+ {
15
+ email: raw_info["email"],
16
+ first_name: raw_info["first_name"],
17
+ last_name: raw_info["last_name"],
18
+ role: raw_info["role"]
19
+ }
29
20
  end
30
21
 
31
22
  def raw_info
32
- @raw_info ||= access_token.get('/api/v2/me.json').parsed
23
+ @raw_info ||= access_token.get('/api/v2/me').parsed
33
24
  end
34
25
 
35
- def authorize_params
36
- super.tap do |params|
37
- %w[scope client_options].each do |v|
38
- if request.params[v]
39
- params[v.to_sym] = request.params[v]
40
- end
41
- end
42
- end
26
+ # https://github.com/intridea/omniauth-oauth2/issues/81
27
+ def callback_url
28
+ full_host + script_name + callback_path
43
29
  end
44
30
  end
45
31
  end
@@ -1,5 +1,5 @@
1
1
  module Omniauth
2
2
  module Whiplash
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -24,5 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rspec"
25
25
 
26
26
  spec.add_dependency 'omniauth', '~> 1.0'
27
- spec.add_dependency 'omniauth-oauth2', '~> 1.3.1'
27
+ spec.add_dependency 'omniauth-oauth2', '~> 1.4'
28
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-whiplash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Dickson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-04-25 00:00:00.000000000 Z
12
+ date: 2017-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -73,14 +73,14 @@ dependencies:
73
73
  requirements:
74
74
  - - "~>"
75
75
  - !ruby/object:Gem::Version
76
- version: 1.3.1
76
+ version: '1.4'
77
77
  type: :runtime
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
- version: 1.3.1
83
+ version: '1.4'
84
84
  description: Omniauth Strategy for Whiplash Merchandising
85
85
  email:
86
86
  - mark@whiplashmerch.com
@@ -124,9 +124,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  requirements: []
126
126
  rubyforge_project:
127
- rubygems_version: 2.4.5.1
127
+ rubygems_version: 2.6.11
128
128
  signing_key:
129
129
  specification_version: 4
130
130
  summary: Omniauth Strategy for Whiplash Merchandising
131
131
  test_files: []
132
- has_rdoc: