Repository navigation
Expand file tree
/
Copy pathapp.java
More file actions
68 lines (55 loc) · 1.74 KB
/
Copy pathapp.java
File metadata and controls
68 lines (55 loc) · 1.74 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.javalin:javalin:4.1.1 org.slf4j:slf4j-simple:1.7.31
//SOURCES Opener.java
import java.util.Map;
import java.util.Map.Entry;
import io.javalin.http.Context;
class app extends Opener {
private Map<String, String> usrPwds;
@Override
public Integer call() throws Exception {
String users = System.getenv("DOP_USERS");
if (users == null) {
System.out.println("Missing DOP_USERS environment variable");
return 1;
}
usrPwds = toMap(users);
System.out.println("Users: " + usrPwds.keySet());
return super.call();
}
@Override
public String authorized(Context ctx) {
String ip = getRemoteAddress(ctx);
if (isBlocked(ip)) {
System.out.println("IP is blocked: " + ip);
return null;
}
String token = ctx.req.getParameter("token");
if (token == null) {
System.out.println("Missing parameter for authentication, need: token");
logAuth(ip, false);
return null;
}
String usr = null;
for (Entry<String, String> e : usrPwds.entrySet()) {
if (e.getValue().equals(token)) {
usr = e.getKey();
}
}
if (usr == null) {
System.out.println("No user for provided token: " + token);
logAuth(ip, false);
return null;
}
logAuth(ip, true);
System.out.println("User authorized: " + usr);
return usr;
}
public static void main(String... args) throws Exception {
app app = new app();
int res = app.call();
if (res != 0) {
System.exit(res);
}
}
}