Skip to content

feat: Login via CLI #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix lint issues
  • Loading branch information
bryphe-coder committed Feb 17, 2022
commit 8ea29203a78b7b4d2e75b52dd44c0e7ab505bf42
11 changes: 5 additions & 6 deletions cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

const (
goosWindows = "windows"
goosLinux = "linux"
goosDarwin = "darwin"
)

Expand Down Expand Up @@ -173,7 +172,7 @@ func saveSessionToken(cmd *cobra.Command, client *codersdk.Client, sessionToken
client.SessionToken = sessionToken
resp, err := client.User(cmd.Context(), "me")
if err != nil {
return xerrors.Errorf("get user: ", err)
return xerrors.Errorf("get user: %w", err)
}

config := createConfig(cmd)
Expand Down Expand Up @@ -203,7 +202,7 @@ func isWSL() (bool, error) {
}

// openURL opens the provided URL via user's default browser
func openURL(url string) error {
func openURL(urlToOpen string) error {
var cmd string
var args []string

Expand All @@ -215,10 +214,10 @@ func openURL(url string) error {
if wsl {
cmd = "cmd.exe"
args = []string{"/c", "start"}
url = strings.ReplaceAll(url, "&", "^&")
args = append(args, url)
urlToOpen = strings.ReplaceAll(urlToOpen, "&", "^&")
args = append(args, urlToOpen)
return exec.Command(cmd, args...).Start()
}

return browser.OpenURL(url)
return browser.OpenURL(urlToOpen)
}
3 changes: 2 additions & 1 deletion cli/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/console"
"github.com/stretchr/testify/require"
)

func TestLogin(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion cli/workspacecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package cli_test
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/console"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/stretchr/testify/require"
)

func TestWorkspaceCreate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func New(options *Options) http.Handler {
r.Use(
httpmw.ExtractAPIKey(options.Database, nil),
)
r.Post("/", api.postApiKey)
r.Post("/", api.postAPIKey)
})

// Used for setup.
Expand Down
3 changes: 2 additions & 1 deletion coderd/projectimport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"net/http"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/database"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/stretchr/testify/require"
)

func TestPostProjectImportByOrganization(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (api *api) postLogin(rw http.ResponseWriter, r *http.Request) {
}

// Creates a new API key, used for logging in via the CLI
func (api *api) postApiKey(rw http.ResponseWriter, r *http.Request) {
func (api *api) postAPIKey(rw http.ResponseWriter, r *http.Request) {
apiKey := httpmw.APIKey(r)
userID := apiKey.UserID

Expand Down Expand Up @@ -348,10 +348,10 @@ func (api *api) postApiKey(rw http.ResponseWriter, r *http.Request) {
}

// This format is consumed by the APIKey middleware.
generatedApiKey := fmt.Sprintf("%s-%s", keyID, keySecret)
generatedAPIKey := fmt.Sprintf("%s-%s", keyID, keySecret)

render.Status(r, http.StatusCreated)
render.JSON(rw, r, GenerateAPIKeyResponse{Key: generatedApiKey})
render.JSON(rw, r, GenerateAPIKeyResponse{Key: generatedAPIKey})
}

// Clear the user's session cookie
Expand Down
4 changes: 2 additions & 2 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestPostAPIKey(t *testing.T) {
// Clear session token
client.SessionToken = ""
// ...and request an API key
_, err := client.CreateApiKey(context.Background())
_, err := client.CreateAPIKey(context.Background())
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
Expand All @@ -139,7 +139,7 @@ func TestPostAPIKey(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
_ = coderdtest.CreateInitialUser(t, client)
apiKey, err := client.CreateApiKey(context.Background())
apiKey, err := client.CreateAPIKey(context.Background())
require.NotNil(t, apiKey)
require.GreaterOrEqual(t, len(apiKey.Key), 2)
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions codersdk/projectimport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)

func TestCreateProjectImportJob(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions codersdk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func (c *Client) CreateUser(ctx context.Context, req coderd.CreateUserRequest) (
return user, json.NewDecoder(res.Body).Decode(&user)
}

// CreateApiKey calls the /api-key API
func (c *Client) CreateApiKey(ctx context.Context) (*coderd.GenerateAPIKeyResponse, error) {
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/api-keys"), nil)
// CreateAPIKey calls the /api-key API
func (c *Client) CreateAPIKey(ctx context.Context) (*coderd.GenerateAPIKeyResponse, error) {
res, err := c.request(ctx, http.MethodPost, "/api/v2/api-keys", nil)
if err != nil {
return nil, err
}
Expand Down