Skip to content

Commit 12f91b1

Browse files
Merge pull request #2 from gresham-computing/client-secret-security
DWN-27040: Changes when the client secret is given to the UI
2 parents 0ae12c2 + 2008404 commit 12f91b1

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultOAuth2ClientDetailsEntityService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,16 @@ public ClientDetailsEntity saveNewClient(ClientDetailsEntity client) {
150150

151151
ensureNoReservedScopes(client);
152152

153+
String plaintextSecret = client.getClientSecret();
154+
153155
if(!Strings.isNullOrEmpty(client.getClientSecret())) {
154156
client.setClientSecret(this.passwordEncoder.encode(client.getClientSecret()));
155157
}
156158

157159
ClientDetailsEntity c = clientRepository.saveClient(client);
158160

161+
c.setClientSecret(plaintextSecret);
162+
159163
statsService.resetCache();
160164

161165
return c;
@@ -432,9 +436,11 @@ public ClientDetailsEntity updateClient(ClientDetailsEntity oldClient, ClientDet
432436
// make sure a client doesn't get any special system scopes
433437
ensureNoReservedScopes(newClient);
434438

435-
if(!Strings.isNullOrEmpty(newClient.getClientSecret())) {
439+
if (Strings.isNullOrEmpty(newClient.getClientSecret())){
440+
newClient.setClientSecret(oldClient.getClientSecret());
441+
}else{
436442
newClient.setClientSecret(this.passwordEncoder.encode(newClient.getClientSecret()));
437-
}
443+
}
438444

439445
return clientRepository.updateClient(oldClient.getId(), newClient);
440446
}

openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ public PKCEAlgorithm deserialize(JsonElement json, Type typeOfT, JsonDeserializa
229229
public String apiGetAllClients(Model model, Authentication auth) {
230230

231231
Collection<ClientDetailsEntity> clients = clientService.getAllClients();
232+
233+
clients.forEach(client -> client.setClientSecret(null));
234+
232235
model.addAttribute(JsonEntityView.ENTITY, clients);
233236

234237
if (AuthenticationUtilities.isAdmin(auth)) {
@@ -320,6 +323,8 @@ public String apiAddClient(@RequestBody String jsonString, Model m, Authenticati
320323

321324
try {
322325
ClientDetailsEntity newClient = clientService.saveNewClient(client);
326+
327+
//Set the client secret to the plaintext from the request
323328
m.addAttribute(JsonEntityView.ENTITY, newClient);
324329

325330
if (AuthenticationUtilities.isAdmin(auth)) {
@@ -385,6 +390,7 @@ public String apiUpdateClient(@PathVariable("id") Long id, @RequestBody String j
385390
}
386391

387392
ClientDetailsEntity oldClient = clientService.getClientById(id);
393+
String plaintextSecret = client.getClientSecret();
388394

389395
if (oldClient == null) {
390396
logger.error("apiUpdateClient failed; client with id " + id + " could not be found.");
@@ -408,10 +414,10 @@ public String apiUpdateClient(@PathVariable("id") Long id, @RequestBody String j
408414
|| client.getTokenEndpointAuthMethod().equals(AuthMethod.SECRET_POST)
409415
|| client.getTokenEndpointAuthMethod().equals(AuthMethod.SECRET_JWT)) {
410416

411-
// if they've asked for us to generate a client secret (or they left it blank but require one), do so here
412-
if (json.has("generateClientSecret") && json.get("generateClientSecret").getAsBoolean()
413-
|| Strings.isNullOrEmpty(client.getClientSecret())) {
417+
// Once a client has been created, we only update the secret when asked to
418+
if (json.has("generateClientSecret") && json.get("generateClientSecret").getAsBoolean()) {
414419
client = clientService.generateClientSecret(client);
420+
plaintextSecret = client.getClientSecret();
415421
}
416422

417423
} else if (client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY)) {
@@ -438,6 +444,10 @@ public String apiUpdateClient(@PathVariable("id") Long id, @RequestBody String j
438444

439445
try {
440446
ClientDetailsEntity newClient = clientService.updateClient(oldClient, client);
447+
448+
//Set the client secret to the plaintext from the request
449+
newClient.setClientSecret(plaintextSecret);
450+
441451
m.addAttribute(JsonEntityView.ENTITY, newClient);
442452

443453
if (AuthenticationUtilities.isAdmin(auth)) {
@@ -497,6 +507,9 @@ public String apiShowClient(@PathVariable("id") Long id, Model model, Authentica
497507
return JsonErrorView.VIEWNAME;
498508
}
499509

510+
//We don't want the UI to get the secret
511+
client.setClientSecret(null);
512+
500513
model.addAttribute(JsonEntityView.ENTITY, client);
501514

502515
if (AuthenticationUtilities.isAdmin(auth)) {

openid-connect-server/src/main/java/org/mitre/openid/connect/web/DynamicClientRegistrationEndpoint.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ public String registerNewClient(@RequestBody String jsonString, Model m) {
242242
// send it all out to the view
243243

244244
RegisteredClient registered = new RegisteredClient(savedClient, token.getValue(), config.getIssuer() + "register/" + UriUtils.encodePathSegment(savedClient.getClientId(), "UTF-8"));
245+
245246
m.addAttribute("client", registered);
246247
m.addAttribute(HttpCodeView.CODE, HttpStatus.CREATED); // http 201
247248

@@ -377,6 +378,9 @@ public String updateClient(@PathVariable("id") String clientId, @RequestBody Str
377378

378379
RegisteredClient registered = new RegisteredClient(savedClient, token.getValue(), config.getIssuer() + "register/" + UriUtils.encodePathSegment(savedClient.getClientId(), "UTF-8"));
379380

381+
// We don't want the UI to receive the client secret
382+
registered.setClientSecret(null);
383+
380384
// send it all out to the view
381385
m.addAttribute("client", registered);
382386
m.addAttribute(HttpCodeView.CODE, HttpStatus.OK); // http 200

0 commit comments

Comments
 (0)