workos 0.11.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -0
  3. data/.semaphore/semaphore.yml +2 -2
  4. data/Gemfile.lock +2 -2
  5. data/LICENSE +1 -1
  6. data/README.md +13 -230
  7. data/lib/workos.rb +2 -0
  8. data/lib/workos/client.rb +21 -3
  9. data/lib/workos/connection.rb +5 -1
  10. data/lib/workos/directory_user.rb +4 -1
  11. data/lib/workos/organizations.rb +171 -0
  12. data/lib/workos/passwordless.rb +4 -0
  13. data/lib/workos/portal.rb +0 -80
  14. data/lib/workos/profile.rb +8 -10
  15. data/lib/workos/profile_and_token.rb +28 -0
  16. data/lib/workos/sso.rb +25 -100
  17. data/lib/workos/types/connection_struct.rb +1 -0
  18. data/lib/workos/types/directory_user_struct.rb +1 -0
  19. data/lib/workos/version.rb +1 -1
  20. data/spec/lib/workos/organizations_spec.rb +191 -0
  21. data/spec/lib/workos/portal_spec.rb +0 -113
  22. data/spec/lib/workos/sso_spec.rb +35 -115
  23. data/spec/support/fixtures/vcr_cassettes/organization/delete.yml +72 -0
  24. data/spec/support/fixtures/vcr_cassettes/organization/delete_invalid.yml +72 -0
  25. data/spec/support/fixtures/vcr_cassettes/organization/get.yml +73 -0
  26. data/spec/support/fixtures/vcr_cassettes/{sso/create_connection_with_invalid_source.yml → organization/get_invalid.yml} +26 -12
  27. data/spec/support/fixtures/vcr_cassettes/organization/update.yml +73 -0
  28. data/spec/support/fixtures/vcr_cassettes/organization/update_invalid.yml +73 -0
  29. data/spec/support/fixtures/vcr_cassettes/sso/profile.yml +74 -0
  30. data/workos.gemspec +1 -1
  31. metadata +23 -9
  32. data/spec/support/fixtures/vcr_cassettes/sso/create_connection_with_valid_source.yml +0 -63
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40285726466960ebdf92e8c851d172bc9387a0941579c1f2e4fb9b1b262ea071
4
- data.tar.gz: 3ceb76e02b50a129e077bc99822b0bc79615a2a8f4df1315c210766feb6333cb
3
+ metadata.gz: c1e38af4531cb784be6a6cc834752307e8bfcf7379dde43272c0e85b762f9f56
4
+ data.tar.gz: 2de643c621a51264c255d7d8b0a55d1aa7a900f5a1b9bd49877e6f5ad9f47956
5
5
  SHA512:
6
- metadata.gz: 27d9e22ba5b80d6dcfdcdd68886c64b720d7f94ee0724007599a4d2410e56b778771978046930e24a480f1bac0de4823d2dd39c1669cdbc36a4629547a84e9f6
7
- data.tar.gz: f750566f656fc734d9d112bb99285de9f371e64212fdfa404dae62f77473f97cc735990b157cc03e5f10032fe139a48d9a68533cd0f75b81f98d631408e17685
6
+ metadata.gz: '095f641cd40934575456a19cdf4dc5714d010555680d18b98f599467bafaf57b78f6f90988d064f53611f6d95bb7daa6e1ebdea450a7e7a03f43637026856a7b'
7
+ data.tar.gz: 157928a66025475cc4b306fbcc719ea6e3fe6404006f3fb00eb6498d7d4655d18076cbe64874137ba8c301a22561752c5feb40d9158225a89797e5eff36c1503
data/.rubocop.yml CHANGED
@@ -20,3 +20,5 @@ Style/TrailingCommaInArguments:
20
20
  EnforcedStyleForMultiline: 'consistent_comma'
21
21
  Style/TrailingCommaInHashLiteral:
22
22
  EnforcedStyleForMultiline: 'consistent_comma'
