Skip to content

Commit 2885b0f

Browse files
committed
Add valueOf
This commit adds a static factory for returning a constant ClientAuthenticationMethod or creating a new one when there is no match. Issue gh-16825
1 parent 2a24bb0 commit 2885b0f

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

config/src/main/java/org/springframework/security/config/oauth2/client/ClientRegistrationsBeanDefinitionParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -125,7 +125,7 @@ private List<ClientRegistration> getClientRegistrations(Element element, ParserC
125125
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_SECRET))
126126
.ifPresent(builder::clientSecret);
127127
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_CLIENT_AUTHENTICATION_METHOD))
128-
.map(ClientAuthenticationMethod::new)
128+
.map(ClientAuthenticationMethod::valueOf)
129129
.ifPresent(builder::clientAuthenticationMethod);
130130
getOptionalIfNotEmpty(parserContext, clientRegistrationElt.getAttribute(ATT_AUTHORIZATION_GRANT_TYPE))
131131
.map(AuthorizationGrantType::new)

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/ClientAuthenticationMethod.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.Serializable;
2020

21+
import org.springframework.lang.NonNull;
2122
import org.springframework.security.core.SpringSecurityCoreVersion;
2223
import org.springframework.util.Assert;
2324

@@ -92,6 +93,29 @@ public String getValue() {
9293
return this.value;
9394
}
9495

96+
static ClientAuthenticationMethod[] methods() {
97+
return new ClientAuthenticationMethod[] { CLIENT_SECRET_BASIC, CLIENT_SECRET_POST, CLIENT_SECRET_JWT,
98+
PRIVATE_KEY_JWT, NONE, TLS_CLIENT_AUTH, SELF_SIGNED_TLS_CLIENT_AUTH };
99+
}
100+
101+
/**
102+
* A factory to construct a {@link ClientAuthenticationMethod} based on a string,
103+
* returning any constant value that matches.
104+
* @param method the client authentication method
105+
* @return a {@link ClientAuthenticationMethod}; specifically the corresponding
106+
* constant, if any
107+
* @since 6.5
108+
*/
109+
@NonNull
110+
public static ClientAuthenticationMethod valueOf(String method) {
111+
for (ClientAuthenticationMethod m : methods()) {
112+
if (m.getValue().equals(method)) {
113+
return m;
114+
}
115+
}
116+
return new ClientAuthenticationMethod(method);
117+
}
118+
95119
@Override
96120
public boolean equals(Object obj) {
97121
if (this == obj) {

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClientAuthenticationMethodTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,13 @@
1616

1717
package org.springframework.security.oauth2.core;
1818

19+
import java.util.stream.Stream;
20+
21+
import org.junit.jupiter.api.DynamicTest;
1922
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.TestFactory;
24+
25+
import org.springframework.util.StringUtils;
2026

2127
import static org.assertj.core.api.Assertions.assertThat;
2228
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -69,4 +75,23 @@ public void getValueWhenAuthenticationMethodSelfSignedTlsClientAuthThenReturnSel
6975
.isEqualTo("self_signed_tls_client_auth");
7076
}
7177

78+
@Test
79+
public void valueOfWhenAnyAuthenticationMethodThenConstructs() {
80+
String string = new String("any");
81+
ClientAuthenticationMethod method = ClientAuthenticationMethod.valueOf(string);
82+
assertThat(method.getValue()).isSameAs(string);
83+
}
84+
85+
@TestFactory
86+
Stream<DynamicTest> valueOfWhenMatchesStaticThenReturnsStatic() {
87+
return Stream.of(ClientAuthenticationMethod.methods())
88+
.map((method) -> DynamicTest.dynamicTest(testName(method.getValue()),
89+
() -> assertThat(ClientAuthenticationMethod.valueOf(method.getValue())).isSameAs(method)));
90+
}
91+
92+
String testName(String method) {
93+
String methodName = StringUtils.capitalize(method.replaceAll("_", ""));
94+
return "valueOfWhen" + methodName + "ThenReturnsStatic" + methodName;
95+
}
96+
7297
}

0 commit comments

Comments
 (0)