aboutsummaryrefslogtreecommitdiff
path: root/server/auth/google
diff options
context:
space:
mode:
Diffstat (limited to 'server/auth/google')
-rw-r--r--server/auth/google/google.go9
-rw-r--r--server/auth/google/google_test.go19
2 files changed, 22 insertions, 6 deletions
diff --git a/server/auth/google/google.go b/server/auth/google/google.go
index cf780f3..0328d42 100644
--- a/server/auth/google/google.go
+++ b/server/auth/google/google.go
@@ -1,6 +1,7 @@
package google
import (
+ "errors"
"fmt"
"net/http"
"strings"
@@ -26,7 +27,11 @@ type Config struct {
}
// New creates a new Google provider from a configuration.
-func New(c *config.Auth) auth.Provider {
+func New(c *config.Auth) (auth.Provider, error) {
+ if c.ProviderOpts["domain"] == "" {
+ return nil, errors.New("google_opts domain must not be empty")
+ }
+
return &Config{
config: &oauth2.Config{
ClientID: c.OauthClientID,
@@ -36,7 +41,7 @@ func New(c *config.Auth) auth.Provider {
Scopes: []string{googleapi.UserinfoEmailScope, googleapi.UserinfoProfileScope},
},
domain: c.ProviderOpts["domain"],
- }
+ }, nil
}
// A new oauth2 http client.
diff --git a/server/auth/google/google_test.go b/server/auth/google/google_test.go
index c6a3def..4d41986 100644
--- a/server/auth/google/google_test.go
+++ b/server/auth/google/google_test.go
@@ -19,7 +19,7 @@ var (
func TestNew(t *testing.T) {
a := assert.New(t)
- p := newGoogle()
+ p, _ := newGoogle()
g := p.(*Config)
a.Equal(g.config.ClientID, oauthClientID)
a.Equal(g.config.ClientSecret, oauthClientSecret)
@@ -27,10 +27,22 @@ func TestNew(t *testing.T) {
a.Equal(g.domain, domain)
}
+func TestNewWithoutDomain(t *testing.T) {
+ a := assert.New(t)
+
+ domain = ""
+
+ _, err := newGoogle()
+ a.EqualError(err, "google_opts domain must not be empty")
+
+ domain = "example.com"
+}
+
func TestStartSession(t *testing.T) {
a := assert.New(t)
- p := newGoogle()
+ p, err := newGoogle()
+ a.NoError(err)
s := p.StartSession("test_state")
a.Equal(s.State, "test_state")
a.Contains(s.AuthURL, "accounts.google.com/o/oauth2/auth")
@@ -39,13 +51,12 @@ func TestStartSession(t *testing.T) {
a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", oauthClientID))
}
-func newGoogle() auth.Provider {
+func newGoogle() (auth.Provider, error) {
c := &config.Auth{
OauthClientID: oauthClientID,
OauthClientSecret: oauthClientSecret,
OauthCallbackURL: oauthCallbackURL,
ProviderOpts: map[string]string{"domain": domain},
}
- c.ProviderOpts["domain"] = domain
return New(c)
}