Skip to content

Commit 46041b6

Browse files
authored
Add get widget token API support. (#332)
1 parent 000f584 commit 46041b6

File tree

8 files changed

+364
-0
lines changed

8 files changed

+364
-0
lines changed

lib/workos.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def self.key
8181
autoload :VerifyChallenge, 'workos/verify_challenge'
8282
autoload :Webhook, 'workos/webhook'
8383
autoload :Webhooks, 'workos/webhooks'
84+
autoload :Widgets, 'workos/widgets'
8485

8586
# Errors
8687
autoload :APIError, 'workos/errors'

lib/workos/types.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ module Types
77
autoload :Intent, 'workos/types/intent'
88
autoload :ListStruct, 'workos/types/list_struct'
99
autoload :PasswordlessSessionStruct, 'workos/types/passwordless_session_struct'
10+
autoload :WidgetScope, 'workos/types/widget_scope'
1011
end
1112
end

lib/workos/types/widget_scope.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module WorkOS
4+
module Types
5+
# The WidgetScope constants are declarations of a fixed set of values for
6+
# scopes while generating a widget token.
7+
module WidgetScope
8+
USERS_TABLE_MANAGE = 'widgets:users-table:manage'
9+
10+
ALL = [USERS_TABLE_MANAGE].freeze
11+
end
12+
end
13+
end

lib/workos/widgets.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require 'net/http'
4+
5+
module WorkOS
6+
# The Widgets module provides resource methods for working with the Widgets APIs
7+
module Widgets
8+
class << self
9+
include Client
10+
11+
WIDGET_SCOPES = WorkOS::Types::WidgetScope::ALL
12+
13+
# Generate a widget token.
14+
#
15+
# @param [String] organization_id The ID of the organization to generate the token for.
16+
# @param [String] user_id The ID of the user to generate the token for.
17+
# @param [WidgetScope[]] The scopes to generate the token for.
18+
def get_token(organization_id:, user_id:, scopes:)
19+
validate_scopes(scopes)
20+
21+
request = post_request(
22+
auth: true,
23+
body: {
24+
organization_id: organization_id,
25+
user_id: user_id,
26+
scopes: scopes,
27+
},
28+
path: '/widgets/token',
29+
)
30+
31+
response = execute_request(request: request)
32+
33+
JSON.parse(response.body)['token']
34+
end
35+
36+
private
37+
38+
def validate_scopes(scopes)
39+
return if scopes.all? { |scope| WIDGET_SCOPES.include?(scope) }
40+
41+
raise ArgumentError, 'scopes contains an invalid value.' \
42+
" Every item in `scopes` must be in #{WIDGET_SCOPES}"
43+
end
44+
end
45+
end
46+
end

spec/lib/workos/widgets_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
describe WorkOS::Widgets do
4+
it_behaves_like 'client'
5+
6+
describe '.get_token' do
7+
let(:organization_id) { 'org_01JCP9G67MNAH0KC4B72XZ67M7' }
8+
let(:user_id) { 'user_01JCP9H4SHS4N3J6XTKDT7JNPE' }
9+
10+
describe 'with a valid organization_id and user_id and scopes' do
11+
it 'returns a widget token' do
12+
VCR.use_cassette 'widgets/get_token' do
13+
token = described_class.get_token(
14+
organization_id: organization_id,
15+
user_id: user_id,
16+
scopes: ['widgets:users-table:manage'],
17+
)
18+
19+
expect(token).to start_with('eyJhbGciOiJSUzI1NiIsImtpZ')
20+
end
21+
end
22+
end
23+
24+
describe 'with an invalid organization_id' do
25+
it 'raises an error' do
26+
VCR.use_cassette 'widgets/get_token_invalid_organization_id' do
27+
expect do
28+
described_class.get_token(
29+
organization_id: 'bogus-id',
30+
user_id: user_id,
31+
scopes: ['widgets:users-table:manage'],
32+
)
33+
end.to raise_error(
34+
WorkOS::NotFoundError,
35+
/Organization not found: 'bogus-id'/,
36+
)
37+
end
38+
end
39+
end
40+
41+
describe 'with an invalid user_id' do
42+
it 'raises an error' do
43+
VCR.use_cassette 'widgets/get_token_invalid_user_id' do
44+
expect do
45+
described_class.get_token(
46+
organization_id: organization_id,
47+
user_id: 'bogus-id',
48+
scopes: ['widgets:users-table:manage'],
49+
)
50+
end.to raise_error(
51+
WorkOS::NotFoundError,
52+
/User not found: 'bogus-id'/,
53+
)
54+
end
55+
end
56+
end
57+
58+
describe 'with invalid scopes' do
59+
it 'raises an error' do
60+
expect do
61+
described_class.get_token(
62+
organization_id: organization_id,
63+
user_id: user_id,
64+
scopes: ['bogus-scope'],
65+
)
66+
end.to raise_error(
67+
ArgumentError,
68+
/scopes contains an invalid value/,
69+
)
70+
end
71+
end
72+
end
73+
end

spec/support/fixtures/vcr_cassettes/widgets/get_token.yml

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/support/fixtures/vcr_cassettes/widgets/get_token_invalid_organization_id.yml

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/support/fixtures/vcr_cassettes/widgets/get_token_invalid_user_id.yml

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)