Skip to content

Commit ab7a193

Browse files
[SDK-3869] Support client credentials in management client (#437)
* feat: support for create credentials endpoint * feat: add support for get client credentials * feat: support getting single client credential in api v2 * feat: add support for deleting a client credential * chore: fill out doc comments for credentials
1 parent eef91f0 commit ab7a193

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lib/auth0/api/v2/clients.rb

+42
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,54 @@ def patch_client(client_id, options)
7373
patch(path, options)
7474
end
7575

76+
# Creates credentials for a client
77+
# @param client_id [string] The Id of the client to update
78+
# @param options [hash] The payload to send to the endpoint
79+
def create_client_credentials(client_id, options)
80+
raise Auth0::MissingClientId, 'Must specify a client id' if client_id.to_s.empty?
81+
raise Auth0::MissingParameter, 'Must specify a valid body' if options.to_s.empty?
82+
post(client_credentials_path(client_id), options)
83+
end
84+
85+
# Gets the credentials for a client
86+
# @param client_id [string] The Id of the client
87+
# @return [hash] The client credentials
88+
def client_credentials(client_id)
89+
raise Auth0::MissingClientId, 'Must specify a client id' if client_id.to_s.empty?
90+
get(client_credentials_path(client_id))
91+
end
92+
alias get_client_credentials client_credentials
93+
94+
# Gets a client credential by ID
95+
# @param client_id [string] The Id of the client
96+
# @param credential_id [string] The Id of the credential to retrieve
97+
# @return [hash] The credential
98+
def client_credential(client_id, credential_id)
99+
raise Auth0::MissingClientId, 'Must specify a client id' if client_id.to_s.empty?
100+
raise Auth0::MissingParameter, 'Must specify a credential id' if credential_id.to_s.empty?
101+
get("#{client_credentials_path(client_id)}/#{credential_id}")
102+
end
103+
alias get_client_credential client_credential
104+
105+
# Deletes a credential from the specified client
106+
# @param client_id [string] The Id of the client
107+
# @param credential_id [string] The Id of the credential to delete
108+
def delete_client_credential(client_id, credential_id)
109+
raise Auth0::MissingClientId, 'Must specify a client id' if client_id.to_s.empty?
110+
raise Auth0::MissingParameter, 'Must specify a credential id' if credential_id.to_s.empty?
111+
delete("#{client_credentials_path(client_id)}/#{credential_id}")
112+
end
113+
76114
private
77115

78116
# Clients API path
79117
def clients_path
80118
@clients_path ||= '/api/v2/clients'
81119
end
120+
121+
def client_credentials_path(client_id)
122+
"#{clients_path}/#{client_id}/credentials"
123+
end
82124
end
83125
end
84126
end

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

+51
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,55 @@
101101
it { expect { @instance.patch_client('', nil) }.to raise_error 'Must specify a client id' }
102102
it { expect { @instance.patch_client('some', nil) }.to raise_error 'Must specify a valid body' }
103103
end
104+
105+
context '.create_client_credentials' do
106+
it { expect(@instance).to respond_to(:create_client_credentials) }
107+
108+
it 'is expected to send post to /api/v2/clients/1/credentials' do
109+
payload = { credential_type: 'public_key', name: 'my credentials', pem: '' }
110+
111+
expect(@instance).to receive(:post).with('/api/v2/clients/1/credentials', payload)
112+
expect { @instance.create_client_credentials('1', payload) }.not_to raise_error
113+
end
114+
115+
it { expect { @instance.create_client_credentials('', nil) }.to raise_error 'Must specify a client id' }
116+
it { expect { @instance.create_client_credentials('1', nil) }.to raise_error 'Must specify a valid body' }
117+
end
118+
119+
context '.client_credentials' do
120+
it { expect(@instance).to respond_to(:client_credentials) }
121+
it { expect(@instance).to respond_to(:get_client_credentials) }
122+
123+
it 'is expected to send get to /api/v2/clients/1/credentials' do
124+
expect(@instance).to receive(:get).with('/api/v2/clients/1/credentials')
125+
expect { @instance.client_credentials('1') }.not_to raise_error
126+
end
127+
128+
it { expect { @instance.client_credentials('') }.to raise_error 'Must specify a client id' }
129+
end
130+
131+
context '.client_credential' do
132+
it { expect(@instance).to respond_to(:client_credential) }
133+
it { expect(@instance).to respond_to(:get_client_credential) }
134+
135+
it 'is expected to send get to /api/v2/clients/1/credentials/2' do
136+
expect(@instance).to receive(:get).with('/api/v2/clients/1/credentials/2')
137+
expect { @instance.client_credential('1', '2') }.not_to raise_error
138+
end
139+
140+
it { expect { @instance.client_credential('', '') }.to raise_error 'Must specify a client id' }
141+
it { expect { @instance.client_credential('1', '') }.to raise_error 'Must specify a credential id' }
142+
end
143+
144+
context '.delete_client_credential', focus: true do
145+
it { expect(@instance).to respond_to(:delete_client_credential) }
146+
147+
it 'is expected to delete /api/v2/clients/1/credentials/2' do
148+
expect(@instance).to receive(:delete).with('/api/v2/clients/1/credentials/2')
149+
expect { @instance.delete_client_credential('1', '2') }.not_to raise_error
150+
end
151+
152+
it { expect { @instance.delete_client_credential('', '') }.to raise_error 'Must specify a client id' }
153+
it { expect { @instance.delete_client_credential('1', '') }.to raise_error 'Must specify a credential id' }
154+
end
104155
end

0 commit comments

Comments
 (0)