Skip to content

Commit 0a4a46b

Browse files
authored
Merge pull request #165 from SenseUnit/auth_enhancements
Auth enhancements 3
2 parents 7f6a343 + db991ac commit 0a4a46b

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Authentication parameters are passed as URI via `-auth` parameter. Scheme of URI
270270
* `blacklist` - location of file with list of serial numbers of blocked certificates, one per each line in form of hex-encoded colon-separated bytes. Example: `ab:01:02:03`. Empty lines and comments starting with `#` are ignored.
271271
* `reload` - interval for certificate blacklist file reload, if it was modified since last load. Use negative duration to disable autoreload. Default: `15s`.
272272
* `next` - optional URL specifying the next auth provider to chain to, if cert authentication succeeded. Example: `-auth 'cert://?next=static%3A%2F%2F%3Fusername%3Dadmin%26password%3D123456'`.
273+
* `else` - optional URL specifying the next auth provider to chain to, if authentication failed.
273274
* `redis` - use external Redis database to lookup password verifiers for users. The password format is similar to `basicfile` mode or `htpasswd` encoding except username goes into Redis key name, colon is skipped and the rest goes to value of this key. For example, login-password pair `test` / `123456` can be encoded as Redis key `test` with value `$2y$05$zs1EJayCIyYtG.NQVzu9SeNvMP0XYWa42fQv.XNDx33wwbg98SnUq`. Example of auth parameter: `-auth 'redis://?url=redis%3A//default%3A123456Y%40redis-14623.c531.europe-west3-1.gce.redns.redis-cloud.com%3A17954/0&key_prefix=auth_'`. Parameters:
274275
* `url` - URL specifying Redis instance to connect to. See [ParseURL](https://pkg.go.dev/github.com/redis/go-redis/v9#ParseURL) documentation for the complete specification of Redis URL format.
275276
* `key_prefix` - prefix to prepend to each key before lookup. Helps isolate keys under common prefix. Default is empty string (`""`).

auth/cert.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type CertAuth struct {
3232
stopOnce sync.Once
3333
stopChan chan struct{}
3434
next Auth
35+
reject Auth
3536
}
3637

3738
func NewCertAuth(param_url *url.URL, logger *clog.CondLogger) (*CertAuth, error) {
@@ -70,19 +71,32 @@ func NewCertAuth(param_url *url.URL, logger *clog.CondLogger) (*CertAuth, error)
7071
}
7172
auth.next = nap
7273
}
74+
if nextAuth := values.Get("else"); nextAuth != "" {
75+
nap, err := NewAuth(nextAuth, logger)
76+
if err != nil {
77+
return nil, fmt.Errorf("chained auth provider construction failed: %w", err)
78+
}
79+
auth.reject = nap
80+
}
7381

7482
return auth, nil
7583
}
7684

85+
func (auth *CertAuth) handleReject(ctx context.Context, wr http.ResponseWriter, req *http.Request) (string, bool) {
86+
if auth.reject != nil {
87+
return auth.reject.Validate(ctx, wr, req)
88+
}
89+
http.Error(wr, BAD_REQ_MSG, http.StatusBadRequest)
90+
return "", false
91+
}
92+
7793
func (auth *CertAuth) Validate(ctx context.Context, wr http.ResponseWriter, req *http.Request) (string, bool) {
7894
if req.TLS == nil || len(req.TLS.VerifiedChains) < 1 || len(req.TLS.VerifiedChains[0]) < 1 {
79-
http.Error(wr, BAD_REQ_MSG, http.StatusBadRequest)
80-
return "", false
95+
return auth.handleReject(ctx, wr, req)
8196
}
8297
eeCert := req.TLS.VerifiedChains[0][0]
8398
if auth.blacklist.Load().file.Has(eeCert.SerialNumber) {
84-
http.Error(wr, BAD_REQ_MSG, http.StatusBadRequest)
85-
return "", false
99+
return auth.handleReject(ctx, wr, req)
86100
}
87101
if auth.next != nil {
88102
return auth.next.Validate(ctx, wr, req)
@@ -99,6 +113,9 @@ func (auth *CertAuth) Stop() {
99113
if auth.next != nil {
100114
auth.next.Stop()
101115
}
116+
if auth.reject != nil {
117+
auth.reject.Stop()
118+
}
102119
close(auth.stopChan)
103120
})
104121
}

0 commit comments

Comments
 (0)