23
+ AllCops:
24
+ TargetRubyVersion: 2.5
@@ -33,13 +33,13 @@ blocks:
33
33
  - name: Ruby 1.9.3
34
34
  commands:
35
35
  - checkout
36
- - sem-version ruby 1.9.3
36
+ - sem-version ruby 1.9.3-p551
37
37
  - bundle install
38
38
  - bundle exec rspec
39
39
  - name: Ruby 2.0.0
40
40
  commands:
41
41
  - checkout
42
- - sem-version ruby 2.0.0
42
+ - sem-version ruby 2.0.0-p648
43
43
  - bundle install
44
44
  - bundle exec rspec
45
45
  - name: Ruby 2.3.4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- workos (0.11.0)
4
+ workos (1.2.0)
5
5
  sorbet-runtime (~> 0.5)
6
6
 
7
7
  GEM
@@ -60,7 +60,7 @@ GEM
60
60
  simplecov_json_formatter (0.1.2)
61
61
  sorbet (0.5.6388)
62
62
  sorbet-static (= 0.5.6388)
63
- sorbet-runtime (0.5.6391)
63
+ sorbet-runtime (0.5.6424)
64
64
  sorbet-static (0.5.6388-universal-darwin-14)
65
65
  sorbet-static (0.5.6388-universal-darwin-15)
66
66
  sorbet-static (0.5.6388-universal-darwin-16)
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019 WorkOS
3
+ Copyright (c) 2021 WorkOS
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,22 +1,22 @@
1
- # workos-ruby [![codecov](https://codecov.io/gh/workos-inc/workos-ruby/branch/master/graph/badge.svg)](https://codecov.io/gh/workos-inc/workos-ruby)
1
+ # WorkOS Ruby Library
2
2
 
3
- WorkOS official Ruby gem for interacting with WorkOS APIs
3
+ The WorkOS library for Ruby provides convenient access to the WorkOS API from applications written in Ruby.
4
4
 
5
5
  ## Documentation
6
6
 
