Skip to content

Commit 8f7793f

Browse files
authored
Merge pull request #12 from team-xquare/138-github-oauth
2 parents 036b40f + 0bad19b commit 8f7793f

File tree

10 files changed

+119
-6
lines changed

10 files changed

+119
-6
lines changed

git-application/src/main/kotlin/com/xquare/git/git/exceptions/GitExceptions.kt

+3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ sealed class GitExceptions(
1111

1212
class AlreadyExists(message: String = ALREADY_EXISTS) : GitExceptions(409, message)
1313

14+
class UnauthorizedToken(message: String = UNAUTHORIZED_TOKEN) : GitExceptions(401, message)
15+
1416
companion object {
1517
private const val NOT_FOUND = "Not Found"
1618
private const val ALREADY_EXISTS = "Already Exists"
19+
private const val UNAUTHORIZED_TOKEN = "Unauthorized Token"
1720
}
1821
}

git-application/src/main/kotlin/com/xquare/git/git/facade/GitFacade.kt

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@ package com.xquare.git.git.facade
22

33
import com.xquare.git.git.dto.FindAllUserResponse
44
import com.xquare.git.git.dto.FindUserElement
5-
import com.xquare.git.git.usecase.CheckUsernameUseCase
65
import com.xquare.git.git.usecase.FindAllGitUseCase
76
import com.xquare.git.git.usecase.FindGitByCurrentUserIdUseCase
87
import com.xquare.git.git.usecase.SaveUsernameUseCase
98
import com.xquare.git.git.usecase.UpdateGitUseCase
9+
import com.xquare.git.github.usecase.GetGithubAccessTokenUseCase
10+
import com.xquare.git.github.usecase.GetGithubUserInfoUseCase
1011
import com.xquare.git.user.usecase.GetUserIdUseCase
1112
import org.springframework.stereotype.Component
1213
import org.springframework.transaction.annotation.Transactional
1314

1415
@Component
1516
open class GitFacade(
1617
private val saveUsernameUseCase: SaveUsernameUseCase,
17-
private val checkUsernameUseCase: CheckUsernameUseCase,
1818
private val findAllGitUseCase: FindAllGitUseCase,
1919
private val updateGitUseCase: UpdateGitUseCase,
2020
private val getUserIdUseCase: GetUserIdUseCase,
2121
private val findGitByCurrentUserId: FindGitByCurrentUserIdUseCase,
22+
private val getGithubAccessTokenUseCase: GetGithubAccessTokenUseCase,
23+
private val getGithubUserInfoUseCase: GetGithubUserInfoUseCase,
2224
) {
23-
open suspend fun saveUsername(currentUserId: String?, username: String) {
24-
checkUsernameUseCase.execute(username)
25+
open suspend fun saveUsername(currentUserId: String?, code: String) {
2526
val userId = getUserIdUseCase.execute(currentUserId)
27+
val token = getGithubAccessTokenUseCase.execute(code)
28+
val username = getGithubUserInfoUseCase.execute(token)
29+
2630
saveUsernameUseCase.execute(userId, username)
2731
}
2832

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.xquare.git.github.dto
2+
3+
data class GetGithubUserInfoResponse(
4+
val login: String?,
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.xquare.git.github.dto
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty
4+
5+
data class TokenResponse(
6+
@JsonProperty("access_token")
7+
val accessToken: String?,
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.xquare.git.github.spi
2+
3+
interface GithubPort : QueryGithubPort
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.xquare.git.github.spi
2+
3+
import com.xquare.git.github.dto.GetGithubUserInfoResponse
4+
import com.xquare.git.github.dto.TokenResponse
5+
6+
interface QueryGithubPort {
7+
suspend fun getAccessToken(code: String): TokenResponse
8+
suspend fun getGithubUserInfo(token: String): GetGithubUserInfoResponse
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.xquare.git.github.usecase
2+
3+
import com.xquare.git.annotations.UseCase
4+
import com.xquare.git.git.exceptions.GitExceptions
5+
import com.xquare.git.github.spi.QueryGithubPort
6+
7+
@UseCase
8+
class GetGithubAccessTokenUseCase(
9+
private val queryGithubPort: QueryGithubPort,
10+
) {
11+
suspend fun execute(code: String): String {
12+
return queryGithubPort.getAccessToken(code).accessToken
13+
?: throw GitExceptions.UnauthorizedToken()
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.xquare.git.github.usecase
2+
3+
import com.xquare.git.annotations.UseCase
4+
import com.xquare.git.git.exceptions.GitExceptions
5+
import com.xquare.git.github.spi.QueryGithubPort
6+
7+
@UseCase
8+
class GetGithubUserInfoUseCase(
9+
private val queryGithubPort: QueryGithubPort,
10+
) {
11+
suspend fun execute(token: String): String {
12+
return queryGithubPort.getGithubUserInfo(token).login
13+
?: throw GitExceptions.NotFound()
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.xquare.git.persistence.github.spi
2+
3+
import com.xquare.git.github.dto.GetGithubUserInfoResponse
4+
import com.xquare.git.github.dto.TokenResponse
5+
import com.xquare.git.github.spi.GithubPort
6+
import org.springframework.beans.factory.annotation.Value
7+
import org.springframework.http.MediaType
8+
import org.springframework.stereotype.Component
9+
import org.springframework.web.reactive.function.client.WebClient
10+
import org.springframework.web.reactive.function.client.awaitBody
11+
12+
@Component
13+
class GithubPersistenceAdapter(
14+
private val webClient: WebClient,
15+
16+
@Value("\${auth.github.client-id}")
17+
private val clientId: String,
18+
19+
@Value("\${auth.github.client-secret}")
20+
private val clientSecret: String,
21+
) : GithubPort {
22+
override suspend fun getAccessToken(code: String): TokenResponse {
23+
return webClient.get()
24+
.uri {
25+
it.scheme("https")
26+
.host("github.com")
27+
.path("/login/oauth/access_token")
28+
.queryParam("client_id", clientId)
29+
.queryParam("client_secret", clientSecret)
30+
.queryParam("code", code)
31+
.build()
32+
}
33+
.accept(MediaType.APPLICATION_JSON)
34+
.retrieve()
35+
.awaitBody()
36+
}
37+
38+
override suspend fun getGithubUserInfo(token: String): GetGithubUserInfoResponse {
39+
return webClient.get()
40+
.uri {
41+
it.scheme("https")
42+
.host("api.github.com")
43+
.path("/user")
44+
.build()
45+
}
46+
.header("Authorization", "token $token")
47+
.accept(MediaType.APPLICATION_JSON)
48+
.retrieve()
49+
.awaitBody()
50+
}
51+
}

git-presentation/src/main/kotlin/com/xquare/git/GitHandler.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class GitHandler(
1616
) {
1717
suspend fun saveUsername(serverRequest: ServerRequest): ServerResponse {
1818
val currentUserId = serverRequest.headers().firstHeader("Request-User-Id")
19-
val username = serverRequest.queryParam("username").orElse("")
20-
gitFacade.saveUsername(currentUserId, username)
19+
val code = serverRequest.queryParam("code").orElse("")
20+
gitFacade.saveUsername(currentUserId, code)
2121

2222
return ServerResponse.created(URI("/gits")).buildAndAwait()
2323
}

0 commit comments

Comments
 (0)