Skip to content

Commit

Permalink
Merge pull request #421 from nullable-eth/up-web-login
Browse files Browse the repository at this point in the history
add alpha command for web login
  • Loading branch information
branden committed Feb 1, 2024
2 parents c2480b1 + 6682164 commit 19dd493
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 36 deletions.
36 changes: 20 additions & 16 deletions cmd/up/login.go → cmd/up/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package login

import (
"bytes"
Expand Down Expand Up @@ -50,13 +50,13 @@ const (
)

// BeforeApply sets default values in login before assignment and validation.
func (c *loginCmd) BeforeApply() error { //nolint:unparam
func (c *LoginCmd) BeforeApply() error { //nolint:unparam
c.stdin = os.Stdin
c.prompter = input.NewPrompter()
return nil
}

func (c *loginCmd) AfterApply(kongCtx *kong.Context) error {
func (c *LoginCmd) AfterApply(kongCtx *kong.Context) error {
upCtx, err := upbound.NewFromFlags(c.Flags, upbound.AllowMissingProfile())
if err != nil {
return err
Expand Down Expand Up @@ -97,9 +97,9 @@ func (c *loginCmd) AfterApply(kongCtx *kong.Context) error {
return nil
}

// loginCmd adds a user or token profile with session token to the up config
// LoginCmd adds a user or token profile with session token to the up config
// file.
type loginCmd struct {
type LoginCmd struct {
client uphttp.Client
stdin io.Reader
prompter input.Prompter
Expand All @@ -113,7 +113,7 @@ type loginCmd struct {
}

// Run executes the login command.
func (c *loginCmd) Run(ctx context.Context, p pterm.TextPrinter, upCtx *upbound.Context) error { // nolint:gocyclo
func (c *LoginCmd) Run(ctx context.Context, p pterm.TextPrinter, upCtx *upbound.Context) error { // nolint:gocyclo
if c.Token == "-" {
b, err := io.ReadAll(c.stdin)
if err != nil {
Expand Down Expand Up @@ -150,9 +150,20 @@ func (c *loginCmd) Run(ctx context.Context, p pterm.TextPrinter, upCtx *upbound.
return errors.Wrap(err, errLoginFailed)
}
defer res.Body.Close() // nolint:gosec,errcheck
return errors.Wrap(setSession(ctx, p, upCtx, res, profType, auth.ID), errLoginFailed)
}

// auth is the request body sent to authenticate a user or token.
type auth struct {
ID string `json:"id"`
Password string `json:"password"`
Remember bool `json:"remember"`
}

func setSession(ctx context.Context, p pterm.TextPrinter, upCtx *upbound.Context, res *http.Response, profType profile.Type, authID string) error {
session, err := extractSession(res, upbound.CookieName)
if err != nil {
return errors.Wrap(err, errLoginFailed)
return err
}

// If profile name was not provided and no default exists, set name to 'default'.
Expand All @@ -163,7 +174,7 @@ func (c *loginCmd) Run(ctx context.Context, p pterm.TextPrinter, upCtx *upbound.
// Re-initialize profile for this login.
profile := profile.Profile{
Type: profType,
ID: auth.ID,
ID: authID,
// Set session early so that it can be used to fetch user info if
// necessary.
Session: session,
Expand Down Expand Up @@ -195,17 +206,10 @@ func (c *loginCmd) Run(ctx context.Context, p pterm.TextPrinter, upCtx *upbound.
if err := upCtx.CfgSrc.UpdateConfig(upCtx.Cfg); err != nil {
return errors.Wrap(err, errUpdateConfig)
}
p.Printfln("%s logged in", auth.ID)
p.Printfln("%s logged in", authID)
return nil
}

// auth is the request body sent to authenticate a user or token.
type auth struct {
ID string `json:"id"`
Password string `json:"password"`
Remember bool `json:"remember"`
}

// constructAuth constructs the body of an Upbound Cloud authentication request
// given the provided credentials.
func constructAuth(username, token, password string) (*auth, profile.Type, error) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/up/login_test.go → cmd/up/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package login

import (
"bytes"
Expand Down Expand Up @@ -40,19 +40,19 @@ func TestRun(t *testing.T) {

cases := map[string]struct {
reason string
cmd *loginCmd
cmd *LoginCmd
ctx *upbound.Context
err error
}{
"ErrorNoUserOrToken": {
reason: "If neither user or token is provided an error should be returned.",
cmd: &loginCmd{},
cmd: &LoginCmd{},
ctx: &upbound.Context{},
err: errors.Wrap(errors.New(errNoUserOrToken), errLoginFailed),
},
"ErrLoginFailed": {
reason: "If Upbound Cloud endpoint is ",
cmd: &loginCmd{
cmd: &LoginCmd{
client: &mocks.MockClient{
DoFn: func(req *http.Request) (*http.Response, error) {
return nil, errBoom
Expand Down
10 changes: 5 additions & 5 deletions cmd/up/logout.go → cmd/up/login/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package login

import (
"context"
Expand All @@ -36,7 +36,7 @@ const (
)

// AfterApply sets default values in login after assignment and validation.
func (c *logoutCmd) AfterApply(kongCtx *kong.Context) error {
func (c *LogoutCmd) AfterApply(kongCtx *kong.Context) error {
upCtx, err := upbound.NewFromFlags(c.Flags)
if err != nil {
return err
Expand All @@ -50,16 +50,16 @@ func (c *logoutCmd) AfterApply(kongCtx *kong.Context) error {
return nil
}

// logoutCmd invalidates a stored session token for a given profile.
type logoutCmd struct {
// LogoutCmd invalidates a stored session token for a given profile.
type LogoutCmd struct {
client up.Client

// Common Upbound API configuration
Flags upbound.Flags `embed:""`
}

// Run executes the logout command.
func (c *logoutCmd) Run(ctx context.Context, p pterm.TextPrinter, upCtx *upbound.Context) error {
func (c *LogoutCmd) Run(ctx context.Context, p pterm.TextPrinter, upCtx *upbound.Context) error {
if upCtx.Profile.IsSpace() {
return fmt.Errorf("logout is not supported for space profile %q", upCtx.ProfileName)
}
Expand Down
Loading

0 comments on commit 19dd493

Please sign in to comment.