Skip to content

Commit e6602b8

Browse files
committed
add AuthType strategy
1 parent 68c37b7 commit e6602b8

File tree

5 files changed

+70
-29
lines changed

5 files changed

+70
-29
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>uk.guzek</groupId>
88
<artifactId>stomp-auth-client</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.1.0</version>
1010
<packaging>jar</packaging>
1111
<name>${project.groupId}:${project.artifactId}</name>
1212
<description>

src/main/java/uk/guzek/sac/AuthType.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020

2121
import java.util.Map;
2222

23-
public class AuthType {
24-
/**
25-
* Obtains the appropriate headers for authentication made using JSON Web Tokens
26-
* (JWTs).
27-
*
28-
* @param token the JWT string
29-
* @return a header map containing the formatted `Authorization` header
30-
*/
31-
public static Map<String, String> jwt(String token) {
32-
return Map.of("Authorization", "Bearer " + token);
33-
}
23+
public interface AuthType {
24+
Map<String, String> getHeaders();
3425
}

src/main/java/uk/guzek/sac/StompClient.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/**
22
* STOMP Auth Client - A simple STOMP client for Java with authentication support
33
* Copyright © 2024 by Konrad Guzek <konrad@guzek.uk>
4-
4+
* <p>
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
77
* the Free Software Foundation, either version 3 of the License, or
88
* (at your option) any later version.
9-
9+
* <p>
1010
* This program is distributed in the hope that it will be useful,
1111
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1212
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313
* GNU General Public License for more details.
14-
14+
* <p>
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
@@ -41,7 +41,7 @@ public abstract class StompClient extends WebSocketClient {
4141

4242
/**
4343
* Initialises the client.
44-
*
44+
*
4545
* @param serverUri the uri of the server
4646
* @param host the host to use to send the CONNECT STOMP frame
4747
*/
@@ -51,7 +51,7 @@ public StompClient(URI serverUri, String host) {
5151

5252
/**
5353
* Initialises the client.
54-
*
54+
*
5555
* @param serverUri the URI of the STOMP server
5656
* @param headers optional key-value header pairs to use during the initial
5757
* HTTP Upgrade request
@@ -63,7 +63,34 @@ public StompClient(URI serverUri, Map<String, String> headers, String host) {
6363

6464
/**
6565
* Initialises the client.
66-
*
66+
*
67+
* @param serverUri the URI of the STOMP server
68+
* @param authType optional authentication method from `uk.guzek.sac.auth.*` to use during the initial
69+
* HTTP Upgrade request
70+
* @param host the host to use to send the CONNECT STOMP frame
71+
*/
72+
public StompClient(URI serverUri, AuthType authType, String host) {
73+
this(serverUri, authType, host, 10);
74+
}
75+
76+
/**
77+
* Initialises the client.
78+
*
79+
* @param serverUri the URI of the STOMP server
80+
* @param authType optional authentication method from `uk.guzek.sac.auth.*` to use during the initial
81+
* HTTP Upgrade request
82+
* @param host the host to use to send the CONNECT STOMP frame
83+
* @param timeout optional length of time to allow for lost messages to be
84+
* sent when the client is closed, in
85+
* seconds; defaults to 10
86+
*/
87+
public StompClient(URI serverUri, AuthType authType, String host, int timeout) {
88+
this(serverUri, authType.getHeaders(), host, timeout);
89+
}
90+
91+
/**
92+
* Initialises the client.
93+
*
6794
* @param serverUri the URI of the STOMP server
6895
* @param headers optional key-value header pairs to use during the initial
6996
* HTTP Upgrade request
@@ -79,8 +106,7 @@ public StompClient(URI serverUri, Map<String, String> headers, String host, int
79106
}
80107

81108
private void executeMessageQueue() {
82-
if (!connected)
83-
return;
109+
if (!connected) return;
84110
for (Runnable runnable : messageQueue) {
85111
runnable.run();
86112
}
@@ -136,7 +162,7 @@ private int sendDisconnectFrame() {
136162

137163
/**
138164
* Send a SEND frame which is not JSON or plaintext.
139-
*
165+
*
140166
* @param message the payload to send
141167
* @param destination the path to send the message to
142168
* @param contentType the MIME content type of the payload
@@ -147,7 +173,7 @@ public void sendSendFrame(String message, String destination, String contentType
147173

148174
/**
149175
* Send a plaintext SEND frame.
150-
*
176+
*
151177
* @param message the plaintext payload to send
152178
* @param destination the path to send the message to
153179
*/
@@ -157,7 +183,7 @@ public void sendText(String message, String destination) {
157183

158184
/**
159185
* Send a JSON SEND frame.
160-
*
186+
*
161187
* @param object the object payload to be serialised as JSON and sent
162188
* @param destination the path to send the message to
163189
* @throws JsonProcessingException passed on from Jackson's `writeValueAsString`
@@ -169,7 +195,7 @@ public void sendJson(Object object, String destination) throws JsonProcessingExc
169195

170196
/**
171197
* Calls `handler` whenever the server sends MESSAGE frames to `destination`.
172-
*
198+
*
173199
* @param destination the path of the resource to subscribe to
174200
* @param handler a biconsumable which takes a string-string header map and a body string`
175201
* @return the id of the subscription (incremental)
@@ -232,7 +258,7 @@ public void onConnected() {
232258
* Called for each frame received by the client.
233259
* Use this to handle other frames sent by the server which are not MESSAGE
234260
* frames sent to subscriptions.
235-
*
261+
*
236262
* @param frame the frame type
237263
* @param headers a key-value pair of frame headers
238264
* @param body the frame body (can be an empty string, not null)
@@ -243,7 +269,7 @@ public void onConnected() {
243269
* Called for each message received by the client. For STOMP messages, use
244270
* {@link #onStompFrame}.
245271
* Do not override this method unless you know what you are doing.
246-
*
272+
*
247273
* @param message the raw message sent by the server
248274
*/
249275
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package uk.guzek.sac.auth;
2+
3+
import uk.guzek.sac.AuthType;
4+
5+
import java.util.Map;
6+
7+
public class JwtAuth implements AuthType {
8+
private final String token;
9+
10+
/**
11+
* Initialises a JSON Web Token authentication method.
12+
*
13+
* @param token the JWT string
14+
*/
15+
public JwtAuth(String token) {
16+
this.token = token;
17+
}
18+
19+
@Override
20+
public Map<String, String> getHeaders() {
21+
return Map.of("Authorization", "Bearer " + token);
22+
}
23+
}

src/main/java/uk/guzek/sac/examples/ExampleClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@
2222
import org.slf4j.LoggerFactory;
2323
import uk.guzek.sac.AuthType;
2424
import uk.guzek.sac.StompClient;
25+
import uk.guzek.sac.auth.JwtAuth;
2526

2627
import java.net.URI;
2728
import java.util.Map;
2829

2930
public class ExampleClient extends StompClient {
3031
Logger logger = LoggerFactory.getLogger(ExampleClient.class);
3132

32-
public ExampleClient(URI serverUri, Map<String, String> headers, String host) {
33-
super(serverUri, headers, host);
33+
public ExampleClient(URI serverUri, AuthType authType, String host) {
34+
super(serverUri, authType, host);
3435
}
3536

3637
@Override
@@ -59,7 +60,7 @@ public static void main(String[] args) {
5960
String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJrZ3V6ZWsiLCJpYXQiOjE3MzIwMjc1MjksImV4cCI6MTczMjA3MDcyOX0.1z_pqhLQA7USpGmBEyjBK6ImHBahGp4-yNn5ovg1Xow";
6061
// this isn't really necessary, except maybe with strict CORS on your server?
6162
String host = "test.example.com";
62-
ExampleClient client = new ExampleClient(URI.create(serverUri), AuthType.jwt(token), host);
63+
StompClient client = new ExampleClient(URI.create(serverUri), new JwtAuth(token), host);
6364
// remember to call `.connect()` after initialising the client
6465
client.connect();
6566
client.subscribe("/topic/greetings",

0 commit comments

Comments
 (0)