7
- Complete documentation for the latest version of WorkOS Ruby Gem can be found [here](https://workos-inc.github.io/workos-ruby/).
7
+ See the [API Reference](https://workos.com/docs/reference/client-libraries) for Ruby usage examples.
8
8
 
9
9
  ## Installation
10
10
 
11
- To get started, you can install the WorkOS gem via RubyGems with:
11
+ Install the package with:
12
12
 
13
- ```ruby
13
+ ```
14
14
  gem install workos
15
15
  ```
16
16
 
17
17
  If you're using Bundler to manage your application's gems, add the WorkOS gem to your Gemfile:
18
18
 
19
- ```ruby
19
+ ```
20
20
  source 'https://rubygems.org'
21
21
 
22
22
  gem 'workos'
@@ -24,17 +24,13 @@ gem 'workos'
24
24
 
25
25
  ## Configuration
26
26
 
27
- To use the SDK you must first provide your API key from the [WorkOS Developer Dashboard](https://dashboard.workos.com/api-keys).
28
-
29
- You can do this through the `WORKOS_API_KEY` environment variable or by calling `WorkOS.key = [your API key]`.
30
-
31
- The WorkOS Gem will read the environment variable `WORKOS_API_KEY`:
27
+ To use the library you must provide an API key, located in the WorkOS dashboard, as an environment variable `WORKOS_API_KEY`:
32
28
 
33
29
  ```sh
34
30
  $ WORKOS_API_KEY=[your api key] ruby app.rb
35
31
  ```
36
32
 
37
- Alternatively, you may set the key yourself, such as in an initializer in your application load path:
33
+ Or, you may set the key yourself, such as in an initializer in your application load path:
38
34
 
39
35
  ```ruby
40
36
  # /config/initializers/workos.rb
@@ -42,222 +38,9 @@ Alternatively, you may set the key yourself, such as in an initializer in your a
42
38
  WorkOS.key = '[your api key]'
43
39
  ```
44
40
 
45
- ## The SSO Module
46
-
47
- The SSO Module provides convenient methods for authenticating a Single Sign On (SSO) user via WorkOS. WorkOS SSO follows the OAuth 2.0 specification.
48
-
49
- First, you'll direct your SSO users to an `authorization_url`. They will sign in to their SSO account with their Identity Provider, and be redirected to a
50
- callback URL that you set in your WorkOS Dashboard. The user will be redirected with a `code` URL parameter, which you can then exchange for a WorkOS::Profile
51
- using the `WorkOS::SSO.get_profile` method.
52
-
53
- See our Ruby SSO example app for a [complete example](https://github.com/workos-inc/ruby-sso-example).
54
-
55
- ```ruby
56
- WorkOS::SSO.authorization_url(domain:, client_id:, redirect_uri:, state: {})
57
- ```
58
-
59
- > Generate an authorization URL to intitiate the WorkOS OAuth2 workflow.
60
-
61
- `WorkOS::SSO.authorization_url` accepts four arguments:
62
-
63
- - `domain` (string) — the authenticating user's company domain, without protocol (ex. `example.com`)
64
- - `client_id` (string) — your application's WorkOS [Client ID](https://dashboard.workos.com/sso/configuration) (ex. `project_01JG3BCPTRTSTTWQR4VSHXGWCQ`)
65
- - `state` (optional, hash) — an optional hash used to manage state across authorization transactions (ex. `{ next_page: '/docs'}`)
66
- - `redirect_uri` (string) — a callback URL where your application redirects the user-agent after an authorization code is granted (ex. `workos.dev/callback`). This must match one of your configured callback URLs for the associated environment on your WorkOS dashboard.
67
-
68
- This method will return an OAuth2 query string of the form:
69
-
70
- `https://${domain}/sso/authorize?response_type=code&client_id=${clientID}&redirect_uri=${redirectURI}&state=${state}`
71
-
72
- For example, when used in a [Sinatra app](http://sinatrarb.com/):
73
-
74
- ```ruby
75
- DOMAIN = 'example.com'
76
- CLIENT_ID = '{clientId}'
77
- REDIRECT_URI = 'http://localhost:4567/callback'
78
-
79
- get '/auth' do
80
- authorization_url = WorkOS::SSO.authorization_url(
81
- domain: DOMAIN,
82
- client_id: CLIENT_ID,
83
- redirect_uri: REDIRECT_URI,
84
- )
85
-
86
- redirect authorization_url
87
- end
88
- ```
89
-
90
- The user would be redirected to:
91
-
92
- `https://api.workos.com/sso/authorize?response_type=code&client_id={clientID}&redirect_uri=http://localhost:4567/callback`
93
-
94
- WorkOS takes over from here, sending the user to authenticate with their IDP, and on successful login, returns
95
- the user to your callback URL with a `code` parameter. You'll use `WorkOS::SSO.profile` to exchange the
96
- code for a `WorkOS::Profile`.
97
-
98
- ```ruby
99
- WorkOS::SSO.profile(code:, client_id:)
100
- ```
101
-
102
- > Fetch a WorkOS::Profile for an authorized user.
103
-
104
- `WorkOS::SSO.profile` accepts two arguments:
105
-
106
- - `code` (string) — an opaque string provided by the authorization server; will be exchanged for an Access Token when the user's profile is sent
107
- - `client_id` (string) — your application's WorkOS [Client ID](https://dashboard.workos.com/sso/configuration) (ex. `project_01JG3BCPTRTSTTWQR4VSHXGWCQ`)
108
-
109
- This method will return an instance of a `WorkOS::Profile` with the following attributes:
110
-
111
- ```ruby
112
- <WorkOS::Profile:0x00007fb6e4193d20
113
- @id="prof_01DRA1XNSJDZ19A31F183ECQW5",
114
- @email="demo@workos-okta.com",
115
- @first_name="WorkOS",
116
- @connection_id="conn_01EMH8WAK20T42N2NBMNBCYHAG",
117
- @connection_type="OktaSAML",
118
- @last_name="Demo",
119
- @idp_id="00u1klkowm8EGah2H357",
120
- @raw_attributes={
121
- :id=>"prof_01DRA1XNSJDZ19A31F183ECQW5",
122
- :email=>"demo@workos-okta.com",
123
- :first_name=>"WorkOS",
124
- :last_name=>"Demo",
125
- :idp_id=>"00u1klkowm8EGah2H357"
126
- },
127
- >
128
- ```
129
-
130
- Our Sinatra app can be extended to use this method:
131
-
132
- ```ruby
133
- DOMAIN = 'example.com'
134
- CLIENT_ID = '{clientId}'
135
- REDIRECT_URI = 'http://localhost:4567/callback'
136
-
137
- get '/auth' do
138
- authorization_url = WorkOS::SSO.authorization_url(
139
- domain: DOMAIN,
140
- client_id: CLIENT_ID,
141
- redirect_uri: REDIRECT_URI,
142
- )
143
-
144
- redirect authorization_url
145
- end
146
-
147
- get '/callback' do
148
- profile = WorkOS::SSO.profile(
149
- code: params['code'],
150
- client_id: CLIENT_ID,
151
- )
152
-
153
- session[:user] = profile.to_json
154
-
155
- redirect '/'
156
- end
157
- ```
158
-
159
- Given the `WorkOS::Profile`, you can now sign the user in according to your own authentication setup.
160
-
161
- ## The Magic Link Module
162
-
163
- The Magic Link Module provides methods for authenticating a Passwordless user via WorkOS.
164
-
165
- First, you'll create a Passwordless Session for a Magic Link connection.
166
- Then, using the session ID, you'll email a user the Magic Link confirmation URL.
167
- The user can then click on that link to be authenticated to your application.
168
-
169
- > Create a Passwordless Session for a Magic Link Connection.
170
-
171
- `WorkOS::Passwordless.create_session` accepts four arguments:
172
-
173
- - `email` (string) - the email of the user to authenticate.
174
- - `type` (string) - The type of Passwordless Session to create. Currently, the only supported value is `MagicLink`.
175
- - `state` (optional, string) - Optional parameter that a Developer can choose to include in their authorization URL. If included, then the redirect URI received from WorkOS will contain the exact `state` that was passed in the authorization URL.
176
- - `redirect_uri` (string) - a callback URL where your application redirects the user-agent after an authorization code is granted (ex. `workos.dev/callback`). This must match one of your configured callback URLs for the associated environment on your WorkOS dashboard.
177
-
178
- This method will return a Passwordless Session object, containing the following attributes:
179
-
180
- - `id` (string) - the unique ID of the session.
181
- - `email` (string) - the email address of the user for the session.
182
- - `expires_at` (date) - the ISO-8601 datetime at which the session expires.
183
- - `link` (string) - the link for the user to authenticate with. You can use this link to send a custom email to the user, or send an email using the `WorkOS::Passwordless.send_session` method, described below.
184
-
185
- > Email a user the Magic Link confirmation URL.
186
-
187
- `WorkOS::Passwordless.send_session` accepts one argument:
188
-
189
- - `id` (string) - the unique identifier of the Passwordless Session to send an email for.
190
-
191
- This method will return a boolean confirming the Magic Link was sent.
192
-
193
- > Example with Sinatra application
194
-
195
- Our Sinatra app can be altered to use Magic Link:
196
-
197
- ```ruby
198
- CLIENT_ID = '{clientId}'
199
- REDIRECT_URI = 'http://localhost:4567/callback'
200
-
201
- post '/passwordless-auth' do
202
- session = WorkOS::Passwordless.create_session(
203
- email: params[:email],
204
- type: 'MagicLink',
205
- redirect_uri: REDIRECT_URI
206
- )
207
- WorkOS::Passwordless.send_session(session.id)
208
-
209
- redirect '/check-email'
210
- end
211
-
212
- get '/callback' do
213
- profile = WorkOS::SSO.profile(
214
- code: params['code'],
215
- client_id: CLIENT_ID,
216
- )
217
-
218
- session[:user] = profile.to_json
219
-
220
- redirect '/'
221
- end
222
- ```
223
-
224
- ## The Audit Trail Module
225
-
226
- The Audit Trail Module provides methods for creating Audit Trail events on
227
- WorkOS.
228
-
229
- See our [Audit Trail
230
- Overview](https://docs.workos.com/audit-trail/overview) for
231
- more information.
232
-
233
- ```ruby
234
- payload = {
235
- group: 'Foo Corp',
236
- location: '127.0.0.1',
237
- action: 'user.created',
238
- action_type: 'C',
239
- actor_name: 'Foo',
240
- actor_id: 'user_12345',
241
- target_name: 'Bar',
242
- target_id: 'user_67890',
243
- occurred_at: '2020-01-10T15:30:00-05:00',
244
- metadata: {
245
- source: 'Email',
246
- }
247
- }
248
-
249
- WorkOS::AuditTrail.create_event(event: payload)
250
- ```
251
-
252
- ### Idempotency
253
-
254
- To perform an idempotent request, provide an additional idempotency_key
255
- parameter to the `create_event` options.
256
-
257
- ```ruby
258
- WorkOS::AuditTrail.create_event(event: payload, idempotency_key: 'key123456')
259
- ```
41
+ ## More Information
260
42
 
261
- See our [API
262
- Reference](https://docs.workos.com/audit-trail/api-reference#idempotency)
263
- for more information on idempotency keys.
43
+ * [Single Sign-On Guide](https://workos.com/docs/sso/guide)
44
+ * [Directory Sync Guide](https://workos.com/docs/directory-sync/guide)
45
+ * [Admin Portal Guide](https://workos.com/docs/admin-portal/guide)
46
+ * [Magic Link Guide](https://workos.com/docs/magic-link/guide)
data/lib/workos.rb CHANGED
@@ -35,9 +35,11 @@ module WorkOS
35
35
  autoload :Directory, 'workos/directory'
36
36
  autoload :DirectoryGroup, 'workos/directory_group'
37
37
  autoload :Organization, 'workos/organization'
38
+ autoload :Organizations, 'workos/organizations'
38
39
  autoload :Passwordless, 'workos/passwordless'
39
40
  autoload :Portal, 'workos/portal'
40
41
  autoload :Profile, 'workos/profile'
42
+ autoload :ProfileAndToken, 'workos/profile_and_token'
41
43
  autoload :SSO, 'workos/sso'
42
44
  autoload :DirectoryUser, 'workos/directory_user'
43
45
 
data/lib/workos/client.rb CHANGED
@@ -19,7 +19,7 @@ module WorkOS
19
19
 
20
20
  sig do
21
21
  params(
22
- request: T.any(Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Delete),
22
+ request: T.any(Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Delete, Net::HTTP::Put),
23
23
  ).returns(::T.untyped)
24
24
  end
25
25
  def execute_request(request:)
@@ -36,9 +36,10 @@ module WorkOS
36
36
  path: String,
37
37
  auth: T.nilable(T::Boolean),
38
38
  params: T.nilable(Hash),
39
+ access_token: T.nilable(String),
39
40
  ).returns(Net::HTTP::Get)
40
41
  end
41
- def get_request(path:, auth: false, params: {})
42
+ def get_request(path:, auth: false, params: {}, access_token: nil)
42
43
  uri = URI(path)
43
44
  uri.query = URI.encode_www_form(params) if params
44
45
 
@@ -47,7 +48,7 @@ module WorkOS
47
48
  'Content-Type' => 'application/json',
48
49
  )
49
50
 
50
- request['Authorization'] = "Bearer #{WorkOS.key!}" if auth
51
+ request['Authorization'] = "Bearer #{access_token || WorkOS.key!}}" if auth
51
52
  request['User-Agent'] = user_agent
52
53
  request
53
54
  end
@@ -90,6 +91,23 @@ module WorkOS
90
91
  request
91
92
  end
92
93
 
94
+ sig do
95
+ params(
96
+ path: String,
97
+ auth: T.nilable(T::Boolean),
98
+ idempotency_key: T.nilable(String),
99
+ body: T.nilable(Hash),
100
+ ).returns(Net::HTTP::Put)
101
+ end
102
+ def put_request(path:, auth: false, idempotency_key: nil, body: nil)
103
+ request = Net::HTTP::Put.new(path, 'Content-Type' => 'application/json')
104
+ request.body = body.to_json if body
105
+ request['Authorization'] = "Bearer #{WorkOS.key!}" if auth
106
+ request['Idempotency-Key'] = idempotency_key if idempotency_key
107
+ request['User-Agent'] = user_agent
108
+ request
109
+ end
110
+
93
111
  sig { returns(String) }
94
112
  def user_agent
95
113
  engine = defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : 'Ruby'
@@ -5,11 +5,12 @@ module WorkOS
5
5
  # The Connection class provides a lightweight wrapper around
6
6
  # a WorkOS Connection resource. This class is not meant to be instantiated
7
7
  # in user space, and is instantiated internally but exposed.
8
+ # Note: status is deprecated - use state instead
8
9
  class Connection
9
10
  extend T::Sig
10
11
 
11
12
  attr_accessor :id, :name, :connection_type, :domains, :organization_id,
12
- :status
13
+ :state, :status
13
14
 
14
15
  sig { params(json: String).void }
15
16
  def initialize(json)
@@ -20,6 +21,7 @@ module WorkOS
20
21
  @connection_type = T.let(raw.connection_type, String)
21
22
  @domains = T.let(raw.domains, Array)
22
23
  @organization_id = T.let(raw.organization_id, String)
24
+ @state = T.let(raw.state, String)
23
25
  @status = T.let(raw.status, String)
24
26
  end
25
27
 
@@ -30,6 +32,7 @@ module WorkOS
30
32
  connection_type: connection_type,
31
33
  domains: domains,
32
34
  organization_id: organization_id,
35
+ state: state,
33
36
  status: status,
34
37
  }
35
38
  end
@@ -46,6 +49,7 @@ module WorkOS
46
49
  connection_type: hash[:connection_type],
47
50
  domains: hash[:domains],
48
51
  organization_id: hash[:organization_id],
52
+ state: hash[:state],
49
53
  status: hash[:status],
50
54
  )
51
55
  end
@@ -8,7 +8,7 @@ module WorkOS
8
8
  class DirectoryUser
9
9
  extend T::Sig
10
10
 
11
- attr_accessor :id, :emails, :first_name, :last_name, :username, :state,
11
+ attr_accessor :id, :idp_id, :emails, :first_name, :last_name, :username, :state,
12
12
  :raw_attributes
13
13
 
14
14
  sig { params(json: String).void }
@@ -16,6 +16,7 @@ module WorkOS
16
16
  raw = parse_json(json)
17
17
 
18
18
  @id = T.let(raw.id, String)
19
+ @idp_id = T.let(raw.idp_id, String)
19
20
  @emails = T.let(raw.emails, Array)
20
21
  @first_name = raw.first_name
21
22
  @last_name = raw.last_name
@@ -27,6 +28,7 @@ module WorkOS
27
28
  def to_json(*)
28
29
  {
29
30
  id: id,
31
+ idp_id: idp_id,
30
32
  emails: emails,
31
33
  first_name: first_name,
32
34
  last_name: last_name,
@@ -48,6 +50,7 @@ module WorkOS
48
50
 
49
51
  WorkOS::Types::DirectoryUserStruct.new(
50
52
  id: hash[:id],
53
+ idp_id: hash[:idp_id],
51
54
  emails: hash[:emails],
52
55
  first_name: hash[:first_name],
53
56
  last_name: hash[:last_name],