Skip to content

Commit 7ef1d33

Browse files
committed
System Touch
1 parent 9a5c303 commit 7ef1d33

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

source/admin/ModuleAdmin.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package admin;
2+
3+
import commons.CommonRails;
4+
5+
import java.util.Set;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
8+
/**
9+
* Basic administrator for ModuleInstallationService.
10+
* Admin credentials are loaded from the system property or env:
11+
* module.admin.password / MODULE_ADMIN_PASSWORD
12+
* Defaults to "n21admin" if neither is set.
13+
*
14+
* An authenticated session token is stored in SESSIONS for the lifetime
15+
* of the TCP connection.
16+
*/
17+
public class ModuleAdmin
18+
{
19+
private static final String PASSWORD = resolvePassword();
20+
21+
/** Active session tokens — keyed by token string, value = nationalId of admin. */
22+
private static final ConcurrentHashMap<String, Long> SESSIONS = new ConcurrentHashMap<>();
23+
24+
/**
25+
* Attempt login. Returns a session token on success, null on failure.
26+
*/
27+
public static String login(final String SUBMITTED_PASSWORD, final long NATIONAL_ID)
28+
{
29+
if (SUBMITTED_PASSWORD == null || !SUBMITTED_PASSWORD.equals(PASSWORD))
30+
{
31+
CommonRails.printSystemComponent(
32+
new ModuleAdmin(), ModuleAdmin.class.hashCode(),
33+
". ModuleAdmin login FAILED for National ID " + NATIONAL_ID + " .");
34+
return null;
35+
}
36+
String token = Long.toHexString(System.nanoTime()) + Long.toHexString(NATIONAL_ID);
37+
SESSIONS.put(token, NATIONAL_ID);
38+
CommonRails.printSystemComponent(
39+
new ModuleAdmin(), ModuleAdmin.class.hashCode(),
40+
". ModuleAdmin login SUCCESS for National ID " + NATIONAL_ID + " .");
41+
return token;
42+
}
43+
44+
/** Returns true if the token belongs to an authenticated admin session. */
45+
public static boolean isAdmin(final String TOKEN)
46+
{
47+
return TOKEN != null && SESSIONS.containsKey(TOKEN);
48+
}
49+
50+
/** Invalidate a session token on logout/disconnect. */
51+
public static void logout(final String TOKEN)
52+
{
53+
Long id = SESSIONS.remove(TOKEN);
54+
if (id != null)
55+
CommonRails.printSystemComponent(
56+
new ModuleAdmin(), ModuleAdmin.class.hashCode(),
57+
". ModuleAdmin session ended for National ID " + id + " .");
58+
}
59+
60+
private static String resolvePassword()
61+
{
62+
String prop = System.getProperty("module.admin.password");
63+
if (prop != null && !prop.isEmpty()) return prop;
64+
String env = System.getenv("MODULE_ADMIN_PASSWORD");
65+
if (env != null && !env.isEmpty()) return env;
66+
return "n21admin";
67+
}
68+
}

0 commit comments

Comments
 (0)