omniauth-workos 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +20 -0
- data/LICENSE.txt +21 -0
- data/README.md +131 -0
- data/Rakefile +8 -0
- data/lib/omniauth/strategies/workos.rb +114 -0
- data/lib/omniauth-workos/version.rb +7 -0
- data/lib/omniauth-workos.rb +4 -0
- data/omniauth-workos.gemspec +26 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ca6792ae900fd8ff62ec50165bf0dad3925b4a8bf399d45bdd000b42154ee779
|
4
|
+
data.tar.gz: 8f59e1223bd81407fcd02980a16879d769b69d911ee7ca52d54131906528f10f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 39ac12ad8a083de04e6eb3765484fee4f5504e542de39bf97f2058aad0e95c1e89f5200f6526a3ef6d01e18c0705c8ac8dbc6581d06c8629a3c198171edad9e9
|
7
|
+
data.tar.gz: 4db30484b097f977fa668d04b21463d294541ab509c068750365261368c4ea845f971908f2bffeb75bb6d60cdf2b4adf72980bed5e222e86c37bb2f63e505bf1
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem "rake"
|
8
|
+
|
9
|
+
group :development do
|
10
|
+
gem "debug"
|
11
|
+
gem "retest"
|
12
|
+
end
|
13
|
+
|
14
|
+
group :test do
|
15
|
+
gem "rack-test", "~> 1.1"
|
16
|
+
gem "rspec", "~> 3.11"
|
17
|
+
gem "sinatra", "~> 2.2"
|
18
|
+
gem "timecop", "~> 0.9"
|
19
|
+
gem "webmock", "~> 3.14"
|
20
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Joao Fernandes
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
# OmniAuth WorkOS Strategy
|
2
|
+
|
3
|
+
An OmniAuth strategy for authenticating with [WorkOS](https://workos.com). This strategy is based on the [OmniAuth OAuth2 strategy](https://github.com/omniauth/omniauth-oauth2).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'omniauth-workos'
|
11
|
+
```
|
12
|
+
|
13
|
+
If you're using this strategy with Rails, also add the following for CSRF protection:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'omniauth-rails_csrf_protection'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle install
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
To start processing authentication requests, the following steps must be performed:
|
26
|
+
|
27
|
+
1. Initialize the strategy
|
28
|
+
2. Configure the callback controller
|
29
|
+
3. Add the required routes
|
30
|
+
4. Trigger an authentication request
|
31
|
+
|
32
|
+
### Authentication hash
|
33
|
+
|
34
|
+
The WorkOS strategy will provide the standard OmniAuth hash attributes:
|
35
|
+
|
36
|
+
- `:provider` - the name of the strategy, in this case `workos`
|
37
|
+
- `:uid` - the user identifier
|
38
|
+
- `:info` - the user's profile ([can be filtered](#filter-info-fields))
|
39
|
+
- `:credentials` - token requested and data
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
{
|
43
|
+
:provider => "workos",
|
44
|
+
:uid => "prof_01DMC79VCBZ0NY2099737PSVF1",
|
45
|
+
:info => {
|
46
|
+
:connection_id => "conn_01E4ZCR3C56J083X43JQXF3JK5",
|
47
|
+
:organization_id => "org_01EHWNCE74X7JSDV0X3SZ3KJNY",
|
48
|
+
:connection_type => "okta",
|
49
|
+
:email => "todd@foo-corp.com",
|
50
|
+
:name => "Todd Rundgren",
|
51
|
+
:first_name => "Todd",
|
52
|
+
:idp_id => "00u1a0ufowBJlzPlk357",
|
53
|
+
:last_name => "Rundgren",
|
54
|
+
:object => "profile",
|
55
|
+
:raw_attributes" => {...}
|
56
|
+
},
|
57
|
+
:credentials => {
|
58
|
+
:token => "ACCESS_TOKEN",
|
59
|
+
:expires_at => 1485373937,
|
60
|
+
:expires => true
|
61
|
+
}
|
62
|
+
}
|
63
|
+
```
|
64
|
+
|
65
|
+
#### Filter info fields
|
66
|
+
|
67
|
+
To filter the fields in the `info` object, you can specify them when you register the provider:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
provider
|
71
|
+
:workos,
|
72
|
+
ENV['WORKOS_CLIENT_ID'],
|
73
|
+
ENV['WORKOS_CLIENT_SECRET'],
|
74
|
+
ENV['WORKOS_DOMAIN'],
|
75
|
+
{
|
76
|
+
info_fields: %w[email first_name]
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
80
|
+
Possible values are:
|
81
|
+
- `"all"` (default) - don't filter, that is, include all fields
|
82
|
+
- `%w[...]` (an array of **strings**) - the fields to include
|
83
|
+
|
84
|
+
Note: field `"name"` will always be included.
|
85
|
+
|
86
|
+
### Query parameter options
|
87
|
+
|
88
|
+
In some scenarios, you may need to pass specific query parameters to `/sso/authorize`. The following parameters are available to enable this:
|
89
|
+
|
90
|
+
- `connection`
|
91
|
+
- `organization`
|
92
|
+
- `provider`
|
93
|
+
- `login_hint`
|
94
|
+
|
95
|
+
Refer to the [documentation](https://workos.com/docs/reference/sso/authorize/get#authorize-get-endpoint) to see when and how to use them.
|
96
|
+
|
97
|
+
Simply pass these query parameters to your OmniAuth redirect endpoint to enable their behavior.
|
98
|
+
|
99
|
+
## Development
|
100
|
+
|
101
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
102
|
+
|
103
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
104
|
+
|
105
|
+
## Contributing
|
106
|
+
|
107
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jcmfernandes/omniauth-workos.
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
Released under the MIT License. See [{file:LICENSE}](LICENSE).
|
112
|
+
|
113
|
+
Copyright (c) 2022, João Fernandes
|
114
|
+
|
115
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
116
|
+
of this software and associated documentation files (the "Software"), to deal
|
117
|
+
in the Software without restriction, including without limitation the rights
|
118
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
119
|
+
copies of the Software, and to permit persons to whom the Software is
|
120
|
+
furnished to do so, subject to the following conditions:
|
121
|
+
|
122
|
+
The above copyright notice and this permission notice shall be included in
|
123
|
+
all copies or substantial portions of the Software.
|
124
|
+
|
125
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
126
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
127
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
128
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
129
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
130
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
131
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "omniauth-oauth2"
|
4
|
+
|
5
|
+
module OmniAuth::Strategies
|
6
|
+
class WorkOS < OmniAuth::Strategies::OAuth2
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
AUTHORIZE_PARAMS_SESSION_KEY = "omniauth_workos_authorize_params"
|
10
|
+
private_constant :AUTHORIZE_PARAMS_SESSION_KEY
|
11
|
+
|
12
|
+
option :name, "workos"
|
13
|
+
option :client_options,
|
14
|
+
site: "https://api.workos.com",
|
15
|
+
authorize_url: "/sso/authorize",
|
16
|
+
token_url: "/sso/token"
|
17
|
+
option :authorize_options, %w[organization connection provider login_hint]
|
18
|
+
# info_fields:
|
19
|
+
# Either an **array** with the names of the fields as **strings**, or the
|
20
|
+
# the **string** "all" to return all available fields.
|
21
|
+
option :info_fields, "all"
|
22
|
+
|
23
|
+
uid do
|
24
|
+
raw_info.fetch("id")
|
25
|
+
end
|
26
|
+
|
27
|
+
info do
|
28
|
+
if options[:info_fields] == "all"
|
29
|
+
raw_info.clone.tap { |hash| hash.delete("id") }
|
30
|
+
else
|
31
|
+
{}.tap do |result|
|
32
|
+
options[:info_fields].each do |field|
|
33
|
+
field = field.to_s
|
34
|
+
result[field] = raw_info[field]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
credentials do
|
41
|
+
{
|
42
|
+
"token" => access_token.token,
|
43
|
+
# As per the documentation, tokens expire in 10 minutes.
|
44
|
+
"expires" => true,
|
45
|
+
"expires_at" => Time.now.utc.to_i + (10 * 60)
|
46
|
+
}.tap do
|
47
|
+
authorize_params = session.delete(AUTHORIZE_PARAMS_SESSION_KEY) || {}
|
48
|
+
|
49
|
+
# Confirm that the user comes from the connection/organization requested
|
50
|
+
# during the authorize phase.
|
51
|
+
unless authorize_params.key?("connection") || authorize_params.key?("organization")
|
52
|
+
raise Error.new("invalid session; no connection nor organization")
|
53
|
+
end
|
54
|
+
|
55
|
+
if authorize_params.key?("connection") && authorize_params["connection"] != raw_info["connection_id"]
|
56
|
+
raise Error.new("the user's connection_id `#{raw_info["connection_id"]}` doesn't match what was requested `#{authorize_params["connection"]}`")
|
57
|
+
end
|
58
|
+
|
59
|
+
if authorize_params.key?("organization") && authorize_params["organization"] != raw_info["organization_id"]
|
60
|
+
raise Error.new("the user's organization_id `#{raw_info["organization_id"]}` doesn't match what was requested `#{authorize_params["organization"]}`")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def authorize_params
|
66
|
+
super.tap do |params|
|
67
|
+
options[:authorize_options].each do |key|
|
68
|
+
value = request.params[key]
|
69
|
+
next if blank?(value)
|
70
|
+
|
71
|
+
params[key] = value
|
72
|
+
end
|
73
|
+
|
74
|
+
# Store the authorize params in the session because we will need them
|
75
|
+
# during the callback phase to verify that the user comes from the
|
76
|
+
# requested connection/organization.
|
77
|
+
session[AUTHORIZE_PARAMS_SESSION_KEY] = params
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def request_phase
|
82
|
+
if blank?(options.client_id)
|
83
|
+
fail!(:missing_client_id)
|
84
|
+
elsif blank?(options.client_secret)
|
85
|
+
fail!(:missing_client_secret)
|
86
|
+
else
|
87
|
+
super
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def callback_phase
|
92
|
+
super
|
93
|
+
rescue Error => e
|
94
|
+
fail!(:connection_or_organization_mismatch, e)
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def raw_info
|
100
|
+
@raw_info ||= access_token["profile"]
|
101
|
+
end
|
102
|
+
|
103
|
+
# https://github.com/omniauth/omniauth-oauth2/issues/93
|
104
|
+
def callback_url
|
105
|
+
full_host + script_name + callback_path
|
106
|
+
end
|
107
|
+
|
108
|
+
def blank?(obj)
|
109
|
+
obj.respond_to?(:empty?) ? obj.empty? : !obj
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
OmniAuth.config.add_camelization "workos", "WorkOS"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/omniauth-workos/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "omniauth-workos"
|
7
|
+
spec.version = OmniAuth::WorkOS::VERSION
|
8
|
+
spec.authors = ["João Fernandes"]
|
9
|
+
spec.email = ["joao.fernandes@ist.utl.pt"]
|
10
|
+
|
11
|
+
spec.summary = "OmniAuth OAuth2 strategy for the WorkOS platform"
|
12
|
+
spec.homepage = "https://github.com/jcmfernandes/omniauth-workos"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
|
17
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency 'omniauth', '~> 2.0'
|
25
|
+
spec.add_dependency 'omniauth-oauth2', '~> 1.7'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-workos
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- João Fernandes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: omniauth
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: omniauth-oauth2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- joao.fernandes@ist.utl.pt
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- CHANGELOG.md
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/omniauth-workos.rb
|
55
|
+
- lib/omniauth-workos/version.rb
|
56
|
+
- lib/omniauth/strategies/workos.rb
|
57
|
+
- omniauth-workos.gemspec
|
58
|
+
homepage: https://github.com/jcmfernandes/omniauth-workos
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/jcmfernandes/omniauth-workos
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubygems_version: 3.3.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: OmniAuth OAuth2 strategy for the WorkOS platform
|
82
|
+
test_files: []
|