Skip to content

Commit 7c7ad9e

Browse files
committed
Factor out authenticators into a separate package
Just shuffling stuff around, no real changes
1 parent bc2a215 commit 7c7ad9e

File tree

9 files changed

+92
-58
lines changed

9 files changed

+92
-58
lines changed

auth_server/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
auth_server

auth_server/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: update-deps build docker-build
22

3-
all: docker-build
3+
all: build
44

55
update-deps:
66
go get -v -u -f github.com/jteeuwen/go-bindata/... .

auth_server/authn/authn.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2015 Cesanta Software Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package authn
18+
19+
// Authentication plugin interface.
20+
// Implementations must be goroutine-safe.
21+
type Authenticator interface {
22+
// Given a user name and a password (plain text), responds with nil on success
23+
// or with any other error on failure.
24+
Authenticate(user string, password PasswordString) error
25+
26+
// Finalize resources in preparation for shutdown.
27+
// When this call is made there are guaranteed to be no Authenticate requests in flight
28+
// and there will be no more calls made to this instance.
29+
Stop()
30+
}
31+
32+
//go:generate go-bindata -pkg authn -modtime 1 -mode 420 data/
33+
34+
type PasswordString string
35+
36+
func (ps PasswordString) String() string {
37+
if len(ps) == 0 {
38+
return ""
39+
}
40+
return "***"
41+
}

auth_server/server/bindata.go renamed to auth_server/authn/bindata.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

auth_server/server/google_auth.go renamed to auth_server/authn/google_auth.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package server
17+
package authn
1818

