Skip to content

Commit

Permalink
Refactor auth for v0.3.3 auth flows
Browse files Browse the repository at this point in the history
  • Loading branch information
andscoop committed Aug 15, 2018
1 parent 2b09417 commit f596027
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 42 deletions.
15 changes: 4 additions & 11 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 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,
)
}
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 f596027

Please sign in to comment.