@@ -16,12 +16,14 @@ import (
16
16
const admin string = "admin"
17
17
const adminUserID storage.UserID = 1
18
18
19
+
20
+
19
21
// Authenticate implements the interceptor interface.
20
22
// It adds a flag to the request context to indicate if the
21
23
// request is authenticated. If authenticated, it also checks
22
24
// if the request is trying to access an admin URI and returns
23
25
// error if the request is not from the admin user.
24
- func Authenticate (req * http.Request ) (* http.Request , error ) {
26
+ func Authenticate (req * http.Request , respWriter http. ResponseWriter ) (* http.Request , bool , error ) {
25
27
var (
26
28
uid storage.UserID
27
29
sid string
@@ -35,21 +37,25 @@ func Authenticate(req *http.Request) (*http.Request, error) {
35
37
if err != nil {
36
38
log .Info ("cannot extract uid/sid:" , sid , uid , err )
37
39
ctx = context .WithValue (ctx , REASON , err .Error ())
38
- req = req .WithContext (ctx )
39
- return req , authorize (ctx , req )
40
- }
41
- if err = VerifySession (sid , uid , nil ); err != nil {
40
+ } else if err = VerifySession (sid , uid , nil ); err != nil {
42
41
log .Info ("invalid session" , sid , uid , err )
43
42
ctx = context .WithValue (ctx , REASON , err .Error ())
44
- req = req .WithContext (ctx )
45
- return req , authorize (ctx , req )
43
+ } else {
44
+ log .Debug ("User " , uid , " is authenticated" )
45
+ ctx = context .WithValue (ctx , USER_ID , uid )
46
+ ctx = context .WithValue (ctx , SESSION_ID , sid )
47
+ ctx = context .WithValue (ctx , AUTHENTICATED , true )
46
48
}
47
- ctx = context .WithValue (ctx , USER_ID , uid )
48
- ctx = context .WithValue (ctx , SESSION_ID , sid )
49
- ctx = context .WithValue (ctx , AUTHENTICATED , true )
50
49
req = req .WithContext (ctx )
51
- log .Info ("request from" , uid , "is authorized." )
52
- return req , authorize (ctx , req )
50
+ err = authorize (ctx , req )
51
+ if err != nil {
52
+ log .Warn ("User " , uid , " is not authorized to access " , req .URL .Path )
53
+ ctx = context .WithValue (ctx , REASON , err .Error ())
54
+ req = req .WithContext (ctx )
55
+ conf .RedirectToLogin (conf .EncodePath (req .URL ), respWriter , req )
56
+ }
57
+ handledIfError := true
58
+ return req , handledIfError , err
53
59
}
54
60
55
61
// IsSessionTimeout returns if duration since lastAccess exceeds the max session lifetime
0 commit comments