|
| 1 | +/* |
| 2 | +Copyright 2021 The KodeRover Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package login |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/gin-gonic/gin" |
| 21 | + |
| 22 | + loginsvc "github.com/koderover/zadig/v2/pkg/microservice/user/core/service/login" |
| 23 | + internalhandler "github.com/koderover/zadig/v2/pkg/shared/handler" |
| 24 | +) |
| 25 | + |
| 26 | +// @Summary 初始化登录态 MFA 配置 |
| 27 | +// @Description 基于 mfa_challenge_token 生成当前登录挑战所需的 MFA 配置数据,返回 secret、二维码和 required_action |
| 28 | +// @Tags user |
| 29 | +// @Accept json |
| 30 | +// @Produce json |
| 31 | +// @Param body body loginsvc.MFASetupArgs true "body" |
| 32 | +// @Success 200 {object} loginsvc.MFASetupResp |
| 33 | +// @Router /api/v1/login/mfa/setup [post] |
| 34 | +func MFASetup(c *gin.Context) { |
| 35 | + ctx := internalhandler.NewContext(c) |
| 36 | + defer func() { internalhandler.JSONResponse(c, ctx) }() |
| 37 | + |
| 38 | + args := &loginsvc.MFASetupArgs{} |
| 39 | + if err := c.ShouldBindJSON(args); err != nil { |
| 40 | + ctx.RespErr = err |
| 41 | + return |
| 42 | + } |
| 43 | + ctx.Resp, ctx.RespErr = loginsvc.SetupMFA(args, ctx.Logger) |
| 44 | +} |
| 45 | + |
| 46 | +// @Summary 完成登录态 MFA 绑定 |
| 47 | +// @Description 在登录挑战阶段提交 OTP 完成首次 MFA 绑定,成功后返回正式登录态和恢复码 |
| 48 | +// @Tags user |
| 49 | +// @Accept json |
| 50 | +// @Produce json |
| 51 | +// @Param body body loginsvc.MFAEnrollArgs true "body" |
| 52 | +// @Success 200 {object} loginsvc.User |
| 53 | +// @Router /api/v1/login/mfa/enroll [post] |
| 54 | +func MFAEnroll(c *gin.Context) { |
| 55 | + ctx := internalhandler.NewContext(c) |
| 56 | + defer func() { internalhandler.JSONResponse(c, ctx) }() |
| 57 | + |
| 58 | + args := &loginsvc.MFAEnrollArgs{} |
| 59 | + if err := c.ShouldBindJSON(args); err != nil { |
| 60 | + ctx.RespErr = err |
| 61 | + return |
| 62 | + } |
| 63 | + ctx.Resp, ctx.RespErr = loginsvc.EnrollMFA(args, ctx.Logger) |
| 64 | +} |
| 65 | + |
| 66 | +// @Summary 完成登录态 MFA 验证 |
| 67 | +// @Description 在登录挑战阶段提交 OTP 或恢复码完成 MFA 验证,成功后返回正式登录态 |
| 68 | +// @Tags user |
| 69 | +// @Accept json |
| 70 | +// @Produce json |
| 71 | +// @Param body body loginsvc.MFAVerifyArgs true "body" |
| 72 | +// @Success 200 {object} loginsvc.User |
| 73 | +// @Router /api/v1/login/mfa/verify [post] |
| 74 | +func MFAVerify(c *gin.Context) { |
| 75 | + ctx := internalhandler.NewContext(c) |
| 76 | + defer func() { internalhandler.JSONResponse(c, ctx) }() |
| 77 | + |
| 78 | + args := &loginsvc.MFAVerifyArgs{} |
| 79 | + if err := c.ShouldBindJSON(args); err != nil { |
| 80 | + ctx.RespErr = err |
| 81 | + return |
| 82 | + } |
| 83 | + ctx.Resp, ctx.RespErr = loginsvc.VerifyMFA(args, ctx.Logger) |
| 84 | +} |
0 commit comments