Skip to content

Commit a8b1f32

Browse files
committed
Kinda scratch
1 parent d30945c commit a8b1f32

File tree

19 files changed

+230
-50
lines changed

19 files changed

+230
-50
lines changed

cli/askpass.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/coder/coder/cli/gitaskpass"
7+
"github.com/spf13/cobra"
8+
"golang.org/x/xerrors"
9+
)
10+
11+
// askpass is used to internally swap
12+
func askpass() *cobra.Command {
13+
return &cobra.Command{
14+
Use: "askpass",
15+
Args: cobra.ExactArgs(1),
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
prompt, host, err := gitaskpass.Parse(args[0])
18+
if err != nil {
19+
return xerrors.Errorf("parse host: %w", err)
20+
}
21+
if prompt != "Username" {
22+
return nil
23+
}
24+
25+
fmt.Printf("Host: %s\n", host)
26+
// We should request coderd for the authentication token, and
27+
// on a specific status code we get a different response.
28+
29+
return xerrors.New("we here")
30+
},
31+
}
32+
}

cli/gitaskpass/parse.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package gitaskpass
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
7+
"golang.org/x/xerrors"
8+
)
9+
10+
var (
11+
hostReplace = regexp.MustCompile(`^["']+|["':]+$`)
12+
)
13+
14+
// Parse returns the host from a git ask pass prompt.
15+
func Parse(prompt string) (string, string, error) {
16+
parts := strings.Split(prompt, " ")
17+
if len(parts) < 3 {
18+
return "", "", xerrors.Errorf("got <3 parts: %q", prompt)
19+
}
20+
// https://github.com/microsoft/vscode/blob/328646ebc2f5016a1c67e0b23a0734bd598ec5a8/extensions/git/src/askpass-main.ts#L41
21+
host := parts[2]
22+
host = hostReplace.ReplaceAllString(host, "")
23+
return parts[0], host, nil
24+
}

cli/gitaskpass/parse_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package gitaskpass_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/coder/coder/cli/gitaskpass"
9+
)
10+
11+
func TestParse(t *testing.T) {
12+
t.Parallel()
13+
for _, tc := range []struct {
14+
Input string
15+
Output string
16+
}{{
17+
Input: "Username for 'https://github.com': ",
18+
Output: "https://github.com",
19+
}, {
20+
Input: "Username for 'https://enterprise.github.com': ",
21+
Output: "https://enterprise.github.com",
22+
}, {
23+
Input: "Username for 'http://wow.io': ",
24+
Output: "http://wow.io",
25+
}} {
26+
tc := tc
27+
t.Run(tc.Input, func(t *testing.T) {
28+
t.Parallel()
29+
value, err := gitaskpass.Parse(tc.Input)
30+
require.NoError(t, err)
31+
require.Equal(t, tc.Output, value)
32+
})
33+
}
34+
35+
// git_origins: []string{"https://github.com"}
36+
}

cli/root.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ func Root(subcommands []*cobra.Command) *cobra.Command {
152152
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
153153
}
154154
},
155+
Args: func(cmd *cobra.Command, args []string) error {
156+
_, hasGitPrefix := os.LookupEnv("GIT_PREFIX")
157+
if hasGitPrefix {
158+
return nil
159+
}
160+
return cobra.NoArgs(cmd, args)
161+
},
162+
RunE: func(cmd *cobra.Command, args []string) error {
163+
_, hasGitPrefix := os.LookupEnv("GIT_PREFIX")
164+
if hasGitPrefix {
165+
return askpass().ExecuteContext(cmd.Context())
166+
}
167+
return cmd.Help()
168+
},
155169
Example: formatExamples(
156170
example{
157171
Description: "Start a Coder server",

cli/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,11 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
11351135
TokenURL: tokenURL.String(),
11361136
}
11371137
}
1138+
rawOrigin, err := url.Parse(endpoint.AuthURL)
1139+
if err != nil {
1140+
return nil, xerrors.Errorf("parse auth url for endpoint: %w", err)
1141+
}
1142+
origin := fmt.Sprintf("%s://%s", rawOrigin.Scheme, rawOrigin.Host)
11381143

11391144
return &coderd.GithubOAuth2Config{
11401145
OAuth2Config: &oauth2.Config{
@@ -1151,6 +1156,7 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
11511156
AllowSignups: allowSignups,
11521157
AllowOrganizations: allowOrgs,
11531158
AllowTeams: allowTeams,
1159+
Origin: origin,
11541160
AuthenticatedUser: func(ctx context.Context, client *http.Client) (*github.User, error) {
11551161
api, err := createClient(client)
11561162
if err != nil {

coderd/database/databasefake/databasefake.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,7 @@ func (q *fakeQuerier) InsertAPIKey(_ context.Context, arg database.InsertAPIKeyP
16151615
UpdatedAt: arg.UpdatedAt,
16161616
LastUsed: arg.LastUsed,
16171617
LoginType: arg.LoginType,
1618+
LoginOrigin: arg.LoginOrigin,
16181619
Scope: arg.Scope,
16191620
}
16201621
q.apiKeys = append(q.apiKeys, key)
@@ -2574,12 +2575,12 @@ func (q *fakeQuerier) GetUserLinkByLinkedID(_ context.Context, id string) (datab
25742575
return database.UserLink{}, sql.ErrNoRows
25752576
}
25762577

2577-
func (q *fakeQuerier) GetUserLinkByUserIDLoginType(_ context.Context, params database.GetUserLinkByUserIDLoginTypeParams) (database.UserLink, error) {
2578+
func (q *fakeQuerier) GetUserLinkByUserIDAndLogin(_ context.Context, params database.GetUserLinkByUserIDAndLoginParams) (database.UserLink, error) {
25782579
q.mutex.RLock()
25792580
defer q.mutex.RUnlock()
25802581

25812582
for _, link := range q.userLinks {
2582-
if link.UserID == params.UserID && link.LoginType == params.LoginType {
2583+
if link.UserID == params.UserID && link.LoginType == params.LoginType && link.LoginOrigin == params.LoginOrigin {
25832584
return link, nil
25842585
}
25852586
}

coderd/database/dump.sql

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ALTER TABLE user_links DROP CONSTRAINT user_links_pkey;
2+
ALTER TABLE user_links ADD CONSTRAINT user_links_pkey PRIMARY KEY (user_id, login_type);
3+
4+
ALTER TABLE user_links
5+
DROP COLUMN login_origin;
6+
7+
ALTER TABLE api_keys
8+
DROP COLUMN login_origin;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ALTER TABLE user_links
2+
ADD COLUMN login_origin text NOT NULL DEFAULT '';
3+
4+
ALTER TABLE user_links DROP CONSTRAINT user_links_pkey;
5+
ALTER TABLE user_links ADD CONSTRAINT user_links_pkey PRIMARY KEY (user_id, login_type, login_origin);
6+
7+
ALTER TABLE api_keys
8+
ADD COLUMN login_origin text NOT NULL DEFAULT '';

coderd/database/models.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)