Skip to content

Commit 805483b

Browse files
JAMES-4210 Add IMAP SASL bridge scaffold
Add a minimal IMAP bridge for the shared SASL SPI. The bridge keeps IMAP-specific wire handling outside the generic SPI: it converts IMAP AUTHENTICATE input to SaslInitialRequest, handles the SASL-IR "=" empty initial response marker, base64-encodes challenge continuations, decodes client continuation lines, and wires abort/close lifecycle handling around SaslExchange. Add unit tests for initial response decoding, continuation formatting, client response decoding, and exchange cleanup.
1 parent 2bcd3a9 commit 805483b

2 files changed

Lines changed: 232 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/****************************************************************
2+
* Licensed to the Apache Software Foundation (ASF) under one *
3+
* or more contributor license agreements. See the NOTICE file *
4+
* distributed with this work for additional information *
5+
* regarding copyright ownership. The ASF licenses this file *
6+
* to you under the Apache License, Version 2.0 (the *
7+
* "License"); you may not use this file except in compliance *
8+
* with the License. You may obtain a copy of the License at *
9+
* *
10+
* http://www.apache.org/licenses/LICENSE-2.0 *
11+
* *
12+
* Unless required by applicable law or agreed to in writing, *
13+
* software distributed under the License is distributed on an *
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15+
* KIND, either express or implied. See the License for the *
16+
* specific language governing permissions and limitations *
17+
* under the License. *
18+
****************************************************************/
19+
20+
package org.apache.james.imap.processor.sasl;
21+
22+
import java.nio.charset.StandardCharsets;
23+
import java.util.Base64;
24+
import java.util.Objects;
25+
import java.util.Optional;
26+
27+
import org.apache.james.protocols.api.sasl.SaslExchange;
28+
import org.apache.james.protocols.api.sasl.SaslInitialRequest;
29+
import org.apache.james.protocols.api.sasl.SaslProtocol;
30+
import org.apache.james.protocols.api.sasl.SaslStep;
31+
32+
public class ImapSaslBridge {
33+
/**
34+
* Converts an IMAP AUTHENTICATE request into a protocol-neutral SASL initial request.
35+
*/
36+
public SaslInitialRequest initialRequest(String mechanismName, Optional<String> initialClientResponse) {
37+
return new SaslInitialRequest(SaslProtocol.IMAP, mechanismName,
38+
Objects.requireNonNull(initialClientResponse).map(this::decodeInitialClientResponse));
39+
}
40+
41+
/**
42+
* Encodes a SASL challenge payload as an IMAP continuation payload.
43+
*/
44+
public String continuation(SaslStep.Challenge challenge) {
45+
return challenge.payload()
46+
.map(Base64.getEncoder()::encodeToString)
47+
.orElse("");
48+
}
49+
50+
/**
51+
* Decodes an IMAP client continuation line and forwards it to the SASL exchange.
52+
*/
53+
public SaslStep onClientResponse(SaslExchange exchange, byte[] line) {
54+
return exchange.onResponse(decodeBase64(stripTrailingCrlf(line)));
55+
}
56+
57+
/**
58+
* Aborts and closes an active SASL exchange.
59+
*/
60+
public void abort(SaslExchange exchange) {
61+
exchange.abort();
62+
exchange.close();
63+
}
64+
65+
/**
66+
* Closes an active SASL exchange.
67+
*/
68+
public void close(SaslExchange exchange) {
69+
exchange.close();
70+
}
71+
72+
private byte[] decodeBase64(String value) {
73+
return Base64.getDecoder().decode(value);
74+
}
75+
76+
private byte[] decodeInitialClientResponse(String value) {
77+
if (value.equals("=")) {
78+
return new byte[0];
79+
}
80+
return decodeBase64(value);
81+
}
82+
83+
private String stripTrailingCrlf(byte[] line) {
84+
String value = new String(line, StandardCharsets.US_ASCII);
85+
if (value.endsWith("\r\n")) {
86+
return value.substring(0, value.length() - 2);
87+
}
88+
return value;
89+
}
90+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/****************************************************************
2+
* Licensed to the Apache Software Foundation (ASF) under one *
3+
* or more contributor license agreements. See the NOTICE file *
4+
* distributed with this work for additional information *
5+
* regarding copyright ownership. The ASF licenses this file *
6+
* to you under the Apache License, Version 2.0 (the *
7+
* "License"); you may not use this file except in compliance *
8+
* with the License. You may obtain a copy of the License at *
9+
* *
10+
* http://www.apache.org/licenses/LICENSE-2.0 *
11+
* *
12+
* Unless required by applicable law or agreed to in writing, *
13+
* software distributed under the License is distributed on an *
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15+
* KIND, either express or implied. See the License for the *
16+
* specific language governing permissions and limitations *
17+
* under the License. *
18+
****************************************************************/
19+
20+
package org.apache.james.imap.processor.sasl;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
import java.nio.charset.StandardCharsets;
25+
import java.util.ArrayList;
26+
import java.util.Base64;
27+
import java.util.List;
28+
import java.util.Optional;
29+
30+
import org.apache.james.core.Username;
31+
import org.apache.james.protocols.api.sasl.SaslExchange;
32+
import org.apache.james.protocols.api.sasl.SaslIdentity;
33+
import org.apache.james.protocols.api.sasl.SaslInitialRequest;
34+
import org.apache.james.protocols.api.sasl.SaslProtocol;
35+
import org.apache.james.protocols.api.sasl.SaslStep;
36+
import org.junit.jupiter.api.Test;
37+
38+
class ImapSaslBridgeTest {
39+
private static final Username USER = Username.of("user@example.com");
40+
private static final SaslIdentity IDENTITY = new SaslIdentity(USER, USER);
41+
42+
private final ImapSaslBridge testee = new ImapSaslBridge();
43+
44+
private static class RecordingExchange implements SaslExchange {
45+
private final List<String> lifecycleEvents;
46+
private byte[] lastClientResponse;
47+
48+
private RecordingExchange() {
49+
this.lifecycleEvents = new ArrayList<>();
50+
}
51+
52+
@Override
53+
public SaslStep firstStep() {
54+
return new SaslStep.Challenge(Optional.of(bytes("challenge")));
55+
}
56+
57+
@Override
58+
public SaslStep onResponse(byte[] clientResponse) {
59+
lastClientResponse = clientResponse.clone();
60+
return new SaslStep.Success(IDENTITY, Optional.empty(), "success");
61+
}
62+
63+
@Override
64+
public void abort() {
65+
lifecycleEvents.add("abort");
66+
}
67+
68+
@Override
69+
public void close() {
70+
lifecycleEvents.add("close");
71+
}
72+
}
73+
74+
@Test
75+
void initialRequestShouldDecodeInitialClientResponse() {
76+
String encodedInitialResponse = Base64.getEncoder().encodeToString(bytes("initial"));
77+
78+
SaslInitialRequest request = testee.initialRequest("PLAIN", Optional.of(encodedInitialResponse));
79+
80+
assertThat(request.protocol()).isEqualTo(SaslProtocol.IMAP);
81+
assertThat(request.mechanismName()).isEqualTo("PLAIN");
82+
assertThat(request.initialResponse()).hasValueSatisfying(value -> assertThat(value).containsExactly(bytes("initial")));
83+
}
84+
85+
@Test
86+
void initialRequestShouldDecodeEqualSignAsEmptyInitialClientResponse() {
87+
SaslInitialRequest request = testee.initialRequest("PLAIN", Optional.of("="));
88+
89+
assertThat(request.initialResponse()).hasValueSatisfying(value -> assertThat(value).isEmpty());
90+
}
91+
92+
@Test
93+
void continuationShouldBase64EncodeChallengePayload() {
94+
SaslStep.Challenge challenge = new SaslStep.Challenge(Optional.of(bytes("challenge")));
95+
96+
String continuation = testee.continuation(challenge);
97+
98+
assertThat(continuation).isEqualTo(Base64.getEncoder().encodeToString(bytes("challenge")));
99+
}
100+
101+
@Test
102+
void continuationShouldReturnEmptyStringWhenChallengeHasNoPayload() {
103+
SaslStep.Challenge challenge = new SaslStep.Challenge(Optional.empty());
104+
105+
String continuation = testee.continuation(challenge);
106+
107+
assertThat(continuation).isEmpty();
108+
}
109+
110+
@Test
111+
void onClientResponseShouldDecodeLineAndContinueExchange() {
112+
RecordingExchange exchange = new RecordingExchange();
113+
byte[] line = (Base64.getEncoder().encodeToString(bytes("response")) + "\r\n").getBytes(StandardCharsets.US_ASCII);
114+
115+
SaslStep step = testee.onClientResponse(exchange, line);
116+
117+
assertThat(((SaslStep.Success) step).identity()).isEqualTo(IDENTITY);
118+
assertThat(exchange.lastClientResponse).containsExactly(bytes("response"));
119+
}
120+
121+
@Test
122+
void abortShouldAbortThenCloseExchange() {
123+
RecordingExchange exchange = new RecordingExchange();
124+
125+
testee.abort(exchange);
126+
127+
assertThat(exchange.lifecycleEvents).containsExactly("abort", "close");
128+
}
129+
130+
@Test
131+
void closeShouldCloseExchange() {
132+
RecordingExchange exchange = new RecordingExchange();
133+
134+
testee.close(exchange);
135+
136+
assertThat(exchange.lifecycleEvents).containsExactly("close");
137+
}
138+
139+
private static byte[] bytes(String value) {
140+
return value.getBytes(StandardCharsets.UTF_8);
141+
}
142+
}

0 commit comments

Comments
 (0)