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

trydofor / professional-mirana / #68

31 Aug 2024 02:56AM UTC coverage: 84.4% (+1.0%) from 83.382%
#68

push

web-flow
Merge pull request #45 from trydofor/develop

v2.7.3 with minor change

474 of 568 new or added lines in 27 files covered. (83.45%)

8 existing lines in 6 files now uncovered.

5237 of 6205 relevant lines covered (84.4%)

0.84 hits per line

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

20.0
/src/main/java/pro/fessional/mirana/tk/Ticket.java
1
package pro.fessional.mirana.tk;
2

3
import org.jetbrains.annotations.NotNull;
4

5
import java.io.Serializable;
6

7
/**
8
 * <pre>
9
 * A short, expireable, kickable, verifiable, with some business meaning, instead of meaningless random token.
10
 * Where `Data` suffix is business semantics, `Part` suffix is transfer semantics, and the layout of different perspectives is.
11
 *
12
 * Business layout: SigData + `~` + SigPart
13
 * - SigData = PubPart + (`~` + BizData)?
14
 * - PubPart = `mod` + `-` + `due` + `-` + `seq`
15
 * - BizData: business data, e.g. plaintext Json
16
 *
17
 * Transfer layout: PubPart + `~` + SecPart
18
 * - SecPart = (BizPart + `~`)? + SigPart
19
 * - BizPart: encrypted BizData
20
 * - SigPart: Signature data, sign the SigData data.
21
 *
22
 * `mod`: convention mode, encryption and signature, BizPart type, etc. for deserialization. English and number
23
 * `due`: expiration date, number of seconds since 1970-01-01, used to determine time expiration. Positive integers
24
 * `seq`: serial number, used to determine old or new, business expiration, positive integer
25
 * `salt`: encryption or signing secret key, such as symmetric secret key, asymmetric private key.
26
 *
27
 * When parsing, the easier to understand steps are,
28
 * (1) Split the Ticket with the 1st `~` into 2 segments: PubData and SecData.
29
 * (2) Split the 1st segment into 3 parts with 2 `-`: PubMod, PubDue, PubSeq.
30
 * (3) Split the 2nd segment into 2 parts with 1 `~`: BizPart, SigPart
31
 * (4) Decrypt BizPart and verify SigData signature with PubMod convention.
32
 *
33
 * </pre>
34
 *
35
 * @author trydofor
36
 * @since 2021-01-24
37
 */
38
public interface Ticket extends Serializable {
39

40
    /**
41
     * default separator of pub
42
     */
43
    char Sep1 = '-';
44

45
    /**
46
     * default separator of parts
47
     */
48
    char Sep2 = '~';
49

50
    /**
51
     * The convention schema, including encryption algorithm, signature method,
52
     * is a convention of the BizPart structure, supported [az09].
53
     */
54
    @NotNull
55
    String getPubMod();
56

57
    /**
58
     * Expiration, in seconds from 1970-01-01, not negative
59
     */
60
    long getPubDue();
61

62
    /**
63
     * Serial number, non-negative, incremental and non-consecutive.
64
     * generally less than 10 with special definitions.
65
     */
66
    int getPubSeq();
67

68
    /**
69
     * Biz-data part, optional(empty means no biz-data), less than 1k.
70
     * Format defined by PubMod. base64 is url-safe, NoPad format, supports [az09-_]
71
     */
72
    @NotNull
73
    String getBizPart();
74

75
    /**
76
     * Signature part, usually within 50 characters, to ensure that the Ticket has not been tampered with.
77
     * Schema defined by PubMod. base64 is url-safe, NoPad format, supports [az09-_]
78
     */
79
    @NotNull
80
    String getSigPart();
81

82
    /**
83
     * Get the signature data, i.e. `pub-part` + (`~ ` + `biz-part`)?
84
     * When parsing the ticket string, cache the biz-data of the original ticket.
85
     *
86
     * @return the signature part
87
     */
88
    @NotNull
89
    default String getSigData() {
90
        final String biz = getBizPart();
1✔
91
        if (biz.isEmpty()) {
1✔
92
            return getPubMod() + Sep1 + getPubDue() + Sep1 + getPubSeq();
1✔
93
        }
94
        else {
95
            return getPubMod() + Sep1 + getPubDue() + Sep1 + getPubSeq() + Sep2 + biz;
1✔
96
        }
97
    }
98

99
    /**
100
     * serialize the Ticket
101
     *
102
     * @return Ticket
103
     */
104
    @NotNull
105
    default String serialize() {
106
        return serialize(Sep1, Sep2);
1✔
107
    }
108

109
    /**
110
     * serialize the Ticket
111
     *
112
     * @return Ticket
113
     */
114
    @NotNull
115
    default String serialize(char sep1, char sep2) {
116
        final String mod = getPubMod();
×
117
        final String sig = getSigPart();
×
118
        if (mod.isEmpty() || sig.isEmpty()) {
×
119
            throw new IllegalStateException("sig-part or pub-mod is empty");
×
120
        }
121
        final String biz = getBizPart();
×
122
        StringBuilder sb = new StringBuilder(biz.length() + 100);
×
123
        sb.append(mod);
×
NEW
124
        sb.append(sep1).append(getPubDue());
×
NEW
125
        sb.append(sep1).append(getPubSeq());
×
126
        if (!biz.isEmpty()) {
×
NEW
127
            sb.append(sep2).append(biz);
×
128
        }
NEW
129
        sb.append(sep2).append(sig);
×
130
        return sb.toString();
×
131
    }
132

133
    interface Mutable extends Ticket {
134
        void setPubMod(String mod);
135

136
        void setPubDue(long exp);
137

138
        void setPubSeq(int seq);
139

140
        void setBizPart(String biz);
141

142
        void setSigPart(String sig);
143

144
        /**
145
         * Copy the contents of part in tk, excluding sigData.
146
         *
147
         * @param tk source ticket
148
         */
149
        default void copyPart(Ticket tk) {
150
            if (tk == null) return;
×
151
            this.setPubMod(tk.getPubMod());
×
152
            this.setPubDue(tk.getPubDue());
×
153
            this.setPubSeq(tk.getPubSeq());
×
154
            this.setBizPart(tk.getBizPart());
×
155
            this.setSigPart(tk.getSigPart());
×
156
        }
×
157
    }
158
}
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