Compare commits
1 commit
e5cf6d291a
...
616b1da8af
Author | SHA1 | Date | |
---|---|---|---|
616b1da8af |
7 changed files with 9 additions and 43 deletions
|
@ -41,7 +41,7 @@ public abstract class JsonWebsocketHandler extends TextWebSocketHandler {
|
||||||
try{
|
try{
|
||||||
session.close(reason);
|
session.close(reason);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
logger.info("Unable to Close the Session", exception);
|
logger.debug("Unable to Close the Session", exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,11 @@ public class ConnectionWebsocketHandler extends JsonWebsocketHandler {
|
||||||
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
|
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
|
||||||
try{
|
try{
|
||||||
String payload = message.getPayload();
|
String payload = message.getPayload();
|
||||||
switch ( objectMapper.readTree(payload).get("$id").asText().toLowerCase()) {
|
switch ( objectMapper.readTree(payload).get("$id").asText()) {
|
||||||
case "requestconnectiontoken" -> handleRequestConnectionToken(session, payload);
|
case "RequestConnectionToken" -> handleRequestConnectionToken(session, payload);
|
||||||
default -> this.closeSession(session, CloseStatus.BAD_DATA);
|
default -> this.closeSession(session, CloseStatus.BAD_DATA);
|
||||||
}
|
}
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
System.out.println(exception.getMessage());
|
|
||||||
this.closeSession(session, CloseStatus.BAD_DATA);
|
this.closeSession(session, CloseStatus.BAD_DATA);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,6 +35,6 @@ public class ConnectionWebsocketHandler extends JsonWebsocketHandler {
|
||||||
RequestConnectionTokenMessage msg = objectMapper.readValue(payload, RequestConnectionTokenMessage.class);
|
RequestConnectionTokenMessage msg = objectMapper.readValue(payload, RequestConnectionTokenMessage.class);
|
||||||
Player player = this.sessionPlayers.get(session);
|
Player player = this.sessionPlayers.get(session);
|
||||||
String jwt = this.sessionsService.createSession(player, msg.getChannel());
|
String jwt = this.sessionsService.createSession(player, msg.getChannel());
|
||||||
new ProvidedConnectionTokenMessage(msg.getChannel(), jwt).send(session);
|
new ProvidedConnectionTokenMessage(channel, jwt).send(session);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package de.towerdefence.server.server.channels.connection.in;
|
package de.towerdefence.server.server.channels.connection.in;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import de.towerdefence.server.session.Channel;
|
import de.towerdefence.server.session.Channel;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
@ -8,7 +7,6 @@ import lombok.*;
|
||||||
@Data
|
@Data
|
||||||
@NotNull
|
@NotNull
|
||||||
public class RequestConnectionTokenMessage {
|
public class RequestConnectionTokenMessage {
|
||||||
@JsonProperty("$id")
|
private String $id;
|
||||||
private String messageId;
|
|
||||||
private Channel channel;
|
private Channel channel;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,21 +21,20 @@ public class JwtService {
|
||||||
this.secretKey = Keys.hmacShaKeyFor(config.getSecret().getBytes(StandardCharsets.UTF_8));
|
this.secretKey = Keys.hmacShaKeyFor(config.getSecret().getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String generateToken(String username, Channel channel, long ttl) {
|
public String generateToken(String username, long ttl) {
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
Date issueDate = new Date(now);
|
Date issueDate = new Date(now);
|
||||||
Date expirationDate = new Date(now + ttl * 1000);
|
Date expirationDate = new Date(now + ttl * 1000);
|
||||||
|
|
||||||
return Jwts.builder()
|
return Jwts.builder()
|
||||||
.subject(username)
|
.subject(username)
|
||||||
.claim("channel", channel.getJsonName())
|
|
||||||
.issuedAt(issueDate)
|
.issuedAt(issueDate)
|
||||||
.expiration(expirationDate)
|
.expiration(expirationDate)
|
||||||
.signWith(secretKey)
|
.signWith(secretKey)
|
||||||
.compact();
|
.compact();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<String> verifyToken(String token, Channel channel) {
|
public Optional<String> verifyToken(String token) {
|
||||||
Claims claims = Jwts.parser()
|
Claims claims = Jwts.parser()
|
||||||
.verifyWith(secretKey)
|
.verifyWith(secretKey)
|
||||||
.build()
|
.build()
|
||||||
|
@ -46,16 +45,6 @@ public class JwtService {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
Channel tokenChannel;
|
|
||||||
try {
|
|
||||||
tokenChannel = Channel.fromJsonName(claims.get("channel", String.class));
|
|
||||||
} catch (IllegalArgumentException ignored) {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
if(!channel.equals(tokenChannel)) {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Optional.of(claims.getSubject());
|
return Optional.of(claims.getSubject());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class SessionsService {
|
||||||
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||||
|
|
||||||
public String createSession(Player player, Channel channel){
|
public String createSession(Player player, Channel channel){
|
||||||
String jwt = jwtService.generateToken(player.getUsername(), channel, TIME_TO_LIVE_SECONDS);
|
String jwt = jwtService.generateToken(player.getUsername(), TIME_TO_LIVE_SECONDS);
|
||||||
if(tokenGrants.containsKey(jwt)){
|
if(tokenGrants.containsKey(jwt)){
|
||||||
throw new IllegalStateException("The exact same JWT allready exists");
|
throw new IllegalStateException("The exact same JWT allready exists");
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ public class SessionsService {
|
||||||
if (grantedChannel == null || !grantedChannel.equals(channel)) {
|
if (grantedChannel == null || !grantedChannel.equals(channel)) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
Optional<String> username = jwtService.verifyToken(jwt, channel);
|
Optional<String> username = jwtService.verifyToken(jwt);
|
||||||
if (username.isEmpty()) {
|
if (username.isEmpty()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
response=$(curl -s -X 'POST' \
|
|
||||||
'http://localhost:8080/api/v1/player/login' \
|
|
||||||
-H 'accept: application/json' \
|
|
||||||
-H 'Content-Type: application/json' \
|
|
||||||
-d '{
|
|
||||||
"username": "Player1",
|
|
||||||
"password": "1234"
|
|
||||||
}')
|
|
||||||
|
|
||||||
token=$(echo "$response" | jq -r .token)
|
|
||||||
payload='{"$id": "RequestConnectionToken", "channel": "time"}'
|
|
||||||
response=$(echo "$payload" | websocat ws://localhost:8080/ws/connection -H "Authorization: $token")
|
|
||||||
|
|
||||||
time_token=$(echo "$response" | jq -r .token)
|
|
||||||
websocat ws://localhost:8080/ws/time -H "Authorization: $time_token"
|
|
|
@ -33,7 +33,6 @@ channels:
|
||||||
Specific Channel
|
Specific Channel
|
||||||
payload:
|
payload:
|
||||||
type: object
|
type: object
|
||||||
additionalProperties: false
|
|
||||||
properties:
|
properties:
|
||||||
$id:
|
$id:
|
||||||
type: string
|
type: string
|
||||||
|
@ -52,7 +51,6 @@ channels:
|
||||||
Specific Channel
|
Specific Channel
|
||||||
payload:
|
payload:
|
||||||
type: object
|
type: object
|
||||||
additionalProperties: false
|
|
||||||
properties:
|
properties:
|
||||||
$id:
|
$id:
|
||||||
type: string
|
type: string
|
||||||
|
@ -77,7 +75,6 @@ channels:
|
||||||
description: The Current time in Unix Time
|
description: The Current time in Unix Time
|
||||||
payload:
|
payload:
|
||||||
type: object
|
type: object
|
||||||
additionalProperties: false
|
|
||||||
properties:
|
properties:
|
||||||
$id:
|
$id:
|
||||||
type: string
|
type: string
|
||||||
|
|
Loading…
Add table
Reference in a new issue