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 #36 from jarias/develop
Browse files Browse the repository at this point in the history
Release 0.1.0-beta.15
  • Loading branch information
jarias committed May 25, 2016
2 parents 3ba1ae9 + 582e066 commit 745e619
Show file tree
Hide file tree
Showing 23 changed files with 539 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.envrc
coverage.txt
stormpath-sdk-go.test
Expand Down
39 changes: 36 additions & 3 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Account struct {
Directory *Directory `json:"directory,omitempty"`
Tenant *Tenant `json:"tenant,omitempty"`
EmailVerificationToken *resource `json:"emailVerificationToken,omitempty"`
AccessTokens *OAuthTokens `json:"accessTokens,omitempty"`
RefreshTokens *OAuthTokens `json:"refreshTokens,omitempty"`
}

//Accounts represents a paged result of Account objects
Expand Down Expand Up @@ -51,6 +53,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 All @@ -64,7 +67,6 @@ func GetAccount(href string, criteria Criteria) (*Account, error) {

err := client.get(
buildAbsoluteURL(href, criteria.ToQueryString()),
emptyPayload(),
account,
)

Expand All @@ -77,7 +79,7 @@ func GetAccount(href string, criteria Criteria) (*Account, error) {

//Refresh refreshes the resource by doing a GET to the resource href endpoint
func (account *Account) Refresh() error {
return client.get(account.Href, emptyPayload(), account)
return client.get(account.Href, account)
}

//Update updates the given resource, by doing a POST to the resource Href
Expand Down Expand Up @@ -135,7 +137,6 @@ func (account *Account) GetGroupMemberships(criteria Criteria) (*GroupMembership
account.GroupMemberships.Href,
criteria.ToQueryString(),
),
emptyPayload(),
groupMemberships,
)

Expand All @@ -159,3 +160,35 @@ func VerifyEmailToken(token string) (*Account, error) {

return account, nil
}

//GetRefreshTokens returns the account's refreshToken collection
func (account *Account) GetRefreshTokens(criteria OAuthTokenCriteria) (*OAuthTokens, error) {
refreshTokens := &OAuthTokens{}

err := client.get(
buildAbsoluteURL(account.RefreshTokens.Href, criteria.ToQueryString()),
refreshTokens,
)

if err != nil {
return nil, err
}

return refreshTokens, nil
}

//GetAccessTokens returns the acounts's accessToken collection
func (account *Account) GetAccessTokens(criteria OAuthTokenCriteria) (*OAuthTokens, error) {
accessTokens := &OAuthTokens{}

err := client.get(
buildAbsoluteURL(account.AccessTokens.Href, criteria.ToQueryString()),
accessTokens,
)

if err != nil {
return nil, err
}

return accessTokens, nil
}
8 changes: 4 additions & 4 deletions account_creation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type AccountCreationPolicy struct {

//Refresh refreshes the resource by doing a GET to the resource href endpoint
func (policy *AccountCreationPolicy) Refresh() error {
return client.get(policy.Href, emptyPayload(), policy)
return client.get(policy.Href, policy)
}

//Update updates the given resource, by doing a POST to the resource Href
Expand All @@ -25,7 +25,7 @@ func (policy *AccountCreationPolicy) Update() error {

//GetVerificationEmailTemplates loads the policy VerificationEmailTemplates collection and returns it
func (policy *AccountCreationPolicy) GetVerificationEmailTemplates() (*EmailTemplates, error) {
err := client.get(policy.VerificationEmailTemplates.Href, emptyPayload(), policy.VerificationEmailTemplates)
err := client.get(policy.VerificationEmailTemplates.Href, policy.VerificationEmailTemplates)

if err != nil {
return nil, err
Expand All @@ -36,7 +36,7 @@ func (policy *AccountCreationPolicy) GetVerificationEmailTemplates() (*EmailTemp

//GetVerificationSuccessEmailTemplates loads the policy VerificationSuccessEmailTemplates collection and returns it
func (policy *AccountCreationPolicy) GetVerificationSuccessEmailTemplates() (*EmailTemplates, error) {
err := client.get(policy.VerificationSuccessEmailTemplates.Href, emptyPayload(), policy.VerificationSuccessEmailTemplates)
err := client.get(policy.VerificationSuccessEmailTemplates.Href, policy.VerificationSuccessEmailTemplates)

if err != nil {
return nil, err
Expand All @@ -47,7 +47,7 @@ func (policy *AccountCreationPolicy) GetVerificationSuccessEmailTemplates() (*Em

//GetWelcomeEmailTemplates loads the policy WelcomeEmailTemplates collection and returns it
func (policy *AccountCreationPolicy) GetWelcomeEmailTemplates() (*EmailTemplates, error) {
err := client.get(policy.WelcomeEmailTemplates.Href, emptyPayload(), policy.WelcomeEmailTemplates)
err := client.get(policy.WelcomeEmailTemplates.Href, policy.WelcomeEmailTemplates)

if err != nil {
return nil, err
Expand Down
57 changes: 57 additions & 0 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,63 @@ import (
"github.com/stretchr/testify/assert"
)

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

application := createTestApplication()
defer application.Purge()

account := createTestAccount(application)

application.GetOAuthToken(account.Username, "1234567z!A89")

tokens, err := account.GetRefreshTokens(MakeOAuthTokensCriteria())

assert.NoError(t, err)
assert.NotEmpty(t, tokens.Href)
assert.Equal(t, 0, tokens.Offset)
assert.Equal(t, 25, tokens.Limit)
assert.NotEmpty(t, tokens.Items)
}

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

application := createTestApplication()
defer application.Purge()

account := createTestAccount(application)

application.GetOAuthToken(account.Username, "1234567z!A89")

tokens, err := account.GetAccessTokens(MakeOAuthTokensCriteria())

assert.NoError(t, err)
assert.NotEmpty(t, tokens.Href)
assert.Equal(t, 0, tokens.Offset)
assert.Equal(t, 25, tokens.Limit)
assert.NotEmpty(t, tokens.Items)
}

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

application := createTestApplication()
defer application.Purge()

account := createTestAccount(application)

application.GetOAuthToken(account.Username, "1234567z!A89")

tokens, _ := account.GetAccessTokens(MakeOAuthTokensCriteria())

token := tokens.Items[0]

err := token.Delete()

assert.NoError(t, err)
}

func TestAccountJsonMarshaling(t *testing.T) {
t.Parallel()
account := NewAccount("test@test.org", "123", "test@test.org", "test", "test")
Expand Down
137 changes: 70 additions & 67 deletions application.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package stormpath

import (
"bytes"
"encoding/base64"
"errors"
"net/url"
"time"
"bytes"

"github.com/dgrijalva/jwt-go"
"github.com/nu7hatch/gouuid"
Expand All @@ -25,6 +25,7 @@ type Application struct {
AccountStoreMappings *AccountStoreMappings `json:"accountStoreMappings,omitempty"`
DefaultAccountStoreMapping *AccountStoreMapping `json:"defaultAccountStoreMapping,omitempty"`
DefaultGroupStoreMapping *AccountStoreMapping `json:"defaultGroupStoreMapping,omitempty"`
OAuthPolicy *OAuthPolicy `json:"oAuthPolicy,omitempty"`
}

//Applications represents a paged result or applications
Expand All @@ -41,24 +42,6 @@ type IDSiteCallbackResult struct {
Status string
}

//OAuthResponse represents an OAuth2 response from StormPath
type OAuthResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
StormpathAccessTokenHref string `json:"stormpath_access_token_href"`
}

type AccessToken struct {
resource
Account *Account `json:"account,omitempty"`
Tenant *Tenant `json:"tenant,omitempty"`
Application *Application `json:"application,omitempty"`
JWT string `json:"jwt"`
ExpandedJWT map[string]interface{} `json:"expandedJwt"`
}

//NewApplication creates a new application
func NewApplication(name string) *Application {
return &Application{Name: name}
Expand All @@ -70,7 +53,6 @@ func GetApplication(href string, criteria Criteria) (*Application, error) {

err := client.get(
buildAbsoluteURL(href, criteria.ToQueryString()),
emptyPayload(),
application,
)

Expand All @@ -79,7 +61,7 @@ func GetApplication(href string, criteria Criteria) (*Application, error) {

//Refresh refreshes the resource by doing a GET to the resource href endpoint
func (app *Application) Refresh() error {
return client.get(app.Href, emptyPayload(), app)
return client.get(app.Href, app)
}

//Update updates the given resource, by doing a POST to the resource Href
Expand All @@ -96,7 +78,7 @@ func (app *Application) Purge() error {
return err
}
for _, m := range accountStoreMappings.Items {
client.delete(m.AccountStore.Href, emptyPayload())
client.delete(m.AccountStore.Href)
}

return app.Delete()
Expand All @@ -110,7 +92,6 @@ func (app *Application) GetAccountStoreMappings(criteria Criteria) (*AccountStor

err := client.get(
buildAbsoluteURL(app.AccountStoreMappings.Href, criteria.ToQueryString()),
emptyPayload(),
accountStoreMappings,
)

Expand Down Expand Up @@ -173,48 +154,6 @@ func (app *Application) AuthenticateAccount(username string, password string) (*
return account, nil
}

//GetOAuthToken creates a OAuth2 token response for a given user credentials
func (app *Application) GetOAuthToken(username string, password string) (*OAuthResponse, error) {
response := &OAuthResponse{}

values := url.Values{
"grant_type": {"password"},
"username": {username},
"password": {password},
}
body := &bytes.Buffer{}
canonicalizeQueryString(body, values)

err := client.postURLEncodedForm(
buildAbsoluteURL(app.Href, "oauth/token"),
body.String(),
response,
)

if err != nil {
return nil, err
}

return response, nil
}

//Validate Token against Application
func (app *Application) ValidateToken(token string) (*AccessToken, error) {
response := &AccessToken{}

err := client.get(
buildAbsoluteURL(app.Href, "authTokens", token),
emptyPayload(),
response,
)

if err != nil {
return nil, err
}

return response, nil
}

//SendPasswordResetEmail sends a password reset email to the given user
//
//See: http://docs.stormpath.com/rest/product-guide/#reset-an-accounts-password
Expand All @@ -239,7 +178,7 @@ func (app *Application) SendPasswordResetEmail(email string) (*AccountPasswordRe
func (app *Application) ValidatePasswordResetToken(token string) (*AccountPasswordResetToken, error) {
passwordResetToken := &AccountPasswordResetToken{}

err := client.get(buildAbsoluteURL(app.Href, "passwordResetTokens", token), emptyPayload(), passwordResetToken)
err := client.get(buildAbsoluteURL(app.Href, "passwordResetTokens", token), passwordResetToken)

if err != nil {
return nil, err
Expand Down Expand Up @@ -283,7 +222,6 @@ func (app *Application) GetGroups(criteria Criteria) (*Groups, error) {

err := client.get(
buildAbsoluteURL(app.Groups.Href, criteria.ToQueryString()),
emptyPayload(),
groups,
)

Expand Down Expand Up @@ -370,3 +308,68 @@ func (app *Application) HandleIDSiteCallback(URL string) (*IDSiteCallbackResult,

return result, nil
}

//GetOAuthToken creates a OAuth2 token response for a given user credentials
func (app *Application) GetOAuthToken(username string, password string) (*OAuthResponse, error) {
response := &OAuthResponse{}

values := url.Values{
"grant_type": {"password"},
"username": {username},
"password": {password},
}
body := &bytes.Buffer{}
canonicalizeQueryString(body, values)

err := client.postURLEncodedForm(
buildAbsoluteURL(app.Href, "oauth/token"),
body.String(),
response,
)

if err != nil {
return nil, err
}

return response, nil
}

//RefreshOAuthToken refreshes an OAuth2 token using the provided refresh_token and returns a new OAuth reponse
func (app *Application) RefreshOAuthToken(refreshToken string) (*OAuthResponse, error) {
response := &OAuthResponse{}

values := url.Values{
"grant_type": {"refresh_token"},
"refresh_token": {refreshToken},
}
body := &bytes.Buffer{}
canonicalizeQueryString(body, values)

err := client.postURLEncodedForm(
buildAbsoluteURL(app.Href, "oauth/token"),
body.String(),
response,
)

if err != nil {
return nil, err
}

return response, nil
}

//ValidateToken against the application
func (app *Application) ValidateToken(token string) (*OAuthToken, error) {
response := &OAuthToken{}

err := client.get(
buildAbsoluteURL(app.Href, "authTokens", token),
response,
)

if err != nil {
return nil, err
}

return response, nil
}
Loading

0 comments on commit 745e619

Please sign in to comment.