-
Notifications
You must be signed in to change notification settings - Fork 62
/
token.go
92 lines (75 loc) · 2.42 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package handler
import (
"errors"
"net/http"
"time"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/pkg/gtime"
"github.com/bangumi/server/internal/web/req"
"github.com/bangumi/server/internal/web/res"
)
func (h Handler) CreatePersonalAccessToken(c *fiber.Ctx) error {
v := h.GetHTTPAccessor(c)
if !v.Login {
return c.Redirect("/demo/login")
}
var r req.CreatePersonalAccessToken
if err := json.UnmarshalNoEscape(c.Body(), &r); err != nil {
return res.JSONError(c, err)
}
if err := h.Common.V.Struct(r); err != nil {
return h.ValidationError(c, err)
}
token, err := h.a.CreateAccessToken(c.UserContext(), v.ID, r.Name, gtime.OneDay*time.Duration(r.DurationDays))
if err != nil {
return errgo.Wrap(err, "failed to create token")
}
return c.JSON(token)
}
func (h Handler) DeletePersonalAccessToken(c *fiber.Ctx) error {
v := h.GetHTTPAccessor(c)
if !v.Login {
return c.Redirect("/demo/login")
}
var r req.DeletePersonalAccessToken
if err := json.UnmarshalNoEscape(c.Body(), &r); err != nil {
return res.JSONError(c, err)
}
if err := h.Common.V.Struct(r); err != nil {
return h.ValidationError(c, err)
}
token, err := h.a.GetTokenByID(c.UserContext(), r.ID)
if err != nil {
if errors.Is(err, domain.ErrNotFound) {
return res.BadRequest("token not exist")
}
return errgo.Wrap(err, "failed to get token info")
}
if token.UserID != v.ID {
return res.Unauthorized("you don't have this token")
}
ok, err := h.a.DeleteAccessToken(c.UserContext(), r.ID)
if err != nil {
return errgo.Wrap(err, "failed to delete token")
}
if !ok {
return c.SendStatus(http.StatusNotFound)
}
return c.SendStatus(http.StatusNoContent)
}