Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c4d8f8

Browse files
committed
chore: add --full flag to external auth agent command
1 parent 93a233a commit 7c4d8f8

File tree

7 files changed

+66
-30
lines changed

7 files changed

+66
-30
lines changed

cli/externalauth.go

+31-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func (r *RootCmd) externalAuth() *serpent.Command {
2828

2929
func (r *RootCmd) externalAuthAccessToken() *serpent.Command {
3030
var extra string
31+
var full bool
3132
return &serpent.Command{
3233
Use: "access-token <provider>",
3334
Short: "Print auth for an external provider",
@@ -55,12 +56,22 @@ fi
5556
Middleware: serpent.Chain(
5657
serpent.RequireNArgs(1),
5758
),
58-
Options: serpent.OptionSet{{
59-
Name: "Extra",
60-
Flag: "extra",
61-
Description: "Extract a field from the \"extra\" properties of the OAuth token.",
62-
Value: serpent.StringOf(&extra),
63-
}},
59+
Options: serpent.OptionSet{
60+
{
61+
Name: "Extra",
62+
Flag: "extra",
63+
Description: "Extract a field from the \"extra\" properties of the OAuth token.",
64+
Value: serpent.StringOf(&extra),
65+
}, {
66+
Name: "Full",
67+
Description: "Print the full response from the external auth provider as json.",
68+
Required: false,
69+
Flag: "full",
70+
FlagShorthand: "",
71+
Default: "false",
72+
Value: serpent.BoolOf(&full),
73+
},
74+
},
6475

6576
Handler: func(inv *serpent.Invocation) error {
6677
ctx := inv.Context()
@@ -86,6 +97,20 @@ fi
8697
}
8798
return cliui.Canceled
8899
}
100+
101+
if extra != "" && full {
102+
return xerrors.Errorf("cannot specify both --extra and --full")
103+
}
104+
105+
if full {
106+
data, err := json.Marshal(extAuth)
107+
if err != nil {
108+
return xerrors.Errorf("marshal auth: %w", err)
109+
}
110+
_, _ = inv.Stdout.Write(data)
111+
return nil
112+
}
113+
89114
if extra != "" {
90115
if extAuth.TokenExtra == nil {
91116
return xerrors.Errorf("no extra properties found for token")

coderd/apidoc/docs.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/workspaceagents.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"time"
1616

1717
"github.com/google/uuid"
18-
"github.com/sqlc-dev/pqtype"
1918
"golang.org/x/exp/maps"
2019
"golang.org/x/exp/slices"
2120
"golang.org/x/mod/semver"
@@ -1991,7 +1990,7 @@ func (api *API) workspaceAgentsExternalAuth(rw http.ResponseWriter, r *http.Requ
19911990
})
19921991
return
19931992
}
1994-
resp, err := createExternalAuthResponse(externalAuthConfig.Type, externalAuthLink.OAuthAccessToken, externalAuthLink.OAuthExtra)
1993+
resp, err := createExternalAuthResponse(externalAuthConfig.Type, externalAuthLink)
19951994
if err != nil {
19961995
handleRetrying(http.StatusInternalServerError, codersdk.Response{
19971996
Message: "Failed to create external auth response.",
@@ -2064,7 +2063,7 @@ func (api *API) workspaceAgentsExternalAuthListen(ctx context.Context, rw http.R
20642063
if !valid {
20652064
continue
20662065
}
2067-
resp, err := createExternalAuthResponse(externalAuthConfig.Type, externalAuthLink.OAuthAccessToken, externalAuthLink.OAuthExtra)
2066+
resp, err := createExternalAuthResponse(externalAuthConfig.Type, externalAuthLink)
20682067
if err != nil {
20692068
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
20702069
Message: "Failed to create external auth response.",
@@ -2080,33 +2079,34 @@ func (api *API) workspaceAgentsExternalAuthListen(ctx context.Context, rw http.R
20802079
// createExternalAuthResponse creates an ExternalAuthResponse based on the
20812080
// provider type. This is to support legacy `/workspaceagents/me/gitauth`
20822081
// which uses `Username` and `Password`.
2083-
func createExternalAuthResponse(typ, token string, extra pqtype.NullRawMessage) (agentsdk.ExternalAuthResponse, error) {
2082+
func createExternalAuthResponse(typ string, link database.ExternalAuthLink) (agentsdk.ExternalAuthResponse, error) {
20842083
var resp agentsdk.ExternalAuthResponse
20852084
switch typ {
20862085
case string(codersdk.EnhancedExternalAuthProviderGitLab):
20872086
// https://stackoverflow.com/questions/25409700/using-gitlab-token-to-clone-without-authentication
20882087
resp = agentsdk.ExternalAuthResponse{
20892088
Username: "oauth2",
2090-
Password: token,
2089+
Password: link.OAuthAccessToken,
20912090
}
20922091
case string(codersdk.EnhancedExternalAuthProviderBitBucketCloud), string(codersdk.EnhancedExternalAuthProviderBitBucketServer):
20932092
// The string "bitbucket" was a legacy parameter that needs to still be supported.
20942093
// https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/#Cloning-a-repository-with-an-access-token
20952094
resp = agentsdk.ExternalAuthResponse{
20962095
Username: "x-token-auth",
2097-
Password: token,
2096+
Password: link.OAuthAccessToken,
20982097
}
20992098
default:
21002099
resp = agentsdk.ExternalAuthResponse{
2101-
Username: token,
2100+
Username: link.OAuthAccessToken,
21022101
}
21032102
}
2104-
resp.AccessToken = token
2103+
resp.RefreshToken = link.OAuthRefreshToken
2104+
resp.AccessToken = link.OAuthAccessToken
21052105
resp.Type = typ
21062106

21072107
var err error
2108-
if extra.Valid {
2109-
err = json.Unmarshal(extra.RawMessage, &resp.TokenExtra)
2108+
if link.OAuthExtra.Valid {
2109+
err = json.Unmarshal(link.OAuthExtra.RawMessage, &resp.TokenExtra)
21102110
}
21112111
return resp, err
21122112
}

codersdk/agentsdk/agentsdk.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -561,15 +561,16 @@ func (c *Client) PostLogSource(ctx context.Context, req PostLogSource) (codersdk
561561
}
562562

563563
type ExternalAuthResponse struct {
564-
AccessToken string `json:"access_token"`
565-
TokenExtra map[string]interface{} `json:"token_extra"`
566-
URL string `json:"url"`
567-
Type string `json:"type"`
564+
AccessToken string `json:"access_token" table:"access_token"`
565+
RefreshToken string `json:"refresh_token" table:"refresh_token"`
566+
TokenExtra map[string]interface{} `json:"token_extra" table:"-"`
567+
URL string `json:"url" table:"url"`
568+
Type string `json:"type" table:"type,default_sort"`
568569

569570
// Deprecated: Only supported on `/workspaceagents/me/gitauth`
570571
// for backwards compatibility.
571-
Username string `json:"username"`
572-
Password string `json:"password"`
572+
Username string `json:"username" table:"-"`
573+
Password string `json:"password" table:"-"`
573574
}
574575

575576
// ExternalAuthRequest is used to request an access token for a provider.

docs/api/agents.md

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api/schemas.md

+10-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)