Skip to content

Commit

Permalink
backup stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed Jul 31, 2023
1 parent e638ef6 commit 3b21e81
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
49 changes: 25 additions & 24 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
plugins {
id("org.springframework.boot") version "2.5.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
java
id("org.springframework.boot") version "2.5.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
java
}

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-data-redis")
implementation("org.springframework.boot:spring-boot-starter-data-rest")
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.mariadb.jdbc:mariadb-java-client:2.7.3")

developmentOnly("org.springframework.boot:spring-boot-devtools")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("com.h2database:h2")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-data-redis")
implementation("org.springframework.boot:spring-boot-starter-data-rest")
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.mariadb.jdbc:mariadb-java-client:3.0.5")
implementation("com.h2database:h2:2.1.212")

developmentOnly("org.springframework.boot:spring-boot-devtools")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("com.h2database:h2")
}


Expand All @@ -32,11 +33,11 @@ version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_16

tasks {
withType<JavaCompile> {
options.encoding = "UTF-8"
}
withType<JavaCompile> {
options.encoding = "UTF-8"
}

withType<Test> {
useJUnitPlatform()
}
withType<Test> {
useJUnitPlatform()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import net.developerden.backend.security.DiscordUser;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -12,7 +11,7 @@ public class InfoController {

@GetMapping("/user")
public String user(@AuthenticationPrincipal DiscordUser principal) {
return "Hello, %s (%s)".formatted(principal.fullName(), principal.id());
return "Hello, %s (%s) with token %s".formatted(principal.fullName(), principal.id(), principal.token().getTokenValue());
}

@GetMapping("/ping")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.developerden.backend.security;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.user.OAuth2User;

import java.util.Collection;
Expand All @@ -11,7 +12,9 @@
/**
* Java representation of all of the data that Discord gives us from the /users/@me API endpoint.
*/
public record DiscordUser(String id, String username, String discriminator, String locale) implements OAuth2User {
public record DiscordUser(String id, String username, String discriminator,
String locale,
OAuth2AccessToken token) implements OAuth2User {
public String fullName() {
return username + "#" + discriminator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

final var entity = new HttpEntity<>(headers);

final var user = new RestTemplate()
.exchange(DISCORD_USERINFO_URI, HttpMethod.GET,
entity, DiscordUser.class)
var user = new RestTemplate()
.exchange(DISCORD_USERINFO_URI, HttpMethod.GET, entity, DiscordUser.class)
.getBody();

return user;
return new DiscordUser(user.id(), user.username(), user.discriminator(), user.locale(), userRequest.getAccessToken());
}
}
7 changes: 4 additions & 3 deletions src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
spring:
jpa:
hibernate:
ddl-auto: none
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.H2Dialect
datasource:
url: jdbc:h2:~/testdb;DB_CLOSE_ON_EXIT=FALSE
url: jdbc:h2:file:./testdb
username: user
password: pass
driver-class-name: org.h2.Driver
Expand All @@ -17,7 +18,7 @@ spring:
clientSecret: ${DDB_CLIENT_SECRET}
authorizationGrantType: authorization_code
redirectUri: "{baseUrl}/login/oauth2/code/discord"
scope: identify
scope: identify,applications.commands.permissions.update
provider:
discord:
authorizationUri: https://discord.com/api/oauth2/authorize
Expand Down

0 comments on commit 3b21e81

Please sign in to comment.