-
Notifications
You must be signed in to change notification settings - Fork 29
Add client scope mapper crud #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
385ec77
config: added rspec-its
5f1a955
config: required rspec-its
1c60cf9
test: client_authz_scope_protocol_mapper_client
3ff1e18
feat: scope mapper crud client
f918da1
revert: remove rspec-its
7e2e72b
chore: remove unnecessary rspec-its requirement
7ecf94b
refactor: resolve ambiguous naming of client scope classes / accessors
24bc230
docs: add documentation for new methods
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
lib/keycloak-admin/client/client_scope_protocol_mapper_client.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| module KeycloakAdmin | ||
| class ClientScopeProtocolMapperClient < Client | ||
| def initialize(configuration, realm_client, client_scope_id) | ||
| super(configuration) | ||
|
|
||
| raise ArgumentError.new("realm must be defined") unless realm_client.name_defined? | ||
|
|
||
| @realm_client = realm_client | ||
| @client_scope_id = client_scope_id | ||
| end | ||
|
|
||
| def list | ||
| response = execute_http do | ||
| RestClient::Resource.new(protocol_mappers_url, @configuration.rest_client_options).get(headers) | ||
| end | ||
|
|
||
| JSON.parse(response).map { |h| ProtocolMapperRepresentation.from_hash(h) } | ||
| end | ||
|
|
||
| def get(mapper_id) | ||
| response = execute_http do | ||
| RestClient::Resource.new(protocol_mappers_url(mapper_id), @configuration.rest_client_options).get(headers) | ||
| end | ||
|
|
||
| ProtocolMapperRepresentation.from_hash(JSON.parse(response)) | ||
| end | ||
|
|
||
| def create!(mapper_representation) | ||
| execute_http do | ||
| RestClient::Resource.new(protocol_mappers_url, @configuration.rest_client_options).post( | ||
| create_payload(mapper_representation), headers | ||
| ) | ||
| end | ||
|
|
||
| true | ||
| end | ||
|
|
||
| def update(mapper_representation) | ||
| execute_http do | ||
| RestClient::Resource.new(protocol_mappers_url(mapper_representation.id), @configuration.rest_client_options).put( | ||
| create_payload(mapper_representation), headers | ||
| ) | ||
| end | ||
|
|
||
| true | ||
| end | ||
|
|
||
| def delete(mapper_id) | ||
| execute_http do | ||
| RestClient::Resource.new(protocol_mappers_url(mapper_id), @configuration.rest_client_options).delete(headers) | ||
| end | ||
|
|
||
| true | ||
| end | ||
|
|
||
| def protocol_mappers_url(mapper_id = nil) | ||
| base = "#{@realm_client.realm_admin_url}/client-scopes/#{@client_scope_id}/protocol-mappers/models" | ||
|
|
||
| mapper_id ? "#{base}/#{mapper_id}" : base | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
spec/client/client_scope_protocol_mapper_client_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| RSpec.describe KeycloakAdmin::ClientScopeProtocolMapperClient do | ||
| let(:realm_name) { "valid-realm" } | ||
| let(:client_scope_id) { "valid-scope-id" } | ||
| let(:mapper_id) { "valid-mapper-id" } | ||
|
|
||
| let(:mapper_json) do | ||
| <<~JSON | ||
| {"id":"valid-mapper-id","name":"my-claim","protocol":"openid-connect","protocolMapper":"oidc-hardcoded-claim-mapper","config":{"claim.name":"my_claim","claim.value":"bar","access.token.claim":"true"}} | ||
| JSON | ||
| end | ||
|
|
||
| let(:audience_mapper_json) do | ||
| <<~JSON | ||
| {"protocol":"openid-connect","protocolMapper":"oidc-audience-mapper","name":"audience-config-rvw-123","config":{"included.client.audience":"","included.custom.audience":"https://api.example.com","id.token.claim":"false","access.token.claim":"true","lightweight.claim":"false","introspection.token.claim":"true"}} | ||
| JSON | ||
| end | ||
|
|
||
| describe "#initialize" do | ||
| context "when realm_name is defined" do | ||
| it "does not raise any error" do | ||
| expect { KeycloakAdmin.realm(realm_name).client_scope_protocol_mappers(client_scope_id) }.to_not raise_error | ||
| end | ||
| end | ||
|
|
||
| context "when realm_name is not defined" do | ||
| it "raises an argument error" do | ||
| expect { KeycloakAdmin.realm(nil).client_scope_protocol_mappers(client_scope_id) }.to raise_error(ArgumentError) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#list" do | ||
| before(:each) do | ||
| @client = KeycloakAdmin.realm(realm_name).client_scope_protocol_mappers(client_scope_id) | ||
| stub_token_client | ||
| allow_any_instance_of(RestClient::Resource).to receive(:get).and_return stub_response | ||
| end | ||
|
|
||
| context "with a hardcoded claim mapper" do | ||
| let(:stub_response) { "[#{mapper_json}]" } | ||
|
|
||
| it "returns one mapper" do | ||
| expect(@client.list.size).to eq 1 | ||
| end | ||
|
|
||
| it "returns the correct mapper attributes" do | ||
| expect(@client.list.first).to have_attributes(id: "valid-mapper-id", name: "my-claim", protocol: "openid-connect", protocolMapper: "oidc-hardcoded-claim-mapper") | ||
| end | ||
| end | ||
|
|
||
| context "with an audience mapper" do | ||
| let(:stub_response) { "[#{audience_mapper_json}]" } | ||
|
|
||
| it "returns one mapper" do | ||
| expect(@client.list.size).to eq 1 | ||
| end | ||
|
|
||
| it "returns the correct mapper attributes" do | ||
| expect(@client.list.first).to have_attributes(name: "audience-config-rvw-123", protocol: "openid-connect", protocolMapper: "oidc-audience-mapper") | ||
| end | ||
| end | ||
|
|
||
| context "with multiple mappers" do | ||
| let(:stub_response) { "[#{mapper_json},#{audience_mapper_json}]" } | ||
|
|
||
| it "returns two mappers" do | ||
| expect(@client.list.size).to eq 2 | ||
| end | ||
|
|
||
| it "includes both mapper names" do | ||
| expect(@client.list.map(&:name)).to include("my-claim", "audience-config-rvw-123") | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#get" do | ||
| before(:each) do | ||
| @client = KeycloakAdmin.realm(realm_name).client_scope_protocol_mappers(client_scope_id) | ||
| stub_token_client | ||
| allow_any_instance_of(RestClient::Resource).to receive(:get).and_return stub_response | ||
| end | ||
|
|
||
| context "with a hardcoded claim mapper" do | ||
| let(:stub_response) { mapper_json } | ||
|
|
||
| it "returns the correct id" do | ||
| expect(@client.get(mapper_id).id).to eq "valid-mapper-id" | ||
| end | ||
|
|
||
| it "returns the correct name" do | ||
| expect(@client.get(mapper_id).name).to eq "my-claim" | ||
| end | ||
|
|
||
| it "returns the correct protocol" do | ||
| expect(@client.get(mapper_id).protocol).to eq "openid-connect" | ||
| end | ||
|
|
||
| it "returns the correct protocolMapper" do | ||
| expect(@client.get(mapper_id).protocolMapper).to eq "oidc-hardcoded-claim-mapper" | ||
| end | ||
| end | ||
|
|
||
| context "with an audience mapper" do | ||
| let(:stub_response) { audience_mapper_json } | ||
|
|
||
| it "returns the correct name" do | ||
| expect(@client.get(mapper_id).name).to eq "audience-config-rvw-123" | ||
| end | ||
|
|
||
| it "returns the correct protocol" do | ||
| expect(@client.get(mapper_id).protocol).to eq "openid-connect" | ||
| end | ||
|
|
||
| it "returns the correct protocolMapper" do | ||
| expect(@client.get(mapper_id).protocolMapper).to eq "oidc-audience-mapper" | ||
| end | ||
|
|
||
| it "returns the correct config" do | ||
| expect(@client.get(mapper_id).config).to include( | ||
| "included.custom.audience" => "https://api.example.com", | ||
| "access.token.claim" => "true", | ||
| "introspection.token.claim" => "true", | ||
| "id.token.claim" => "false" | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#create!" do | ||
| before(:each) do | ||
| @client = KeycloakAdmin.realm(realm_name).client_scope_protocol_mappers(client_scope_id) | ||
| stub_token_client | ||
| allow_any_instance_of(RestClient::Resource).to receive(:post).and_return stub_response | ||
| end | ||
|
|
||
| context "with a hardcoded claim mapper" do | ||
| let(:stub_response) { mapper_json } | ||
| let(:mapper_representation) do | ||
| mapper = KeycloakAdmin::ProtocolMapperRepresentation.new | ||
| mapper.name = "my-claim" | ||
| mapper.protocol = "openid-connect" | ||
| mapper.protocolMapper = "oidc-hardcoded-claim-mapper" | ||
| mapper.config = { "claim.name" => "my_claim", "claim.value" => "bar", "access.token.claim" => "true" } | ||
| mapper | ||
| end | ||
|
|
||
| it "creates successfully" do | ||
| expect(@client.create!(mapper_representation)).to be true | ||
| end | ||
| end | ||
|
|
||
| context "with an audience mapper" do | ||
| let(:stub_response) { audience_mapper_json } | ||
| let(:mapper_representation) do | ||
| mapper = KeycloakAdmin::ProtocolMapperRepresentation.new | ||
| mapper.name = "audience-config-rvw-123" | ||
| mapper.protocol = "openid-connect" | ||
| mapper.protocolMapper = "oidc-audience-mapper" | ||
| mapper.config = { | ||
| "included.client.audience" => "", | ||
| "included.custom.audience" => "https://api.example.com", | ||
| "id.token.claim" => "false", | ||
| "access.token.claim" => "true", | ||
| "lightweight.claim" => "false", | ||
| "introspection.token.claim" => "true" | ||
| } | ||
| mapper | ||
| end | ||
|
|
||
| it "creates successfully" do | ||
| expect(@client.create!(mapper_representation)).to be true | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#update" do | ||
| before(:each) do | ||
| @client = KeycloakAdmin.realm(realm_name).client_scope_protocol_mappers(client_scope_id) | ||
| stub_token_client | ||
| allow_any_instance_of(RestClient::Resource).to receive(:put).and_return "" | ||
| end | ||
|
|
||
| context "with a hardcoded claim mapper" do | ||
| let(:mapper_representation) { KeycloakAdmin::ProtocolMapperRepresentation.from_hash(JSON.parse(mapper_json)) } | ||
|
|
||
| it "calls put on the mapper url" do | ||
| expect_any_instance_of(RestClient::Resource).to receive(:put).with(anything, anything) | ||
| @client.update(mapper_representation) | ||
| end | ||
| end | ||
|
|
||
| context "with an audience mapper" do | ||
| let(:mapper_representation) { KeycloakAdmin::ProtocolMapperRepresentation.from_hash(JSON.parse(audience_mapper_json)) } | ||
|
|
||
| it "calls put on the mapper url" do | ||
| expect_any_instance_of(RestClient::Resource).to receive(:put).with(anything, anything) | ||
| @client.update(mapper_representation) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#delete" do | ||
| before(:each) do | ||
| @client = KeycloakAdmin.realm(realm_name).client_scope_protocol_mappers(client_scope_id) | ||
| stub_token_client | ||
| allow_any_instance_of(RestClient::Resource).to receive(:delete).and_return "" | ||
| end | ||
|
|
||
| it "returns true" do | ||
| expect(@client.delete(mapper_id)).to eq true | ||
| end | ||
| end | ||
|
|
||
| describe "#protocol_mappers_url" do | ||
| let(:client) { KeycloakAdmin.realm(realm_name).client_scope_protocol_mappers(client_scope_id) } | ||
| let(:base_url) { "http://auth.service.io/auth/admin/realms/valid-realm/client-scopes/valid-scope-id/protocol-mappers/models" } | ||
|
|
||
| context "without a mapper_id" do | ||
| it "returns the base url" do | ||
| expect(client.protocol_mappers_url).to eq base_url | ||
| end | ||
| end | ||
|
|
||
| context "with a mapper_id" do | ||
| it "returns the url with mapper_id appended" do | ||
| expect(client.protocol_mappers_url(mapper_id)).to eq "#{base_url}/valid-mapper-id" | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.