-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathBasicTokenDecoder.java
38 lines (30 loc) · 1.18 KB
/
BasicTokenDecoder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package nextstep.security.util;
import static nextstep.security.util.SecurityConstants.BASIC_TOKEN_PREFIX;
import java.nio.charset.StandardCharsets;
import nextstep.security.exception.AuthenticationException;
import nextstep.security.userdetail.UserDetail;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
@Component
public class BasicTokenDecoder implements TokenDecoder {
@Override
public UserDetail decodeToken(String token) {
String base64Token = token.substring(BASIC_TOKEN_PREFIX.length());
String decodedToken = new String(Base64Utils.decodeFromString(base64Token),
StandardCharsets.UTF_8);
validateBasicToken(token);
String[] parts = decodedToken.split(":");
if (parts.length != 2) {
throw new AuthenticationException();
}
return new UserDetail(parts[0], parts[1]);
}
private void validateBasicToken(String authorization) {
if (authorization == null) {
throw new AuthenticationException();
}
if (!authorization.startsWith(BASIC_TOKEN_PREFIX)) {
throw new AuthenticationException();
}
}
}