Skip to content

Commit

Permalink
domain is now array
Browse files Browse the repository at this point in the history
  • Loading branch information
typester committed Aug 6, 2014
1 parent c8a378f commit e637bb8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Conf struct {
Addr string `yaml:"address"`
SSL SSLConf `yaml:"ssl"`
Auth AuthConf `yaml:"auth"`
Domain string `yaml:"domain"`
Domain []string `yaml:"domain"`
Proxies []ProxyConf `yaml:"proxy"`
Htdocs string `yaml:"htdocs"`
}
Expand Down
15 changes: 10 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ proxy:
}
}

func TestParseSingleDomain(t *testing.T) {
func TestParseMultiDomain(t *testing.T) {
f, err := ioutil.TempFile("", "")
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -78,8 +78,9 @@ proxy:
dest: http://example.com/bar
strip_path: yes
domain: 'example1.com'
domain:
- 'example1.com'
- 'example2.com'
`
if err := ioutil.WriteFile(f.Name(), []byte(data), 0644); err != nil {
t.Error(err)
Expand All @@ -90,8 +91,12 @@ domain: 'example1.com'
t.Error(err)
}

if conf.Domain != "example1.com" {
t.Errorf("unexpected address: %s", conf.Domain)
if len(conf.Domain) != 2 {
t.Errorf("unexpected domains num: %d", len(conf.Domain))
}

if conf.Domain[0] != "example1.com" || conf.Domain[1] != "example2.com" {
t.Errorf("unexpected domains: %+v", conf.Domain)
}
}

18 changes: 15 additions & 3 deletions httpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func base64Decode(s string) ([]byte, error) {
return base64.URLEncoding.DecodeString(s)
}

func restrictDomain(domain string) martini.Handler {
func restrictDomain(domain []string) martini.Handler {
return func(c martini.Context, tokens oauth2.Tokens, w http.ResponseWriter, r *http.Request) {
// skip websocket
if isWebsocket(r) {
Expand Down Expand Up @@ -205,8 +205,20 @@ func restrictDomain(domain string) martini.Handler {
}

if email, ok := info["email"].(string); ok {
if domain == "" || strings.HasSuffix(email, "@"+domain) {
user := &User{email}
var user *User
if len(domain) > 0 {
for _, d := range domain {
if strings.HasSuffix(email, "@"+d) {
user = &User{email}
break
}
}
} else {
user = &User{email}
}

if user != nil {
log.Printf("user %s logged in", email)
c.Map(user)
} else {
log.Printf("email doesn't allow: %s", email)
Expand Down

0 comments on commit e637bb8

Please sign in to comment.