Skip to content

Commit 74eaac9

Browse files
authored
[SDK-4546] Add orgs in client credentials support (#540)
2 parents cc8bb2d + 29e81f4 commit 74eaac9

8 files changed

+207
-5
lines changed

lib/auth0/api/authentication_endpoints.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def api_token(
3131
request_params = {
3232
grant_type: 'client_credentials',
3333
client_id: client_id,
34-
audience: audience
34+
audience: audience,
35+
organization: organization
3536
}
3637

3738
populate_client_assertion_or_secret(request_params, client_id: client_id, client_secret: client_secret)

lib/auth0/api/v2/client_grants.rb

+27-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ module ClientGrants
1111
# @param audience [string] The audience of the client grant to retrieve.
1212
# @param page [int] Page number to get, 0-based.
1313
# @param per_page [int] Results per page if also passing a page number.
14+
# @param allow_any_organization [bool] Optional filter on allow_any_organization.
1415
# @return [json] Returns the client grants.
15-
def client_grants (client_id: nil, audience: nil, page: nil, per_page: nil)
16+
def client_grants (client_id: nil, audience: nil, page: nil, per_page: nil, allow_any_organization: nil)
1617
request_params = {
1718
client_id: client_id,
1819
audience: audience,
1920
page: page,
20-
per_page: per_page
21+
per_page: per_page,
22+
allow_any_organization: allow_any_organization
2123
}
2224
get(client_grants_path, request_params)
2325
end
@@ -54,6 +56,29 @@ def patch_client_grant(client_grant_id, options)
5456
end
5557
alias update_client_grant patch_client_grant
5658

59+
60+
# Get the organizations associated to a client grant.
61+
# @param id [string] The client_grant_id of the client grant.
62+
# @param options [hash] The Hash options used to define the paging of results
63+
# * :per_page [integer] The amount of entries per page. Default: 50. Max value: 100.
64+
# * :page [integer] The page number. Zero based.
65+
# * :from [string] For checkpoint pagination, the ID from which to start selection from.
66+
# * :take [integer] For checkpoint pagination, the number of entries to retrieve. Default is 50.
67+
# * :include_totals [boolean] True to include query summary in the result, false or nil otherwise.
68+
# @return [json] Returns the organizations.
69+
def get_client_grants_organizations(client_grant_id, options = {})
70+
raise Auth0::InvalidParameter, 'Must specify a client grant id' if client_grant_id.to_s.empty?
71+
request_params = {
72+
per_page: options.fetch(:per_page, nil),
73+
page: options.fetch(:page, nil),
74+
from: options.fetch(:from, nil),
75+
take: options.fetch(:take, nil),
76+
include_totals: options.fetch(:include_totals, nil)
77+
}
78+
path = "#{client_grants_path}/#{client_grant_id}/organizations"
79+
get(path, request_params)
80+
end
81+
5782
private
5883

5984
# Client Grants API path

lib/auth0/api/v2/organizations.rb

+50
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,52 @@ def delete_organizations_member_roles(organization_id, user_id, roles = [])
330330
end
331331
alias remove_organizations_member_roles delete_organizations_member_roles
332332

333+
# Get client grants associated to an organization
334+
# @param organization_id [string] The Organization ID
335+
# @param options [hash] The Hash options used to define the paging of results
336+
# * :client_id [string] The client_id of the client grant to retrieve.
337+
# * :audience [string] The audience of the client grant to retrieve.
338+
# * :per_page [integer] The amount of entries per page. Default: 50. Max value: 100.
339+
# * :page [integer] The page number. Zero based.
340+
# * :include_totals [boolean] True to include query summary in the result, false or nil otherwise.
341+
def get_organizations_client_grants(organization_id, options= {})
342+
raise Auth0::MissingOrganizationId, 'Must supply a valid organization_id' if organization_id.to_s.empty?
343+
request_params = {
344+
client_id: options.fetch(:client_id, nil),
345+
audience: options.fetch(:audience, nil),
346+
per_page: options.fetch(:per_page, nil),
347+
page: options.fetch(:page, nil),
348+
include_totals: options.fetch(:include_totals, nil)
349+
}
350+
path = "#{organizations_client_grants_path(organization_id)}"
351+
get(path, request_params)
352+
end
353+
354+
# Associate a client grant with an organization
355+
# @param organization_id [string] The Organization ID
356+
# @param grant_id [string] The Client Grant ID you want to associate to the Organization.
357+
def create_organizations_client_grant(organization_id, grant_id)
358+
raise Auth0::MissingOrganizationId, 'Must supply a valid organization_id' if organization_id.to_s.empty?
359+
raise Auth0::InvalidParameter, 'Must supply a valid grant_id' if grant_id.to_s.empty?
360+
361+
body = {}
362+
body[:grant_id] = grant_id
363+
364+
path = "#{organizations_client_grants_path(organization_id)}"
365+
post(path, body)
366+
end
367+
368+
# Remove a client grant from an organization
369+
# @param organization_id [string] The Organization ID
370+
# @param grant_id [string] The Client Grant ID you want to remove from the Organization.
371+
def delete_organizations_client_grant(organization_id, grant_id)
372+
raise Auth0::MissingOrganizationId, 'Must supply a valid organization_id' if organization_id.to_s.empty?
373+
raise Auth0::InvalidParameter, 'Must supply a valid grant_id' if grant_id.to_s.empty?
374+
375+
path = "#{organizations_path}/#{organization_id}/client-grants/#{grant_id}"
376+
delete(path)
377+
end
378+
333379
private
334380
# Organizations API path
335381
def organizations_path
@@ -351,6 +397,10 @@ def organizations_member_roles_path(org_id, user_id)
351397
def organizations_invitations_path(org_id)
352398
"#{organizations_path}/#{org_id}/invitations"
353399
end
400+
401+
def organizations_client_grants_path(org_id)
402+
"#{organizations_path}/#{org_id}/client-grants"
403+
end
354404
end
355405
end
356406
end

spec/lib/auth0/api/authentication_endpoints_spec.rb

+28
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
grant_type: 'client_credentials',
5757
client_id: client_id,
5858
audience: api_identifier,
59+
organization: nil,
5960
client_secret: client_secret
6061
}.to_json
6162
))
@@ -74,6 +75,33 @@
7475
expect(result.expires_in).not_to be_nil
7576
end
7677

