-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
196 lines (157 loc) · 5.27 KB
/
auth.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package handlers
import (
"context"
"encoding/json"
"net/http"
"sync"
"time"
"github.com/toddgaunt/bastion/internal/auth"
"github.com/toddgaunt/bastion/internal/errors"
"github.com/toddgaunt/bastion/internal/log"
)
const claimsKey contextKey = "claims"
var tokensMutex = sync.Mutex{}
var tokens = make(map[auth.BearerToken]auth.Claims)
var duration = time.Duration(time.Second * 5)
// GetClaims retrieves the claims associated with a bearer token.
func GetClaims(token auth.BearerToken) (auth.Claims, bool) {
tokensMutex.Lock()
claims, ok := tokens[token]
delete(tokens, token)
tokensMutex.Unlock()
return claims, ok
}
// AddClaims associates a bearer token with authentication claims.
func AddClaims(token auth.BearerToken, claims auth.Claims) {
tokensMutex.Lock()
tokens[token] = claims
tokensMutex.Unlock()
}
var invalidCredentials = errors.Note{
StatusCode: http.StatusUnauthorized,
Detail: "enter a valid username and password",
}
// authResponse is the payload containing the tokens that the authentication endpoints return.
type authResponse struct {
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
}
// Authorize is a middleware that verifies the authorization token provided by
// a request, and continues to the next handler if successful.
func (env Env) Authorize(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
claims, err := env.SignKey.Verify(auth.JWT(token))
if err != nil {
prob := errors.Note{StatusCode: http.StatusUnauthorized, Detail: "couldn't verify token"}.Wrap(err)
handleError(w, prob, env.Logger)
return
}
if !claims.IsValid(env.Clock.Now()) {
prob := errors.Note{}.Wrap(errors.New("expired claims"))
handleError(w, prob, env.Logger)
return
}
ctx := context.WithValue(r.Context(), claimsKey, claims)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// Login authenticates a user and returns to them an access token and a refresh token.
func (env Env) Login(w http.ResponseWriter, r *http.Request) {
fn := func(w http.ResponseWriter, r *http.Request) errors.Problem {
username, password, ok := r.BasicAuth()
if !ok {
w.Header().Set("Www-Authenticate", `Basic realm="restricted"`)
return invalidCredentials.Wrap(errors.New("user must enter basic auth"))
}
claims, err := env.Auth.Authenticate(username, password)
if err != nil {
w.Header().Set("Www-Authenticate", `Basic realm="restricted"`)
return invalidCredentials.Wrap(err)
}
// Log successful authentications.
env.Logger.With("username", username).Print(log.Info, "Authenticated user")
refreshToken, err := auth.NewBearerToken()
if err != nil {
return statusInternal.Wrap(err)
}
AddClaims(refreshToken, claims)
accessToken, err := env.SignKey.Sign(claims, env.Clock.Now(), duration)
if err != nil {
return statusInternal.Wrap(err)
}
resp := authResponse{
RefreshToken: string(refreshToken),
AccessToken: string(accessToken),
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
return statusInternal.Wrap(enc.Encode(resp))
}
err := fn(w, r)
handleError(w, err, env.Logger)
}
// Token accepts a refresh Token from the request and sends back a new refresh and access Token.
func (env Env) Token(w http.ResponseWriter, r *http.Request) {
fn := func(w http.ResponseWriter, r *http.Request) errors.Problem {
var refreshToken auth.BearerToken
dec := json.NewDecoder(r.Body)
err := dec.Decode(&refreshToken)
if err != nil {
return errors.Note{
StatusCode: http.StatusBadRequest,
Detail: "failed to decode token",
}.Wrap(err)
}
claims, ok := GetClaims(refreshToken)
if !ok {
return errors.Note{
Op: "Auth",
StatusCode: http.StatusUnauthorized,
Detail: "invalid refresh token",
}.Wrap(errors.Errorf("refresh token %s not found", refreshToken))
//w.Header().Set("WWW-Authenticate", `Basic realm="restricted"`)
//w.Header().Add("Location", "/auth/login")
//w.WriteHeader(http.StatusTemporaryRedirect)
//return nil
}
if !claims.IsValid(env.Clock.Now()) {
return errors.Note{
Op: "Auth",
StatusCode: http.StatusUnauthorized,
Detail: "invalid refresh token",
}.Wrap(errors.New("refresh token claims are expired"))
}
// Log successful authentications.
env.Logger.With("token", refreshToken, "claims", claims).Print(log.Info, "Refreshed authentication")
resp, prob := env.generateTokens(claims)
if prob != nil {
return prob
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
return errors.Note{
StatusCode: http.StatusInternalServerError,
}.Wrap(enc.Encode(resp))
}
err := fn(w, r)
handleError(w, err, env.Logger)
}
func (env Env) generateTokens(claims auth.Claims) (*authResponse, errors.Error) {
refreshToken, err := auth.NewBearerToken()
if err != nil {
return nil, statusInternal.Wrap(err)
}
AddClaims(refreshToken, claims)
accessToken, err := env.SignKey.Sign(claims, env.Clock.Now(), duration)
if err != nil {
return nil, statusInternal.Wrap(err)
}
resp := authResponse{
RefreshToken: string(refreshToken),
AccessToken: string(accessToken),
}
return &resp, nil
}