Skip to content
This repository has been archived by the owner on Apr 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #34 from jarias/issue/17
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
jarias committed May 25, 2016
2 parents 105e59e + d12f640 commit 42e7e89
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type SocialAccount struct {
type ProviderData struct {
ProviderID string `json:"providerId"`
AccessToken string `json:"accessToken,omitempty"`
Code string `json:"code,omitempty"`
}

//NewAccount returns a pointer to an Account with the minimum data required
Expand Down
56 changes: 56 additions & 0 deletions directory.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package stormpath

const (
Facebook = "facebook"
Google = "google"
GitHub = "github"
LinkedIn = "linkedin"
)

//Directory represents a Stormpath directory object
//
//See: http://docs.stormpath.com/rest/product-guide/#directories
Expand All @@ -10,6 +17,7 @@ type Directory struct {
Status string `json:"status,omitempty"`
Groups *Groups `json:"groups,omitempty"`
Tenant *Tenant `json:"tenant,omitempty"`
Provider *Provider `json:"provider,omitempty"`
AccountCreationPolicy *AccountCreationPolicy `json:"accountCreationPolicy,omitempty"`
}

Expand All @@ -19,11 +27,59 @@ type Directories struct {
Items []Directory `json:"items"`
}

//Provider represents the directory provider (cloud, google, github, facebook or linkedin)
type Provider struct {
resource
OAuthProvider
ProviderID string `json:"providerId,omitempty"`
}

//OAuthProvider represents a generic OAuth2 provider for all the social type directories
type OAuthProvider struct {
ClientID string `json:"clientId,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
RedirectURI string `json:"redirectUri,omitempty"`
}

//NewDirectory creates a new directory with the given name
func NewDirectory(name string) *Directory {
return &Directory{Name: name}
}

func newSocialDirectory(name string, clientID string, clientSecret string, redirectURI string, provider string) *Directory {
directory := NewDirectory(name)
directory.Provider = &Provider{
ProviderID: provider,
OAuthProvider: OAuthProvider{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURI: redirectURI,
},
}

return directory
}

//NewFacebookDirectory creates a new directory with a Facebook backed provider
func NewFacebookDirectory(name string, clientID string, clientSecret string) *Directory {
return newSocialDirectory(name, clientID, clientSecret, "", Facebook)
}

//NewGithubDirectory creates a new directory with a GitHub backed provider
func NewGithubDirectory(name string, clientID string, clientSecret string) *Directory {
return newSocialDirectory(name, clientID, clientSecret, "", GitHub)
}

//NewGoogleDirectory creates a new directory with a Google backed provider
func NewGoogleDirectory(name string, clientID string, clientSecret string, redirectURI string) *Directory {
return newSocialDirectory(name, clientID, clientSecret, redirectURI, Google)
}

//NewLinkedInDirectory creates a new directory with a LinkedIn backend provider
func NewLinkedInDirectory(name string, clientID string, clientSecret string, redirectURI string) *Directory {
return newSocialDirectory(name, clientID, clientSecret, redirectURI, LinkedIn)
}

//GetDirectory loads a directory by href and criteria
func GetDirectory(href string, criteria Criteria) (*Directory, error) {
directory := &Directory{}
Expand Down
5 changes: 5 additions & 0 deletions directory_criteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ func (c DirectoryCriteria) WithTenant() DirectoryCriteria {
c.expandedAttributes = append(c.expandedAttributes, "tenant")
return c
}

func (c DirectoryCriteria) WithProvider() DirectoryCriteria {
c.expandedAttributes = append(c.expandedAttributes, "provider")
return c
}
80 changes: 80 additions & 0 deletions directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,83 @@ func TestDirectoryRegisterAccount(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, account.Href)
}

func TestCreateGoogleDirectory(t *testing.T) {
t.Parallel()

directory := NewGoogleDirectory("google-"+randomName(), "ClientID", "ClientSercret", "http://localhost:8080")
defer directory.Delete()

err := tenant.CreateDirectory(directory)

assert.NoError(t, err)
assert.NotEmpty(t, directory.Href)

d, err := GetDirectory(directory.Href, MakeDirectoryCriteria().WithProvider())

assert.NoError(t, err)
assert.Equal(t, Google, d.Provider.ProviderID)
assert.Equal(t, directory.Provider.ClientID, d.Provider.ClientID)
assert.Equal(t, directory.Provider.ClientSecret, d.Provider.ClientSecret)
assert.Equal(t, directory.Provider.RedirectURI, d.Provider.RedirectURI)
}

func TestCreateLinkedInDirectory(t *testing.T) {
t.Parallel()

directory := NewLinkedInDirectory("linkedin-"+randomName(), "ClientID", "ClientSercret", "http://localhost:8080")
defer directory.Delete()

err := tenant.CreateDirectory(directory)

assert.NoError(t, err)
assert.NotEmpty(t, directory.Href)

d, err := GetDirectory(directory.Href, MakeDirectoryCriteria().WithProvider())

assert.NoError(t, err)
assert.Equal(t, LinkedIn, d.Provider.ProviderID)
assert.Equal(t, directory.Provider.ClientID, d.Provider.ClientID)
assert.Equal(t, directory.Provider.ClientSecret, d.Provider.ClientSecret)
assert.Equal(t, directory.Provider.RedirectURI, d.Provider.RedirectURI)
}

func TestCreateFacebookDirectory(t *testing.T) {
t.Parallel()

directory := NewFacebookDirectory("facebook-"+randomName(), "ClientID", "ClientSercret")
defer directory.Delete()

err := tenant.CreateDirectory(directory)

assert.NoError(t, err)
assert.NotEmpty(t, directory.Href)

d, err := GetDirectory(directory.Href, MakeDirectoryCriteria().WithProvider())

assert.NoError(t, err)
assert.Equal(t, Facebook, d.Provider.ProviderID)
assert.Equal(t, directory.Provider.ClientID, d.Provider.ClientID)
assert.Equal(t, directory.Provider.ClientSecret, d.Provider.ClientSecret)
assert.Empty(t, d.Provider.RedirectURI)
}

func TestCreateGithubDirectory(t *testing.T) {
t.Parallel()

directory := NewGithubDirectory("github-"+randomName(), "ClientID", "ClientSercret")
defer directory.Delete()

err := tenant.CreateDirectory(directory)

assert.NoError(t, err)
assert.NotEmpty(t, directory.Href)

d, err := GetDirectory(directory.Href, MakeDirectoryCriteria().WithProvider())

assert.NoError(t, err)
assert.Equal(t, GitHub, d.Provider.ProviderID)
assert.Equal(t, directory.Provider.ClientID, d.Provider.ClientID)
assert.Equal(t, directory.Provider.ClientSecret, d.Provider.ClientSecret)
assert.Empty(t, d.Provider.RedirectURI)
}

0 comments on commit 42e7e89

Please sign in to comment.