Skip to content

Commit f3bf599

Browse files
Create Jackson Mixin for OneTimeTokenAuthenticationToken
Closes gh-18095 Signed-off-by: Marcus Hert da Coregio <[email protected]>
1 parent f548aaf commit f3bf599

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

core/src/main/java/org/springframework/security/jackson2/CoreJackson2Module.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.security.authentication.BadCredentialsException;
2626
import org.springframework.security.authentication.RememberMeAuthenticationToken;
2727
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
28+
import org.springframework.security.authentication.ott.OneTimeTokenAuthenticationToken;
2829
import org.springframework.security.core.authority.SimpleGrantedAuthority;
2930
import org.springframework.security.core.userdetails.User;
3031

@@ -70,6 +71,7 @@ public void setupModule(SetupContext context) {
7071
context.setMixInAnnotations(UsernamePasswordAuthenticationToken.class,
7172
UsernamePasswordAuthenticationTokenMixin.class);
7273
context.setMixInAnnotations(BadCredentialsException.class, BadCredentialsExceptionMixin.class);
74+
context.setMixInAnnotations(OneTimeTokenAuthenticationToken.class, OneTimeTokenAuthenticationTokenMixin.class);
7375
}
7476

7577
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.jackson2;
18+
19+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
20+
import com.fasterxml.jackson.annotation.JsonCreator;
21+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
24+
import org.springframework.security.core.GrantedAuthority;
25+
26+
import java.util.Collection;
27+
28+
/**
29+
* Jackson Mixin class helps in serialize/deserialize
30+
* {@link org.springframework.security.authentication.ott.OneTimeTokenAuthenticationToken}.
31+
*
32+
* <pre>
33+
* ObjectMapper mapper = new ObjectMapper();
34+
* mapper.registerModule(new CoreJackson2Module());
35+
* </pre>
36+
*
37+
* @author Marcus Da Coregio
38+
* @since 6.5.7
39+
* @see CoreJackson2Module
40+
* @see SecurityJackson2Modules
41+
*
42+
*/
43+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
44+
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE,
45+
getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY, isGetterVisibility = JsonAutoDetect.Visibility.NONE)
46+
@JsonIgnoreProperties(ignoreUnknown = true)
47+
abstract class OneTimeTokenAuthenticationTokenMixin {
48+
49+
@JsonCreator
50+
OneTimeTokenAuthenticationTokenMixin(@JsonProperty("principal") Object principal,
51+
@JsonProperty("authorities") Collection<? extends GrantedAuthority> authorities) {
52+
}
53+
54+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.springframework.security.authentication.ott;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.security.core.authority.AuthorityUtils;
6+
import org.springframework.security.jackson2.SecurityJackson2Modules;
7+
8+
import java.io.IOException;
9+
10+
import static org.assertj.core.api.Assertions.assertThatNoException;
11+
12+
class OneTimeTokenAuthenticationTokenTests {
13+
14+
// gh-18095
15+
@Test
16+
void shouldBeAbleToDeserializeFromJsonWithDefaultTypingActivated() throws IOException {
17+
ObjectMapper mapper = new ObjectMapper();
18+
mapper.registerModules(SecurityJackson2Modules.getModules(getClass().getClassLoader()));
19+
OneTimeTokenAuthenticationToken oneTimeTokenAuthenticationToken = new OneTimeTokenAuthenticationToken(
20+
"principal", AuthorityUtils.createAuthorityList("ROLE_USER"));
21+
byte[] serialized = mapper.writeValueAsBytes(oneTimeTokenAuthenticationToken);
22+
assertThatNoException().isThrownBy(() -> mapper.readValue(serialized, OneTimeTokenAuthenticationToken.class));
23+
}
24+
25+
}

0 commit comments

Comments
 (0)