-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathauthentication.go
101 lines (77 loc) · 2.43 KB
/
authentication.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
93
94
95
96
97
98
99
100
101
// SPDX-License-Identifier: Apache-2.0
package server
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-vela/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
)
const (
// TokenRefreshResp represents a JSON return for a token refresh.
//nolint:gosec // not a hardcoded credential
TokenRefreshResp = `{
"token": "header.payload.signature"
}`
)
// getTokenRefresh returns mock JSON for a http GET.
func getTokenRefresh(c *gin.Context) {
data := []byte(TokenRefreshResp)
var body library.Token
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusOK, body)
}
// getAuthenticate returns mock response for a http GET.
//
// Don't pass "state" and "code" params to receive an error response.
func getAuthenticate(c *gin.Context) {
data := []byte(TokenRefreshResp)
state := c.Request.FormValue("state")
code := c.Request.FormValue("code")
err := "error"
if len(state) == 0 && len(code) == 0 {
c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &err})
return
}
var body library.Token
_ = json.Unmarshal(data, &body)
c.SetCookie(constants.RefreshTokenName, "refresh", 2, "/", "", true, true)
c.JSON(http.StatusOK, body)
}
// getAuthenticateFromToken returns mock response for a http POST.
//
// Don't pass "Token" in header to receive an error message.
func getAuthenticateFromToken(c *gin.Context) {
data := []byte(TokenRefreshResp)
err := "error"
token := c.Request.Header.Get("Token")
if len(token) == 0 {
c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &err})
}
var body library.Token
_ = json.Unmarshal(data, &body)
c.JSON(http.StatusOK, body)
}
// validateToken returns mock response for a http GET.
//
// Don't pass "Authorization" in header to receive an unauthorized error message.
func validateToken(c *gin.Context) {
err := "error"
token := c.Request.Header.Get("Authorization")
if len(token) == 0 {
c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &err})
}
c.JSON(http.StatusOK, "vela-server")
}
// validateOAuthToken returns mock response for a http GET.
//
// Don't pass "Authorization" in header to receive an unauthorized error message.
func validateOAuthToken(c *gin.Context) {
err := "error"
token := c.Request.Header.Get("Authorization")
if len(token) == 0 {
c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &err})
}
c.JSON(http.StatusOK, "oauth token was created by vela")
}