Skip to content

Commit

Permalink
Merge pull request #110 from astronomerio/feature/implement-v0.3.3-au…
Browse files Browse the repository at this point in the history
…th-flows

Feature/implement v0.3.3 auth flows
  • Loading branch information
andscoop committed Aug 16, 2018
2 parents c87a92b + 808afa8 commit 54118e3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 48 deletions.
19 changes: 4 additions & 15 deletions auth/auth.go
Expand Up @@ -48,14 +48,7 @@ func getWorkspaceByLabel(label string) *houston.Workspace {
func oAuth(oAuthUrl string) string {
fmt.Println("\n" + messages.HOUSTON_OAUTH_REDIRECT)
fmt.Println(oAuthUrl + "\n")
authSecret := input.InputText(messages.INPUT_OAUTH_TOKEN)

token, err := api.CreateOAuthToken(authSecret)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return token.Token.Value
return input.InputText(messages.INPUT_OAUTH_TOKEN)
}

// registryAuth authenticates with the private registry
Expand Down Expand Up @@ -112,13 +105,13 @@ func Login(domain string, oAuthOnly bool) error {
}

username := ""
if !oAuthOnly {
if !oAuthOnly && authConfig.LocalEnabled {
username = input.InputText(messages.INPUT_USERNAME)
}

if len(username) == 0 {
if authConfig.GoogleEnabled {
token = oAuth(authConfig.OauthUrl)
if authConfig.GoogleEnabled || authConfig.Auth0Enabled || authConfig.GithubEnabled {
token = oAuth(c.GetAppURL() + "/login?source=cli")
} else {
fmt.Println(messages.HOUSTON_OAUTH_DISABLED)
os.Exit(1)
Expand All @@ -131,10 +124,6 @@ func Login(domain string, oAuthOnly bool) error {
}
}

c, err = cluster.GetCluster(domain)
if err != nil {
return err
}
c.SetContextKey("token", token)

// Attempt to set projectworkspace if there is only one workspace
Expand Down
4 changes: 3 additions & 1 deletion config/config.go
Expand Up @@ -41,7 +41,9 @@ var (
CloudAPIPort: newCfg("cloud.api.port", "443"),
CloudAPIToken: newCfg("cloud.api.token", ""),
Context: newCfg("context", ""),
LocalAPIURL: newCfg("local.api.url", ""),
LocalEnabled: newCfg("local.enabled", ""),
LocalHouston: newCfg("local.houston", ""),
LocalOrbit: newCfg("local.orbit", ""),
PostgresUser: newCfg("postgres.user", "postgres"),
PostgresPassword: newCfg("postgres.password", "postgres"),
PostgresHost: newCfg("postgres.host", "postgres"),
Expand Down
15 changes: 15 additions & 0 deletions config/context.go
Expand Up @@ -168,10 +168,25 @@ func (c Context) SwitchContext() error {

// GetAPIURL returns full Houston API Url for the provided Context
func (c Context) GetAPIURL() string {
if len(CFG.LocalEnabled.GetString()) != 0 {
return CFG.LocalHouston.GetString()
}
return fmt.Sprintf(
"%s://houston.%s:%s/v1",
CFG.CloudAPIProtocol.GetString(),
c.Domain,
CFG.CloudAPIPort.GetString(),
)
}

// GetAppURL returns full Houston API Url for the provided Context
func (c Context) GetAppURL() string {
if len(CFG.LocalEnabled.GetString()) != 0 {
return CFG.LocalOrbit.GetString()
}
return fmt.Sprintf(
"%s://app.%s",
CFG.CloudAPIProtocol.GetString(),
c.Domain,
)
}
4 changes: 3 additions & 1 deletion config/types.go
Expand Up @@ -13,7 +13,9 @@ type cfgs struct {
CloudAPIPort cfg
CloudAPIToken cfg
Context cfg
LocalAPIURL cfg
LocalEnabled cfg
LocalHouston cfg
LocalOrbit cfg
PostgresUser cfg
PostgresPassword cfg
PostgresHost cfg
Expand Down
32 changes: 4 additions & 28 deletions houston/houston.go
Expand Up @@ -17,9 +17,11 @@ import (
var (
authConfigGetRequest = `
query GetAuthConfig {
authConfig(state: "cli") {
authConfig(redirect: "") {
localEnabled
googleEnabled
githubEnabled
auth0Enabled
googleOAuthUrl
}
}`
Expand Down Expand Up @@ -102,9 +104,8 @@ var (
tokenBasicCreateRequest = `
mutation createBasicToken {
createToken(
authStrategy:LOCAL
identity:"%s",
credentials:"%s"
password:"%s"
) {
user {
uuid
Expand All @@ -119,18 +120,6 @@ var (
}
}`

tokenOAuthCreateRequest = `
mutation createOauthBasicToken {
createToken(
authStrategy:%s
credentials:"%s"
) {
token {
value
}
}
}`

userCreateRequest = `
mutation CreateUser {
createUser(
Expand Down Expand Up @@ -365,19 +354,6 @@ func (c *Client) CreateBasicToken(email, password string) (*AuthUser, error) {
return response.Data.CreateToken, nil
}

// CreateOAuthToken passes an OAuth type and authCode to createOauthTokenRequest in order allow houston to authenticate user
// Returns a Token structure with the users ID and Token inside.
func (c *Client) CreateOAuthToken(authCode string) (*AuthUser, error) {
request := fmt.Sprintf(tokenOAuthCreateRequest, "GOOGLE_OAUTH", authCode)

response, err := c.QueryHouston(request)
if err != nil {
return nil, errors.Wrap(err, "CreateOAuthToken Failed")
}

return response.Data.CreateToken, nil
}

// CreateUser sends request to request to Houston in order to create a new platform User
// Returns an AuthUser object containing an token
func (c *Client) CreateUser(email string, password string) (*AuthUser, error) {
Expand Down
7 changes: 4 additions & 3 deletions houston/types.go
Expand Up @@ -23,9 +23,10 @@ type HoustonResponse struct {

// AuthConfig holds data related to oAuth and basic authentication
type AuthConfig struct {
LocalEnabled bool `json:"localEnabled"`
GoogleEnabled bool `json:"googleEnabled"`
OauthUrl string `json:"googleOAuthUrl"`
LocalEnabled bool `json:"localEnabled"`
GoogleEnabled bool `json:"googleEnabled"`
GithubEnabled bool `json:"githubEnabled"`
Auth0Enabled bool `json:"auth0Enabled"`
}

type AuthUser struct {
Expand Down

0 comments on commit 54118e3

Please sign in to comment.