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 4e82c88 commit 744614e
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

package com.tencent.devops.auth.api.user

import com.tencent.devops.auth.pojo.OauthRepository
import com.tencent.devops.auth.pojo.OauthRelResource
import com.tencent.devops.auth.pojo.OauthResetUrl
import com.tencent.devops.auth.pojo.UserOauthInfo
import com.tencent.devops.auth.pojo.enum.OauthType
import com.tencent.devops.common.api.auth.AUTH_HEADER_USER_ID
Expand All @@ -41,6 +42,7 @@ import javax.ws.rs.Consumes
import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.HeaderParam
import javax.ws.rs.POST
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
Expand Down Expand Up @@ -83,7 +85,7 @@ interface UserOauthResource {
@Parameter(description = "每页多少条", required = false, example = "20")
@QueryParam("pageSize")
pageSize: Int? = null
): Result<Page<OauthRepository>>
): Result<Page<OauthRelResource>>

@DELETE
@Path("/{projectId}/delete")
Expand All @@ -100,7 +102,7 @@ interface UserOauthResource {
oauthType: OauthType
): Result<Boolean>

@DELETE
@POST
@Path("/reOauth")
@Operation(summary = "重置授权")
fun reOauth(
Expand All @@ -110,5 +112,5 @@ interface UserOauthResource {
@Parameter(description = "授权类型", required = true)
@QueryParam("oauthType")
oauthType: OauthType
): Result<Boolean>
): Result<OauthResetUrl>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tencent.devops.auth.pojo

import io.swagger.v3.oas.annotations.media.Schema

