This repository has been archived by the owner on Apr 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
directory.go
167 lines (136 loc) · 5.3 KB
/
directory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
type Directory struct {
accountStoreResource
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
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"`
PasswordPolicy *PasswordPolicy `json:"passwordPolicy,omitempty"`
}
//Directories represnets a paged result of directories
type Directories struct {
collectionResource
Items []Directory `json:"items,omitempty"`
}
//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)
}
//CreateDirectory creates a new directory for the given tenant
//
//See: http://docs.stormpath.com/rest/product-guide/#tenant-directories
func CreateDirectory(dir *Directory) error {
return client.post(buildRelativeURL("directories"), dir, dir)
}
//GetDirectory loads a directory by href and criteria
func GetDirectory(href string, criteria DirectoryCriteria) (*Directory, error) {
directory := &Directory{}
err := client.get(
buildAbsoluteURL(href, criteria.toQueryString()),
directory,
)
if err != nil {
return nil, err
}
return directory, nil
}
//Refresh refreshes the resource by doing a GET to the resource href endpoint
func (dir *Directory) Refresh() error {
return client.get(dir.Href, dir)
}
//Update updates the given resource, by doing a POST to the resource Href
func (dir *Directory) Update() error {
return client.post(dir.Href, dir, dir)
}
//GetAccountCreationPolicy loads the directory account creation policy
func (dir *Directory) GetAccountCreationPolicy() (*AccountCreationPolicy, error) {
err := client.get(buildAbsoluteURL(dir.AccountCreationPolicy.Href), dir.AccountCreationPolicy)
if err != nil {
return nil, err
}
return dir.AccountCreationPolicy, nil
}
//GetGroups returns all the groups from a directory
func (dir *Directory) GetGroups(criteria GroupCriteria) (*Groups, error) {
err := client.get(
buildAbsoluteURL(dir.Groups.Href, criteria.toQueryString()),
dir.Groups,
)
if err != nil {
return nil, err
}
return dir.Groups, nil
}
//CreateGroup creates a new group in the directory
func (dir *Directory) CreateGroup(group *Group) error {
return client.post(dir.Groups.Href, group, group)
}
//RegisterAccount registers a new account into the directory
//
//See: http://docs.stormpath.com/rest/product-guide/#directory-accounts
func (dir *Directory) RegisterAccount(account *Account) error {
return client.post(dir.Accounts.Href, account, account)
}
//RegisterSocialAccount registers a new account into the application using an external provider Google, Facebook
//
//See: http://docs.stormpath.com/rest/product-guide/#accessing-accounts-with-google-authorization-codes-or-an-access-tokens
func (dir *Directory) RegisterSocialAccount(socialAccount *SocialAccount) (*Account, error) {
account := &Account{}
err := client.post(dir.Accounts.Href, socialAccount, account)
if err != nil {
return nil, err
}
return account, nil
}