Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
feat(backend+chart): allow configuring cors for images
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Nov 20, 2022
1 parent ec7511c commit 5705a15
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
17 changes: 17 additions & 0 deletions backend/src/main/java/io/papermc/hangarauth/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.papermc.hangarauth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

Expand All @@ -18,10 +20,19 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.papermc.hangarauth.config.custom.GeneralConfig;

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

private final GeneralConfig generalConfig;

@Autowired
public WebConfig(GeneralConfig generalConfig) {
this.generalConfig = generalConfig;
}

@Bean
public RestTemplate restTemplate(List<HttpMessageConverter<?>> messageConverters) {
RestTemplate restTemplate = new RestTemplate();
Expand Down Expand Up @@ -59,4 +70,10 @@ private List<MediaType> getSupportedMediaTypes() {
MediaType.APPLICATION_OCTET_STREAM
);
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/avatar/**").allowedOrigins(generalConfig.allowedOrigins());
registry.addMapping("/image/**").allowedOrigins(generalConfig.allowedOrigins());
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.papermc.hangarauth.config.custom;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.context.annotation.Configuration;

@ConfigurationProperties("auth")
public record GeneralConfig(
@DefaultValue("http://localhost:3001") String publicHost,
@DefaultValue("supersecret2") String apiKey,
@DefaultValue("http://localhost:3333") String hangarFrontendHost,
@DefaultValue("http://localhost:3333") String hangarBackendHost
@DefaultValue("http://localhost:3333") String hangarBackendHost,
@DefaultValue("http://localhost:3000") String[] allowedOrigins
) {
}
1 change: 1 addition & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ auth:
api-key: "supersecret"
hangar-frontend-host: "http://localhost:3333"
hangar-backend-host: "http://localhost:3333"
allowed-origins: "http://localhost:3000, http://localhost:3001"
kratos:
public-url: "http://localhost:4433"
public-backend-url: "http://localhost:4433"
Expand Down
1 change: 1 addition & 0 deletions chart/templates/secret-hangarauth-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ stringData:
public-host: "{{ .Values.base.authHost }}"
hangar-frontend-host: "{{ .Values.base.hangarHost }}"
hangar-backend-host: "http://hangar-backend:8080"
allowed-origins: "{{ .Values.backend.config.auth.allowedOrigins }}"
kratos:
public-url: "{{ .Values.base.authHost }}/kratos"
public-backend-url: "{{ .Values.backend.config.auth.kratos.publicBackendUrl }}"
Expand Down
1 change: 1 addition & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ backend:
database: "hangarauth"
options: "?currentSchema=auth"
auth:
allowedOrigins: "https://*.hangar.test"
kratos:
adminUrl: "http://hangarauth-kratos:4434"
publicBackendUrl: "http://hangarauth-kratos:4433"
Expand Down

0 comments on commit 5705a15

Please sign in to comment.