-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdelete_token.go
79 lines (62 loc) · 1.83 KB
/
delete_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
// SPDX-License-Identifier: Apache-2.0
package user
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal/token"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
)
// swagger:operation DELETE /api/v1/user/token users DeleteToken
//
// Delete a token for the current authenticated user
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully delete a token for the current user
// schema:
// type: string
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"
// '503':
// description: Unable to delete a token for the current user
// schema:
// "$ref": "#/definitions/Error"
// DeleteToken represents the API handler to revoke
// and recreate a user token.
func DeleteToken(c *gin.Context) {
// capture middleware values
l := c.MustGet("logger").(*logrus.Entry)
u := user.Retrieve(c)
ctx := c.Request.Context()
l.Debugf("revoking token for user %s", u.GetName())
tm := c.MustGet("token-manager").(*token.Manager)
// compose JWT token for user
rt, at, err := tm.Compose(c, u)
if err != nil {
retErr := fmt.Errorf("unable to compose token for user %s: %w", u.GetName(), err)
util.HandleError(c, http.StatusServiceUnavailable, retErr)
return
}
u.SetRefreshToken(rt)
// send API call to update the user
_, err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)
util.HandleError(c, http.StatusServiceUnavailable, retErr)
return
}
l.Info("user updated - token rotated")
c.JSON(http.StatusOK, types.Token{Token: &at})
}