@Schema(title = "用户Oauth关联的资源")
data class OauthRelResource(
@get:Schema(title = "资源名称")
val name: String,
@get:Schema(title = "资源链接")
val url: String
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tencent.devops.auth.pojo

import io.swagger.v3.oas.annotations.media.Schema

@Schema(title = "重置Oauth授权信息")
data class OauthResetUrl(
val url: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ data class UserOauthInfo(
@get:Schema(title = "创建时间")
val createTime: Long?,
@get:Schema(title = "授权类型")
val type: OauthType
val type: OauthType,
@get:Schema(title = "授权类型")
val expired: Boolean
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.tencent.devops.auth.resources

import com.tencent.devops.auth.api.user.UserOauthResource
import com.tencent.devops.auth.pojo.OauthRepository
import com.tencent.devops.auth.pojo.OauthRelResource
import com.tencent.devops.auth.pojo.OauthResetUrl
import com.tencent.devops.auth.pojo.UserOauthInfo
import com.tencent.devops.auth.pojo.enum.OauthType
import com.tencent.devops.auth.service.UserOauthService
Expand All @@ -24,7 +25,7 @@ class UserOauthResourceImpl @Autowired constructor(
oauthType: OauthType,
page: Int?,
pageSize: Int?
): Result<Page<OauthRepository>> {
): Result<Page<OauthRelResource>> {
return Result(
userOauthService.relRepo(
userId = userId,
Expand All @@ -45,7 +46,9 @@ class UserOauthResourceImpl @Autowired constructor(
return Result(true)
}

override fun reOauth(userId: String, oauthType: OauthType): Result<Boolean> {
TODO("Not yet implemented")
override fun reOauth(userId: String, oauthType: OauthType): Result<OauthResetUrl> {
return Result(
userOauthService.reOauth(userId = userId, oauthType = oauthType)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tencent.devops.auth.service

import com.tencent.devops.auth.pojo.OauthRepository
import com.tencent.devops.auth.pojo.OauthRelResource
import com.tencent.devops.auth.pojo.OauthResetUrl
import com.tencent.devops.auth.pojo.UserOauthInfo
import com.tencent.devops.auth.pojo.enum.OauthType
import com.tencent.devops.auth.service.self.OauthService
Expand Down Expand Up @@ -35,24 +36,13 @@ class UserOauthService @Autowired constructor(
oauthType: OauthType,
page: Int,
pageSize: Int
): Page<OauthRepository> {
val relRepo = getService(oauthType).relRepo(
): Page<OauthRelResource> {
return getService(oauthType).relSource(
userId = userId,
projectId = projectId,
page = page,
pageSize = pageSize
)
return Page(
page = relRepo.page,
count = relRepo.count,
pageSize = relRepo.pageSize,
records = relRepo.records.map {
OauthRepository(
aliasName = it.aliasName,
url = it.url
)
}
)
}

fun delete(
Expand All @@ -66,8 +56,8 @@ class UserOauthService @Autowired constructor(
)
}

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

private fun getService(oauthType: OauthType): OauthService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
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.enum.OauthType
import com.tencent.devops.common.api.enums.ScmType
import com.tencent.devops.common.api.exception.ErrorCodeException
import com.tencent.devops.common.api.pojo.Page
import com.tencent.devops.common.client.Client
import com.tencent.devops.repository.api.ServiceOauthResource
import com.tencent.devops.repository.api.ServiceRepositoryResource
import com.tencent.devops.repository.pojo.RepoOauthRefVo

/**
* 代码库OAUTH授权
* 代码库OAUTH授权抽象类
*/
abstract class AbstractRepoOauthService(
open val client: Client,
open val oauthType: OauthType
) : OauthService {
override fun relRepo(userId: String, projectId: String, page: Int, pageSize: Int): Page<RepoOauthRefVo> {
override fun relSource(userId: String, projectId: String, page: Int, pageSize: Int): Page<OauthRelResource> {
return client.get(ServiceRepositoryResource::class).listOauthRepo(
projectId = projectId,
userId = userId,
scmType = convertOauthType(),
page = page,
pageSize = pageSize
).data ?: Page(page = page, pageSize = pageSize, records = listOf(), count = 0)
).data?.let { pageInfo ->
Page(
records = pageInfo.records.map {
OauthRelResource(
name = it.aliasName,
url = it.url
)
},
count = pageInfo.count,
page = pageInfo.page,
pageSize = pageSize
)
} ?: Page(page = page, pageSize = pageSize, records = listOf(), count = 0)
}

override fun delete(userId: String, projectId: String) {
Expand All @@ -33,7 +46,11 @@ abstract class AbstractRepoOauthService(
errorCode = AuthMessageCode.OAUTH_INFO_OCCUPIED_CANNOT_DELETE
)
}
//TODO: 调用接口删除oauth信息
// 调用接口删除oauth信息
client.get(ServiceOauthResource::class).deleteOauth(
userId = userId,
scmType = convertOauthType()
)
}

private fun convertOauthType(): ScmType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.tencent.devops.auth.service.self

import com.tencent.devops.auth.pojo.OauthRelResource
import com.tencent.devops.auth.pojo.OauthResetUrl
import com.tencent.devops.auth.pojo.UserOauthInfo
import com.tencent.devops.auth.pojo.enum.OauthType
import com.tencent.devops.common.api.pojo.Page
import com.tencent.devops.repository.pojo.RepoOauthRefVo

/**
* 用户OAUTH资源接口
Expand All @@ -15,9 +15,9 @@ interface OauthService {
fun get(userId: String, projectId: String): UserOauthInfo?

/**
* 授权代码库列表
* 授权信息关联的资源列表
*/
fun relRepo(userId: String, projectId: String, page: Int, pageSize: Int): Page<RepoOauthRefVo>
fun relSource(userId: String, projectId: String, page: Int, pageSize: Int): Page<OauthRelResource>

/**
* 删除授权
Expand All @@ -27,5 +27,5 @@ interface OauthService {
/**
* 重置授权
*/
fun reOauth(userId: String)
fun reOauth(userId: String): OauthResetUrl
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.tencent.devops.auth.service.self

import com.tencent.devops.auth.pojo.OauthResetUrl
import com.tencent.devops.auth.pojo.UserOauthInfo
import com.tencent.devops.auth.pojo.enum.OauthType
import com.tencent.devops.common.client.Client
import com.tencent.devops.repository.api.ServiceOauthResource
import com.tencent.devops.repository.api.scm.ServiceGitResource
import com.tencent.devops.repository.pojo.enums.TokenTypeEnum
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

Expand All @@ -21,24 +23,33 @@ class RepoGitOauthService @Autowired constructor(
) {
override fun get(userId: String, projectId: String): UserOauthInfo? {
val gitToken = client.get(ServiceOauthResource::class).gitGet(userId).data ?: return null
// 授权过期
var expired = false
// 存量数据中可能存在A用户授权到B用户的情况
val oauthUserName = client.get(ServiceGitResource::class).getUserInfoByToken(
token = gitToken.accessToken,
tokenType = TokenTypeEnum.OAUTH
).data?.username ?: ""
val oauthUserName = try {
client.get(ServiceGitResource::class).getUserInfoByToken(
token = gitToken.accessToken,
tokenType = TokenTypeEnum.OAUTH
).data
} catch (ignored: Exception) {
logger.warn("get user info failed", ignored)
expired = true
null
}?.username ?: ""
return UserOauthInfo(
username = oauthUserName,
repoCount = countOauthRepo(projectId = projectId, userId = userId),
createTime = gitToken.createTime,
type = oauthType
type = oauthType,
expired = expired
)
}

override fun reOauth(userId: String) {
override fun reOauth(userId: String): OauthResetUrl {
TODO("Not yet implemented")
}

override fun delete(userId: String, projectId: String) {
super.delete(userId, projectId)
companion object{
private val logger = LoggerFactory.getLogger(RepoGitOauthService::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.tencent.devops.auth.service.self

import com.tencent.devops.auth.pojo.OauthResetUrl
import com.tencent.devops.auth.pojo.UserOauthInfo
import com.tencent.devops.auth.pojo.enum.OauthType
import com.tencent.devops.common.client.Client
import com.tencent.devops.repository.api.ServiceGithubResource
import com.tencent.devops.repository.api.github.ServiceGithubUserResource
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

Expand All @@ -22,18 +24,31 @@ class RepoGithubOauthService @Autowired constructor(
val gitToken = client.get(ServiceGithubResource::class).getAccessToken(
userId = userId
).data ?: return null
val oauthUserName = client.get(ServiceGithubUserResource::class).getUser(
token = gitToken.accessToken
).data?.login ?: ""
var expired = false
// github用户名和userId不一致
val oauthUserName = try {
client.get(ServiceGithubUserResource::class).getUser(
token = gitToken.accessToken
).data
} catch (ignored: Exception) {
logger.warn("get user info failed", ignored)
expired = true
null
}?.login ?: ""
return UserOauthInfo(
username = oauthUserName,
repoCount = countOauthRepo(projectId = projectId, userId = userId),
createTime = gitToken.createTime,
type = oauthType
type = oauthType,
expired = expired
)
}

override fun reOauth(userId: String) {
override fun reOauth(userId: String) : OauthResetUrl {
TODO("Not yet implemented")
}

companion object{
val logger = LoggerFactory.getLogger(RepoGithubOauthService::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package com.tencent.devops.repository.api

import com.tencent.devops.common.api.auth.AUTH_HEADER_USER_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_USER_ID_DEFAULT_VALUE
import com.tencent.devops.common.api.enums.ScmType
import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.repository.pojo.AuthorizeResult
import com.tencent.devops.repository.pojo.enums.RedirectUrlTypeEnum
Expand All @@ -38,6 +39,7 @@ import io.swagger.v3.oas.annotations.tags.Tag
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import javax.ws.rs.Consumes
import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.HeaderParam
import javax.ws.rs.Path
Expand Down Expand Up @@ -123,4 +125,16 @@ interface ServiceOauthResource {
@QueryParam("refreshToken")
refreshToken: Boolean? = false
): Result<AuthorizeResult>

@Operation(summary = "移除代码库OAUTH授权")
@DELETE
@Path("/users/{userId}")
fun deleteOauth(
@Parameter(description = "用户ID", required = true)
@PathParam("userId")
userId: String,
@Parameter(description = "代码库类型", required = true)
@QueryParam("scmType")
scmType: ScmType
): Result<Boolean>
}
Loading

0 comments on commit 744614e

Please sign in to comment.