• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 27145358627

08 Jun 2026 02:39PM UTC coverage: 20.682% (-0.3%) from 20.947%
27145358627

push

github

web-flow
Merge pull request #479 from knowledgepixels/feat/about-pages-478

Resource-page tabs, presets, and role-gated view actions (#478, #302)

1052 of 6429 branches covered (16.36%)

Branch coverage included in aggregate %.

2642 of 11432 relevant lines covered (23.11%)

3.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.6
src/main/java/com/knowledgepixels/nanodash/NanodashSession.java
1
package com.knowledgepixels.nanodash;
2

3
import com.knowledgepixels.nanodash.component.NanopubResults;
4
import com.knowledgepixels.nanodash.domain.User;
5
import com.knowledgepixels.nanodash.page.OrcidLoginPage;
6
import com.knowledgepixels.nanodash.page.ProfilePage;
7
import jakarta.servlet.http.HttpServletRequest;
8
import jakarta.servlet.http.HttpSession;
9
import jakarta.xml.bind.DatatypeConverter;
10
import org.apache.commons.io.FileUtils;
11
import org.apache.wicket.PageReference;
12
import org.apache.wicket.Session;
13
import org.apache.wicket.protocol.http.WebSession;
14
import org.apache.wicket.request.Request;
15
import org.apache.wicket.request.flow.RedirectToUrlException;
16
import org.apache.wicket.request.mapper.parameter.PageParameters;
17
import org.eclipse.rdf4j.model.IRI;
18
import org.eclipse.rdf4j.model.ValueFactory;
19
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
20
import org.nanopub.extra.security.*;
21
import org.nanopub.extra.setting.IntroNanopub;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

25
import java.io.File;
26
import java.io.IOException;
27
import java.io.Serializable;
28
import java.nio.charset.StandardCharsets;
29
import java.security.KeyPair;
30
import java.util.Date;
31
import java.util.List;
32
import java.util.Locale;
33
import java.util.Map;
34
import java.util.concurrent.ConcurrentHashMap;
35
import java.util.concurrent.ConcurrentMap;
36

37
import org.apache.wicket.markup.html.WebPage;
38
import org.nanopub.Nanopub;
39

40
/**
41
 * Represents a session in the Nanodash application.
42
 */
43
public class NanodashSession extends WebSession {
44

45
    private transient HttpSession httpSession;
46
    private static final Logger logger = LoggerFactory.getLogger(NanodashSession.class);
9✔
47

48
    /**
49
     * Retrieves the current Nanodash session.
50
     *
51
     * @return The current NanodashSession instance.
52
     */
53
    public static NanodashSession get() {
54
        return (NanodashSession) Session.get();
9✔
55
    }
56

57
    /**
58
     * Returns the current user's agent IRI, or null when no session is bound to
59
     * the current thread (e.g. a background or non-request context) or no user is
60
     * logged in. Safe to call outside a request cycle, unlike {@link #get()}.
61
     *
62
     * @return the current user IRI, or null
63
     */
64
    public static IRI getCurrentUserIriOrNull() {
65
        return Session.exists() ? get().getUserIri() : null;
×
66
    }
67

68
    /**
69
     * Constructs a new NanodashSession for the given request.
70
     * Initializes the HTTP session and loads profile information.
71
     *
72
     * @param request The HTTP request.
73
     */
74
    public NanodashSession(Request request) {
75
        super(request);
9✔
76
        setLocale(Locale.US);
12✔
77
        httpSession = ((HttpServletRequest) request.getContainerRequest()).getSession();
18✔
78
        bind();
6✔
79
        loadProfileInfo();
6✔
80
    }
3✔
81

82
    @Override
83
    public Session setLocale(Locale locale) {
84
        return super.setLocale(Locale.US);
12✔
85
    }
86

87
    private static ValueFactory vf = SimpleValueFactory.getInstance();
9✔
88

89
//        private IntroExtractor introExtractor;
90

91
    private String userDir = System.getProperty("user.home") + "/.nanopub/";
15✔
92
    private NanopubResults.ViewMode nanopubResultsViewMode = NanopubResults.ViewMode.LIST;
9✔
93

94
    private KeyPair keyPair;
95
    private IRI userIri;
96
    private ConcurrentMap<IRI, IntroNanopub> introNps;
97
//        private Boolean isOrcidLinked;
98
//        private String orcidLinkError;
99

100
    private Integer localIntroCount = null;
9✔
101
    private IntroNanopub localIntro = null;
9✔
102

103
    private Date lastTimeIntroPublished = null;
9✔
104

105
    /**
106
     * Loads profile information for the user.
107
     * Initializes user-related data such as keys and introductions.
108
     */
109
    public void loadProfileInfo() {
110
        localIntroCount = null;
9✔
111
        localIntro = null;
9✔
112
        NanodashPreferences prefs = NanodashPreferences.get();
6✔
113
        if (prefs.isOrcidLoginMode()) {
9!
114
            File usersDir = new File(System.getProperty("user.home") + "/.nanopub/nanodash-users/");
×
115
            if (!usersDir.exists()) usersDir.mkdir();
×
116
        }
117
        if (userIri == null && !prefs.isReadOnlyMode() && !prefs.isOrcidLoginMode()) {
27!
118
            if (getOrcidFile().exists()) {
12!
119
                try {
120
                    String orcid = FileUtils.readFileToString(getOrcidFile(), StandardCharsets.UTF_8).trim();
×
121
                    //String orcid = Files.readString(orcidFile.toPath(), StandardCharsets.UTF_8).trim();
122
                    if (orcid.matches(ProfilePage.ORCID_PATTERN)) {
×
123
                        userIri = vf.createIRI("https://orcid.org/" + orcid);
×
124
                        if (httpSession != null) httpSession.setMaxInactiveInterval(24 * 60 * 60);  // 24h
×
125
                    }
126
                } catch (IOException ex) {
×
127
                    logger.error("Couldn't read ORCID file", ex);
×
128
                }
×
129
            }
130
        }
131
        if (userIri != null && keyPair == null) {
9!
132
            File keyFile = getKeyFile();
×
133
            if (keyFile.exists()) {
×
134
                try {
135
                    keyPair = SignNanopub.loadKey(keyFile.getPath(), SignatureAlgorithm.RSA);
×
136
                } catch (Exception ex) {
×
137
                    logger.error("Couldn't load key pair", ex);
×
138
                }
×
139
            } else {
140
                // Automatically generate new keys
141
                makeKeys();
×
142
            }
143
        }
144
        if (userIri != null && keyPair != null && introNps == null) {
9!
145
            introNps = new ConcurrentHashMap<>(User.getIntroNanopubs(getPubkeyString()));
×
146
        }
147
//                checkOrcidLink();
148
    }
3✔
149

150
    /**
151
     * Checks if the user's profile is complete.
152
     *
153
     * @return True if the profile is complete, false otherwise.
154
     */
155
    public boolean isProfileComplete() {
156
        return userIri != null && keyPair != null && introNps != null;
×
157
    }
158

159
    /**
160
     * Redirects the user to the login page if their profile is incomplete.
161
     *
162
     * @param path       The path to redirect to after login.
163
     * @param parameters The page parameters for the redirect.
164
     */
165
    public void redirectToLoginIfNeeded(String path, PageParameters parameters) {
166
        String loginUrl = getLoginUrl(path, parameters);
×
167
        if (loginUrl == null) return;
×
168
        throw new RedirectToUrlException(loginUrl);
×
169
    }
170

171
    /**
172
     * Retrieves the login URL for the user.
173
     *
174
     * @param path       The path to redirect to after login.
175
     * @param parameters The page parameters for the redirect.
176
     * @return The login URL, or null if the user is already logged in.
177
     */
178
    public String getLoginUrl(String path, PageParameters parameters) {
179
        if (isProfileComplete()) return null;
×
180
        if (NanodashPreferences.get().isOrcidLoginMode()) {
×
181
            return OrcidLoginPage.getOrcidLoginUrl(path, parameters);
×
182
        } else {
183
            return ProfilePage.MOUNT_PATH;
×
184
        }
185
    }
186

187
    /**
188
     * Retrieves the public key as a Base64-encoded string.
189
     *
190
     * @return The public key string, or null if the key pair is not set.
191
     */
192
    public String getPubkeyString() {
193
        if (keyPair == null) return null;
×
194
        return DatatypeConverter.printBase64Binary(keyPair.getPublic().getEncoded()).replaceAll("\\s", "");
×
195
    }
196

197
    /**
198
     * Retrieves the public key hash for the user.
199
     *
200
     * @return The SHA-256 hash of the public key, or null if the public key is not set.
201
     */
202
    public String getPubkeyhash() {
203
        String pubkey = getPubkeyString();
×
204
        if (pubkey == null) return null;
×
205
        return Utils.createSha256HexHash(pubkey);
×
206
    }
207

208
    /**
209
     * Checks if the user's public key is approved.
210
     *
211
     * @return True if the public key is approved, false otherwise.
212
     */
213
    public boolean isPubkeyApproved() {
214
        if (keyPair == null || userIri == null) return false;
×
215
        return User.isApprovedPubkeyhashForUser(getPubkeyhash(), userIri);
×
216
    }
217

218
    /**
219
     * Retrieves the user's key pair.
220
     *
221
     * @return The key pair.
222
     */
223
    public KeyPair getKeyPair() {
224
        return keyPair;
×
225
    }
226

227
    /**
228
     * Generates a new key pair for the user.
229
     */
230
    public void makeKeys() {
231
        try {
232
            MakeKeys.make(getKeyFile().getAbsolutePath().replaceFirst("_rsa$", ""), SignatureAlgorithm.RSA);
×
233
            keyPair = SignNanopub.loadKey(getKeyFile().getPath(), SignatureAlgorithm.RSA);
×
234
        } catch (Exception ex) {
×
235
            logger.error("Couldn't create key pair", ex);
×
236
        }
×
237
    }
×
238

239
    /**
240
     * Retrieves the user's IRI.
241
     *
242
     * @return The user's IRI, or null if not set.
243
     */
244
    public IRI getUserIri() {
245
        return userIri;
9✔
246
    }
247

248
    /**
249
     * Retrieves the user's introduction nanopublications.
250
     *
251
     * @return A list of user's introduction nanopublications.
252
     */
253
    public List<IntroNanopub> getUserIntroNanopubs() {
254
        return User.getIntroNanopubs(userIri);
×
255
    }
256

257
    /**
258
     * Counts the number of local introduction nanopublications.
259
     *
260
     * @return The count of local introduction nanopublications.
261
     */
262
    public int getLocalIntroCount() {
263
        if (localIntroCount == null) {
×
264
            localIntroCount = 0;
×
265
            for (IntroNanopub inp : getUserIntroNanopubs()) {
×
266
                if (isIntroWithLocalKey(inp)) {
×
267
                    localIntroCount++;
×
268
                    localIntro = inp;
×
269
                }
270
            }
×
271
            if (localIntroCount > 1) localIntro = null;
×
272
        }
273
        return localIntroCount;
×
274
    }
275

276
    /**
277
     * Retrieves the local introduction nanopublication.
278
     *
279
     * @return The local introduction nanopublication, or null if not found.
280
     */
281
    public IntroNanopub getLocalIntro() {
282
        getLocalIntroCount();
×
283
        return localIntro;
×
284
    }
285

286
    /**
287
     * Checks if the given introduction nanopublication is associated with the local key.
288
     *
289
     * @param inp The introduction nanopublication.
290
     * @return True if associated with the local key, false otherwise.
291
     */
292
    public boolean isIntroWithLocalKey(IntroNanopub inp) {
293
        IRI location = Utils.getLocation(inp);
×
294
        NanopubSignatureElement el = Utils.getNanopubSignatureElement(inp);
×
295
        String siteUrl = NanodashPreferences.get().getWebsiteUrl();
×
296
        if (location != null && siteUrl != null) {
×
297
            String l = location.stringValue();
×
298
            // TODO: Solve the name change recognition in a better way:
299
            if (!l.equals(siteUrl) && !l.replace("nanobench", "nanodash").equals(siteUrl)) return false;
×
300
        }
301
        if (!getPubkeyString().equals(el.getPublicKeyString())) return false;
×
302
        for (KeyDeclaration kd : inp.getKeyDeclarations()) {
×
303
            if (getPubkeyString().equals(kd.getPublicKeyString())) return true;
×
304
        }
×
305
        return false;
×
306
    }
307

308
    /**
309
     * Sets the user's ORCID identifier.
310
     *
311
     * @param orcid The ORCID identifier.
312
     */
313
    public void setOrcid(String orcid) {
314
        if (!orcid.matches(ProfilePage.ORCID_PATTERN)) {
×
315
            throw new RuntimeException("Illegal ORCID identifier: " + orcid);
×
316
        }
317
        if (NanodashPreferences.get().isOrcidLoginMode()) {
×
318
            userDir = System.getProperty("user.home") + "/.nanopub/nanodash-users/" + orcid + "/";
×
319
            File f = new File(userDir);
×
320
            if (!f.exists()) f.mkdir();
×
321
        } else {
×
322
            try {
323
                FileUtils.writeStringToFile(getOrcidFile(), orcid + "\n", StandardCharsets.UTF_8);
×
324
                //                        Files.writeString(orcidFile.toPath(), orcid + "\n");
325
            } catch (IOException ex) {
×
326
                logger.error("Couldn't write ORCID file", ex);
×
327
            }
×
328
        }
329
        userIri = vf.createIRI("https://orcid.org/" + orcid);
×
330
        loadProfileInfo();
×
331
        if (httpSession != null) httpSession.setMaxInactiveInterval(24 * 60 * 60);  // 24h
×
332
    }
×
333

334
    /**
335
     * Logs out the user and invalidates the session.
336
     */
337
    public void logout() {
338
        userIri = null;
×
339
        invalidateNow();
×
340
    }
×
341

342
    /**
343
     * Retrieves the user's introduction nanopublications as a map.
344
     *
345
     * @return A map of introduction nanopublications.
346
     */
347
    public Map<IRI, IntroNanopub> getIntroNanopubs() {
348
        return introNps;
×
349
    }
350

351
//        public void checkOrcidLink() {
352
//                if (isOrcidLinked == null && userIri != null) {
353
//                        orcidLinkError = "";
354
//                        introExtractor = null;
355
//                        try {
356
//                                introExtractor = IntroNanopub.extract(userIri.stringValue(), null);
357
//                                if (introExtractor.getIntroNanopub() == null) {
358
//                                        orcidLinkError = "ORCID account is not linked.";
359
//                                        isOrcidLinked = false;
360
//                                } else {
361
//                                        IntroNanopub inp = IntroNanopub.get(userIri.stringValue(), introExtractor);
362
//                                        if (introNps != null && introNps.containsKey(inp.getNanopub().getUri())) {
363
//                                                // TODO: also check whether introduction contains local key
364
//                                                isOrcidLinked = true;
365
//                                        } else {
366
//                                                isOrcidLinked = false;
367
//                                                orcidLinkError = "Error: ORCID is linked to another introduction nanopublication.";
368
//                                        }
369
//                                }
370
//                        } catch (Exception ex) {
371
//                                logger.error("ORCID check failed");
372
//                                orcidLinkError = "ORCID check failed.";
373
//                        }
374
//                }
375
//        }
376
//
377
//        public void resetOrcidLinked() {
378
//                isOrcidLinked = null;
379
//        }
380
//
381
//        public boolean isOrcidLinked() {
382
//                checkOrcidLink();
383
//                return isOrcidLinked != null && isOrcidLinked == true;
384
//        }
385
//
386
//        public String getOrcidLinkError() {
387
//                return orcidLinkError;
388
//        }
389
//
390
//        public String getOrcidName() {
391
//                if (introExtractor == null || introExtractor.getName() == null) return null;
392
//                if (introExtractor.getName().trim().isEmpty()) return null;
393
//                return introExtractor.getName();
394
//        }
395

396
    /**
397
     * Retrieves the file for storing the user's ORCID identifier.
398
     *
399
     * @return The ORCID file.
400
     */
401
    private File getOrcidFile() {
402
        return new File(userDir + "orcid");
21✔
403
    }
404

405
    /**
406
     * Retrieves the file for storing the user's private key.
407
     *
408
     * @return The key file.
409
     */
410
    public File getKeyFile() {
411
        return new File(userDir + "id_rsa");
×
412
    }
413

414
    /**
415
     * Sets the time when the introduction was last published.
416
     */
417
    public void setIntroPublishedNow() {
418
        lastTimeIntroPublished = new Date();
×
419
    }
×
420

421
    /**
422
     * Checks if the introduction has been published.
423
     *
424
     * @return True if the introduction has been published, false otherwise.
425
     */
426
    public boolean hasIntroPublished() {
427
        return lastTimeIntroPublished != null;
×
428
    }
429

430
    /**
431
     * Calculates the time since the last introduction was published.
432
     *
433
     * @return The time in milliseconds since the last introduction was published, or Long.MAX_VALUE if it has never been published.
434
     */
435
    public long getTimeSinceLastIntroPublished() {
436
        if (lastTimeIntroPublished == null) return Long.MAX_VALUE;
×
437
        return new Date().getTime() - lastTimeIntroPublished.getTime();
×
438
    }
439

440
    /**
441
     * Sets the view mode for displaying nanopublication results.
442
     *
443
     * @param viewMode The desired view mode (e.g., GRID or LIST).
444
     */
445
    public void setNanopubResultsViewMode(NanopubResults.ViewMode viewMode) {
446
        this.nanopubResultsViewMode = viewMode;
×
447
    }
×
448

449
    /**
450
     * Retrieves the current view mode for displaying nanopublication results.
451
     *
452
     * @return The current view mode.
453
     */
454
    public NanopubResults.ViewMode getNanopubResultsViewMode() {
455
        return this.nanopubResultsViewMode;
×
456
    }
457

458
    // --- Preview nanopub support ---
459

460
    public static class PreviewNanopub implements Serializable {
461
        private final Nanopub nanopub;
462
        private final PageParameters pageParams;
463
        private final Class<? extends WebPage> confirmPageClass;
464
        private final boolean consentChecked;
465
        private final PageReference sourcePageRef;
466

467
        public PreviewNanopub(Nanopub nanopub, PageParameters pageParams, Class<? extends WebPage> confirmPageClass, boolean consentChecked, PageReference sourcePageRef) {
×
468
            this.nanopub = nanopub;
×
469
            this.pageParams = pageParams;
×
470
            this.confirmPageClass = confirmPageClass;
×
471
            this.consentChecked = consentChecked;
×
472
            this.sourcePageRef = sourcePageRef;
×
473
        }
×
474

475
        public Nanopub getNanopub() { return nanopub; }
×
476
        public PageParameters getPageParams() { return pageParams; }
×
477
        public Class<? extends WebPage> getConfirmPageClass() { return confirmPageClass; }
×
478
        public boolean isConsentChecked() { return consentChecked; }
×
479
        public PageReference getSourcePageRef() { return sourcePageRef; }
×
480
    }
481

482
    private ConcurrentMap<String, PreviewNanopub> previewMap = new ConcurrentHashMap<>();
15✔
483

484
    public void setPreviewNanopub(String id, PreviewNanopub preview) {
485
        previewMap.put(id, preview);
×
486
    }
×
487

488
    public PreviewNanopub getPreviewNanopub(String id) {
489
        return previewMap.get(id);
×
490
    }
491

492
    public PreviewNanopub removePreviewNanopub(String id) {
493
        return previewMap.remove(id);
×
494
    }
495

496
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc