Skip to content

Commit 5cb1c07

Browse files
committed
Updated src.
1 parent b375146 commit 5cb1c07

File tree

14 files changed

+385
-109
lines changed

14 files changed

+385
-109
lines changed

src/ngx_auth/exec/check_ldap/config.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import (
44
"os"
55

66
"github.com/naoina/toml"
7+
8+
"ngx_auth/htstat"
79
)
810

911
type NgxLdapAuthConfig struct {
10-
SocketType string
11-
SocketPath string
12-
CacheSeconds uint32 `toml:",omitempty"`
13-
UseEtag bool `toml:",omitempty"`
14-
AuthRealm string `toml:",omitempty"`
12+
SocketType string
13+
SocketPath string
14+
CacheSeconds uint32 `toml:",omitempty"`
15+
NegCacheSeconds uint32 `toml:",omitempty"`
16+
UseEtag bool `toml:",omitempty"`
17+
UseSerializedAuth bool `toml:",omitempty"`
18+
AuthRealm string `toml:",omitempty"`
1519

1620
HostUrl string
1721
StartTls int `toml:",omitempty"`
@@ -21,15 +25,19 @@ type NgxLdapAuthConfig struct {
2125
BindDn string
2226
UniqFilter string `toml:",omitempty"`
2327
Timeout int `toml:",omitempty"`
28+
29+
Response htstat.HttpStatusTbl `toml:",omitempty"`
2430
}
2531

2632
type NgxLdapPathAuthConfig struct {
27-
SocketType string
28-
SocketPath string
29-
CacheSeconds uint32 `toml:",omitempty"`
30-
UseEtag bool `toml:",omitempty"`
31-
AuthRealm string `toml:",omitempty"`
32-
PathHeader string `toml:",omitempty"`
33+
SocketType string
34+
SocketPath string
35+
CacheSeconds uint32 `toml:",omitempty"`
36+
NegCacheSeconds uint32 `toml:",omitempty"`
37+
UseEtag bool `toml:",omitempty"`
38+
UseSerializedAuth bool `toml:",omitempty"`
39+
AuthRealm string `toml:",omitempty"`
40+
PathHeader string `toml:",omitempty"`
3341

3442
Ldap struct {
3543
HostUrl string
@@ -50,6 +58,8 @@ type NgxLdapPathAuthConfig struct {
5058
DefaultRight string `toml:",omitempty"`
5159
PathRight map[string]string `toml:",omitempty"`
5260
}
61+
62+
Response htstat.HttpStatusTbl `toml:",omitempty"`
5363
}
5464

5565
func load_ldap_auth_config(file string) (*NgxLdapAuthConfig, error) {

src/ngx_auth/exec/ngx_header_path_auth/handle.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"encoding/binary"
55
"fmt"
6-
"io"
76
"net/http"
87

98
"ngx_auth/etag"
@@ -52,25 +51,59 @@ func makeEtag(ms int64, user, rpath string) string {
5251
return etag.Make(tm, etag.Crypt(tm, []byte(user)), []byte(pathid))
5352
}
5453

54+
func isModified(hd http.Header, org_tag string) bool {
55+
if_nmatch := hd.Get("If-None-Match")
56+
57+
if if_nmatch != "" {
58+
return !isEtagMatch(if_nmatch, org_tag)
59+
}
60+
61+
return true
62+
}
63+
64+
func isEtagMatch(tag_str string, org_tag string) bool {
65+
tags, _ := etag.Split(tag_str)
66+
for _, tag := range tags {
67+
if tag == org_tag {
68+
return true
69+
}
70+
}
71+
72+
return false
73+
}
74+
5575
func TestAuthHandler(w http.ResponseWriter, r *http.Request) {
56-
w.Header().Set("Cache-Control", "no-store")
5776
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
77+
w.Header().Set("Cache-Control", "no-store")
5878

5979
rpath := r.Header.Get(PathHeader)
6080
if rpath == "" {
61-
http.Error(w, "No path header", http.StatusForbidden)
81+
HttpResponse.Nopath.Error(w)
6282
return
6383
}
6484

6585
user := r.Header.Get(UserHeader)
6686
if user == "" {
67-
http.Error(w, "No user header", http.StatusForbidden)
87+
HttpResponse.Nouser.Error(w)
6888
return
6989
}
90+
91+
if NegCacheSeconds > 0 {
92+
w.Header().Set("Cache-Control",
93+
fmt.Sprintf("max-age=%d, must-revalidate", NegCacheSeconds))
94+
}
95+
7096
tag := makeEtag(StartTimeMS, user, rpath)
97+
w.Header().Set("Etag", tag)
98+
if UseEtag {
99+
if !isModified(r.Header, tag) {
100+
w.WriteHeader(http.StatusNotModified)
101+
return
102+
}
103+
}
71104

72105
if !get_path_right(rpath, user) {
73-
http.Error(w, "Forbidden", http.StatusForbidden)
106+
HttpResponse.Forbidden.Error(w)
74107
return
75108
}
76109

@@ -79,5 +112,5 @@ func TestAuthHandler(w http.ResponseWriter, r *http.Request) {
79112
fmt.Sprintf("max-age=%d, must-revalidate", CacheSeconds))
80113
}
81114
w.Header().Set("Etag", tag)
82-
io.WriteString(w, "Authorized\n")
115+
HttpResponse.Ok.Error(w)
83116
}

src/ngx_auth/exec/ngx_header_path_auth/main.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/naoina/toml"
1818

1919
"ngx_auth/authz"
20+
"ngx_auth/htstat"
2021
)
2122

2223
func die(format string, v ...interface{}) {
@@ -29,11 +30,13 @@ func warn(format string, v ...interface{}) {
2930
}
3031

3132
type NgxHeaderPathAuthConfig struct {
32-
SocketType string
33-
SocketPath string
34-
CacheSeconds uint32 `toml:",omitempty"`
35-
PathHeader string `toml:",omitempty"`
36-
UserHeader string `toml:",omitempty"`
33+
SocketType string
34+
SocketPath string
35+
CacheSeconds uint32 `toml:",omitempty"`
36+
NegCacheSeconds uint32 `toml:",omitempty"`
37+
UseEtag bool `toml:",omitempty"`
38+
PathHeader string `toml:",omitempty"`
39+
UserHeader string `toml:",omitempty"`
3740

3841
Authz struct {
3942
UserMapConfig string `toml:",omitempty"`
@@ -43,11 +46,15 @@ type NgxHeaderPathAuthConfig struct {
4346
DefaultRight string `toml:",omitempty"`
4447
PathRight map[string]string `toml:",omitempty"`
4548
}
49+
50+
Response htstat.HttpStatusTbl `toml:",omitempty"`
4651
}
4752

4853
var SocketType string
4954
var SocketPath string
5055
var CacheSeconds uint32
56+
var NegCacheSeconds uint32
57+
var UseEtag bool
5158

5259
var PathHeader = "X-Authz-Path"
5360
var PathPatternReg *regexp.Regexp
@@ -58,6 +65,8 @@ var NomatchRight string
5865
var DefaultRight string
5966
var PathRight map[string]string
6067

68+
var HttpResponse htstat.HttpStatusTbl
69+
6170
var StartTimeMS int64
6271

6372
func init() {
@@ -95,6 +104,8 @@ func init() {
95104
}
96105

97106
CacheSeconds = cfg.CacheSeconds
107+
NegCacheSeconds = cfg.NegCacheSeconds
108+
UseEtag = cfg.UseEtag
98109

99110
if cfg.PathHeader != "" {
100111
PathHeader = cfg.PathHeader
@@ -141,6 +152,13 @@ func init() {
141152
}
142153
}
143154

155+
cfg.Response.SetDefault()
156+
if !cfg.Response.IsValid() {
157+
die("response code config error.")
158+
return
159+
}
160+
HttpResponse = cfg.Response
161+
144162
StartTimeMS = time.Now().UnixMicro()
145163
}
146164

src/ngx_auth/exec/ngx_ldap_auth/handle.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@ package main
33
import (
44
"encoding/binary"
55
"fmt"
6-
"io"
76
"net/http"
87
"strings"
98

9+
"github.com/l4go/var_mtx"
10+
1011
"ngx_auth/etag"
1112
"ngx_auth/ldap_auth"
1213
)
1314

15+
var userMtx = var_mtx.NewVarMutex()
16+
1417
func auth(user string, pass string) bool {
1518
la, err := ldap_auth.NewLdapAuth(LdapAuthConfig)
1619
if err != nil {
1720
return false
1821
}
1922
defer la.Close()
2023

24+
if UseSerializedAuth {
25+
userMtx.Lock(user)
26+
defer userMtx.Unlock(user)
27+
}
28+
2129
ok_auth, _, err := la.Authenticate(user, pass)
2230
if err != nil {
2331
return false
@@ -26,10 +34,10 @@ func auth(user string, pass string) bool {
2634
return ok_auth
2735
}
2836

29-
func http_not_auth(w http.ResponseWriter, r *http.Request) {
37+
func http_not_auth(w http.ResponseWriter, _ *http.Request) {
3038
realm := strings.Replace(AuthRealm, `"`, `\"`, -1)
3139
w.Header().Add("WWW-Authenticate", `Basic realm="`+realm+`"`)
32-
http.Error(w, "Not authorized", http.StatusUnauthorized)
40+
HttpResponse.Unauth.Error(w)
3341
}
3442

3543
func set_int64bin(bin []byte, v int64) {
@@ -66,22 +74,24 @@ func isEtagMatch(tag_str string, org_tag string) bool {
6674
}
6775

6876
func TestAuthHandler(w http.ResponseWriter, r *http.Request) {
69-
w.Header().Set("Cache-Control", "no-store")
7077
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
78+
w.Header().Set("Cache-Control", "no-store")
7179

7280
user, pass, ok := r.BasicAuth()
7381
if !ok {
7482
http_not_auth(w, r)
7583
return
7684
}
7785

86+
if NegCacheSeconds > 0 {
87+
w.Header().Set("Cache-Control",
88+
fmt.Sprintf("max-age=%d, must-revalidate", NegCacheSeconds))
89+
}
90+
7891
tag := makeEtag(StartTimeMS, user, pass)
92+
w.Header().Set("Etag", tag)
7993
if UseEtag {
8094
if !isModified(r.Header, tag) {
81-
if CacheSeconds > 0 {
82-
w.Header().Set("Cache-Control",
83-
fmt.Sprintf("max-age=%d, must-revalidate", CacheSeconds))
84-
}
8595
w.Header().Set("Etag", tag)
8696
w.WriteHeader(http.StatusNotModified)
8797
return
@@ -97,6 +107,5 @@ func TestAuthHandler(w http.ResponseWriter, r *http.Request) {
97107
w.Header().Set("Cache-Control",
98108
fmt.Sprintf("max-age=%d, must-revalidate", CacheSeconds))
99109
}
100-
w.Header().Set("Etag", tag)
101-
io.WriteString(w, "Authorized\n")
110+
HttpResponse.Ok.Error(w)
102111
}

src/ngx_auth/exec/ngx_ldap_auth/main.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/l4go/task"
1616
"github.com/naoina/toml"
1717

18+
"ngx_auth/htstat"
1819
"ngx_auth/ldap_auth"
1920
)
2021

@@ -28,11 +29,13 @@ func warn(format string, v ...interface{}) {
2829
}
2930

3031
type NgxLdapAuthConfig struct {
31-
SocketType string
32-
SocketPath string
33-
CacheSeconds uint32 `toml:",omitempty"`
34-
UseEtag bool `toml:",omitempty"`
35-
AuthRealm string `toml:",omitempty"`
32+
SocketType string
33+
SocketPath string
34+
CacheSeconds uint32 `toml:",omitempty"`
35+
NegCacheSeconds uint32 `toml:",omitempty"`
36+
UseEtag bool `toml:",omitempty"`
37+
UseSerializedAuth bool `toml:",omitempty"`
38+
AuthRealm string `toml:",omitempty"`
3639

3740
HostUrl string
3841
StartTls int `toml:",omitempty"`
@@ -42,14 +45,20 @@ type NgxLdapAuthConfig struct {
4245
BindDn string
4346
UniqFilter string `toml:",omitempty"`
4447
Timeout int `toml:",omitempty"`
48+
49+
Response htstat.HttpStatusTbl `toml:",omitempty"`
4550
}
4651

4752
var SocketType string
4853
var SocketPath string
4954
var CacheSeconds uint32
55+
var NegCacheSeconds uint32
5056
var UseEtag bool
5157
var AuthRealm string
58+
var UseSerializedAuth bool
59+
5260
var LdapAuthConfig *ldap_auth.Config
61+
var HttpResponse htstat.HttpStatusTbl
5362

5463
var StartTimeMS int64
5564

@@ -88,7 +97,9 @@ func init() {
8897
}
8998

9099
CacheSeconds = cfg.CacheSeconds
100+
NegCacheSeconds = cfg.NegCacheSeconds
91101
UseEtag = cfg.UseEtag
102+
UseSerializedAuth = cfg.UseSerializedAuth
92103

93104
if cfg.AuthRealm == "" {
94105
die("relm is required")
@@ -106,6 +117,13 @@ func init() {
106117
Timeout: cfg.Timeout,
107118
}
108119

120+
cfg.Response.SetDefault()
121+
if !cfg.Response.IsValid() {
122+
die("response code config error.")
123+
return
124+
}
125+
HttpResponse = cfg.Response
126+
109127
StartTimeMS = time.Now().UnixMicro()
110128
}
111129

0 commit comments

Comments
 (0)