Skip to content

Commit a59fe06

Browse files
committed
generate random fake password for private key clients and shared secret crypto clients to avoid accidentally using client secret authentication and bypassing authentication, closes mitreid-connect#715
1 parent 9dfac35 commit a59fe06

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

openid-connect-common/src/main/java/org/mitre/oauth2/service/impl/DefaultClientUserDetailsService.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
******************************************************************************/
1717
package org.mitre.oauth2.service.impl;
1818

19+
import java.math.BigInteger;
20+
import java.security.SecureRandom;
1921
import java.util.ArrayList;
2022
import java.util.Collection;
2123

24+
import org.mitre.oauth2.model.ClientDetailsEntity;
25+
import org.mitre.oauth2.model.ClientDetailsEntity.AuthMethod;
26+
import org.mitre.oauth2.service.ClientDetailsEntityService;
2227
import org.springframework.beans.factory.annotation.Autowired;
2328
import org.springframework.security.core.GrantedAuthority;
2429
import org.springframework.security.core.authority.SimpleGrantedAuthority;
2530
import org.springframework.security.core.userdetails.User;
2631
import org.springframework.security.core.userdetails.UserDetails;
2732
import org.springframework.security.core.userdetails.UserDetailsService;
2833
import org.springframework.security.core.userdetails.UsernameNotFoundException;
29-
import org.springframework.security.oauth2.provider.ClientDetails;
30-
import org.springframework.security.oauth2.provider.ClientDetailsService;
3134
import org.springframework.stereotype.Service;
3235

3336
import com.google.common.base.Strings;
@@ -42,16 +45,27 @@
4245
public class DefaultClientUserDetailsService implements UserDetailsService {
4346

4447
@Autowired
45-
private ClientDetailsService clientDetailsService;
48+
private ClientDetailsEntityService clientDetailsService;
4649

4750
@Override
4851
public UserDetails loadUserByUsername(String clientId) throws UsernameNotFoundException {
4952

50-
ClientDetails client = clientDetailsService.loadClientByClientId(clientId);
53+
ClientDetailsEntity client = clientDetailsService.loadClientByClientId(clientId);
5154

5255
if (client != null) {
5356

5457
String password = Strings.nullToEmpty(client.getClientSecret());
58+
59+
if (client.getTokenEndpointAuthMethod() != null &&
60+
(client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY) ||
61+
client.getTokenEndpointAuthMethod().equals(AuthMethod.SECRET_JWT))) {
62+
63+
// Issue a random password each time to prevent password auth from being used (or skipped)
64+
// for private key or shared key clients, see #715
65+
66+
password = new BigInteger(512, new SecureRandom()).toString(16);
67+
}
68+
5569
boolean enabled = true;
5670
boolean accountNonExpired = true;
5771
boolean credentialsNonExpired = true;
@@ -72,11 +86,11 @@ public UserDetails loadUserByUsername(String clientId) throws UsernameNotFoundE
7286

7387
}
7488

75-
public ClientDetailsService getClientDetailsService() {
89+
public ClientDetailsEntityService getClientDetailsService() {
7690
return clientDetailsService;
7791
}
7892

79-
public void setClientDetailsService(ClientDetailsService clientDetailsService) {
93+
public void setClientDetailsService(ClientDetailsEntityService clientDetailsService) {
8094
this.clientDetailsService = clientDetailsService;
8195
}
8296

0 commit comments

Comments
 (0)