Skip to content

Commit

Permalink
feat: add validate developer token
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliothmoon committed Jan 7, 2025
1 parent 75b0ab9 commit 07bc749
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/main/kotlin/biz/service.kt → src/main/kotlin/biz/cdkey.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package biz


import datasource.DB
import exception.ServiceException
import model.PlanParams
Expand Down Expand Up @@ -45,7 +46,7 @@ fun next(): String {
}


fun acquire(params: PlanParams): Resp {
fun acquireCDK(params: PlanParams): Resp {
with(params) {
(params.expireTime == null || params.expireTime!!.isBefore(LocalDateTime.now())).throwIf("过期时间设置有误")
}
Expand All @@ -64,7 +65,7 @@ fun acquire(params: PlanParams): Resp {
}


fun validate(params: ValidateParams): Resp {
fun validateCDK(params: ValidateParams): Resp {
with(params) {
specificationId.isNullOrBlank().throwIf("specificationId不能为空")
cdk.isNullOrBlank().throwIf("CDK不能为空")
Expand Down Expand Up @@ -105,8 +106,5 @@ fun validate(params: ValidateParams): Resp {
set(OperationLog.type, "validate")
}


// TODO 重定向下载地址

return Resp.success(1)
}
return Resp.success()
}
64 changes: 64 additions & 0 deletions src/main/kotlin/biz/devloper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package biz

import datasource.DB
import model.Resp
import model.entity.Application
import model.entity.ApplicationToken
import org.ktorm.dsl.*
import utils.throwIf
import utils.throwIfNot
import utils.throwIfNullOrEmpty
import java.util.*

/**
* Dev Token验证
* @param [token]
* @return [Resp]
*/
fun validateToken(token: String?): Resp {
val tk = token.throwIfNullOrEmpty("Token不能为空")
val qr = DB.from(ApplicationToken)
.select(ApplicationToken.status, ApplicationToken.id)
.where {
ApplicationToken.applicationToken eq tk
}
.limit(1)
.iterator()
qr.hasNext().throwIfNot("Token无效")

qr.next().apply {
(get(ApplicationToken.status) != 1).throwIf("Token状态不正确")
}

return Resp.success()
}

/**
* WIP
* @param [applicationName]
* @return [Resp]
*/
fun createApplication(applicationName: String?): Resp {
applicationName.throwIfNullOrEmpty("应用名不能为空")

DB.insertAndGenerateKey(Application) {
set(Application.applicationName, applicationName)
}
return Resp.success()
}

/**
* WIP
* @param [applicationId]
* @return [Resp]
*/
fun createApplicationToken(applicationId: Int?): Resp {
applicationId.throwIfNullOrEmpty("应用Id不能为空")

val uuid = UUID.randomUUID().toString()
DB.insert(ApplicationToken) {
set(ApplicationToken.applicationId, applicationId)
set(ApplicationToken.applicationToken, uuid)
}
return Resp.success(uuid)
}
14 changes: 14 additions & 0 deletions src/main/kotlin/model/entity/entities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ object OperationLog : Table<Nothing>("mwf_operation_log") {
val type = varchar("type")
val createdAt = datetime("created_at")
}

object Application : Table<Nothing>("mirrorc_application") {
val id = int("id").primaryKey()
val applicationName = varchar("application_name")
val createdAt = datetime("created_at")
}

object ApplicationToken : Table<Nothing>("mirrorc_application_token") {
val id = int("id").primaryKey()
val applicationId = int("application_id")
val applicationToken = varchar("application_token")
val status = int("status")
val createdAt = datetime("created_at")
}
15 changes: 15 additions & 0 deletions src/main/kotlin/model/param.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ class ValidateParams {
* */
var source: String? = null
}


class CreateApplicationParams {
/**
* 应用名称
* */
var applicationName: String? = null
}

class CreateTokenParams {
/**
* 应用ID
* */
var applicationId: Int? = null
}
19 changes: 15 additions & 4 deletions src/main/kotlin/router/router.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package router

import biz.acquire
import biz.validate
import biz.acquireCDK
import biz.validateCDK
import biz.validateToken
import com.alibaba.fastjson2.JSON
import exception.ServiceException
import io.vertx.core.Promise
Expand Down Expand Up @@ -67,7 +68,7 @@ private fun dispatch(router: Router) {
val p = JSON.parseObject(it, PlanParams::class.java)
ctx.response().putHeader("Content-Type", "application/json")
Promise.promise<String>().execute(ctx) {
acquire(p).toJson()
acquireCDK(p).toJson()
}
}
}
Expand All @@ -76,8 +77,18 @@ private fun dispatch(router: Router) {
val p = JSON.parseObject(it, ValidateParams::class.java)
ctx.response().putHeader("Content-Type", "application/json")
Promise.promise<String>().execute(ctx) {
validate(p).toJson()
validateCDK(p).toJson()
}
}
}

router.post("/develop/validate").handler { ctx ->
val token: String? = ctx.request().getParam("token")
ctx.response().putHeader("Content-Type", "application/json")
Promise.promise<String>().execute(ctx) {
validateToken(token).toJson()
}
}


}
7 changes: 7 additions & 0 deletions src/main/kotlin/utils/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ fun Boolean.throwIf(msg: String) {
}
}

fun <T> T?.throwIfNullOrEmpty(msg: String): T {
if (this == null || (this is String && this.isEmpty())) {
throw ServiceException(msg)
}
return this
}

fun Boolean.throwIfNot(msg: String) {
(!this).throwIf(msg)
}
Expand Down

0 comments on commit 07bc749

Please sign in to comment.