1919
import (
2020
"encoding/json"
@@ -33,7 +33,14 @@ import (
3333
"golang.org/x/crypto/bcrypt"
3434
)
3535

36-
//go:generate go-bindata -pkg server -modtime 1 -mode 420 data/google_auth.tmpl
36+
type GoogleAuthConfig struct {
37+
Domain string `yaml:"domain,omitempty"`
38+
ClientId string `yaml:"client_id,omitempty"`
39+
ClientSecret string `yaml:"client_secret,omitempty"`
40+
ClientSecretFile string `yaml:"client_secret_file,omitempty"`
41+
TokenDB string `yaml:"token_db,omitempty"`
42+
HTTPTimeout int `yaml:"http_timeout,omitempty"`
43+
}
3744

3845
type GoogleAuthRequest struct {
3946
Action string `json:"action,omitempty"`
@@ -150,7 +157,7 @@ func NewGoogleAuth(c *GoogleAuthConfig) (*GoogleAuth, error) {
150157
}, nil
151158
}
152159

153-
func (ga *GoogleAuth) doGoogleAuth(rw http.ResponseWriter, req *http.Request) {
160+
func (ga *GoogleAuth) DoGoogleAuth(rw http.ResponseWriter, req *http.Request) {
154161
if req.Method == "GET" {
155162
ga.doGoogleAuthPage(rw, req)
156163
return

auth_server/server/static_auth.go renamed to auth_server/authn/static_auth.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,38 @@
1414
limitations under the License.
1515
*/
1616

17-
package server
17+
package authn
1818

1919
import (
20+
"encoding/json"
2021
"errors"
2122
"golang.org/x/crypto/bcrypt"
2223
)
2324

24-
type StaticUsersAuth struct {
25+
type Requirements struct {
26+
Password *PasswordString `yaml:"password,omitempty" json:"password,omitempty"`
27+
}
28+
29+
type staticUsersAuth struct {
2530
users map[string]*Requirements
2631
}
2732

28-
func (sua *StaticUsersAuth) Authenticate(user string, password PasswordString) error {
33+
func (r Requirements) String() string {
34+
p := r.Password
35+
if p != nil {
36+
pm := PasswordString("***")
37+
r.Password = &pm
38+
}
39+
b, _ := json.Marshal(r)
40+
r.Password = p
41+
return string(b)
42+
}
43+
44+
func NewStaticUserAuth(users map[string]*Requirements) *staticUsersAuth {
45+
return &staticUsersAuth{users: users}
46+
}
47+
48+
func (sua *staticUsersAuth) Authenticate(user string, password PasswordString) error {
2949
reqs := sua.users[user]
3050
if reqs == nil {
3151
return errors.New("unknown user")
@@ -38,5 +58,5 @@ func (sua *StaticUsersAuth) Authenticate(user string, password PasswordString) e
3858
return nil
3959
}
4060

41-
func (sua *StaticUsersAuth) Stop() {
61+
func (sua *staticUsersAuth) Stop() {
4262
}

auth_server/server/config.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@ import (
2828
"sort"
2929
"strings"
3030

31+
"github.com/cesanta/docker_auth/auth_server/authn"
3132
mapset "github.com/deckarep/golang-set"
3233
"github.com/docker/libtrust"
3334
yaml "gopkg.in/yaml.v2"
3435
)
3536

3637
type Config struct {
37-
Server ServerConfig `yaml:"server"`
38-
Token TokenConfig `yaml:"token"`
39-
Users map[string]*Requirements `yaml:"users,omitempty"`
40-
GoogleAuth *GoogleAuthConfig `yaml:"google_auth,omitempty"`
41-
ACL []*ACLEntry `yaml:"acl"`
38+
Server ServerConfig `yaml:"server"`
39+
Token TokenConfig `yaml:"token"`
40+
Users map[string]*authn.Requirements `yaml:"users,omitempty"`
41+
GoogleAuth *authn.GoogleAuthConfig `yaml:"google_auth,omitempty"`
42+
ACL []*ACLEntry `yaml:"acl"`
4243
}
4344

4445
type ServerConfig struct {
@@ -60,15 +61,6 @@ type TokenConfig struct {
6061
privateKey libtrust.PrivateKey
6162
}
6263

63-
type GoogleAuthConfig struct {
64-
Domain string `yaml:"domain,omitempty"`
65-
ClientId string `yaml:"client_id,omitempty"`
66-
ClientSecret string `yaml:"client_secret,omitempty"`
67-
ClientSecretFile string `yaml:"client_secret_file,omitempty"`
68-
TokenDB string `yaml:"token_db,omitempty"`
69-
HTTPTimeout int `yaml:"http_timeout,omitempty"`
70-
}
71-
7264
type ACLEntry struct {
7365
Match *MatchConditions `yaml:"match"`
7466
Actions *[]string `yaml:"actions,flow"`
@@ -80,27 +72,13 @@ type MatchConditions struct {
8072
Name *string `yaml:"name,omitempty" json:"name,omitempty"`
8173
}
8274

83-
type Requirements struct {
84-
Password *PasswordString `yaml:"password,omitempty" json:"password,omitempty"`
85-
}
8675
type aclEntryJSON *ACLEntry
8776

8877
func (e ACLEntry) String() string {
8978
b, _ := json.Marshal(e)
9079
return string(b)
9180
}
9281

93-
func (r Requirements) String() string {
94-
p := r.Password
95-
if p != nil {
96-
pm := PasswordString("***")
97-
r.Password = &pm
98-
}
99-
b, _ := json.Marshal(r)
100-
r.Password = p
101-
return string(b)
102-
}
103-
10482
func matchString(pp *string, s string) bool {
10583
if pp == nil {
10684
return true

auth_server/server/server.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ import (
2727
"strings"
2828
"time"
2929

30+
"github.com/cesanta/docker_auth/auth_server/authn"
3031
"github.com/docker/distribution/registry/auth/token"
3132
"github.com/golang/glog"
3233
)
3334

3435
type AuthRequest struct {
3536
RemoteAddr string
3637
User string
37-
Password PasswordString
38+
Password authn.PasswordString
3839

3940
Account string
4041
Type string
@@ -47,33 +48,19 @@ func (ar AuthRequest) String() string {
4748
return fmt.Sprintf("{%s:%s@%s %s %s %s %s}", ar.User, ar.Password, ar.RemoteAddr, ar.Account, strings.Join(ar.Actions, ","), ar.Type, ar.Name)
4849
}
4950

50-
type PasswordString string
51-
52-
func (ps PasswordString) String() string {
53-
if len(ps) == 0 {
54-
return ""
55-
}
56-
return "***"
57-
}
58-
59-
type Authenticator interface {
60-
Authenticate(user string, password PasswordString) error
61-
Stop()
62-
}
63-
6451
type AuthServer struct {
6552
config *Config
66-
authenticators []Authenticator
67-
ga *GoogleAuth
53+
authenticators []authn.Authenticator
54+
ga *authn.GoogleAuth
6855
}
6956

7057
func NewAuthServer(c *Config) (*AuthServer, error) {
7158
as := &AuthServer{config: c}
7259
if c.Users != nil {
73-
as.authenticators = append(as.authenticators, &StaticUsersAuth{c.Users})
60+
as.authenticators = append(as.authenticators, authn.NewStaticUserAuth(c.Users))
7461
}
7562
if c.GoogleAuth != nil {
76-
ga, err := NewGoogleAuth(c.GoogleAuth)
63+
ga, err := authn.NewGoogleAuth(c.GoogleAuth)
7764
if err != nil {
7865
return nil, err
7966
}
@@ -88,7 +75,7 @@ func (as *AuthServer) ParseRequest(req *http.Request) (*AuthRequest, error) {
8875
user, password, haveBasicAuth := req.BasicAuth()
8976
if haveBasicAuth {
9077
ar.User = user
91-
ar.Password = PasswordString(password)
78+
ar.Password = authn.PasswordString(password)
9279
}
9380
ar.Account = req.FormValue("account")
9481
if ar.Account == "" {
@@ -204,7 +191,7 @@ func (as *AuthServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
204191
case req.URL.Path == "/auth":
205192
as.doAuth(rw, req)
206193
case req.URL.Path == "/google_auth" && as.ga != nil:
207-
as.ga.doGoogleAuth(rw, req)
194+
as.ga.DoGoogleAuth(rw, req)
208195
default:
209196
http.Error(rw, "Not found", http.StatusNotFound)
210197
return

0 commit comments

Comments
 (0)