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 #51 from jarias/develop
Browse files Browse the repository at this point in the history
Release 0.1.0-beta.19
  • Loading branch information
jarias authored Jun 17, 2016
2 parents 968bd17 + 3031186 commit 773536f
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 169 deletions.
52 changes: 28 additions & 24 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stormpath
import (
"fmt"
"os"
"strings"

"time"

Expand Down Expand Up @@ -67,15 +68,18 @@ func LoadConfiguration() (ClientConfiguration, error) {
ProxyPassword: "",
}

viper.SetConfigType("yaml")
viper.SetConfigFile("stormpath.yaml")
viper.AddConfigPath("~/.stormpath")
viper.AddConfigPath(".")
viper.AutomaticEnv()
viper.ReadInConfig()
v := viper.New()

c.APIKeyID = viper.GetString("stormpath.client.apiKey.id")
c.APIKeySecret = viper.GetString("stormpath.client.apiKey.secret")
v.SetConfigType("yaml")
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.SetConfigName("stormpath")
v.AddConfigPath(os.Getenv("HOME") + "/.stormpath")
v.AddConfigPath(".")
v.ReadInConfig()

c.APIKeyID = v.GetString("stormpath.client.apiKey.id")
c.APIKeySecret = v.GetString("stormpath.client.apiKey.secret")
id, secret, err := loadCredentials(c.APIKeyFile)
if err == nil {
c.APIKeyID = id
Expand All @@ -86,31 +90,31 @@ func LoadConfiguration() (ClientConfiguration, error) {
return c, fmt.Errorf("API credentials couldn't be loaded")
}

if viper.Get("stormpath.client.cacheManager.enabled") != nil {
c.CacheManagerEnabled = viper.GetBool("stormpath.client.cacheManager.enabled")
if v.Get("stormpath.client.cacheManager.enabled") != nil {
c.CacheManagerEnabled = v.GetBool("stormpath.client.cacheManager.enabled")
}
if viper.Get("stormpath.client.cacheManager.defaultTtl") != nil {
c.CacheTTL = time.Duration(viper.GetInt("stormpath.client.cacheManager.defaultTtl")) * time.Second
if v.Get("stormpath.client.cacheManager.defaultTtl") != nil {
c.CacheTTL = time.Duration(v.GetInt("stormpath.client.cacheManager.defaultTtl")) * time.Second
}
if viper.Get("stormpath.client.cacheManager.defaultTti") != nil {
c.CacheTTI = time.Duration(viper.GetInt("stormpath.client.cacheManager.defaultTti")) * time.Second
if v.Get("stormpath.client.cacheManager.defaultTti") != nil {
c.CacheTTI = time.Duration(v.GetInt("stormpath.client.cacheManager.defaultTti")) * time.Second
}

if viper.GetString("stormpath.client.baseUrl") != "" {
c.BaseURL = viper.GetString("stormpath.client.baseUrl")
if v.GetString("stormpath.client.baseUrl") != "" {
c.BaseURL = v.GetString("stormpath.client.baseUrl")
}
if viper.Get("stormpath.client.connectionTimeout") != nil {
c.ConnectionTimeout = viper.GetInt("stormpath.client.connectionTimeout")
if v.Get("stormpath.client.connectionTimeout") != nil {
c.ConnectionTimeout = v.GetInt("stormpath.client.connectionTimeout")
}

if viper.GetString("stormpath.client.authenticationScheme") != "" {
c.AuthenticationScheme = viper.GetString("stormpath.client.authenticationScheme")
if v.GetString("stormpath.client.authenticationScheme") != "" {
c.AuthenticationScheme = v.GetString("stormpath.client.authenticationScheme")
}

c.ProxyHost = viper.GetString("stormpath.client.proxy.host")
c.ProxyPort = viper.GetInt("stormpath.client.proxy.port")
c.ProxyUsername = viper.GetString("stormpath.client.proxy.username")
c.ProxyPassword = viper.GetString("stormpath.client.proxy.password")
c.ProxyHost = v.GetString("stormpath.client.proxy.host")
c.ProxyPort = v.GetInt("stormpath.client.proxy.port")
c.ProxyUsername = v.GetString("stormpath.client.proxy.username")
c.ProxyPassword = v.GetString("stormpath.client.proxy.password")

return c, nil
}
Expand Down
2 changes: 1 addition & 1 deletion stormpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

//Version is the current SDK Version
const version = "0.1.0-beta.18"
const version = "0.1.0-beta.19"

const (
Enabled = "ENABLED"
Expand Down
18 changes: 9 additions & 9 deletions web/assets.go

Large diffs are not rendered by default.

207 changes: 128 additions & 79 deletions web/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package stormpathweb

import (
"bytes"
"os"
"strings"
"time"

"github.com/jarias/stormpath-sdk-go"
Expand All @@ -12,13 +14,18 @@ import (
var Config = &webConfig{}

type webConfig struct {
//Application
ApplicationName string
ApplicationHref string
//General web
Produces []string
BasePath string
//Login
LoginURI string
LoginNextURI string
LoginView string
LoginEnabled bool
LoginForm form
//Logout
LogoutURI string
LogoutNextURI string
Expand All @@ -29,6 +36,7 @@ type webConfig struct {
RegisterView string
RegisterEnabled bool
RegisterAutoLoginEnabled bool
RegisterForm form
//Forgot password
ForgotPasswordURI string
ForgotPasswordNextURI string
Expand Down Expand Up @@ -91,122 +99,130 @@ type webConfig struct {
func loadConfig() {
stormpath.InitLog()

viper.SetConfigType("yaml")
viper.AutomaticEnv()
v := viper.New()

v.SetConfigType("yaml")
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

//Load bundled default config
defaultConfig, err := Asset("config/web.stormpath.yaml")
if err != nil {
stormpath.Logger.Panicf("[ERROR] Couldn't load default bundle configuration: %s", err)
}

viper.ReadConfig(bytes.NewBuffer(defaultConfig))
v.ReadConfig(bytes.NewBuffer(defaultConfig))

//Merge users custom configuration
viper.SetConfigFile("stormpath.yaml")
viper.AddConfigPath("~/.stormpath/")
viper.AddConfigPath(".")
err = viper.MergeInConfig()
v.SetConfigName("stormpath")
v.AddConfigPath(os.Getenv("HOME") + "/.stormpath")
v.AddConfigPath(".")
err = v.MergeInConfig()
if err != nil {
stormpath.Logger.Println("[WARN] User didn't provide custom configuration")
}

Config.Produces = viper.GetStringSlice("stormpath.web.produces")
Config.BasePath = viper.GetString("stormpath.web.basePath")
Config.Produces = v.GetStringSlice("stormpath.web.produces")
Config.BasePath = v.GetString("stormpath.web.basePath")

Config.ApplicationHref = v.GetString("stormpath.application.href")
Config.ApplicationName = v.GetString("stormpath.application.name")

loadSocialConfig()
loadCookiesConfig()
loadEndpointsConfig()
loadOAuth2Config()
loadSocialConfig(v)
loadCookiesConfig(v)
loadEndpointsConfig(v)
loadOAuth2Config(v)
}

func loadOAuth2Config() {
Config.OAuth2Enabled = viper.GetBool("stormpath.web.oauth2.enabled")
Config.OAuth2URI = viper.GetString("stormpath.web.oauth2.uri")
Config.OAuth2ClientCredentialsGrantTypeEnabled = viper.GetBool("stormpath.web.oauth2.client_credentials.enabled")
Config.OAuth2ClientCredentialsGrantTypeAccessTokenTTL = time.Duration(viper.GetInt("stormpath.web.oauth2.client_credentials.accessToken.ttl")) * time.Second
Config.OAuth2PasswordGrantTypeEnabled = viper.GetBool("stormpath.web.oauth2.password.enabled")
Config.OAuth2PasswordGrantTypeValidationStrategy = viper.GetString("stormpath.web.oauth2.password.validationStrategy")
func loadOAuth2Config(v *viper.Viper) {
Config.OAuth2Enabled = v.GetBool("stormpath.web.oauth2.enabled")
Config.OAuth2URI = v.GetString("stormpath.web.oauth2.uri")
Config.OAuth2ClientCredentialsGrantTypeEnabled = v.GetBool("stormpath.web.oauth2.client_credentials.enabled")
Config.OAuth2ClientCredentialsGrantTypeAccessTokenTTL = time.Duration(v.GetInt("stormpath.web.oauth2.client_credentials.accessToken.ttl")) * time.Second
Config.OAuth2PasswordGrantTypeEnabled = v.GetBool("stormpath.web.oauth2.password.enabled")
Config.OAuth2PasswordGrantTypeValidationStrategy = v.GetString("stormpath.web.oauth2.password.validationStrategy")
}

func loadSocialConfig() {
Config.FacebookCallbackURI = viper.GetString("stormpath.web.social.facebook.uri")
Config.FacebookScope = viper.GetString("stormpath.web.social.facebook.scope")
Config.GoogleCallbackURI = viper.GetString("stormpath.web.social.google.uri")
Config.GoogleScope = viper.GetString("stormpath.web.social.google.scope")
Config.LinkedinCallbackURI = viper.GetString("stormpath.web.social.linkedin.uri")
Config.LinkedinScope = viper.GetString("stormpath.web.social.linkedin.scope")
Config.GithubCallbackURI = viper.GetString("stormpath.web.social.github.uri")
Config.GithubScope = viper.GetString("stormpath.web.social.github.scope")
func loadSocialConfig(v *viper.Viper) {
Config.FacebookCallbackURI = v.GetString("stormpath.web.social.facebook.uri")
Config.FacebookScope = v.GetString("stormpath.web.social.facebook.scope")
Config.GoogleCallbackURI = v.GetString("stormpath.web.social.google.uri")
Config.GoogleScope = v.GetString("stormpath.web.social.google.scope")
Config.LinkedinCallbackURI = v.GetString("stormpath.web.social.linkedin.uri")
Config.LinkedinScope = v.GetString("stormpath.web.social.linkedin.scope")
Config.GithubCallbackURI = v.GetString("stormpath.web.social.github.uri")
Config.GithubScope = v.GetString("stormpath.web.social.github.scope")
}

func loadCookiesConfig() {
func loadCookiesConfig(v *viper.Viper) {
//AccessToken
Config.AccessTokenCookieHTTPOnly = viper.GetBool("stormpath.web.accessTokenCookie.httpOnly")
Config.AccessTokenCookieName = viper.GetString("stormpath.web.accessTokenCookie.name")
Config.AccessTokenCookieSecure = loadBoolPtr("stormpath.web.accessTokenCookie.secure")
Config.AccessTokenCookiePath = viper.GetString("stormpath.web.accessTokenCookie.path")
Config.AccessTokenCookieDomain = viper.GetString("stormpath.web.accessTokenCookie.domain")
Config.AccessTokenCookieHTTPOnly = v.GetBool("stormpath.web.accessTokenCookie.httpOnly")
Config.AccessTokenCookieName = v.GetString("stormpath.web.accessTokenCookie.name")
Config.AccessTokenCookieSecure = loadBoolPtr("stormpath.web.accessTokenCookie.secure", v)
Config.AccessTokenCookiePath = v.GetString("stormpath.web.accessTokenCookie.path")
Config.AccessTokenCookieDomain = v.GetString("stormpath.web.accessTokenCookie.domain")
//RefreshToken
Config.RefreshTokenCookieHTTPOnly = viper.GetBool("stormpath.web.refreshTokenCookie.httpOnly")
Config.RefreshTokenCookieName = viper.GetString("stormpath.web.refreshTokenCookie.name")
Config.RefreshTokenCookieSecure = loadBoolPtr("stormpath.web.refreshTokenCookie.secure")
Config.RefreshTokenCookiePath = viper.GetString("stormpath.web.refreshTokenCookie.path")
Config.RefreshTokenCookieDomain = viper.GetString("stormpath.web.refreshTokenCookie.domain")
Config.RefreshTokenCookieHTTPOnly = v.GetBool("stormpath.web.refreshTokenCookie.httpOnly")
Config.RefreshTokenCookieName = v.GetString("stormpath.web.refreshTokenCookie.name")
Config.RefreshTokenCookieSecure = loadBoolPtr("stormpath.web.refreshTokenCookie.secure", v)
Config.RefreshTokenCookiePath = v.GetString("stormpath.web.refreshTokenCookie.path")
Config.RefreshTokenCookieDomain = v.GetString("stormpath.web.refreshTokenCookie.domain")
}

func loadEndpointsConfig() {
func loadEndpointsConfig(v *viper.Viper) {
//Login
Config.LoginURI = viper.GetString("stormpath.web.login.uri")
Config.LoginNextURI = viper.GetString("stormpath.web.login.nextUri")
Config.LoginView = viper.GetString("stormpath.web.login.view")
Config.LoginEnabled = viper.GetBool("stormpath.web.login.enabled")
Config.LoginURI = v.GetString("stormpath.web.login.uri")
Config.LoginNextURI = v.GetString("stormpath.web.login.nextUri")
Config.LoginView = v.GetString("stormpath.web.login.view")
Config.LoginEnabled = v.GetBool("stormpath.web.login.enabled")
Config.LoginForm = buildForm("login", v)
//Register
Config.RegisterURI = viper.GetString("stormpath.web.register.uri")
Config.RegisterView = viper.GetString("stormpath.web.register.view")
Config.RegisterNextURI = viper.GetString("stormpath.web.register.uri")
Config.RegisterEnabled = viper.GetBool("stormpath.web.register.enabled")
Config.RegisterAutoLoginEnabled = viper.GetBool("stormpath.web.register.autoLogin")
Config.RegisterURI = v.GetString("stormpath.web.register.uri")
Config.RegisterView = v.GetString("stormpath.web.register.view")
Config.RegisterNextURI = v.GetString("stormpath.web.register.uri")
Config.RegisterEnabled = v.GetBool("stormpath.web.register.enabled")
Config.RegisterAutoLoginEnabled = v.GetBool("stormpath.web.register.autoLogin")
Config.RegisterForm = buildForm("register", v)
//Verify
Config.VerifyURI = viper.GetString("stormpath.web.verifyEmail.uri")
Config.VerifyEnabled = loadBoolPtr("stormpath.web.verifyEmail.enabled")
Config.VerifyView = viper.GetString("stormpath.web.verifyEmail.view")
Config.VerifyNextURI = viper.GetString("stormpath.web.verifyEmail.nextUri")
Config.VerifyURI = v.GetString("stormpath.web.verifyEmail.uri")
Config.VerifyEnabled = loadBoolPtr("stormpath.web.verifyEmail.enabled", v)
Config.VerifyView = v.GetString("stormpath.web.verifyEmail.view")
Config.VerifyNextURI = v.GetString("stormpath.web.verifyEmail.nextUri")
//Forgot Password
Config.ForgotPasswordURI = viper.GetString("stormpath.web.forgotPassword.uri")
Config.ForgotPasswordNextURI = viper.GetString("stormpath.web.forgotPassword.nextUri")
Config.ForgotPasswordView = viper.GetString("stormpath.web.forgotPassword.view")
Config.ForgotPasswordEnabled = loadBoolPtr("stormpath.web.forgotPassword.enabled")
Config.ForgotPasswordURI = v.GetString("stormpath.web.forgotPassword.uri")
Config.ForgotPasswordNextURI = v.GetString("stormpath.web.forgotPassword.nextUri")
Config.ForgotPasswordView = v.GetString("stormpath.web.forgotPassword.view")
Config.ForgotPasswordEnabled = loadBoolPtr("stormpath.web.forgotPassword.enabled", v)
//Change Password
Config.ChangePasswordURI = viper.GetString("stormpath.web.changePassword.uri")
Config.ChangePasswordNextURI = viper.GetString("stormpath.web.changePassword.nextUri")
Config.ChangePasswordView = viper.GetString("stormpath.web.changePassword.view")
Config.ChangePasswordEnabled = loadBoolPtr("stormpath.web.changePassword.enabled")
Config.ChangePasswordAutoLoginEnabled = viper.GetBool("stormpath.web.changePassword.autoLogin")
Config.ChangePasswordErrorURI = viper.GetString("stormpath.web.changePassword.errorUri")
Config.ChangePasswordURI = v.GetString("stormpath.web.changePassword.uri")
Config.ChangePasswordNextURI = v.GetString("stormpath.web.changePassword.nextUri")
Config.ChangePasswordView = v.GetString("stormpath.web.changePassword.view")
Config.ChangePasswordEnabled = loadBoolPtr("stormpath.web.changePassword.enabled", v)
Config.ChangePasswordAutoLoginEnabled = v.GetBool("stormpath.web.changePassword.autoLogin")
Config.ChangePasswordErrorURI = v.GetString("stormpath.web.changePassword.errorUri")
//Logout
Config.LogoutURI = viper.GetString("stormpath.web.logout.uri")
Config.LogoutNextURI = viper.GetString("stormpath.web.logout.nextUri")
Config.LogoutEnabled = viper.GetBool("stormpath.web.logout.enabled")
Config.LogoutURI = v.GetString("stormpath.web.logout.uri")
Config.LogoutNextURI = v.GetString("stormpath.web.logout.nextUri")
Config.LogoutEnabled = v.GetBool("stormpath.web.logout.enabled")
//IDSite
Config.IDSiteEnabled = viper.GetBool("stormpath.web.idSite.enabled")
Config.IDSiteLoginURI = viper.GetString("stormpath.web.idSite.loginUri")
Config.IDSiteForgotURI = viper.GetString("stormpath.web.idSite.forgotUri")
Config.IDSiteRegisterURI = viper.GetString("stormpath.web.idSite.registerUri")
Config.CallbackEnabled = viper.GetBool("stormpath.web.callback.enabled")
Config.CallbackURI = viper.GetString("stormpath.web.callback.uri")
Config.IDSiteEnabled = v.GetBool("stormpath.web.idSite.enabled")
Config.IDSiteLoginURI = v.GetString("stormpath.web.idSite.loginUri")
Config.IDSiteForgotURI = v.GetString("stormpath.web.idSite.forgotUri")
Config.IDSiteRegisterURI = v.GetString("stormpath.web.idSite.registerUri")
Config.CallbackEnabled = v.GetBool("stormpath.web.callback.enabled")
Config.CallbackURI = v.GetString("stormpath.web.callback.uri")
//Me
Config.MeEnabled = viper.GetBool("stormpath.web.me.enabled")
Config.MeURI = viper.GetString("stormpath.web.me.uri")
Config.MeExpand = viper.GetStringMap("stormpath.web.me.expand")
Config.MeEnabled = v.GetBool("stormpath.web.me.enabled")
Config.MeURI = v.GetString("stormpath.web.me.uri")
Config.MeExpand = v.GetStringMap("stormpath.web.me.expand")
}

func loadBoolPtr(key string) *bool {
val := viper.Get(key)
func loadBoolPtr(key string, v *viper.Viper) *bool {
val := v.Get(key)
if val == nil {
return nil
}
b := viper.GetBool(key)
b := v.GetBool(key)
return &b
}

Expand Down Expand Up @@ -243,3 +259,36 @@ func IsVerifyEnabled(application *stormpath.Application) bool {

return false
}

func buildForm(formName string, v *viper.Viper) form {
form := form{}

for _, fieldName := range getConfiguredFormFieldNames(formName, v) {
field := field{
Name: fieldName,
Label: v.GetString("stormpath.web." + formName + ".form.fields." + fieldName + ".label"),
PlaceHolder: v.GetString("stormpath.web." + formName + ".form.fields." + fieldName + ".placeHolder"),
Visible: v.GetBool("stormpath.web." + formName + ".form.fields." + fieldName + ".visible"),
Enabled: v.GetBool("stormpath.web." + formName + ".form.fields." + fieldName + ".enabled"),
Required: v.GetBool("stormpath.web." + formName + ".form.fields." + fieldName + ".required"),
Type: v.GetString("stormpath.web." + formName + ".form.fields." + fieldName + ".type"),
}
if field.Enabled {
form.Fields = append(form.Fields, field)
}
}

return form
}

func getConfiguredFormFieldNames(formName string, v *viper.Viper) []string {
configuredFields := v.GetStringMapString("stormpath.web." + formName + ".form.fields")
fieldOrder := v.GetStringSlice("stormpath.web." + formName + ".form.fieldOrder")

for fieldName := range configuredFields {
if !contains(fieldOrder, fieldName) {
fieldOrder = append(fieldOrder, fieldName)
}
}
return fieldOrder
}
Loading

0 comments on commit 773536f

Please sign in to comment.