-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: Receive Kakao Server Access Token (OAuth2) #5
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b9f894b
feat: Member Entity ํ์ ์์ฑ ์ถ๊ฐ
h-beeen 13b2d35
feat: Configuration ๊ณ์ธต ์์
h-beeen 70c687d
feat: Init Kakao Oauth / Redirect API
h-beeen 91fa771
feat: OAuth2 Provider ๊ธฐ์ค ๋ถ๊ธฐ ํ ๋ก๊ทธ์ธ API๋ก Redirect
h-beeen 266069d
feat: JWT ๊ธฐ๋ณธ ๋ก์ง / ProviderId ๊ธฐ๋ฐ ํ์๊ฐ์
/๋ก๊ทธ์ธ ๋ก์ง
h-beeen b88fc88
chore: ํจํค์ง ๊ตฌ์กฐ ๊ฐ์
h-beeen a1b2c68
feat: JWT ์ฐ๋ ์๋ฃ ๋ฐ ๊ตฌ์กฐ ๊ฐ์
h-beeen dd3e100
feat: OAtuh / Security Logic ์ฝ๋๋ฆฌ๋ทฐ ๋ฐ์ (#8)
h-beeen 674e84b
Merge remote-tracking branch 'origin/develop' into feat/#1
h-beeen 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
Submodule CONFIG
updated
from 6231ff to ee2d67
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
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/com/vacgom/backend/application/auth/AuthFactory.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,30 @@ | ||
package com.vacgom.backend.application.auth | ||
|
||
import com.vacgom.backend.domain.auth.oauth.OauthConnector | ||
import com.vacgom.backend.domain.auth.oauth.OauthUriGenerator | ||
import com.vacgom.backend.domain.auth.oauth.constants.ProviderType | ||
import com.vacgom.backend.global.exception.error.BusinessException | ||
import com.vacgom.backend.global.security.exception.AuthError | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class AuthFactory( | ||
private val connectors: List<OauthConnector>, | ||
private val uriProviders: List<OauthUriGenerator> | ||
) { | ||
fun getAuthConnector(provider: String): OauthConnector { | ||
val providerType = ProviderType.from(provider) | ||
|
||
return connectors.firstOrNull { | ||
it.isSupported(providerType) | ||
} ?: throw BusinessException(AuthError.UNSUPPORTED_PROVIDER) | ||
} | ||
|
||
fun getAuthUriGenerator(provider: String): OauthUriGenerator { | ||
val providerType = ProviderType.from(provider) | ||
|
||
return uriProviders.firstOrNull { | ||
it.isSupported(providerType) | ||
} ?: throw BusinessException(AuthError.UNSUPPORTED_PROVIDER) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/com/vacgom/backend/application/auth/AuthService.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,55 @@ | ||
package com.vacgom.backend.application.auth | ||
|
||
import com.vacgom.backend.application.auth.dto.LoginResponse | ||
import com.vacgom.backend.application.auth.dto.MemberResponse | ||
import com.vacgom.backend.application.auth.dto.TokenResponse | ||
import com.vacgom.backend.domain.auth.constants.Role.ROLE_TEMP_USER | ||
import com.vacgom.backend.domain.auth.oauth.constants.ProviderType | ||
import com.vacgom.backend.domain.member.Member | ||
import com.vacgom.backend.global.security.jwt.JwtService | ||
import com.vacgom.backend.infrastructure.member.persistence.MemberRepository | ||
import jakarta.transaction.Transactional | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.stereotype.Component | ||
import java.net.URI | ||
|
||
@Component | ||
class AuthService( | ||
private val authFactory: AuthFactory, | ||
private val jwtService: JwtService, | ||
private val memberRepository: MemberRepository | ||
) { | ||
fun createRedirectHeaders(redirectUri: URI): HttpHeaders { | ||
val headers = HttpHeaders() | ||
headers.location = redirectUri | ||
return headers | ||
} | ||
|
||
fun getAuthorizationUri(provider: String): URI { | ||
return authFactory.getAuthUriGenerator(provider).generate() | ||
} | ||
|
||
@Transactional | ||
fun login( | ||
providerType: String, | ||
code: String | ||
): LoginResponse { | ||
val authConnector = authFactory.getAuthConnector(providerType) | ||
val oauthToken = authConnector.fetchOauthToken(code) | ||
val memberInfo = authConnector.fetchMemberInfo(oauthToken.accessToken) | ||
val member = findOrCreateMember(memberInfo.id, ProviderType.from(providerType)) | ||
|
||
val memberResponse = MemberResponse(member.id!!, member.role) | ||
val tokenResponse = TokenResponse(jwtService.createAccessToken(member)) | ||
|
||
return LoginResponse(memberResponse, tokenResponse) | ||
} | ||
|
||
private fun findOrCreateMember( | ||
kakaoProviderId: Long, | ||
providerType: ProviderType | ||
): Member { | ||
return memberRepository.findByProviderIdAndProviderType(kakaoProviderId, providerType) | ||
?: memberRepository.save(Member(kakaoProviderId, providerType, ROLE_TEMP_USER)) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/vacgom/backend/application/auth/dto/LoginResponse.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,6 @@ | ||
package com.vacgom.backend.application.auth.dto | ||
|
||
data class LoginResponse( | ||
val member: MemberResponse, | ||
val token: TokenResponse | ||
) |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/vacgom/backend/application/auth/dto/MemberResponse.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,9 @@ | ||
package com.vacgom.backend.application.auth.dto | ||
|
||
import com.vacgom.backend.domain.auth.constants.Role | ||
import java.util.* | ||
|
||
data class MemberResponse( | ||
val memberId: UUID, | ||
val role: Role | ||
) |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/vacgom/backend/application/auth/dto/OauthTokenResponse.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,7 @@ | ||
package com.vacgom.backend.application.auth.dto | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming | ||
|
||
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy::class) | ||
data class OauthTokenResponse(val accessToken: String) |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/vacgom/backend/application/auth/dto/ResourceIdResponse.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,5 @@ | ||
package com.vacgom.backend.application.auth.dto | ||
|
||
data class ResourceIdResponse( | ||
var id: Long | ||
) |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/vacgom/backend/application/auth/dto/TokenResponse.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,6 @@ | ||
package com.vacgom.backend.application.auth.dto | ||
|
||
|
||
data class TokenResponse( | ||
val accessToken: String | ||
) |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/vacgom/backend/domain/auth/RefreshToken.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.vacgom.backend.domain.auth | ||
|
||
import com.vacgom.backend.domain.member.Member | ||
import com.vacgom.backend.global.auditing.BaseEntity | ||
import jakarta.persistence.* | ||
import java.time.LocalDateTime | ||
|
||
|
||
@Entity | ||
@Table(name = "t_refresh_token") | ||
class RefreshToken( | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id", nullable = false) | ||
var member: Member, | ||
var token: String, | ||
var userAgent: String, | ||
var expiredAt: LocalDateTime | ||
) : BaseEntity() { | ||
@Id | ||
@GeneratedValue | ||
@Column(name = "rt_id") | ||
private val id: Long? = null | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/vacgom/backend/domain/auth/constants/Role.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,19 @@ | ||
package com.vacgom.backend.domain.auth.constants | ||
|
||
enum class Role { | ||
ROLE_GUEST, | ||
ROLE_TEMP_USER, | ||
ROLE_USER; | ||
|
||
fun isGuest(role: Role): Boolean { | ||
return role == ROLE_GUEST | ||
} | ||
|
||
fun isTempUser(role: Role): Boolean { | ||
return role == ROLE_TEMP_USER | ||
} | ||
|
||
fun isUser(role: Role): Boolean { | ||
return role == ROLE_USER | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/vacgom/backend/domain/auth/oauth/OauthConnector.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,11 @@ | ||
package com.vacgom.backend.domain.auth.oauth | ||
|
||
import com.vacgom.backend.application.auth.dto.OauthTokenResponse | ||
import com.vacgom.backend.application.auth.dto.ResourceIdResponse | ||
import com.vacgom.backend.domain.auth.oauth.constants.ProviderType | ||
|
||
interface OauthConnector { | ||
fun isSupported(provider: ProviderType): Boolean | ||
fun fetchOauthToken(code: String): OauthTokenResponse | ||
fun fetchMemberInfo(accessToken: String): ResourceIdResponse | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/vacgom/backend/domain/auth/oauth/OauthUriGenerator.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,9 @@ | ||
package com.vacgom.backend.domain.auth.oauth | ||
|
||
import com.vacgom.backend.domain.auth.oauth.constants.ProviderType | ||
import java.net.URI | ||
|
||
interface OauthUriGenerator { | ||
fun isSupported(provider: ProviderType): Boolean | ||
fun generate(): URI | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/vacgom/backend/domain/auth/oauth/constants/ProviderType.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,19 @@ | ||
package com.vacgom.backend.domain.auth.oauth.constants | ||
|
||
import com.vacgom.backend.global.exception.error.BusinessException | ||
import com.vacgom.backend.global.security.exception.AuthError | ||
|
||
enum class ProviderType(val provider: String) { | ||
KAKAO("kakao"); | ||
|
||
companion object { | ||
fun from(provider: String): ProviderType { | ||
return entries.firstOrNull { it.provider == provider } | ||
?: throw BusinessException(AuthError.UNSUPPORTED_PROVIDER) | ||
h-beeen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
fun isKakao(): Boolean { | ||
return this == KAKAO | ||
} | ||
} |
24 changes: 14 additions & 10 deletions
24
src/main/kotlin/com/vacgom/backend/domain/member/Member.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 |
---|---|---|
@@ -1,21 +1,25 @@ | ||
package com.vacgom.backend.domain.member | ||
|
||
import com.vacgom.backend.domain.auth.constants.Role | ||
import com.vacgom.backend.domain.auth.oauth.constants.ProviderType | ||
import com.vacgom.backend.global.auditing.BaseEntity | ||
import jakarta.persistence.* | ||
import org.hibernate.annotations.GenericGenerator | ||
import java.util.* | ||
|
||
@Entity | ||
@Table(name = "t_member") | ||
class Member(nickname: String) : BaseEntity() { | ||
class Member( | ||
var providerId: Long, | ||
@Enumerated(EnumType.STRING) var providerType: ProviderType, | ||
@Enumerated(EnumType.STRING) var role: Role, | ||
) : BaseEntity() { | ||
|
||
@Id | ||
@Column(name = "member_id") | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
var id: Long? = null | ||
@GeneratedValue(generator = "uuid2") | ||
@GenericGenerator(name = "uuid2", strategy = "uuid2") | ||
@Column(columnDefinition = "BINARY(16)", name = "member_id") | ||
val id: UUID? = null | ||
|
||
@Column(name = "nickname") | ||
val nickname: String | ||
|
||
init { | ||
this.nickname = nickname | ||
} | ||
var name: String? = null | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/vacgom/backend/exception/member/MemberError.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,13 @@ | ||
package com.vacgom.backend.exception.member | ||
|
||
import com.vacgom.backend.global.exception.error.ErrorCode | ||
import org.springframework.http.HttpStatus | ||
|
||
|
||
enum class MemberError( | ||
override val message: String, | ||
override val status: HttpStatus, | ||
override val code: String | ||
) : ErrorCode { | ||
NOT_FOUND("์ฌ์ฉ์๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", HttpStatus.NOT_FOUND, "M_001"), | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
id๋ ๋ค๋ฅธ ํ๋ผํผํฐ๋ค ์ฒ๋ผ ์์ฑ์ ์๋ฆฌ์์ ์ ์ํ์ ๋ ๋ฌธ์ ์์ต๋๋ค !
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.
๊ทธ๋ฆฌ๊ณ RefreshToken ์ํฐํฐ๋ BaseEntity ์์์ ์๋ฐ๊ณ ์๋๋ฐ ์๋๋ ๊ฒ์ธ์ง๋ ๊ถ๊ธํฉ๋๋ค !!
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.
@HyungJu ๊ถ๊ธํ ์ ์ด ์์ต๋๋ค!!
์์ฑ์๋ฅผ ํตํด RefreshToken์ ์์ฑํ๋ ค๊ณ ํ ๋,
id์ ๊ฐ์ GeneratedValue Property๋ฅผ null๋ก ํ ๋นํด ์์ ํ๋์?
๊ฐ์ฒด ์์ฑ ํ๋ผ๋ฏธํฐ์ null์ ๋ฃ๋๊ฒ ์ด์ํด์ ๋ถ๋ฆฌํด๋ณด์๋๋ฐ, ์ ๋ถ๋ถ๋ ์๊ฒฌ ๋ถํ๋๋ฆด๊ฒ์!!
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.
RefreshToken์ญ์ BaseEntity ์์์ ๋ฐ์์ผ ๋ง์ต๋๋ค!
์ ๋ถ๋ถ๋ ๋ฐ์ํด๋๊ฒ์!!
p.s.Redis ๋ง์ด๊ทธ๋ ์ด์ ์ผ๋์ค..
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.
์ฌ์ค ์ Domain Model -> Entity ๊ฐ ๋ณํ์ ํ ๋ id๋ ํจ๊ป ๋งคํ์ ํด์ค์ผ ํ๊ธฐ ๋๋ฌธ์ .. ๊ทธ๋ ๊ฒ ํ๊ณ ์์ด์
(์ด๋ฏธ์ง ์ฐธ์กฐ)
์๋๋ฉด named argument๋ฅผ ์ฌ์ฉํ๋ฉด ์กฐ๊ธ ๋ณด๊ธฐ ์ข๊ฒ ํํํ ์๋ ์์ต๋๋ค ! (named argument๋ฅผ ์ฌ์ฉํ๋ฉด default value๊ฐ ์ง์ ๋์ด ์์๋ ํด๋น argument๋ ์๋ตํ ์ ์์ด์)
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.
์คํธ ์ด ๋ถ๋ถ์ ์ฒ์ ์ ํ๋ ์ ๋ณด์ธ๋ฐ์..
์ด๋ค ๋๋์ธ์ง๋ ์ ๊ฒ ๊ฐ์ต๋๋ค!
์ ๋ถ๋ถ๋ #8 PR์์ ๋ฐ์ํด๋ณผ๊ฒ์!!