CreateAccessToken godoc @Security ApiKey @Summary [Deprecated: use POST:/token/{app}/{username} instead] @Description create access token for a user @Tags Access token @Accept json @Produce json @Param username path string true "username" @Param current_use
(ctx *gin.Context)
| 45 | // @Failure 500 {object} types.APIInternalServerError "Internal server error" |
| 46 | // @Router /user/{username}/tokens [post] |
| 47 | func (h *AccessTokenHandler) Create(ctx *gin.Context) { |
| 48 | currentUser := httpbase.GetCurrentUser(ctx) |
| 49 | if currentUser == "" { |
| 50 | httpbase.UnauthorizedError(ctx, component.ErrUserNotFound) |
| 51 | return |
| 52 | } |
| 53 | var req types.CreateUserTokenRequest |
| 54 | if err := ctx.ShouldBindJSON(&req); err != nil { |
| 55 | slog.Error("Bad request format", "error", err) |
| 56 | httpbase.BadRequest(ctx, err.Error()) |
| 57 | return |
| 58 | } |
| 59 | var err error |
| 60 | _, err = h.sc.CheckRequest(ctx, &req) |
| 61 | if err != nil { |
| 62 | slog.Error("failed to check sensitive request", slog.Any("error", err)) |
| 63 | httpbase.BadRequest(ctx, fmt.Errorf("sensitive check failed: %w", err).Error()) |
| 64 | return |
| 65 | } |
| 66 | if req.Application == "" { |
| 67 | req.Application = types.AccessTokenAppGit |
| 68 | } |
| 69 | |
| 70 | req.Username = ctx.Param("username") |
| 71 | if currentUser != req.Username { |
| 72 | slog.Error("user can only create its own access token", slog.String("current_user", currentUser), slog.String("username", req.Username)) |
| 73 | httpbase.UnauthorizedError(ctx, errors.New("user can only create its own access token")) |
| 74 | return |
| 75 | } |
| 76 | token, err := h.c.Create(ctx, &req) |
| 77 | if err != nil { |
| 78 | slog.Error("Failed to create user access token", slog.String("user_name", req.Username), slog.Any("error", err)) |
| 79 | httpbase.ServerError(ctx, err) |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | httpbase.OK(ctx, token) |
| 84 | } |
| 85 | |
| 86 | // CreateAppToken godoc |
| 87 | // @Security ApiKey |
no test coverage detected