Skip to content

Commit c38d3ed

Browse files
author
Steve Riesenberg
committed
Use spec-recommended user code format
1 parent 92b8b85 commit c38d3ed

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Diff for: oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2DeviceAuthorizationRequestAuthenticationProvider.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ public OAuth2DeviceCode generate(OAuth2TokenContext context) {
224224

225225
private static final class UserCodeStringKeyGenerator implements StringKeyGenerator {
226226

227+
// @formatter:off
228+
private static final char[] VALID_CHARS = {
229+
'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
230+
'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Z'
231+
};
232+
// @formatter:on
233+
227234
private final BytesKeyGenerator keyGenerator;
228235

229236
public UserCodeStringKeyGenerator() {
@@ -235,9 +242,10 @@ public String generateKey() {
235242
byte[] bytes = this.keyGenerator.generateKey();
236243
StringBuilder sb = new StringBuilder();
237244
for (byte b : bytes) {
238-
int offset = Math.abs(b % 26);
239-
sb.append((char) ('A' + offset));
245+
int offset = Math.abs(b % 20);
246+
sb.append(VALID_CHARS[offset]);
240247
}
248+
sb.insert(4, '-');
241249
return sb.toString();
242250
}
243251

Diff for: oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/authentication/OAuth2EndpointUtils.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.Collections;
1919
import java.util.HashMap;
20-
import java.util.Locale;
2120
import java.util.Map;
2221

2322
import jakarta.servlet.http.HttpServletRequest;
@@ -84,8 +83,11 @@ static void throwError(String errorCode, String parameterName, String errorUri)
8483
}
8584

8685
static String normalizeUserCode(String userCode) {
87-
Assert.notNull(userCode, "userCode cannot be null");
88-
return userCode.toUpperCase(Locale.ROOT).replaceAll("[^A-Z]+", "");
86+
Assert.hasText(userCode, "userCode cannot be empty");
87+
StringBuilder sb = new StringBuilder(userCode.toUpperCase().replaceAll("[^A-Z\\d]+", ""));
88+
Assert.isTrue(sb.length() == 8, "userCode must be exactly 8 alpha/numeric characters");
89+
sb.insert(4, '-');
90+
return sb.toString();
8991
}
9092

9193
}

0 commit comments

Comments
 (0)