Skip to content

Commit

Permalink
Raid Stats command without authentication | Phase 1 (#65)
Browse files Browse the repository at this point in the history
* First commit

* This new branch will be the one with the actual changes

* Finished setting up the raid-stats command

* Finished first phase for getting raid stats without authentication

---------

Co-authored-by: Daniel Villavicencio <danielvillavicencio@Daniels-MBP.attlocal.net>
  • Loading branch information
dvillavicencio and Daniel Villavicencio authored Feb 19, 2024
1 parent 5952b9e commit 63c043c
Show file tree
Hide file tree
Showing 22 changed files with 77 additions and 935 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@EnableCaching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

@Data
@Configuration
@ConfigurationProperties(prefix = "bungie.api")
public class BungieConfiguration implements OAuth2Configuration {
public class BungieConfiguration {

/**
* The name of the Bungie API key header
Expand Down Expand Up @@ -63,6 +61,12 @@ public class BungieConfiguration implements OAuth2Configuration {
*/
private String callbackUrl;

/**
* Default bungie client used to make general API calls to Bungie.net
*
* @param builder The default WebClient.Builder defined in the main application
* @return {@link BungieClient}
*/
@Bean("defaultBungieClient")
public BungieClient bungieCharacterClient(WebClient.Builder builder) {
var webClient = builder
Expand All @@ -77,6 +81,12 @@ public BungieClient bungieCharacterClient(WebClient.Builder builder) {
.createClient(BungieClient.class);
}

/**
* Bungie client used to make API calls to the stats.bungie.net domain
*
* @param builder The default WebClient.Builder defined in the main application
* @return {@link BungieClient}
*/
@Bean(name = "pgcrBungieClient")
public BungieClient pgcrBungieClient(WebClient.Builder builder) {
var webClient = builder
Expand All @@ -85,7 +95,7 @@ public BungieClient pgcrBungieClient(WebClient.Builder builder) {
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.codecs(clientCodecConfigurer -> clientCodecConfigurer.defaultCodecs()
.maxInMemorySize(1024 * 1024 * 15))
.maxInMemorySize(1024 * 1024 * 5))
.build();
return HttpServiceProxyFactory.builder()
.exchangeAdapter(WebClientAdapter.create(webClient))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Data
@Configuration
@ConfigurationProperties(prefix = "discord.api")
public class DiscordConfiguration implements OAuth2Configuration {
public class DiscordConfiguration {

/**
* Base Url for Discord API calls
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public enum SlashCommand {
WEEKLY_DUNGEON("weekly_dungeon", false),
WEEKLY_RAID("weekly_raid", false),
RAID_STATS("raid_stats", true),
RAID_MAP("raid_map", false),
EXPERIMENTAL_RAID_STATS("experimental_raid_stats", false);
RAID_MAP("raid_map", false);

@Getter
private final String commandName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ApplicationCommandFactory(
SlashCommand.WEEKLY_DUNGEON, weeklyDungeonHandler,
SlashCommand.AUTHORIZE, authorizeMessageHandler,
SlashCommand.RAID_MAP, raidMapHandler,
SlashCommand.EXPERIMENTAL_RAID_STATS, raidStatsHandler);
SlashCommand.RAID_STATS, raidStatsHandler);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public AutocompleteFactory(
RaidStatsHandler raidStatsHandler) {
this.autocompleteFactory = Map.of(
SlashCommand.RAID_MAP, raidMapHandler,
SlashCommand.EXPERIMENTAL_RAID_STATS, raidStatsHandler);
SlashCommand.RAID_STATS, raidStatsHandler);
}

@Override
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.danielvm.destiny2bot.filter;

import com.danielvm.destiny2bot.config.DiscordConfiguration;
import com.danielvm.destiny2bot.exception.InvalidSignatureException;
import com.danielvm.destiny2bot.util.CryptoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.stereotype.Component;
Expand All @@ -21,23 +21,21 @@
@Component
@Order(1)
@Slf4j
public class SignatureFilter implements WebFilter {
public class ValidSignatureWebFilter implements WebFilter {

private static final String SIGNATURE_HEADER_NAME = "X-Signature-Ed25519";
private static final String TIMESTAMP_HEADER_NAME = "X-Signature-Timestamp";

private final DiscordConfiguration discordConfiguration;

public SignatureFilter(DiscordConfiguration discordConfiguration) {
public ValidSignatureWebFilter(DiscordConfiguration discordConfiguration) {
this.discordConfiguration = discordConfiguration;
}

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
Flux<DataBuffer> body = exchange.getRequest().getBody();

return DataBufferUtils.join(body)
return DataBufferUtils.join(exchange.getRequest().getBody())
.map(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
Expand All @@ -58,7 +56,12 @@ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
log.error(
"There was a request with invalid signature. Signature: [{}], Timestamp: [{}]",
signature, timestamp);
return Mono.error(new InvalidSignatureException("The signature passed in was invalid"));
String errorMessage = "The signature passed in was invalid. Timestamp: [%s], Signature [%s]"
.formatted(timestamp, signature);
exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
DataBuffer buffer = exchange.getResponse().bufferFactory()
.wrap(errorMessage.getBytes());
return exchange.getResponse().writeWith(Mono.just(buffer));
}

DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
Expand Down

This file was deleted.

16 changes: 0 additions & 16 deletions src/main/java/com/danielvm/destiny2bot/service/MessageService.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import com.danielvm.destiny2bot.client.BungieClientWrapper;
import com.danielvm.destiny2bot.dto.RaidEntry;
import com.danielvm.destiny2bot.dto.destiny.Activity;
import com.danielvm.destiny2bot.dto.destiny.BungieResponse;
import com.danielvm.destiny2bot.dto.destiny.PostGameCarnageReport;
import com.danielvm.destiny2bot.dto.destiny.RaidStatistics;
import com.danielvm.destiny2bot.dto.discord.Interaction;
import com.danielvm.destiny2bot.enums.ManifestEntity;
import java.util.Collections;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClientException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -81,6 +85,11 @@ public Mono<Map<String, RaidStatistics>> calculateRaidLevelStats(

private Mono<RaidEntry> addPGCRDetails(RaidEntry raidEntry) {
return pgcrBungieClient.getPostGameCarnageReport(raidEntry.getInstanceId())
.onErrorResume(WebClientException.class, err -> {
log.warn("Response too big to parse, ignoring and falling back to default value");
return Mono.just(new BungieResponse<>(
new PostGameCarnageReport(null, false, Collections.emptyList())));
})
.map(pgcr -> {
raidEntry.setIsFromBeginning(pgcr.getResponse().getActivityWasStartedFromBeginning());
return raidEntry;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/danielvm/destiny2bot/util/CryptoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public static boolean validateSignature(
Hex.decodeHex(signature.toCharArray()));
} catch (DecoderException de) {
throw new InvalidSignatureException(
"The passed in signature was invalid. Signature [%s], Timestamp [%s], Message [%s]"
.formatted(signature, timestamp, rawBody), de);
"Something wrong happened while decoding the request body");
}
}
}
1 change: 0 additions & 1 deletion src/main/java/com/danielvm/destiny2bot/util/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class HttpUtil {

private static final String JSON_PAYLOAD_HEADER = "payload_json";
private static final String JSON_PAYLOAD_CONTENT_DISPOSITION_VALUE = "form-data; name=\"payload_json\"";
private static final String FILE_PAYLOAD_HEADER_TEMPLATE = "form-data; name=\"files[%s]\"; filename=\"%s\"";
private static final String FILE_PAYLOAD_INDEX = "files[%s]";

private HttpUtil() {
Expand Down
Loading

0 comments on commit 63c043c

Please sign in to comment.