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 +4 -4
- data/README.md +129 -11
- data/lib/omniauth/strategies/whiplash.rb +12 -26
- data/lib/omniauth/whiplash/version.rb +1 -1
- data/omniauth-whiplash.gemspec +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5bf177db66912eed1afa18035b59b3801edfa2a
|
4
|
+
data.tar.gz: 647973b4ad6f231446c470ff7f57703f2db1fdec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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
|
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
|
-
|
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
|
-
|
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
|
-
|
38
|
-
|
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
|
-
|
42
|
-
|
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
|
-
|
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.
|
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
|
-
{
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
23
|
+
@raw_info ||= access_token.get('/api/v2/me').parsed
|
33
24
|
end
|
34
25
|
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
data/omniauth-whiplash.gemspec
CHANGED
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.
|
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:
|
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.
|
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.
|
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.
|
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:
|