78+
it 'requests a new token using organization' do
79+
expect(RestClient::Request).to receive(:execute).with(hash_including(
80+
method: :post,
81+
url: 'https://samples.auth0.com/oauth/token',
82+
payload: {
83+
grant_type: 'client_credentials',
84+
client_id: client_id,
85+
audience: api_identifier,
86+
organization: 'foo',
87+
client_secret: client_secret
88+
}.to_json
89+
))
90+
.and_return(StubResponse.new({
91+
"access_token" => "test_response",
92+
"expires_in" => 86400,
93+
"scope" => "scope"},
94+
true,
95+
200))
96+
97+
result = client_secret_instance.send :api_token, audience: api_identifier, organization: 'foo'
98+
99+
expect(result).to be_a_kind_of(Auth0::ApiToken)
100+
expect(result.access_token).not_to be_nil
101+
expect(result.scope).not_to be_nil
102+
expect(result.expires_in).not_to be_nil
103+
end
104+
77105
it 'requests a new token using client_assertion' do
78106
expect(RestClient::Request).to receive(:execute) do |arg|
79107
expect(arg).to match(

spec/lib/auth0/api/v2/client_grants_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
expect(@instance).to receive(:get).with(
1515
'/api/v2/client-grants', {
1616
client_id: nil,
17+
allow_any_organization: nil,
1718
audience: nil,
1819
page: nil,
1920
per_page: nil
@@ -27,6 +28,7 @@
2728
expect(@instance).to receive(:get).with(
2829
'/api/v2/client-grants', {
2930
client_id: '1',
31+
allow_any_organization: nil,
3032
audience: audience,
3133
page: nil,
3234
per_page: nil
@@ -38,12 +40,25 @@
3840
expect(@instance).to receive(:get).with(
3941
'/api/v2/client-grants', {
4042
client_id: nil,
43+
allow_any_organization: nil,
4144
audience: nil,
4245
page: 1,
4346
per_page: 2
4447
})
4548
expect { @instance.client_grants(page: 1, per_page: 2) }.not_to raise_error
4649
end
50+
51+
it 'is expected to send get /api/v2/client-grants/ with allow_any_organization' do
52+
expect(@instance).to receive(:get).with(
53+
'/api/v2/client-grants', {
54+
client_id: nil,
55+
allow_any_organization: true,
56+
audience: nil,
57+
page: nil,
58+
per_page: nil
59+
})
60+
expect { @instance.client_grants(allow_any_organization: true) }.not_to raise_error
61+
end
4762
end
4863

4964
context '.create_client_grant' do
@@ -73,4 +88,19 @@
7388
it { expect { @instance.patch_client_grant('', nil) }.to raise_error 'Must specify a client grant id' }
7489
it { expect { @instance.patch_client_grant('some', nil) }.to raise_error 'Must specify a valid body' }
7590
end
91+
92+
context '.get_client_grants_organizations' do
93+
it { expect(@instance).to respond_to(:get_client_grants_organizations) }
94+
it 'is expected to send get to /api/v2/client-grants/organizations' do
95+
expect(@instance).to receive(:get).with('/api/v2/client-grants/1/organizations', {
96+
per_page: nil,
97+
page: nil,
98+
from: nil,
99+
take: nil,
100+
include_totals: nil
101+
})
102+
expect { @instance.get_client_grants_organizations('1') }.not_to raise_error
103+
end
104+
it { expect { @instance.get_client_grants_organizations('') }.to raise_error 'Must specify a client grant id' }
105+
end
76106
end

spec/lib/auth0/api/v2/organizations_spec.rb

+66
Original file line numberDiff line numberDiff line change
@@ -639,4 +639,70 @@
639639
expect { @instance.delete_organizations_member_roles('org_id', 'user_id') }.to raise_error 'Must supply an array of role ids'
640640
end
641641
end
642+
643+
context '.get_organizations_client_grants' do
644+
it 'is expected to respond to a get_organizations_client_grants method' do
645+
expect(@instance).to respond_to(:get_organizations_client_grants)
646+
end
647+
648+
it 'is expected to get /api/v2/organizations/org_id/client-grants' do
649+
expect(@instance).to receive(:get).with(
650+
'/api/v2/organizations/org_id/client-grants', {
651+
per_page: nil,
652+
page: nil,
653+
client_id: nil,
654+
audience: nil,
655+
include_totals: nil
656+
})
657+
expect { @instance.get_organizations_client_grants('org_id') }.not_to raise_error
658+
end
659+
660+
it 'is expected to get /api/v2/organizations/org_id/client-grants with custom parameters' do
661+
expect(@instance).to receive(:get).with(
662+
'/api/v2/organizations/org_id/client-grants', {
663+
per_page: 10,
664+
page: 1,
665+
client_id: 'client_id',
666+
audience: 'api',
667+
include_totals: true
668+
})
669+
expect do
670+
@instance.get_organizations_client_grants(
671+
'org_id',
672+
per_page: 10,
673+
page: 1,
674+
client_id: 'client_id',
675+
audience: 'api',
676+
include_totals: true
677+
)
678+
end.not_to raise_error
679+
end
680+
end
681+
682+
context '.create_organizations_client_grants' do
683+
it 'is expected to respond to a create_organizations_client_grants method' do
684+
expect(@instance).to respond_to(:create_organizations_client_grant)
685+
end
686+
687+
it 'is expected to post /api/v2/organizations/org_id/client-grants' do
688+
expect(@instance).to receive(:post).with(
689+
'/api/v2/organizations/org_id/client-grants', {
690+
grant_id: 'grant_id'
691+
})
692+
expect { @instance.create_organizations_client_grant('org_id', 'grant_id') }.not_to raise_error
693+
end
694+
end
695+
696+
context '.delete_organizations_client_grant' do
697+
it 'is expected to respond to a delete_organizations_client_grant method' do
698+
expect(@instance).to respond_to(:delete_organizations_client_grant)
699+
end
700+
701+
it 'is expected to delete /api/v2/organizations/org_id/client-grants' do
702+
expect(@instance).to receive(:delete).with(
703+
'/api/v2/organizations/org_id/client-grants/grant_id')
704+
expect { @instance.delete_organizations_client_grant('org_id', 'grant_id') }.not_to raise_error
705+
end
706+
end
707+
642708
end

spec/lib/auth0/mixins/initializer_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class MockClass
6464
grant_type: 'client_credentials',
6565
client_id: client_id,
6666
client_secret: client_secret,
67-
audience: api_identifier
67+
audience: api_identifier,
68+
organization: nil
6869
}
6970

7071
expect(RestClient::Request).to receive(:execute) do |arg|

spec/lib/auth0/mixins/token_management_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
grant_type: 'client_credentials',
1212
client_id: client_id,
1313
client_secret: client_secret,
14-
audience: api_identifier
14+
audience: api_identifier,
15+
organization: nil
1516
} }
1617

1718
let(:params) { {

0 commit comments

Comments
 (0)