-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorizer.java
More file actions
28 lines (21 loc) · 854 Bytes
/
Authorizer.java
File metadata and controls
28 lines (21 loc) · 854 Bytes
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
package httpserver;
import httpserver.request.Request;
import java.util.Base64;
public class Authorizer {
public boolean authorize(Request request) {
if (!request.hasHeader("Authorization")) {
return false;
}
String authorizationHeader = request.getHeaderValue("Authorization");
String[] credentials = decodeBasicAuthHeader(authorizationHeader);
return validateCredentials(credentials);
}
private boolean validateCredentials(String[] credentials) {
return credentials[0].equals("admin") && credentials[1].equals("hunter2");
}
public String[] decodeBasicAuthHeader(String authHeaderValue) {
String[] parts = authHeaderValue.split(" ", 2);
String decoded = new String(Base64.getDecoder().decode(parts[1]));
return decoded.split(":", 2);
}
}