| .. | .. |
|---|
| 1 | +/* |
|---|
| 2 | +* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. |
|---|
| 3 | +*/ |
|---|
| 1 | 4 | package net.curisit.securis.ioc; |
|---|
| 2 | 5 | |
|---|
| 3 | 6 | import java.io.File; |
|---|
| .. | .. |
|---|
| 18 | 21 | import com.google.inject.AbstractModule; |
|---|
| 19 | 22 | import com.google.inject.Provides; |
|---|
| 20 | 23 | |
|---|
| 24 | +/** |
|---|
| 25 | +* SecurisModule |
|---|
| 26 | +* <p> |
|---|
| 27 | +* Guice module that provides application-level infrastructural dependencies |
|---|
| 28 | +* (base URI, app directories, DB files list, support email/hash, etc.). |
|---|
| 29 | +* <p> |
|---|
| 30 | +* Configuration: |
|---|
| 31 | +* - Reads server port from /securis-server.properties (key: "port"). |
|---|
| 32 | +* - Defaults to port 9997 when not present or on read errors. |
|---|
| 33 | +* - Constructs base URI as http://0.0.0.0:{port}/ with UriBuilder. |
|---|
| 34 | +* - Creates working directories under ${user.home}/.SeCuris on demand. |
|---|
| 35 | +* |
|---|
| 36 | +* Security note: |
|---|
| 37 | +* - getPassword/getFilePassword are simple helpers; secrets should be |
|---|
| 38 | +* managed via a secure vault/env vars in production. |
|---|
| 39 | +* |
|---|
| 40 | +* @author JRA |
|---|
| 41 | +* Last reviewed by JRA on Oct 7, 2025. |
|---|
| 42 | +*/ |
|---|
| 21 | 43 | public class SecurisModule extends AbstractModule { |
|---|
| 22 | 44 | |
|---|
| 23 | 45 | private static final int DEFAULT_PORT = 9997; |
|---|
| .. | .. |
|---|
| 25 | 47 | |
|---|
| 26 | 48 | private static final Logger LOG = LogManager.getLogger(SecurisModule.class); |
|---|
| 27 | 49 | |
|---|
| 50 | + /** configure<p>Currently no explicit bindings; providers below supply instances. */ |
|---|
| 28 | 51 | @Override |
|---|
| 29 | | - protected void configure() { |
|---|
| 52 | + protected void configure() { } |
|---|
| 30 | 53 | |
|---|
| 31 | | - } |
|---|
| 32 | | - |
|---|
| 54 | + /** |
|---|
| 55 | + * getPassword<p> |
|---|
| 56 | + * Composite password (example use with encrypted H2 URL). |
|---|
| 57 | + * |
|---|
| 58 | + * @return concatenated password string |
|---|
| 59 | + */ |
|---|
| 33 | 60 | public String getPassword() { |
|---|
| 34 | 61 | return getFilePassword() + " " + "53curi5"; |
|---|
| 35 | 62 | } |
|---|
| 36 | 63 | |
|---|
| 64 | + /** |
|---|
| 65 | + * getFilePassword<p> |
|---|
| 66 | + * Standalone file password (for H2 CIPHER). |
|---|
| 67 | + * |
|---|
| 68 | + * @return file password string |
|---|
| 69 | + */ |
|---|
| 37 | 70 | public String getFilePassword() { |
|---|
| 38 | 71 | return "cur151T"; |
|---|
| 39 | 72 | } |
|---|
| 40 | 73 | |
|---|
| 74 | + /** |
|---|
| 75 | + * getUrl<p> |
|---|
| 76 | + * H2 JDBC URL with AES cipher pointing to {appDir}/db/securis. |
|---|
| 77 | + * |
|---|
| 78 | + * @param appDir application working directory |
|---|
| 79 | + * @return JDBC URL (H2) |
|---|
| 80 | + */ |
|---|
| 41 | 81 | public String getUrl(File appDir) { |
|---|
| 42 | 82 | return String.format("jdbc:h2:%s/db/securis;CIPHER=AES", appDir.getAbsolutePath()); |
|---|
| 43 | 83 | } |
|---|
| 44 | 84 | |
|---|
| 85 | + /** |
|---|
| 86 | + * getBaseURI<p> |
|---|
| 87 | + * Provide the base URI for the HTTP server using configured or default port. |
|---|
| 88 | + * |
|---|
| 89 | + * @return base URI (http://0.0.0.0:{port}/) |
|---|
| 90 | + */ |
|---|
| 45 | 91 | @Named("base-uri") |
|---|
| 46 | 92 | @Provides |
|---|
| 47 | 93 | @ApplicationScoped |
|---|
| 48 | 94 | public URI getBaseURI() { |
|---|
| 49 | | - // Read from configuration, where? |
|---|
| 50 | 95 | try { |
|---|
| 51 | 96 | String url = MessageFormat.format("http://{0}/", "0.0.0.0"); |
|---|
| 52 | 97 | LOG.debug("Server url{}", url); |
|---|
| .. | .. |
|---|
| 56 | 101 | } |
|---|
| 57 | 102 | } |
|---|
| 58 | 103 | |
|---|
| 104 | + /** |
|---|
| 105 | + * getPort<p> |
|---|
| 106 | + * Read port from properties file or return default. |
|---|
| 107 | + * |
|---|
| 108 | + * @return HTTP port |
|---|
| 109 | + */ |
|---|
| 59 | 110 | private int getPort() { |
|---|
| 60 | 111 | Integer port; |
|---|
| 61 | 112 | Properties prop = new Properties(); |
|---|
| 62 | 113 | try { |
|---|
| 63 | 114 | prop.load(getClass().getResourceAsStream(PROPERTIES_FILE_NAME)); |
|---|
| 64 | 115 | port = Integer.valueOf(prop.getProperty("port")); |
|---|
| 65 | | - if (port == null) { |
|---|
| 66 | | - return DEFAULT_PORT; |
|---|
| 67 | | - } else { |
|---|
| 68 | | - return port; |
|---|
| 69 | | - } |
|---|
| 116 | + return (port == null ? DEFAULT_PORT : port); |
|---|
| 70 | 117 | } catch (Exception ex) { |
|---|
| 71 | 118 | return DEFAULT_PORT; |
|---|
| 72 | 119 | } |
|---|
| 73 | 120 | } |
|---|
| 74 | 121 | |
|---|
| 122 | + /** |
|---|
| 123 | + * getAppDbFiles<p> |
|---|
| 124 | + * List of SQL files to initialize the application DB. |
|---|
| 125 | + * |
|---|
| 126 | + * @return list of classpath resource paths |
|---|
| 127 | + */ |
|---|
| 75 | 128 | protected List<String> getAppDbFiles() { |
|---|
| 76 | | - |
|---|
| 77 | 129 | return Arrays.asList("/db/schema.sql"); |
|---|
| 78 | 130 | } |
|---|
| 79 | 131 | |
|---|
| 132 | + /** |
|---|
| 133 | + * getTemporaryDir<p> |
|---|
| 134 | + * Provide a temp directory inside the app working dir (.TEMP). |
|---|
| 135 | + * Creates it if missing and marks for deletion on exit. |
|---|
| 136 | + * |
|---|
| 137 | + * @return temp directory or null if creation failed |
|---|
| 138 | + */ |
|---|
| 80 | 139 | @Named("temporary-dir") |
|---|
| 81 | 140 | @Provides |
|---|
| 82 | 141 | @ApplicationScoped |
|---|
| 83 | 142 | public File getTemporaryDir() { |
|---|
| 84 | | - String tmp = getAppDir().getAbsolutePath(); |
|---|
| 85 | | - tmp += File.separator + ".TEMP"; |
|---|
| 143 | + String tmp = getAppDir().getAbsolutePath() + File.separator + ".TEMP"; |
|---|
| 86 | 144 | File ftmp = new File(tmp); |
|---|
| 87 | 145 | if (!ftmp.exists()) { |
|---|
| 88 | 146 | if (!ftmp.mkdirs()) { |
|---|
| .. | .. |
|---|
| 94 | 152 | return ftmp; |
|---|
| 95 | 153 | } |
|---|
| 96 | 154 | |
|---|
| 155 | + /** |
|---|
| 156 | + * getAppDir<p> |
|---|
| 157 | + * Provide the app working directory under ${user.home}/.SeCuris (creates if missing). |
|---|
| 158 | + * |
|---|
| 159 | + * @return working directory or null if creation failed |
|---|
| 160 | + */ |
|---|
| 97 | 161 | @Named("app-dir") |
|---|
| 98 | 162 | @Provides |
|---|
| 99 | 163 | @ApplicationScoped |
|---|
| 100 | 164 | public File getAppDir() { |
|---|
| 101 | 165 | String appDir = System.getProperty("user.home", System.getProperty("user.dir")); |
|---|
| 102 | | - if (appDir == null) { |
|---|
| 103 | | - appDir = "."; |
|---|
| 104 | | - } |
|---|
| 166 | + if (appDir == null) appDir = "."; |
|---|
| 105 | 167 | appDir += File.separator + ".SeCuris"; |
|---|
| 106 | 168 | File fAppDir = new File(appDir); |
|---|
| 107 | 169 | if (!fAppDir.exists()) { |
|---|
| .. | .. |
|---|
| 113 | 175 | return fAppDir; |
|---|
| 114 | 176 | } |
|---|
| 115 | 177 | |
|---|
| 178 | + /** |
|---|
| 179 | + * getSupportEmail<p> |
|---|
| 180 | + * Provide support email address. |
|---|
| 181 | + * |
|---|
| 182 | + * @return email |
|---|
| 183 | + */ |
|---|
| 116 | 184 | @Named("support-email") |
|---|
| 117 | 185 | @Provides |
|---|
| 118 | 186 | @ApplicationScoped |
|---|
| .. | .. |
|---|
| 120 | 188 | return "support@curisit.net"; |
|---|
| 121 | 189 | } |
|---|
| 122 | 190 | |
|---|
| 191 | + /** |
|---|
| 192 | + * getHashLogo<p> |
|---|
| 193 | + * Provide a static content hash for the logo (cache-busting or integrity). |
|---|
| 194 | + * |
|---|
| 195 | + * @return hex SHA-256 |
|---|
| 196 | + */ |
|---|
| 123 | 197 | @Named("hash-logo") |
|---|
| 124 | 198 | @Provides |
|---|
| 125 | 199 | @ApplicationScoped |
|---|
| .. | .. |
|---|
| 127 | 201 | return "1b42616809d4cd8ccf109e3c30d0ab25067f160b30b7354a08ddd563de0096ba"; |
|---|
| 128 | 202 | } |
|---|
| 129 | 203 | |
|---|
| 204 | + /** |
|---|
| 205 | + * getDbFiles<p> |
|---|
| 206 | + * Provide DB initialization files list (delegates to {@link #getAppDbFiles()}). |
|---|
| 207 | + * |
|---|
| 208 | + * @return list of SQL resource paths |
|---|
| 209 | + */ |
|---|
| 130 | 210 | @Named("db-files") |
|---|
| 131 | 211 | @Provides |
|---|
| 132 | 212 | @ApplicationScoped |
|---|
| 133 | 213 | public List<String> getDbFiles() { |
|---|
| 134 | 214 | return getAppDbFiles(); |
|---|
| 135 | 215 | } |
|---|
| 136 | | - |
|---|
| 137 | 216 | } |
|---|
| 217 | + |
|---|