forked from mandiant/gocrack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
141 lines (120 loc) · 3.04 KB
/
auth.go
File metadata and controls
141 lines (120 loc) · 3.04 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package web
import (
"fmt"
"net/http"
"time"
"github.com/fireeye/gocrack/server/authentication"
"github.com/fireeye/gocrack/server/storage"
"github.com/gin-gonic/gin"
)
type expiredAuth struct {
Expired bool `json:"expired"`
Error string `json:"error"`
}
// userIsAdmin will prevent a call to the API if the user is not logged in or is not an admin
func userIsAdmin() gin.HandlerFunc {
return func(c *gin.Context) {
claim := getClaimInformation(c)
if claim == nil || (claim != nil && !claim.IsAdmin) {
c.JSON(http.StatusUnauthorized, &WebAPIError{
StatusCode: http.StatusUnauthorized,
UserError: "You are not authorized to view this resource",
})
c.Abort()
return
}
c.Next()
}
}
func (s *Server) requestHasValidAuth() gin.HandlerFunc {
return func(c *gin.Context) {
authCookie, err := c.Cookie("Auth")
if authCookie == "" || err != nil {
c.JSON(http.StatusUnauthorized, &WebAPIError{
StatusCode: http.StatusUnauthorized,
UserError: "Username and/or password is incorrect",
})
c.Abort()
return
}
claim, err := s.auth.VerifyClaim(authCookie, "gocrack", "api")
if err != nil {
if err == authentication.ErrExpired {
c.JSON(http.StatusUnauthorized, &expiredAuth{
Expired: true,
Error: "Your API key has expired and must be refreshed.",
})
c.Abort()
return
}
c.JSON(http.StatusUnauthorized, &WebAPIError{
StatusCode: http.StatusUnauthorized,
Err: err,
UserError: "Username and/or password is incorrect",
})
c.Abort()
return
}
c.Set("claim", claim)
c.Next()
return
}
}
// LoginRequest defines the structure of a login request
type LoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
IsAPI bool `json:"api_only"`
}
func (s LoginRequest) validate() []string {
errs := make([]string, 0)
if s.Username == "" {
errs = append(errs, "username must not be empty")
}
if s.Password == "" {
errs = append(errs, "password must not be empty")
}
return errs
}
func (s *Server) webSubmitLogin(c *gin.Context) *WebAPIError {
var req LoginRequest
if err := c.BindJSON(&req); err != nil {
return &WebAPIError{
StatusCode: http.StatusBadRequest,
Err: err,
UserError: "Invalid login data",
}
}
if errs := req.validate(); len(errs) > 0 {
c.JSON(http.StatusBadRequest, APIValidationErrors{
Valid: false,
Errors: errs,
})
return nil
}
claim, err := s.auth.Login(req.Username, req.Password, req.IsAPI)
if err != nil || claim == "" {
invalidLoginCounter.Inc()
if err == storage.ErrNotFound {
err = fmt.Errorf("failed login from %s", req.Username)
}
return &WebAPIError{
StatusCode: http.StatusUnauthorized,
Err: err,
UserError: "Username and/or password is incorrect",
}
}
cookie := http.Cookie{
Name: "Auth",
Value: claim,
Expires: time.Now().UTC().Add(time.Hour * 24),
HttpOnly: true,
}
http.SetCookie(c.Writer, &cookie)
c.JSON(http.StatusOK, struct {
Token string `json:"token"`
}{
Token: claim,
})
return nil
}