-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathEmailPasswordAuth.java
52 lines (44 loc) · 1.55 KB
/
EmailPasswordAuth.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package nextstep.security.model;
import nextstep.security.constants.Constants;
import org.springframework.util.Base64Utils;
import java.util.Objects;
public class EmailPasswordAuth {
private String email;
private String password;
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public EmailPasswordAuth(String email, String password) {
this.email = email;
this.password = password;
}
public static EmailPasswordAuth from(String basicAuth) {
if(Objects.isNull(basicAuth) || !basicAuth.startsWith(Constants.Auth.BASIC)) {
return null;
}
String base64Credentials = basicAuth.substring(Constants.Auth.BASIC.length()).trim();
try {
// Base64 문자열을 디코딩
byte[] decoded = Base64Utils.decodeFromString(base64Credentials);
String credentials = new String(decoded);
// 사용자 이름과 비밀번호가 ':'로 구분되는지 확인
String[] parts = credentials.split(":", 2);
if(parts.length == 2) {
return new EmailPasswordAuth(parts[0], parts[1]);
}
return null;
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
}
}
public static EmailPasswordAuth from(String email, String password) {
if(email == null || password == null) {
return null;
}
return new EmailPasswordAuth(email, password);
}
}