Skip to content

Commit 987ceb5

Browse files
Add roles to SSO profile and directory user (#411)
* Add roles to profile and directory user * Separate authz attributes * Move up primary email to public * Move primary email to the bottom * Adjust test
1 parent 40e2a61 commit 987ceb5

File tree

7 files changed

+43
-9
lines changed

7 files changed

+43
-9
lines changed

lib/workos/directory_user.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class DirectoryUser < DeprecatedHashWrapper
88
include HashProvider
99

1010
attr_accessor :id, :idp_id, :email, :emails, :first_name, :last_name, :job_title, :username, :state,
11-
:groups, :role, :custom_attributes, :raw_attributes, :directory_id, :organization_id,
11+
:groups, :role, :roles, :custom_attributes, :raw_attributes, :directory_id, :organization_id,
1212
:created_at, :updated_at
1313

1414
# rubocop:disable Metrics/AbcSize
@@ -37,6 +37,7 @@ def initialize(json)
3737
@state = hash[:state]
3838
@groups = hash[:groups]
3939
@role = hash[:role]
40+
@roles = hash[:roles]
4041
@custom_attributes = hash[:custom_attributes]
4142
@raw_attributes = hash[:raw_attributes]
4243
@created_at = hash[:created_at]
@@ -47,6 +48,13 @@ def initialize(json)
4748
# rubocop:enable Metrics/AbcSize
4849

4950
def to_json(*)
51+
base_attributes.
52+
merge(authorization_attributes)
53+
end
54+
55+
private
56+
57+
def base_attributes
5058
{
5159
id: id,
5260
directory_id: directory_id,
@@ -60,14 +68,22 @@ def to_json(*)
6068
username: username,
6169
state: state,
6270
groups: groups,
63-
role: role,
6471
custom_attributes: custom_attributes,
6572
raw_attributes: raw_attributes,
6673
created_at: created_at,
6774
updated_at: updated_at,
6875
}
6976
end
7077

78+
def authorization_attributes
79+
{
80+
role: role,
81+
roles: roles,
82+
}
83+
end
84+
85+
public
86+
7187
# @deprecated Will be removed in a future major version. Use {#email} instead.
7288
def primary_email
7389
primary_email = (emails || []).find { |email| email[:primary] }

lib/workos/profile.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module WorkOS
99
class Profile
1010
include HashProvider
1111

12-
attr_accessor :id, :email, :first_name, :last_name, :role, :groups, :organization_id,
12+
attr_accessor :id, :email, :first_name, :last_name, :role, :roles, :groups, :organization_id,
1313
:connection_id, :connection_type, :idp_id, :custom_attributes, :raw_attributes
1414

1515
# rubocop:disable Metrics/AbcSize
@@ -21,6 +21,7 @@ def initialize(profile_json)
2121
@first_name = hash[:first_name]
2222
@last_name = hash[:last_name]
2323
@role = hash[:role]
24+
@roles = hash[:roles]
2425
@groups = hash[:groups]
2526
@organization_id = hash[:organization_id]
2627
@connection_id = hash[:connection_id]
@@ -42,6 +43,7 @@ def to_json(*)
4243
first_name: first_name,
4344
last_name: last_name,
4445
role: role,
46+
roles: roles,
4547
groups: groups,
4648
organization_id: organization_id,
4749
connection_id: connection_id,

spec/lib/workos/directory_user_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,23 @@
3737
it 'returns no role' do
3838
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"[email protected]","emails":[{"primary":true,"value":"[email protected]"}, {"primary":false,"value":"[email protected]"}],"first_name":"Bob","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
3939
expect(user.role).to eq(nil)
40+
expect(user.roles).to eq(nil)
4041
end
4142
end
4243

43-
context 'with a role' do
44-
it 'returns the role slug' do
45-
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"[email protected]","emails":[{"primary":true,"value":"[email protected]"}, {"primary":false,"value":"[email protected]"}],"first_name":"Bob","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"role":{"slug":"member"},"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
44+
context 'with a single role' do
45+
it 'returns the highest priority role slug and roles array' do
46+
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"[email protected]","emails":[{"primary":true,"value":"[email protected]"}, {"primary":false,"value":"[email protected]"}],"first_name":"Bob","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"role":{"slug":"member"},"roles":[{"slug":"member"}],"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
4647
expect(user.role).to eq({ slug: 'member' })
48+
expect(user.roles).to eq([{ slug: 'member' }])
49+
end
50+
end
51+
52+
context 'with multiple roles' do
53+
it 'returns the highest priority role slug and roles array' do
54+
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"[email protected]","emails":[{"primary":true,"value":"[email protected]"}, {"primary":false,"value":"[email protected]"}],"first_name":"Bob","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"role":{"slug":"admin"},"roles":[{"slug":"member"}, {"slug":"admin"}],"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
55+
expect(user.role).to eq({ slug: 'admin' })
56+
expect(user.roles).to eq([{ slug: 'member' }, { slug: 'admin' }])
4757
end
4858
end
4959
end

spec/lib/workos/sso_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@
305305
role: {
306306
slug: 'member',
307307
},
308+
roles: [{
309+
slug: 'member',
310+
}],
308311
groups: nil,
309312
organization_id: 'org_01FG53X8636WSNW2WEKB2C31ZB',
310313
custom_attributes: {},
@@ -380,6 +383,9 @@
380383
role: {
381384
slug: 'admin',
382385
},
386+
roles: [{
387+
slug: 'admin',
388+
}],
383389
groups: %w[Admins Developers],
384390
organization_id: 'org_01FG53X8636WSNW2WEKB2C31ZB',
385391
custom_attributes: {

spec/support/fixtures/vcr_cassettes/directory_sync/get_user.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/support/fixtures/vcr_cassettes/sso/profile.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/support/profile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"profile":{"object":"profile","id":"prof_01DRA1XNSJDZ19A31F183ECQW5","email":"[email protected]","first_name":"WorkOS","organization_id":"org_01FG53X8636WSNW2WEKB2C31ZB","connection_id":"conn_01EMH8WAK20T42N2NBMNBCYHAG","connection_type":"OktaSAML","last_name":"Demo","role":{"slug": "admin"},"groups":["Admins","Developers"],"idp_id":"00u1klkowm8EGah2H357","custom_attributes":{"license": "professional"},"raw_attributes":{"id":"prof_01DRA1XNSJDZ19A31F183ECQW5","email":"[email protected]","first_name":"WorkOS","last_name":"Demo","groups":["Admins","Developers"],"idp_id":"00u1klkowm8EGah2H357","license": "professional"}},"access_token":"01DVX6QBS3EG6FHY2ESAA5Q65X"}
1+
{"profile":{"object":"profile","id":"prof_01DRA1XNSJDZ19A31F183ECQW5","email":"[email protected]","first_name":"WorkOS","organization_id":"org_01FG53X8636WSNW2WEKB2C31ZB","connection_id":"conn_01EMH8WAK20T42N2NBMNBCYHAG","connection_type":"OktaSAML","last_name":"Demo","role":{"slug": "admin"},"roles":[{"slug": "admin"}],"groups":["Admins","Developers"],"idp_id":"00u1klkowm8EGah2H357","custom_attributes":{"license": "professional"},"raw_attributes":{"id":"prof_01DRA1XNSJDZ19A31F183ECQW5","email":"[email protected]","first_name":"WorkOS","last_name":"Demo","groups":["Admins","Developers"],"idp_id":"00u1klkowm8EGah2H357","license": "professional"}},"access_token":"01DVX6QBS3EG6FHY2ESAA5Q65X"}

0 commit comments

Comments
 (0)