-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement JwtAgent, Kakao Login #15
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...api/src/main/kotlin/com/whatever/raisedragon/applicationservice/AuthApplicationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.whatever.raisedragon.applicationservice | ||
|
||
import com.whatever.raisedragon.domain.auth.AuthService | ||
import com.whatever.raisedragon.domain.user.Nickname | ||
import com.whatever.raisedragon.domain.user.User | ||
import com.whatever.raisedragon.domain.user.UserService | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
@Transactional | ||
class AuthApplicationService( | ||
private val authService: AuthService, | ||
private val userService: UserService, | ||
) { | ||
|
||
fun kakoLogin(accessToken: String): User { | ||
val kakaoId = authService.verifyKaKao(accessToken) | ||
return userService.loadByOAuthPayload(kakaoId) ?: return userService.create( | ||
User( | ||
oauthTokenPayload = kakaoId, | ||
fcmTokenPayload = null, | ||
nickname = Nickname.generateRandomNickname() | ||
) | ||
) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
raisedragon-api/src/main/kotlin/com/whatever/raisedragon/controller/auth/AuthController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.whatever.raisedragon.controller.auth | ||
|
||
import com.whatever.raisedragon.applicationservice.AuthApplicationService | ||
import com.whatever.raisedragon.common.Response | ||
import io.swagger.v3.oas.annotations.Operation | ||
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.RestController | ||
|
||
@RestController | ||
@RequestMapping("/v1/auth") | ||
class AuthController( | ||
private val authApplicationService: AuthApplicationService, | ||
) { | ||
|
||
@Operation(summary = "LoginAPI", description = "Kakao Login") | ||
@PostMapping("/login") | ||
fun login(@RequestBody loginRequest: LoginRequest): Response<LoginResponse> { | ||
return Response.success( | ||
LoginResponse(authApplicationService.kakoLogin(loginRequest.accessToken)) | ||
) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
raisedragon-api/src/main/kotlin/com/whatever/raisedragon/controller/auth/AuthDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.whatever.raisedragon.controller.auth | ||
|
||
import com.whatever.raisedragon.domain.user.User | ||
import io.swagger.v3.oas.annotations.media.Schema | ||
import jakarta.validation.constraints.NotBlank | ||
|
||
@Schema(description = "[Request] 카카오 로그인") | ||
data class LoginRequest( | ||
@NotBlank | ||
@Schema(description = "카카오에서 발급받은 AccessToken") | ||
val accessToken: String | ||
) | ||
|
||
@Schema(description = "[Request] 카카오 로그인 응답") | ||
data class LoginResponse( | ||
val user: User | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...edragon-core/src/main/kotlin/com/whatever/raisedragon/common/config/RestTemplateConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.whatever.raisedragon.common.config | ||
|
||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.client.RestTemplate | ||
|
||
|
||
@Configuration | ||
class RestTemplateConfig { | ||
@Bean | ||
fun restTemplate(): RestTemplate { | ||
return RestTemplate() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,4 @@ interface JwtAgent { | |
* @return JwtToken 을 반환합니다. | ||
*/ | ||
fun provide(user: User): JwtToken | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/auth/jwt/JwtAgentImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.whatever.raisedragon.domain.auth.jwt | ||
|
||
import com.whatever.raisedragon.domain.user.User | ||
import io.jsonwebtoken.Claims | ||
import io.jsonwebtoken.Jwts | ||
import org.springframework.stereotype.Component | ||
import java.util.* | ||
|
||
@Component | ||
class JwtAgentImpl( | ||
private val jwtGenerator: JwtGenerator | ||
) : JwtAgent { | ||
|
||
companion object { | ||
private const val BEARER_PREFIX = "Bearer " | ||
private const val ACCESS_TOKEN_EXPIRE_TIME = (1000 * 60 * 30).toLong() | ||
private const val REFRESH_TOKEN_EXPIRE_TIME = (1000 * 60 * 60 * 24 * 7).toLong() | ||
} | ||
|
||
override fun provide(user: User): JwtToken { | ||
val now = Date().time | ||
|
||
return JwtToken( | ||
accessToken = BEARER_PREFIX + jwtGenerator.generateAccessToken( | ||
claims = buildClaims(user), | ||
accessTokenExpiredAt = Date(now + ACCESS_TOKEN_EXPIRE_TIME) | ||
), | ||
refreshToken = BEARER_PREFIX + jwtGenerator.generateRefreshToken( | ||
Date(now + REFRESH_TOKEN_EXPIRE_TIME) | ||
) | ||
) | ||
} | ||
|
||
private fun buildClaims(user: User): Claims { | ||
val claims = Jwts.claims() | ||
claims.setSubject(user.nickname.value) | ||
claims["id"] = user.id | ||
return claims | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
raisedragon-core/src/main/kotlin/com/whatever/raisedragon/domain/auth/jwt/JwtGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.whatever.raisedragon.domain.auth.jwt | ||
|
||
import io.jsonwebtoken.Claims | ||
import io.jsonwebtoken.Jwts | ||
import io.jsonwebtoken.io.Decoders | ||
import io.jsonwebtoken.security.Keys | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import java.security.Key | ||
import java.util.* | ||
|
||
@Component | ||
class JwtGenerator { | ||
|
||
@Value("\${jwt.secret-key}") | ||
private var key: String? = null | ||
|
||
internal fun generateAccessToken( | ||
claims: Claims, | ||
accessTokenExpiredAt: Date | ||
): String { | ||
return Jwts.builder() | ||
.signWith(getSigningKey()) | ||
.setHeaderParam("type", "JWT") | ||
.setClaims(claims) | ||
.setExpiration(accessTokenExpiredAt) | ||
.compact() | ||
} | ||
|
||
internal fun generateRefreshToken( | ||
refreshTokenExpireIn: Date | ||
): String { | ||
return Jwts.builder() | ||
.signWith(getSigningKey()) | ||
.setHeaderParam("type", "JWT") | ||
.setExpiration(refreshTokenExpireIn) | ||
.compact() | ||
} | ||
|
||
private fun getSigningKey(): Key { | ||
val keyBytes = Decoders.BASE64.decode(key) | ||
return Keys.hmacShaKeyFor(keyBytes) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...re/src/main/kotlin/com/whatever/raisedragon/domain/auth/oauth/kakao/KakaoLoginResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.whatever.raisedragon.domain.auth.oauth.kakao | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import java.time.LocalDateTime | ||
|
||
data class KakaoLoginResponse( | ||
@JsonProperty("id") | ||
val id: String? = null, | ||
|
||
@JsonProperty("connected_at") | ||
val connectedAt: LocalDateTime? = null, | ||
) |
34 changes: 34 additions & 0 deletions
34
...ore/src/main/kotlin/com/whatever/raisedragon/domain/auth/oauth/kakao/KakaoOAuthService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.whatever.raisedragon.domain.auth.oauth.kakao | ||
|
||
import com.whatever.raisedragon.domain.auth.AuthService | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.MediaType | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.client.RestTemplate | ||
|
||
@Service | ||
class KakaoOAuthService( | ||
private val restTemplate: RestTemplate, | ||
) : AuthService { | ||
|
||
private val KAKAO_USERINFO_REQUEST_URL = "https://kapi.kakao.com/v2/user/me" | ||
override fun verifyKaKao(userToken: String): String { | ||
val headers = HttpHeaders() | ||
headers.contentType = MediaType.APPLICATION_FORM_URLENCODED | ||
headers.setBearerAuth(userToken) | ||
|
||
val request = HttpEntity( | ||
null, | ||
headers | ||
) | ||
|
||
val response = restTemplate.postForEntity( | ||
KAKAO_USERINFO_REQUEST_URL, | ||
request, | ||
KakaoLoginResponse::class.java | ||
) | ||
|
||
return response.body?.id!! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
raisedragon-core/src/main/resources/domain-config/application-prod.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
보통 여기서 readOnly = true 하고 밑에 실제 트랜잭션 동작이 있는곳에 @transactional 붙이지 않나요? readOnly의 default가 true인것 같아서요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는
default
가false
로 알고있는데 이건 그냥 저희끼리만 통일화하면 될 것 같아요~