刘光辉
119 分钟以前 06f314545ac3514d7ce28939920866ae8188d31c
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package jnpf.elnService.template;
 
import jnpf.elnEntity.template.ElnTemplateVersionEntity;
import jnpf.util.JsonUtil;
import org.junit.Test;
 
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
 
public class TemplateAnnotationAuthorizerTest {
 
    @Test
    public void successfulAuthorizationKeepsTenantUntilOuterExecutionCompletes() {
        List<String> events = new ArrayList<>();
        RecordingTenantRoute tenants = new RecordingTenantRoute(events);
        TemplateAnnotationAuthorizer authorizer = authorizer(validPayload("tenant-a", "template-1", "file-1"),
                template("tenant-a", "template-1", "file-1", "DRAFT", "enabled"), true, tenants, events);
 
        TemplateAnnotationAuthorization result = authorizer.authorize("ticket-1", "template-1");
 
        assertEquals(Arrays.asList("access:tenant-a:user-1:template-1:file-1",
                "switch:tenant-a", "lookup:tenant-a:template-1"), events);
        assertEquals("tenant-a", tenants.currentTenant);
        assertEquals("tenant-a", result.getTenantId());
        assertEquals("user-1", result.getUserId());
        assertEquals("template-1", result.getTemplateId());
        assertEquals("file-1", result.getFileId());
        for (Field field : TemplateAnnotationAuthorization.class.getDeclaredFields()) {
            if (!field.isSynthetic()) {
                assertTrue(Modifier.isFinal(field.getModifiers()));
            }
        }
 
        events.add("service:" + tenants.currentTenant);
        tenants.clear();
        assertEquals(Arrays.asList("access:tenant-a:user-1:template-1:file-1",
                "switch:tenant-a", "lookup:tenant-a:template-1", "service:tenant-a", "clear"), events);
        assertNull(tenants.currentTenant);
    }
 
    @Test
    public void requestCannotOverrideSignedTenantAndSequentialTenantsDoNotLeak() {
        List<String> events = new ArrayList<>();
        RecordingTenantRoute tenants = new RecordingTenantRoute(events);
        AtomicReference<String> routedTenant = new AtomicReference<>();
        TemplateAnnotationTicketService tickets = new TemplateAnnotationTicketService(ticket ->
                JsonUtil.getObjectToString(validPayload(ticket, "template-1", "file-1")));
        TemplateAnnotationAuthorizer authorizer = new TemplateAnnotationAuthorizer(tickets,
                templateId -> {
                    routedTenant.set(tenants.currentTenant);
                    return template(tenants.currentTenant, templateId, "file-1", "DRAFT", "enabled");
                }, (tenantId, userId, templateId, fileId) -> true, tenants);
 
        authorizer.authorize("tenant-a", "template-1");
        assertEquals("tenant-a", routedTenant.get());
        assertEquals("tenant-a", tenants.currentTenant);
        tenants.clear();
        assertNull(tenants.currentTenant);
        authorizer.authorize("tenant-b", "template-1");
        assertEquals("tenant-b", routedTenant.get());
        assertEquals("tenant-b", tenants.currentTenant);
        tenants.clear();
        assertNull(tenants.currentTenant);
        assertEquals(Arrays.asList("tenant-a", "tenant-b"), tenants.switchedTenants);
        assertEquals(2, tenants.clearCount);
    }
 
    @Test
    public void tenantSwitchFailureDoesNotQueryTemplateAndStillClears() {
        List<String> events = new ArrayList<>();
        RecordingTenantRoute tenants = new RecordingTenantRoute(events);
        tenants.failSwitch = true;
        AtomicInteger queries = new AtomicInteger();
        TemplateAnnotationAuthorizer authorizer = new TemplateAnnotationAuthorizer(ticketService(validPayload()),
                templateId -> {
                    queries.incrementAndGet();
                    return template("tenant-a", templateId, "file-1", "DRAFT", "enabled");
                }, (tenantId, userId, templateId, fileId) -> true, tenants);
 
        assertStatus(401, () -> authorizer.authorize("ticket-1", "template-1"));
 
        assertEquals(0, queries.get());
        assertEquals(Arrays.asList("switch:tenant-a", "clear"), events);
        assertNull(tenants.currentTenant);
    }
 
    @Test
    public void templateAndFileMustMatchSignedTicket() {
        RecordingTenantRoute tenants = new RecordingTenantRoute(new ArrayList<>());
        TemplateAnnotationAuthorizer authorizer = authorizer(validPayload(),
                template("tenant-a", "template-1", "other-file", "DRAFT", "enabled"), true, tenants,
                new ArrayList<>());
 
        assertStatus(401, () -> authorizer.authorize("ticket-1", "template-2"));
        assertStatus(401, () -> authorizer.authorize("ticket-1", "template-1"));
        assertNull(tenants.currentTenant);
    }
 
    @Test
    public void attachmentMustContainExactlyOneDocx() {
        RecordingTenantRoute tenants = new RecordingTenantRoute(new ArrayList<>());
        ElnTemplateVersionEntity template = template("tenant-a", "template-1", "file-1", "DRAFT", "enabled");
        template.setMubanFile("[{\"fileId\":\"file-1\",\"fileExtension\":\"docx\"},"
                + "{\"fileId\":\"file-2\",\"fileExtension\":\"DOCX\"}]");
        TemplateAnnotationAuthorizer authorizer = authorizer(validPayload(), template, true, tenants,
                new ArrayList<>());
 
        assertStatus(409, () -> authorizer.authorize("ticket-1", "template-1"));
        assertNull(tenants.currentTenant);
    }
 
