Compare commits

..

5 Commits

12 changed files with 237 additions and 128 deletions

23
.devcontainer/Dockerfile Normal file
View File

@ -0,0 +1,23 @@
# [Choice] Java version (use -bullseye variants on local arm64/Apple Silicon): 11, 17, 11-bullseye, 17-bullseye, 11-buster, 17-buster
ARG VARIANT=11-bullseye
FROM mcr.microsoft.com/vscode/devcontainers/java:0-${VARIANT}
# [Option] Install Maven
ARG INSTALL_MAVEN="false"
ARG MAVEN_VERSION=""
# [Option] Install Gradle
ARG INSTALL_GRADLE="false"
ARG GRADLE_VERSION=""
RUN if [ "${INSTALL_MAVEN}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/sdkman/bin/sdkman-init.sh && sdk install maven \"${MAVEN_VERSION}\""; fi \
&& if [ "${INSTALL_GRADLE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/sdkman/bin/sdkman-init.sh && sdk install gradle \"${GRADLE_VERSION}\""; fi
# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1

View File

@ -0,0 +1,30 @@
{
"name": "Java & Mariadb",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace",
// Set *default* container specific settings.json values on container create.
"settings": {
"java.jdt.ls.java.home": "/docker-java-home"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"vscjava.vscode-java-pack",
"pivotal.vscode-boot-dev-pack",
"richardwillis.vscode-gradle-extension-pack",
"eamodio.gitlens",
"donjayamanne.githistory"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// This can be used to network with other containers or with the host.
// "forwardPorts": [5432],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "java -version",
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}

View File

@ -0,0 +1,48 @@
version: '3.8'
volumes:
mysql-data:
services:
app:
container_name: javadev
build:
context: .
dockerfile: Dockerfile
args:
# Update 'VARIANT' to pick an version of Java: 11, 17.
# Append -bullseye or -buster to pin to an OS version.
# Use -bullseye variants on local arm64/Apple Silicon.
VARIANT: "17"
# Options
INSTALL_MAVEN: "false"
MAVEN_VERSION: ""
INSTALL_GRADLE: "true"
GRADLE_VERSION: "7.4.2"
NODE_VERSION: "lts/*"
volumes:
- ..:/workspace:cached
# Overrides default command so things don't shut down after the process ends.
command: sleep infinity
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:db
# Uncomment the next line to use a non-root user for all processes.
# user: vscode
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
db:
container_name: database
image: mariadb:latest
restart: unless-stopped
volumes:
- mysql-data:/var/lib/mysql
environment:
MARIADB_ROOT_PASSWORD: mariadb_root
MARIADB_PASSWORD: claptrap
MARIADB_USER: claptrap
MARIADB_DATABASE: claptrap
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

4
.gitignore vendored
View File

@ -30,3 +30,7 @@ src/main/resources/static/error/js
**.log **.log
.jpb/ .jpb/
**/*.env
bin/

17
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch MainBot",
"request": "launch",
"mainClass": "net.Broken.MainBot",
"projectName": "ClaptrapBot",
"envFile": "${workspaceFolder}/.env"
},
]
}

View File

@ -11,6 +11,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
@RestController @RestController
@RequestMapping("/api/v2/auth") @RequestMapping("/api/v2/auth")
@CrossOrigin(origins = "*", maxAge = 3600) @CrossOrigin(origins = "*", maxAge = 3600)
@ -25,6 +27,7 @@ public class AuthController {
} }
@PostMapping("/discord") @PostMapping("/discord")
@SecurityRequirements(value = {})
public JwtResponse loginDiscord(@Validated @RequestBody Login login) { public JwtResponse loginDiscord(@Validated @RequestBody Login login) {
Authentication authentication = authenticationManager.authenticate( Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(login.redirectUri(), login.code()) new UsernamePasswordAuthenticationToken(login.redirectUri(), login.code())

View File

@ -7,10 +7,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;
@RestController @RestController
@RequestMapping("/api/v2") @RequestMapping("/api/v2")
@CrossOrigin(origins = "*", maxAge = 3600) @CrossOrigin(origins = "*", maxAge = 3600)
@Hidden
public class CrossOptionController { public class CrossOptionController {
/** /**

View File

@ -2,6 +2,7 @@ package net.Broken.Api.Controllers;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
import net.Broken.DB.Entity.UserEntity; import net.Broken.DB.Entity.UserEntity;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
@ -12,12 +13,10 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/api/v2/hello") @RequestMapping("/api/v2/hello")
@CrossOrigin(origins = "*", maxAge = 3600) @CrossOrigin(origins = "*", maxAge = 3600)
public class HelloController { public class HelloController {
@GetMapping("world") @GetMapping("world")
@Operation(security = { @SecurityRequirement(name = "jwt") })
public String helloWorld(Authentication authentication){ public String helloWorld(Authentication authentication){
UserEntity principal = (UserEntity) authentication.getPrincipal(); UserEntity principal = (UserEntity) authentication.getPrincipal();
return "Hello " + principal.getUsername(); return "Hello " + principal.getUsername();

View File

@ -1,18 +1,33 @@
package net.Broken.Api.OpenApi; package net.Broken.Api.OpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme; import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
@Configuration
public class OpenApiConfig { public class OpenApiConfig {
@Bean @Bean
public OpenAPI customOpenAPI() { public OpenAPI customOpenAPI() {
return new OpenAPI().components(new Components() final String securitySchemeName = "JWT";
.addSecuritySchemes("jwt", // final String apiTitle = String.format("%s API", StringUtils.capitalize(moduleName));
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT").name("JWT"))) return new OpenAPI()
.addSecurityItem(new SecurityRequirement().addList("jwt")); .addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
.components(
new Components()
.addSecuritySchemes(securitySchemeName,
new SecurityScheme()
.name(securitySchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
)
);
} }
} }

View File

@ -27,6 +27,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.authorizeRequests() .authorizeRequests()
.antMatchers("/api/v2/auth/**").permitAll() .antMatchers("/api/v2/auth/**").permitAll()
.antMatchers("/swagger-ui/**").permitAll() .antMatchers("/swagger-ui/**").permitAll()
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/v3/api-docs/**").permitAll() .antMatchers("/v3/api-docs/**").permitAll()
.anyRequest().denyAll(); .anyRequest().denyAll();

View File

@ -5,7 +5,6 @@ import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys; import io.jsonwebtoken.security.Keys;
import net.Broken.DB.Entity.UserEntity; import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.UserRepository; import net.Broken.DB.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.security.Key; import java.security.Key;
@ -16,8 +15,6 @@ import java.util.UUID;
@Service @Service
public class JwtService { public class JwtService {
@Value("${security.jwt.secret}")
private String jwtSecret;
private final Key jwtKey; private final Key jwtKey;

View File

@ -1,4 +1,71 @@
databaseChangeLog: databaseChangeLog:
- changeSet:
id: 1653065037086-9
author: seb65 (generated)
changes:
- createTable:
columns:
- column:
autoIncrement: true
constraints:
nullable: false
primaryKey: true
name: id
type: BIGINT
- column:
defaultValueNumeric: 0
name: api_command_count
type: BIGINT
- column:
name: guild_id
type: VARCHAR(255)
- column:
defaultValueNumeric: 0
name: message_count
type: BIGINT
- column:
defaultValueNumeric: 0
name: vocal_time
type: BIGINT
- column:
constraints:
nullable: false
name: user_entity_id
type: INT
tableName: user_stats
- changeSet:
id: 1653065037086-8
author: seb65 (generated)
changes:
- createTable:
columns:
- column:
autoIncrement: true
constraints:
nullable: false
primaryKey: true
name: id
type: INT
- column:
name: api_token
type: VARCHAR(255)
- column:
name: jda_id
type: VARCHAR(255)
- column:
name: name
type: VARCHAR(255)
- column:
name: password
type: VARCHAR(255)
- column:
constraints:
nullable: false
name: is_bot_admin
type: BIT
tableName: user_entity
- changeSet: - changeSet:
id: 1653065037086-1 id: 1653065037086-1
author: seb65 (generated) author: seb65 (generated)
@ -71,17 +138,7 @@ databaseChangeLog:
name: visible_voice_chanel name: visible_voice_chanel
type: VARCHAR(255) type: VARCHAR(255)
tableName: guild_preference_entity_visible_voice_chanel tableName: guild_preference_entity_visible_voice_chanel
- changeSet:
id: 1653065037086-3
author: seb65 (generated)
changes:
- createSequence:
columns:
- column:
defaultValueComputed: 'NULL'
name: next_val
type: BIGINT
tableName: hibernate_sequence
- changeSet: - changeSet:
id: 1653065037086-4 id: 1653065037086-4
author: seb65 (generated) author: seb65 (generated)
@ -218,40 +275,7 @@ databaseChangeLog:
name: is_bot_admin name: is_bot_admin
type: BIT type: BIT
tableName: user_entity tableName: user_entity
- changeSet:
id: 1653065037086-9
author: seb65 (generated)
changes:
- createTable:
columns:
- column:
autoIncrement: true
constraints:
nullable: false
primaryKey: true
name: id
type: BIGINT
- column:
defaultValueNumeric: 0
name: api_command_count
type: BIGINT
- column:
name: guild_id
type: VARCHAR(255)
- column:
defaultValueNumeric: 0
name: message_count
type: BIGINT
- column:
defaultValueNumeric: 0
name: vocal_time
type: BIGINT
- column:
constraints:
nullable: false
name: user_entity_id
type: INT
tableName: user_stats
- changeSet: - changeSet:
id: 1653065037086-10 id: 1653065037086-10
author: seb65 (generated) author: seb65 (generated)
@ -379,7 +403,6 @@ databaseChangeLog:
referencedTableName: guild_preference_entity referencedTableName: guild_preference_entity
validate: true validate: true
databaseChangeLog:
- changeSet: - changeSet:
id: 1653073535100-14 id: 1653073535100-14
author: seb65 (generated) author: seb65 (generated)
@ -411,6 +434,10 @@ databaseChangeLog:
- changeSet: - changeSet:
id: 1653073535100-18 id: 1653073535100-18
author: seb65 (generated) author: seb65 (generated)
preConditions:
- onFail: MARK_RAN
- tableExists:
tableName: hibernate_sequence
changes: changes:
- dropTable: - dropTable:
tableName: hibernate_sequence tableName: hibernate_sequence
@ -544,9 +571,6 @@ databaseChangeLog:
columnDataType: varchar(255) columnDataType: varchar(255)
columnName: welcome_message columnName: welcome_message
tableName: guild_preference_entity tableName: guild_preference_entity
databaseChangeLog:
- changeSet: - changeSet:
id: sebclem-manual-1 id: sebclem-manual-1
author: sebclem author: sebclem
@ -556,9 +580,6 @@ databaseChangeLog:
oldColumnName: jda_id oldColumnName: jda_id
tableName: user_entity tableName: user_entity
columnDataType: varchar(255) columnDataType: varchar(255)
databaseChangeLog:
- changeSet: - changeSet:
id: 1653309183173-13 id: 1653309183173-13
author: seb65 (generated) author: seb65 (generated)
@ -596,58 +617,6 @@ databaseChangeLog:
columnNames: discord_id columnNames: discord_id
constraintName: UC_USER_ENTITYDISCORD_ID_COL constraintName: UC_USER_ENTITYDISCORD_ID_COL
tableName: user_entity tableName: user_entity
- changeSet:
id: 1653309183173-17
author: seb65 (generated)
changes:
- dropForeignKeyConstraint:
baseTableName: pending_pwd_reset_entity
constraintName: FKatrj670ooxj9qiq230iacbt68
- changeSet:
id: 1653309183173-18
author: seb65 (generated)
changes:
- dropForeignKeyConstraint:
baseTableName: guild_preference_entity_visible_voice_chanel
constraintName: FKlyf6ksd3969rqtwm3bqupbniu
- changeSet:
id: 1653309183173-19
author: seb65 (generated)
changes:
- dropTable:
tableName: guild_preference_entity_visible_voice_chanel
- changeSet:
id: 1653309183173-20
author: seb65 (generated)
changes:
- dropTable:
tableName: hibernate_sequence
- changeSet:
id: 1653309183173-21
author: seb65 (generated)
changes:
- dropTable:
tableName: pending_pwd_reset_entity
- changeSet:
id: 1653309183173-22
author: seb65 (generated)
changes:
- dropTable:
tableName: pending_user_entity
- changeSet:
id: 1653309183173-23
author: seb65 (generated)
changes:
- dropColumn:
columnName: api_token
tableName: user_entity
- changeSet:
id: 1653309183173-25
author: seb65 (generated)
changes:
- dropColumn:
columnName: password
tableName: user_entity
- changeSet: - changeSet:
id: 1653309183173-1 id: 1653309183173-1
author: seb65 (generated) author: seb65 (generated)