Skip to content

Commit

Permalink
S3, Test token API
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Jan 14, 2025
1 parent 1f8011d commit 5a59dcd
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ jobs:
coolsms.api-key: ${{ secrets.COOL_SMS_API_KEY }}
coolsms.api-secret: ${{ secrets.COOL_SMS_API_SECRET }}
coolsms.sender-phone: ${{ secrets.COOL_SMS_SENDER_PHONE }}
cloud.aws.credentials.accessKey: ${{ secrets.S3_ACCESS_KEY }}
cloud.aws.credentials.secretKey: ${{ secrets.S3.SECRET_KEY }}
cloud.aws.s3-bucket: ${{ secrets.S3_BUCKET }}
cloud.aws.region.static: ${{ secrets.S3_REGION }}
dev.secret-key: ${{ secrets.DEV_SECRET_KEY }}
spring.profiles.active: 'prd'
- name: Setting firebase
run: |
Expand Down
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ repositories {
}

dependencies {
implementation("org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE")


api("com.google.firebase:firebase-admin:9.3.0")

// Swagger
Expand Down
26 changes: 25 additions & 1 deletion src/main/kotlin/com/ohayo/moyamoya/api/TestController.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.ohayo.moyamoya.api

import com.ohayo.moyamoya.core.UserRepository
import com.ohayo.moyamoya.core.findByPhoneSafety
import com.ohayo.moyamoya.global.CustomException
import com.ohayo.moyamoya.infra.token.JwtClient
import com.ohayo.moyamoya.infra.token.Token
import mu.KLogger
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
Expand All @@ -9,7 +16,10 @@ import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("test")
class TestController(
private val logger: KLogger
private val logger: KLogger,
private val userRepository: UserRepository,
private val jwtClient: JwtClient,
@Value("\${dev.secret-key}") private val secretKey: String
) {
@GetMapping
fun test(
Expand All @@ -19,4 +29,18 @@ class TestController(
logger.info(result)
return result
}

@GetMapping("token")
fun getTestToken(
@RequestParam phone: String,
@RequestParam key: String
): Token {
if (key != secretKey) {
throw CustomException(HttpStatus.UNAUTHORIZED, "히히")
}

return jwtClient.generate(
userRepository.findByPhoneSafety(phone)
)
}
}
16 changes: 16 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/api/upload/UploadApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ohayo.moyamoya.api.upload

import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile

@RestController
@RequestMapping("upload")
class UploadApi(
private val uploadService: UploadService
) {
@PostMapping
fun uploadFile(@RequestParam("file") file: MultipartFile) = uploadService.uploadFile(file)
}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/api/upload/UploadRes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ohayo.moyamoya.api.upload

data class UploadRes(
val url: String
)
40 changes: 40 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/api/upload/UploadService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.ohayo.moyamoya.api.upload

import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.model.PutObjectRequest
import com.ohayo.moyamoya.global.CustomException
import com.ohayo.moyamoya.infra.s3.S3Properties
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import java.util.UUID

@Service
class UploadService(
private val amazonS3: AmazonS3,
private val s3Properties: S3Properties
) {
fun uploadFile(file: MultipartFile): UploadRes {
if (file.isEmpty || file.equals("") || file.originalFilename == null) {
throw CustomException(HttpStatus.BAD_REQUEST, "올바르지 않은 파일")
}

val fileName = "uploads/" + UUID.randomUUID().toString().substring(0, 10) + file.originalFilename!!

try {
amazonS3.putObject(
PutObjectRequest(s3Properties.s3bucket, fileName, file.inputStream, ObjectMetadata().apply {
contentType = file.contentType
contentLength = file.size
})
)

return UploadRes(
url = amazonS3.getUrl(s3Properties.s3bucket, fileName).toString()
)
} catch (e: Exception) {
throw CustomException(HttpStatus.INTERNAL_SERVER_ERROR, "s3 업로드 실패")
}
}
}
11 changes: 6 additions & 5 deletions src/main/kotlin/com/ohayo/moyamoya/api/user/UserApi.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.ohayo.moyamoya.api.user

import com.ohayo.moyamoya.api.user.value.RefreshReq
import com.ohayo.moyamoya.api.user.value.SendCodeReq
import com.ohayo.moyamoya.api.user.value.SignUpReq
import com.ohayo.moyamoya.api.user.value.VerifyCodeReq
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
Expand All @@ -16,14 +17,14 @@ class UserApi(
private val userService: UserService
) {
@PostMapping("send-code")
fun sendCode(@RequestParam phone: String) = userService.sendCode(phone)
fun sendCode(@RequestBody @Valid req: SendCodeReq) = userService.sendCode(req)

@PostMapping("verify-code")
fun verifyCode(@RequestParam phone: String, @RequestParam code: String) = userService.verifyCode(phone, code)
fun verifyCode(@RequestBody @Valid req: VerifyCodeReq) = userService.verifyCode(req)

@PostMapping("sign-up")
fun signUp(@RequestBody @Valid req: SignUpReq) = userService.signUp(req)

@GetMapping
fun getMyInfo() = userService.getMyInfo()

Expand Down
19 changes: 8 additions & 11 deletions src/main/kotlin/com/ohayo/moyamoya/api/user/UserService.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.ohayo.moyamoya.api.user

import com.ohayo.moyamoya.api.common.VoidRes
import com.ohayo.moyamoya.api.user.value.RefreshReq
import com.ohayo.moyamoya.api.user.value.SignUpReq
import com.ohayo.moyamoya.api.user.value.UserRes
import com.ohayo.moyamoya.api.user.value.VerifyCodeRes
import com.ohayo.moyamoya.api.user.value.*
import com.ohayo.moyamoya.core.*
import com.ohayo.moyamoya.core.extension.findByIdSafety
import com.ohayo.moyamoya.global.CustomException
Expand All @@ -26,31 +23,31 @@ class UserService(
private val phoneCodeRepository: PhoneCodeRepository,
private val sessionHolder: UserSessionHolder
) {
fun sendCode(phone: String): VoidRes {
val code = smsClient.sendAuthorizationCode(phone)
fun sendCode(req: SendCodeReq): VoidRes {
val code = smsClient.sendAuthorizationCode(req.phone)
phoneCodeRepository.save(
PhoneCodeEntity(
phone = phone,
phone = req.phone,
code = code
)
)
return VoidRes()
}

@Transactional(rollbackFor = [Exception::class])
fun verifyCode(phone: String, code: String): VerifyCodeRes {
fun verifyCode(req: VerifyCodeReq): VerifyCodeRes {
val codes = phoneCodeRepository.findByStatusAndPhoneAndCode(
status = PhoneCodeEntity.Status.UNUSED,
phone = phone,
code = code
phone = req.phone,
code = req.code
)

if (codes.isEmpty()) throw CustomException(HttpStatus.BAD_REQUEST, "인증 실패")

codes.forEach { it.updateStatus() }
phoneCodeRepository.saveAll(codes)

val user = userRepository.findByPhone(phone)
val user = userRepository.findByPhone(req.phone)
return if (user == null) {
VerifyCodeRes(
isNewUser = true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ohayo.moyamoya.api.user.value

import jakarta.validation.constraints.NotNull

data class SendCodeReq(
@field:NotNull
val phone: String,
)
11 changes: 11 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/api/user/value/VerifyCodeReq.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ohayo.moyamoya.api.user.value

import jakarta.validation.constraints.NotNull

data class VerifyCodeReq(
@field:NotNull
val phone: String,

@field:NotNull
val code: String
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ohayo.moyamoya.global
package com.ohayo.moyamoya.global.config

import com.google.auth.oauth2.GoogleCredentials
import com.google.firebase.FirebaseApp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ohayo.moyamoya.global
package com.ohayo.moyamoya.global.config

import com.ohayo.moyamoya.infra.discord.DiscordProperties
import mu.KotlinLogging
Expand Down
27 changes: 27 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/global/config/S3Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.ohayo.moyamoya.global.config

import com.amazonaws.auth.AWSStaticCredentialsProvider
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import com.ohayo.moyamoya.infra.s3.S3Properties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class S3Config(
private val s3Properties: S3Properties
) {
@Bean
fun AmazonS3Client(): AmazonS3Client = AmazonS3ClientBuilder.standard()
.withRegion(s3Properties.region.static)
.withCredentials(
AWSStaticCredentialsProvider(
BasicAWSCredentials(
s3Properties.credentials.accessKey,
s3Properties.credentials.secretKey
)
)
)
.build() as AmazonS3Client
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ohayo.moyamoya.global
package com.ohayo.moyamoya.global.config

import com.ohayo.moyamoya.global.ErrorResponseSender
import com.ohayo.moyamoya.global.HttpExceptionFilter
import com.ohayo.moyamoya.global.jwt.JwtAuthenticationFilter
import com.ohayo.moyamoya.global.jwt.JwtExceptionFilter
import org.springframework.context.annotation.Bean
Expand All @@ -15,7 +17,6 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.UrlBasedCorsConfigurationSource


@Configuration
@EnableWebSecurity
class SecurityConfig(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.ohayo.moyamoya.global
package com.ohayo.moyamoya.global.config

import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.security.SecurityRequirement
import io.swagger.v3.oas.models.security.SecurityScheme
import org.springframework.context.annotation.Bean
Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/infra/s3/S3Properties.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ohayo.moyamoya.infra.s3

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.bind.ConstructorBinding

@ConfigurationProperties("cloud.aws")
class S3Properties @ConstructorBinding constructor(
val credentials: Credentials,
val s3bucket: String,
val region: Region,
) {
class Credentials(
val accessKey: String,
val secretKey: String
)
class Region(
val static: String
)
}
12 changes: 12 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ coolsms:
api-key: ${COOL_SMS_API_KEY}
api-secret: ${COOL_SMS_API_SECRET}
sender-phone: ${COOL_SMS_SENDER_PHONE}
cloud:
aws:
credentials:
accessKey: ${S3_ACCESS_KEY}
secretKey: ${S3_SECRET_KEY}
s3-bucket: ${S3_BUCKET}
stack:
auto: false
region:
static: ${S3_REGION}
dev:
secret-key: ${DEV_SECRET_KEY}
springdoc:
swagger-ui:
groups-order: DESC # path, query, body, response 순으로 출력
Expand Down

0 comments on commit 5a59dcd

Please sign in to comment.