-
Notifications
You must be signed in to change notification settings - Fork 28
/
token.go
66 lines (55 loc) · 1.59 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
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package api
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/router/middleware/token"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
)
// swagger:operation GET /token-refresh authenticate GetRefreshAccessToken
//
// Refresh an access token
//
// ---
// produces:
// - application/json
// parameters:
// - in: cookie
// name: vela_refresh_token
// required: true
// schema:
// type: string
// responses:
// '200':
// description: Successfully refreshed a token
// schema:
// "$ref": "#/definitions/Login"
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"
// RefreshAccessToken will return a new access token if the provided
// refresh token via cookie is valid.
func RefreshAccessToken(c *gin.Context) {
// capture the refresh token
// TODO: move this into token package and do it internally
// since we are already passsing context
rt, err := token.RetrieveRefreshToken(c.Request)
if err != nil {
retErr := fmt.Errorf("refresh token error: %w", err)
util.HandleError(c, http.StatusUnauthorized, retErr)
return
}
// validate the refresh token and return a new access token
newAccessToken, err := token.Refresh(c, rt)
if err != nil {
retErr := fmt.Errorf("unable to refresh token: %w", err)
util.HandleError(c, http.StatusUnauthorized, retErr)
return
}
c.JSON(http.StatusOK, library.Login{Token: &newAccessToken})
}