Web UI to Create Client App and Contract to Access an API in API Manager (Mule 4)
Join the DZone community and get the full member experience.
Join For FreeWhen you create a policy in API Manager, there is need to create an application and contract between the application and the API instance. API Gateway is responsible to enforce policy based on contact created.
Mule doesn't have a single API that can be used to create an API instance and contract. You can achieve using a sequence of call to their platform API. Click here to see the steps provided by Mulesoft knowledge base.
To make this process user friendly, you can prepare a web UI in the Mule application that would help to achieve entire steps in easy two steps.
Create a Mule Application to Build a UI-Based Web Page
- File -> New Mule Project
Project Name : api-mule-client-contract-registratiom
- Create a properties file
xxxxxxxxxx
http
host"0.0.0.0"
port"8081"
cloudhub
rest
api
url"https://anypoint.mulesoft.com/apiplatform/repository/v2/organizations"
- Create Mule flow as below
xxxxxxxxxx
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="95c71995-11da-4d13-a3e1-f327f82c1bd0" >
<http:listener-connection host="0.0.0.0" port="${http.port}" ></http:listener>
</http:listener-config>
<configuration-properties doc:name="Configuration properties" doc:id="58f39ca5-93c4-4eab-8f58-bf1f1d157a6e" file="config.yaml" ></configuration>
<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" doc:id="2f1aa104-cb1c-42e5-acef-2d58a235e7c9" enableCookies="false" >
<http:request-connection protocol="HTTPS" host="anypoint.mulesoft.com" port="443" ></http:request>
</http:request-config>
<flow name="api-mule-client-contract-registratiomFlow" doc:id="c64ea0b8-1aaf-418b-9118-a293b3ed4c1d" >
<http:listener doc:name="Listener" doc:id="2e4d656c-3527-49eb-b7ff-e01c8aa15272" config-ref="HTTP_Listener_config" path="/index.html"></http:listener>
<set-payload value='<html> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <title>Mule API Consumer Registration</title> <body> <br> <div style=" margin: auto; width: 50%; padding: 10px; " > <div > <div class="card"> <h5 class="card-header">Client Registration Form</h5> <div class="card-body"> <form action="/process-request" method="post"> <div class="form-group"> <label for="organizationId"><strong>Organization Name</strong></label> <input type="text" name="organizationId" class="form-control form-control-sm" id="organizationId" required> </div><div class="form-group"> <label for="consumerName"><strong>Client Name</strong></label> <input type="text" name="consumerName" class="form-control form-control-sm" id="consumerName" required> </div> <div class="form-group"> <label for="consumerDescription"><strong>Client Description</strong></label> <input type="text" name="consumerDescription" class="form-control form-control-sm" id="clientId" required > </div> <div class="form-group"> <label for="clientId"><strong>Client ID</strong></label> <input type="text" name="clientId" class="form-control form-control-sm" id="clientId" required> </div> <div class="form-group"> <label for="clientSecret"><strong>Client Secret</strong></label> <input type="text" name="clientSecret" class="form-control form-control-sm" id="clientSecret" required> </div> <div class="row"> <div class="col"> <div class="form-group"> <label for="autoDiscovery"><strong>Autodiscovery ID</strong></label> <input type="text" name="autoDiscoveryID"class="form-control form-control-sm" id="autoDiscovery" required > </div> </div> <div class="col"> <div class="form-group"> <label for="grantType"><strong>Grant Type</strong></label> <select name="grantType" class="mdb-select colorful-select dropdown-primary md-form" multiple> <option value="client_credentials">Client Credentials</option> <option value="password">Password</option> </select> </div> </div> </div> <div class="form-group"> <label for="jwtToken"><strong>Anypoint Platform token</strong></label> <input type="text" name="jwtToken" class="form-control form-control-sm" id="jwtToken" required > </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> </div> </div> </body> </html>' doc:name="return index.html" doc:id="42a6e73e-2065-4840-8f17-3f7f42a2a403" ></set>
</flow>
<flow name="webformFlow1" doc:id="ff95cfa6-fae0-4b69-b54b-f352efa33188" >
<http:listener doc:name="Listener" doc:id="3a4f5510-b2a5-4fca-80a7-376644fb14a2" config-ref="HTTP_Listener_config" path="/process-request"></http:listener>
<set-variable value="#[%dw 2.0 output application/json --- payload]" doc:name="requestPayload" doc:id="0d8f9b81-14cc-452f-8ddc-af854540e448" variableName="requestPayload"></set>
<logger level="INFO" doc:name="request log" doc:id="99d686a5-8755-45e0-9083-9c31bdd8537d" message="#[vars.requestPayload]"></logger>
<ee:transform doc:name="Transform Message" doc:id="a6459c1a-cd59-4031-a0b8-66f539b12f98" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
"name" : vars.requestPayload.consumerName,
"description" : vars.requestPayload.consumerDescription,
"clientSecret" : vars.requestPayload.clientSecret,
"clientId" : vars.requestPayload.clientId,
"grantTypes" : vars.requestPayload.*grantType,
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="8c9cda44-52d4-4658-957e-f3f0e983d6f0" message="transformed payload :#[payload]"></logger>
<http:request method="POST" doc:name="Register auth0 Client to Anypoint" doc:id="a0f77d86-afc1-4fa6-8e5f-434233544184" url="${cloudhub.rest.api.url}/{organization-id}/applications" target="response1" sendCorrelationId="AUTO">
<http:body ><![CDATA[#[output application/json
---
payload]]]></http:body>
<http:headers ><![CDATA[#[output application/java
---
{
"Authorization" : "Bearer " ++ vars.requestPayload.jwtToken
}]]]></http:headers>
<http:uri-params ><![CDATA[#[output application/java
---
{
"organization-id" : vars.requestPayload.organizationId
}]]]></http:uri-params>
</http:request>
<try doc:name="Try" doc:id="22ee4df3-e4f5-4e12-afe1-9301b2b42210" >
<http:request method="POST" doc:name="Register Consumer App to with the API" doc:id="1bc1b388-c396-43a3-9ab8-bbe5de4d1a1f" url="${cloudhub.rest.api.url}/{organization-id}/applications/{consumer-id}/contracts">
<http:body><![CDATA[#[%dw 2.0
output application/json
---
{
"apiVersionId":vars.requestPayload.autoDiscoveryID,
"applicationId": vars.requestPayload.autoDiscoveryID,
"partyId": vars.requestPayload.autoDiscoveryID,
"partyName": vars.response1.name,
"acceptedTerms": true
}]]]></http:body>
<http:headers><![CDATA[#[output application/java
---
{
"Authorization" : "Bearer " ++ vars.requestPayload.jwtToken
}]]]></http:headers>
<http:uri-params><![CDATA[#[output application/java
---
{
"consumer-id" : vars.response1.id,
"organization-id" : vars.requestPayload.organizationId
}]]]></http:uri-params>
</http:request>
<error-handler>
<on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="254ea273-fbb5-425f-bc11-ca53d415b14f" >
<http:request method="DELETE" doc:name="Delete the Created Consumer " doc:id="a736dd6d-1ff1-4eae-b094-4ca6e973d49f" url="${cloudhub.rest.api.url}/organizations/{organization-id}/applications/{consumer-id}">
<http:uri-params><![CDATA[#[output application/java
---
{
"consumer-id" : vars.response1.id,
"organization-id" : vars.requestPayload.organizationId
}]]]></http:uri-params>
</http:request>
<set-variable value="#[true]" doc:name="Set Variable" doc:id="591e8b2e-d55c-4c74-a2dd-640a618253f6" variableName="isConsumerDeleted" ></set>
</on-error-propagate>
</error-handler>
</try>
<error-handler >
<on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="b0364bb7-ed73-4048-83ea-f3b210a0e6fb" >
<set-payload value="#[payload]" doc:name="Set Payload" doc:id="e5c64a52-adc9-4fcd-827f-07b1dc01ce85" ></set>
</on-error-propagate>
</error-handler>
</flow>
<flow name="api-mule-token-generation-form-flow" doc:id="613859f5-7521-4ffd-b876-8b7106d4fba2" >
<http:listener doc:name="Listener" doc:id="fe0fd7d1-2c34-4cbb-b0be-29a2245f4e49" config-ref="HTTP_Listener_config" path="/token.html"></http:listener>
<set-payload value='<html> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <title>Mule Token Genration</title> <body> <br> <div style=" margin: auto; width: 50%; padding: 10px; " > <div > <div class="card"> <h5 class="card-header">Anypoint platform Toke Genration</h5> <div class="card-body"> <form action="/token" method="post"> <div class="form-group"> <label for="userName"><strong>User Name</strong></label> <input type="text" name="userName" class="form-control form-control-sm" id="userName" required> </div><div class="form-group"> <label for="password"><strong>Password </strong></label> <input type="password" name="password" class="form-control form-control-sm" id="password" required> </div><button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> </div> </div> </body> </html>' doc:name="token generation form" doc:id="db21c255-6e0a-480b-b1ee-64b722079e49" ></set>
</flow>
<flow name="api-mule-token-generate-registratiom-flow" doc:id="24d1f1d8-e104-4746-804e-06cddc373dc6" >
<http:listener doc:name="Listener" doc:id="f951eadf-fdad-4938-bc0d-5ab47bc7444e" config-ref="HTTP_Listener_config" path="/token"></http:listener>
<set-variable value="#[%dw 2.0 output application/json --- payload]" doc:name="Token payload" doc:id="aa572d2f-6337-4499-b800-880a07bcb388" variableName="tokenPayload"></set>
<logger level="INFO" doc:name="Logger" doc:id="d0e79acb-24de-47b4-ad19-72b9769e64f7" message="#[vars.tokenPayload]]"></logger>
<http:request method="POST" doc:name="Request" doc:id="0d311c36-6c94-4d5b-bd76-da9b9d513aab" outputMimeType="application/json" config-ref="HTTP_Request_configuration" path="/accounts/login">
<http:body ><![CDATA[#[%dw 2.0
output application/json
---
{
"username": vars.tokenPayload.userName,
"password": vars.tokenPayload.password
}]]]></http:body>
<http:headers ><![CDATA[#[output application/java
---
{
"Accept" : "*/*",
"Accept-Encoding" : "gzip, deflate, br",
"Content-Type" : "application/json",
"Connection" : "keep-alive"
}]]]></http:headers>
</http:request>
</flow>
</mule>
-
Run Application
Create Anypoint Platform Token :
The token is a must to communicate to Anypoint platform API. Open URL in the browser: http://localhost:8081/token.html
Provide your Anypoint platfrom user name and password and click on submit.
You would see the below response. This token will be needed in the next steps.
xxxxxxxxxx
{
"access_token": "6f55ae90-34f7-44fe-ba77-82cacb8c5a3f",
"token_type": "bearer",
"redirectUrl": "/home/"
}
Run the application and call: GET http://localhost:8081/index.html
Provide all required values, as per the above screenshot, and click on the submit button. The response should look like the following JSON payload. It means that the client and contract have created in API Manager.
xxxxxxxxxx
{
"audit": {
"created": {
"date": "2020-03-31T11:10:24.145Z"
},
"updated": {}
},
"masterOrganizationId": "4b52dfb0-db02-49e3-ac33-ca67fd4a51a1",
"organizationId": "4b52dfb0-db02-49e3-ac33-ca67fd4a51a1",
"id": 881807,
"status": "APPROVED",
"approvedDate": "2020-03-31T11:10:24.228Z",
"rejectedDate": null,
"revokedDate": null,
"applicationId": 396196,
"application": {
"audit": {
"created": {
"date": "2020-03-31T11:10:23.749Z"
},
"updated": {}
},
"masterOrganizationId": "4b52dfb0-db02-49e3-ac33-ca67fd4a51a1",
"id": 396196,
"name": "auth-demo-order-app2",
"description": "client registratin for order application1",
"coreServicesId": "3ytxMlvSIuqZfKZghCoeKXZ6x3qqi7WB",
"url": null,
"grantTypes": [
"client_credentials"
],
"redirectUri": [],
"clientId": "3ytxMlvSIuqZfKZghCoeKXZ6x3qqi7WB",
"clientSecret": "1haFgLq-_O6G3JBzZAL0CgK4KUJImmaEEjtbfoPWtutkfygCK42xZVM7AKa_OIhI",
"owner": "Mukesh Thakur",
"email": "mukesh.aec@gmail.com",
"owners": [
{
"id": "ff13fd4d-4498-4cc6-a71a-a4b18264553c",
"createdAt": "2020-03-16T04:18:17.845Z",
"updatedAt": "2020-03-31T10:26:10.023Z",
"organizationId": "4b52dfb0-db02-49e3-ac33-ca67fd4a51a1",
"firstName": "Mukesh",
"lastName": "Thakur",
"email": "mukesh.aec@gmail.com",
"phoneNumber": "09602109706",
"idprovider_id": "mulesoft",
"username": "mukesh-aec6",
"enabled": true,
"deleted": false,
"lastLogin": "2020-03-31T10:26:00.000Z",
"roles": [
"cc43e20e-c04b-4c86-a29e-49ac7e7f8b58"
],
"type": "host"
}
]
},
"tierId": null,
"tier": null,
"requestedTierId": null,
"requestedTier": null,
"terms": null,
"apiVersionId": 16080933,
"apiVersion": {
"audit": {
"created": {
"date": "2020-03-30T12:33:51.892Z"
},
"updated": {
"date": "2020-03-31T01:43:55.613Z"
}
},
"masterOrganizationId": "4b52dfb0-db02-49e3-ac33-ca67fd4a51a1",
"organizationId": "4b52dfb0-db02-49e3-ac33-ca67fd4a51a1",
"id": 16080933,
"apiId": 211297588,
"portalId": null,
"name": "v1:16080933",
"instanceLabel": "policy-order-API",
"groupId": "4b52dfb0-db02-49e3-ac33-ca67fd4a51a1",
"assetId": "ordersapi",
"assetVersion": "1.0.1",
"productVersion": "v1",
"description": null,
"tags": [],
"order": 1,
"providerId": null,
"rootFileId": null,
"deprecated": false,
"lastActiveDate": "2020-03-31T09:56:30.469Z",
"endpointUri": null,
"environmentId": "426522f5-3b79-4e69-aa9d-3f01ac91182e",
"isPublic": false,
"stage": "release",
"terms": null,
"termsDate": null,
"endpoint": {
"audit": {
"created": {},
"updated": {}
},
"id": 788261,
"type": "raml",
"uri": null,
"apiGatewayVersion": "4.2.2",
"proxyUri": null,
"proxyRegistrationUri": null,
"lastActiveDate": null,
"isCloudHub": null,
"deploymentType": "CH",
"policiesVersion": null,
"referencesUserDomain": null,
"muleVersion4OrAbove": true,
"responseTimeout": null,
"wsdlConfig": null
},
"deployment": null,
"lastActiveDelta": 4433,
"permissions": {
"update": true
}
},
"groupInstanceId": null,
"groupInstance": null,
"partyId": "16080933",
"partyName": "auth-demo-order-app2",
"condition": "APPLIED"
}
Verifying Clients and Contract in API Manager
Click here to download the source code.
Opinions expressed by DZone contributors are their own.
Comments