-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTheRegister.java
155 lines (135 loc) · 7.6 KB
/
TheRegister.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.renomad.inmra;
import com.renomad.inmra.auth.*;
import com.renomad.inmra.featurelogic.letsencrypt.LetsEncrypt;
import com.renomad.inmra.featurelogic.misc.Message;
import com.renomad.inmra.featurelogic.photo.*;
import com.renomad.inmra.security.ISecurityUtils;
import com.renomad.inmra.security.SecurityUtils;
import com.renomad.inmra.utils.MemoriaContext;
import com.renomad.minum.state.Constants;
import com.renomad.minum.state.Context;
import com.renomad.minum.database.Db;
import com.renomad.minum.utils.LRUCache;
import com.renomad.minum.utils.MyThread;
import com.renomad.minum.utils.StringUtils;
import com.renomad.minum.web.WebFramework;
import com.renomad.inmra.administrative.Admin;
import com.renomad.inmra.featurelogic.persons.PersonEndpoints;
import com.renomad.inmra.featurelogic.persons.Person;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import static com.renomad.minum.web.RequestLine.Method.*;
/**
* This class is where all code gets registered to work
* with our web testing.
* <br><br>
* example:
* <pre>{@code
* wf.registerPath(RequestLine.Method.GET, "formentry", sd::formEntry);
* }</pre>
*/
public class TheRegister {
private final WebFramework webFramework;
private final Db<User> userDb;
private final AuthPages ap;
private final PersonEndpoints personEndpoints;
private final UploadPhoto up;
private final ListPhotos lp;
private final Admin admin;
private final LetsEncrypt letsEncrypt;
private final Message message;
private final Constants constants;
public void registerDomains() {
// Register an initial user - "admin"
registerTheAdminUser(userDb, ap);
// general pages
webFramework.registerPath(GET, "index", personEndpoints::listAllPersonsGet);
webFramework.registerPath(GET, "", personEndpoints::listAllPersonsGet);
webFramework.registerPath(GET, "message", message::messagePageGet);
// photos stuff
webFramework.registerPath(GET, "photos", lp::ListPhotosPageGet);
webFramework.registerPath(POST, "upload", up::uploadPhotoReceivePost);
webFramework.registerPath(POST, "copyphoto", up::copyPhotoReceivePost);
webFramework.registerPath(GET, "photo", lp::grabPhotoGet);
webFramework.registerPath(DELETE, "photo", up::photoDelete);
webFramework.registerPath(POST, "deletephoto", up::photoDeletePost);
webFramework.registerPath(PATCH, "photolongdescupdate", up::photoLongDescriptionUpdate);
webFramework.registerPath(POST, "photolongdescupdate", up::photoLongDescriptionUpdatePost);
webFramework.registerPath(PATCH, "photocaptionupdate", up::photoShortDescriptionUpdate);
webFramework.registerPath(POST, "photocaptionupdate", up::photoShortDescriptionUpdatePost);
webFramework.registerPath(GET, "copyphoto", up::copyPhotoGet);
// auth stuff
webFramework.registerPath(GET, "login", ap::loginGet);
webFramework.registerPath(POST, "loginuser", ap::loginUserPost);
webFramework.registerPath(POST, "logout", ap::logoutPost);
webFramework.registerPath(GET, "loggedout", ap::loggedoutGet);
webFramework.registerPath(GET, "register", ap::registerGet);
webFramework.registerPath(POST, "registeruser", ap::registerUserPost);
webFramework.registerPath(GET, "resetpassword", ap::resetUserPasswordGet);
webFramework.registerPath(POST, "resetpassword", ap::resetUserPasswordPost);
// Person stuff
webFramework.registerPath(GET, "editperson", personEndpoints::createNewPersonGet);
webFramework.registerPath(POST, "editperson", personEndpoints::editPersonPost);
webFramework.registerPath(DELETE, "person", personEndpoints::deletePerson);
webFramework.registerPath(POST, "persondelete", personEndpoints::deletePersonPost);
webFramework.registerPath(GET, "editpersons", personEndpoints::editListGet);
webFramework.registerPath(GET, "persons", personEndpoints::listAllPersonsGet);
webFramework.registerPath(GET, "person", personEndpoints::listPersonGet);
webFramework.registerPath(GET, "personsearch", personEndpoints::searchPersonGet);
webFramework.registerPath(GET, "relationsearch", personEndpoints::searchRelationGet);
// adding relations
webFramework.registerPath(POST, "addrelation", personEndpoints::addRelationPost);
// The Administration page - for controlling the system
webFramework.registerPath(GET, "admin", admin::get);
// An endpoint for Certbot / letsencrypt
// see https://eff-certbot.readthedocs.io/en/stable/using.html#webroot
webFramework.registerPartialPath(GET, ".well-known/acme-challenge", letsEncrypt::challengeResponse);
}
public TheRegister(Context context, MemoriaContext memoriaContext) {
this.webFramework = context.getFullSystem().getWebFramework();
this.constants = context.getConstants();
ISecurityUtils securityUtils = new SecurityUtils(context.getExecutorService(), context.getLogger());
// initialize our databases
Db<SessionId> sessionDb = context.getDb("sessions", SessionId.EMPTY);
userDb = context.getDb("users", User.EMPTY);
Db<Photograph> photoDb = context.getDb("photos", Photograph.EMPTY);
Db<Person> personDb = context.getDb("persons", Person.EMPTY);
Db<PhotoToPerson> photoToPersonDb = context.getDb("photo_to_person", PhotoToPerson.EMPTY);
Map<String, byte[]> photoLruCache = LRUCache.getLruCache();
// instantiate the classes
AuthUtils au = new AuthUtils(sessionDb, userDb, context, memoriaContext);
AuthHeader authHeader = new AuthHeader(au, memoriaContext);
ap = new AuthPages(au, authHeader, sessionDb, userDb, context, memoriaContext, securityUtils);
new LoopingSessionReviewing(context, ap).initialize();
PhotoService photoService = new PhotoService(context, memoriaContext, photoDb, photoLruCache, photoToPersonDb, personDb, au);
personEndpoints = new PersonEndpoints(context, memoriaContext, personDb, au, photoToPersonDb, photoDb, photoService, authHeader);
up = new UploadPhoto(context, memoriaContext, au, photoService);
lp = new ListPhotos(context, memoriaContext, au, personEndpoints, photoLruCache, photoToPersonDb, photoDb);
admin = new Admin(au, userDb, sessionDb, context, memoriaContext);
letsEncrypt = new LetsEncrypt(context);
message = new Message(memoriaContext);
}
/**
* If there is no admin user registered for the system yet, this
* will create it.
*/
private void registerTheAdminUser(Db<User> userDb, AuthPages authPages) {
if (userDb.values().stream().noneMatch(x -> x.getUsername().equals("admin"))) {
String newPassword = StringUtils.generateSecureRandomString(20);
try {
MyThread.sleep(500);
System.out.println("\n****************************************\n\n");
System.out.println("Creating a new admin password, see \"admin_password\"");
System.out.println("in the root of the database directory.");
System.out.println("Use a user name of \"admin\" at localhost:8080\\login");
System.out.println("\n\n************************************************\n\n");
Files.writeString(Path.of(constants.dbDirectory).resolve(Path.of("admin_password")), newPassword);
} catch (IOException e) {
throw new RuntimeException(e);
}
authPages.registerUserPost("admin", newPassword);
}
}
}