    @Test
    public void templateMustRemainDraftAndEnabled() {
        RecordingTenantRoute tenants = new RecordingTenantRoute(new ArrayList<>());
        assertStatus(409, () -> authorizer(validPayload(),
                template("tenant-a", "template-1", "file-1", "PUBLISHED", "enabled"), true, tenants,
                new ArrayList<>()).authorize("ticket-1", "template-1"));
        assertStatus(409, () -> authorizer(validPayload(),
                template("tenant-a", "template-1", "file-1", "DRAFT", "disabled"), true, tenants,
                new ArrayList<>()).authorize("ticket-1", "template-1"));
        assertNull(tenants.currentTenant);
    }
 
    @Test
    public void deletedTemplateCannotBeAnnotated() {
        RecordingTenantRoute tenants = new RecordingTenantRoute(new ArrayList<>());
        ElnTemplateVersionEntity template = template("tenant-a", "template-1", "file-1", "DRAFT", "enabled");
        template.setFDeleteMark(1);
 
        assertStatus(409, () -> authorizer(validPayload(), template, true, tenants,
                new ArrayList<>()).authorize("ticket-1", "template-1"));
        assertNull(tenants.currentTenant);
    }
 
    @Test
    public void revokedAnnotationAccessIsForbidden() {
        RecordingTenantRoute tenants = new RecordingTenantRoute(new ArrayList<>());
        ElnTemplateVersionEntity template = template("tenant-a", "template-1", "file-1", "DRAFT", "enabled");
        assertStatus(403, () -> authorizer(validPayload(), template, false, tenants,
                new ArrayList<>()).authorize("ticket-1", "template-1"));
        assertNull(tenants.currentTenant);
    }
 
    @Test
    public void malformedTicketAlsoClearsExistingTenantContext() {
        List<String> events = new ArrayList<>();
        RecordingTenantRoute tenants = new RecordingTenantRoute(events);
        tenants.currentTenant = "stale-tenant";
        TemplateAnnotationAuthorizer authorizer = new TemplateAnnotationAuthorizer(
                new TemplateAnnotationTicketService(ticket -> null), templateId -> null,
                (tenantId, userId, templateId, fileId) -> true, tenants);
 
        assertStatus(401, () -> authorizer.authorize("expired", "template-1"));
 
        assertNull(tenants.currentTenant);
        assertEquals(Arrays.asList("clear"), events);
    }
 
    private TemplateAnnotationAuthorizer authorizer(Map<String, String> payload,
                                                     ElnTemplateVersionEntity template,
                                                     boolean allowed,
                                                     RecordingTenantRoute tenants,
                                                     List<String> events) {
        return new TemplateAnnotationAuthorizer(ticketService(payload), templateId -> {
            events.add("lookup:" + tenants.currentTenant + ":" + templateId);
            return template;
        }, (tenantId, userId, templateId, fileId) -> {
            events.add("access:" + tenantId + ":" + userId + ":" + templateId + ":" + fileId);
            return allowed;
        }, tenants);
    }
 
    private TemplateAnnotationTicketService ticketService(Map<String, String> payload) {
        return new TemplateAnnotationTicketService(ticket -> JsonUtil.getObjectToString(payload));
    }
 
    private ElnTemplateVersionEntity template(String tenantId, String templateId, String fileId,
                                               String status, String enabled) {
        ElnTemplateVersionEntity template = new ElnTemplateVersionEntity();
        template.setFTenantId(tenantId);
        template.setId(templateId);
        template.setMubanFile("[{\"fileId\":\"" + fileId + "\",\"fileExtension\":\"docx\"}]");
        template.setBizStatus(status);
        template.setBizEnabled(enabled);
        return template;
    }
 
    private Map<String, String> validPayload() {
        return validPayload("tenant-a", "template-1", "file-1");
    }
 
    private Map<String, String> validPayload(String tenantId, String templateId, String fileId) {
        Map<String, String> payload = new LinkedHashMap<>();
        payload.put("scope", "onlyoffice:eln-template-annotate");
        payload.put("tenantId", tenantId);
        payload.put("userId", "user-1");
        payload.put("fileId", fileId);
        payload.put("bizDataId", templateId);
        payload.put("bizModule", "eln.template");
        payload.put("bizScene", "eln.template.annotate");
        payload.put("pluginGuid", "asc.{2D6D6CB6-F6FC-45B7-8EC7-0CC1940F1A6F}");
        return payload;
    }
 
    private void assertStatus(int expected, ThrowingRunnable operation) {
        try {
            operation.run();
            fail("expected authorization failure");
        } catch (TemplateAnnotationTicketService.InvalidAnnotationTicketException e) {
            assertEquals(expected, e.getStatus());
        } catch (TemplateAnnotationAuthorizer.AnnotationAuthorizationException e) {
            assertEquals(expected, e.getStatus());
            assertFalse(e.getMessage().contains("module-template"));
            assertFalse(e.getMessage().contains("tenant-a"));
        }
    }
 
    private interface ThrowingRunnable {
        void run();
    }
 
    private static class RecordingTenantRoute implements TemplateAnnotationAuthorizer.TenantRoute {
        private final List<String> events;
        private final List<String> switchedTenants = new ArrayList<>();
        private String currentTenant;
        private boolean failSwitch;
        private int clearCount;
 
        private RecordingTenantRoute(List<String> events) {
            this.events = events;
        }
 
        @Override
        public void switchTo(String tenantId) {
            events.add("switch:" + tenantId);
            switchedTenants.add(tenantId);
            if (failSwitch) {
                throw new IllegalStateException("switch failed");
            }
            currentTenant = tenantId;
        }
 
        @Override
        public void clear() {
            events.add("clear");
            clearCount++;
            currentTenant = null;
        }
    }
}