Last active
February 17, 2020 14:23
-
-
Save justindav1s/32deda286c9da6aaa04508107e997925 to your computer and use it in GitHub Desktop.
This file contains 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
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import org.apache.commons.logging.Log; | |
import org.apache.commons.logging.LogFactory; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.keycloak.representations.idm.ClientRepresentation; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.http.HttpEntity; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import org.springframework.web.client.RestTemplate; | |
import java.util.ArrayList; | |
import java.util.Base64; | |
import java.util.List; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
public class ClientCreationTests { | |
String realm = "demo"; | |
String baseUrl = "http://127.0.0.1:8080"; | |
String reg_clientId = "tpp-registration-client"; | |
String reg_clientSecret = "ee01f93a-5dcc-451d-bba3-25ef2a996245"; | |
String new_client_name = "Liffey Inc"; | |
String new_client_id = "liffey-inc"; | |
String new_client_redirecturl= "http://liffey.com/tpp"; | |
private Log log = LogFactory.getLog(ClientCreationTests.class); | |
@Test | |
public void createClient() { | |
String accessToken = getToken(); | |
log.info("Access Token : "+accessToken); | |
ClientRepresentation client = new ClientRepresentation(); | |
List redirect_uris = new ArrayList(); | |
redirect_uris.add(new_client_redirecturl); | |
client.setRedirectUris(redirect_uris); | |
client.setName(new_client_name); | |
client.setClientId(new_client_id); | |
client.setDirectAccessGrantsEnabled(true); | |
client.setServiceAccountsEnabled(true); | |
client.setStandardFlowEnabled(false); | |
String uri = baseUrl+"/auth/realms/"+realm+"/clients-registrations/default"; | |
log.info("Client Reg URL : "+uri); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.add("Authorization", "Bearer "+accessToken); | |
headers.add("Content-Type", "application/json"); | |
ObjectMapper om = new ObjectMapper(); | |
String clientreqjson = null; | |
try { | |
clientreqjson = om.writeValueAsString(client); | |
log.info("Client Creation request json : "+clientreqjson); | |
} catch (JsonProcessingException e) { | |
e.printStackTrace(); | |
} | |
HttpEntity<String> request = new HttpEntity<>(clientreqjson, headers); | |
RestTemplate restTemplate = new RestTemplate(); | |
ResponseEntity<String> exchange = | |
restTemplate.exchange( | |
uri, | |
HttpMethod.POST, | |
request, | |
String.class); | |
String response = exchange.getBody(); | |
log.info("Response : "+response); | |
String secret = null; | |
try { | |
JSONObject obj = new JSONObject(response); | |
secret = obj.getString("secret"); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
log.info("Client Secret : "+secret); | |
} | |
public String getToken () { | |
RestTemplate restTemplate = new RestTemplate(); | |
log.info("new token"); | |
String uri = baseUrl+"/auth/realms/"+realm+"/protocol/openid-connect/token"; | |
log.info("Token URL : "+uri); | |
String post_body = "grant_type=client_credentials&client_id="+reg_clientId+"&client_secret="+reg_clientSecret; | |
log.info("Post body : "+post_body); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.add("Content-Type", "application/x-www-form-urlencoded"); | |
HttpEntity<String> request = new HttpEntity<>(post_body, headers); | |
ResponseEntity<String> exchange = | |
restTemplate.exchange( | |
uri, | |
HttpMethod.POST, | |
request, | |
String.class); | |
String response = exchange.getBody(); | |
log.info("Token Response : "+response); | |
String accessToken = null; | |
try { | |
JSONObject obj = new JSONObject(response); | |
accessToken = obj.getString("access_token"); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
log.info("Access Token : "+accessToken); | |
return accessToken; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires the following keycloak maven dependencies :