Skip to content

Commit

Permalink
feat:支持管理我的 OAUTH TencentBlueKing#10995
Browse files Browse the repository at this point in the history
  • Loading branch information
hejieehe committed Sep 27, 2024
1 parent 8925be2 commit c3fe4cb
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ interface UserOauthResource {
userId: String,
@Parameter(description = "授权类型", required = true)
@QueryParam("oauthType")
oauthType: OauthType
oauthType: OauthType,
@Parameter(description = "回调链接(授权完以后的链接地址)", required = true)
@QueryParam("redirectUrl")
redirectUrl: String
): Result<OauthResetUrl>
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class UserOauthResourceImpl @Autowired constructor(
val userOauthService: UserOauthService
) : UserOauthResource {
override fun list(userId: String, projectId: String): Result<List<UserOauthInfo>> {
return Result(userOauthService.list(userId = userId, projectId = projectId))
return Result(
userOauthService.list(
userId = userId,
projectId = projectId
)
)
}

override fun relRepo(
Expand All @@ -42,13 +47,25 @@ class UserOauthResourceImpl @Autowired constructor(
projectId: String,
oauthType: OauthType
): Result<Boolean> {
userOauthService.delete(userId = userId,projectId = projectId, oauthType = oauthType)
userOauthService.delete(
userId = userId,
projectId = projectId,
oauthType = oauthType
)
return Result(true)
}

override fun reOauth(userId: String, oauthType: OauthType): Result<OauthResetUrl> {
override fun reOauth(
userId: String,
oauthType: OauthType,
redirectUrl: String
): Result<OauthResetUrl> {
return Result(
userOauthService.reOauth(userId = userId, oauthType = oauthType)
userOauthService.reOauth(
userId = userId,
oauthType = oauthType,
redirectUrl = redirectUrl
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ class UserOauthService @Autowired constructor(
)
}

fun reOauth(userId: String, oauthType: OauthType): OauthResetUrl {
return getService(oauthType).reOauth(userId)
fun reOauth(
userId: String,
oauthType: OauthType,
redirectUrl: String
): OauthResetUrl {
return getService(oauthType).reOauth(
userId = userId,
redirectUrl = redirectUrl
)
}

private fun getService(oauthType: OauthType): OauthService {
Expand All @@ -66,4 +73,4 @@ class UserOauthService @Autowired constructor(
OauthType.GITHUB -> repoGithubOauthService
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tencent.devops.auth.service.self

import com.tencent.devops.auth.constant.AuthMessageCode
import com.tencent.devops.auth.pojo.OauthRelResource
import com.tencent.devops.auth.pojo.OauthResetUrl
import com.tencent.devops.auth.pojo.enum.OauthType
import com.tencent.devops.common.api.enums.ScmType
import com.tencent.devops.common.api.exception.ErrorCodeException
Expand Down Expand Up @@ -53,6 +54,15 @@ abstract class AbstractRepoOauthService(
)
}

override fun reOauth(userId: String, redirectUrl: String): OauthResetUrl {
val reOauthUrl = client.get(ServiceOauthResource::class).reOauthUrl(
userId = userId,
scmType = convertOauthType(),
redirectUrl = redirectUrl
).data ?: ""
return OauthResetUrl(reOauthUrl)
}

private fun convertOauthType(): ScmType {
return when (oauthType) {
OauthType.GIT -> ScmType.CODE_GIT
Expand All @@ -67,4 +77,4 @@ abstract class AbstractRepoOauthService(
scmType = convertOauthType()
).data ?: 0
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ interface OauthService {
/**
* 重置授权
*/
fun reOauth(userId: String): OauthResetUrl
}
fun reOauth(userId: String, redirectUrl: String): OauthResetUrl
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,19 @@ interface ServiceOauthResource {
@QueryParam("scmType")
scmType: ScmType
): Result<Boolean>

@Operation(summary = "获取重置授权链接")
@GET
@Path("/reOauthUrl")
fun reOauthUrl(
@Parameter(description = "用户ID", required = true, example = AUTH_HEADER_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_USER_ID)
userId: String,
@Parameter(description = "回调链接(授权完以后的链接地址)", required = true)
@QueryParam("redirectUrl")
redirectUrl: String,
@Parameter(description = "代码库类型", required = true)
@QueryParam("scmType")
scmType: ScmType
): Result<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import com.tencent.devops.repository.pojo.AuthorizeResult
import com.tencent.devops.repository.pojo.enums.RedirectUrlTypeEnum
import com.tencent.devops.repository.pojo.oauth.GitOauthCallback
import com.tencent.devops.repository.pojo.oauth.GitToken
import com.tencent.devops.repository.pojo.oauth.RepositoryOauthInfo
import com.tencent.devops.repository.service.github.GithubOAuthService
import com.tencent.devops.repository.service.scm.IGitOauthService
import com.tencent.devops.repository.service.tgit.TGitOAuthService
Expand Down Expand Up @@ -113,6 +112,33 @@ class ServiceOauthResourceImpl @Autowired constructor(
return Result(true)
}

override fun reOauthUrl(userId: String, redirectUrl: String, scmType: ScmType): Result<String> {
val url = when (scmType) {
ScmType.CODE_GIT -> {
gitOauthService.getOauthUrl(
userId = userId,
redirectUrl = redirectUrl
)
}

ScmType.GITHUB -> {
githubOAuthService.getGithubOauth(
userId = userId,
projectId = "",
redirectUrlTypeEnum = RedirectUrlTypeEnum.SPEC,
specRedirectUrl = redirectUrl,
repoHashId = null
).redirectUrl
}

else -> {
logger.warn("cannot reset oauth, not support scm type $scmType")
""
}
}
return Result(url)
}

companion object {
private val logger = LoggerFactory.getLogger(ServiceOauthResourceImpl::class.java)
}
Expand Down

0 comments on commit c3fe4cb

Please sign in to comment.