Skip to content

Commit

Permalink
Merge pull request #17 from defer/ft-allowed-domains
Browse files Browse the repository at this point in the history
Google: Add an AllowedDomains config clause
  • Loading branch information
sunfmin authored Jun 3, 2019
2 parents 46aae9f + 12da3be commit 709ba0c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func init() {
Auth.RegisterProvider(google.New(&google.Config{
ClientID: "google client id",
ClientSecret: "google client secret",
AllowedDomains: []string{}, // Accept all domains, instead you can pass a whitelist of acceptable domains
}))

// Allow use Facebook
Expand Down
20 changes: 20 additions & 0 deletions providers/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package google
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strings"

"github.com/qor/auth"
"github.com/qor/auth/auth_identity"
Expand All @@ -30,6 +32,7 @@ type Config struct {
TokenURL string
RedirectURL string
Scopes []string
AllowedDomains []string
AuthorizeHandler func(context *auth.Context) (*claims.Claims, error)
}

Expand Down Expand Up @@ -106,6 +109,10 @@ func New(config *Config) *GoogleProvider {
schema.RawInfo = userInfo
}

if !isDomainAllowed(schema.Email, config.AllowedDomains) {
return nil, auth.ErrUnauthorized
}

authInfo.Provider = provider.GetName()
authInfo.UID = schema.UID

Expand All @@ -132,6 +139,19 @@ func New(config *Config) *GoogleProvider {
return provider
}

func isDomainAllowed(email string, domains []string) bool {
if len(domains) == 0 {
return true
}

for _, domain := range domains {
if strings.HasSuffix(email, fmt.Sprintf("@%s", domain)) {
return true
}
}
return false
}

// GetName return provider name
func (GoogleProvider) GetName() string {
return "google"
Expand Down

0 comments on commit 709ba0c

Please sign in to comment.