Skip to content

Commit 45396b3

Browse files
committed
feat: add endpoints to list all authed external apps
Also add support for unlinking on the coder side to allow reflow.
1 parent 0a16bda commit 45396b3

File tree

19 files changed

+562
-9
lines changed

19 files changed

+562
-9
lines changed

coderd/apidoc/docs.go

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

coderd/apidoc/swagger.json

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

coderd/coderd.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,21 @@ func New(options *Options) *API {
660660
r.Get("/{fileID}", api.fileByID)
661661
r.Post("/", api.postFile)
662662
})
663-
r.Route("/external-auth/{externalauth}", func(r chi.Router) {
663+
r.Route("/external-auth", func(r chi.Router) {
664664
r.Use(
665665
apiKeyMiddleware,
666-
httpmw.ExtractExternalAuthParam(options.ExternalAuthConfigs),
667666
)
668-
r.Get("/", api.externalAuthByID)
669-
r.Post("/device", api.postExternalAuthDeviceByID)
670-
r.Get("/device", api.externalAuthDeviceByID)
667+
// Get without a specific external auth ID will return all external auths.
668+
r.Get("/", api.userExternalAuths)
669+
r.Route("/{externalauth}", func(r chi.Router) {
670+
r.Use(
671+
httpmw.ExtractExternalAuthParam(options.ExternalAuthConfigs),
672+
)
673+
r.Delete("/", api.deleteExternalAuthByID)
674+
r.Get("/", api.externalAuthByID)
675+
r.Post("/device", api.postExternalAuthDeviceByID)
676+
r.Get("/device", api.externalAuthDeviceByID)
677+
})
671678
})
672679
r.Route("/organizations", func(r chi.Router) {
673680
r.Use(

coderd/database/db2sdk/db2sdk.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ import (
1616
"github.com/coder/coder/v2/provisionersdk/proto"
1717
)
1818

19+
func ExternalAuths(auths []database.ExternalAuthLink) []codersdk.ExternalAuthLink {
20+
out := make([]codersdk.ExternalAuthLink, 0, len(auths))
21+
for _, auth := range auths {
22+
out = append(out, ExternalAuth(auth))
23+
}
24+
return out
25+
}
26+
27+
func ExternalAuth(auth database.ExternalAuthLink) codersdk.ExternalAuthLink {
28+
return codersdk.ExternalAuthLink{
29+
ProviderID: auth.ProviderID,
30+
CreatedAt: auth.CreatedAt,
31+
UpdatedAt: auth.UpdatedAt,
32+
HasRefreshToken: auth.OAuthRefreshToken != "",
33+
Expires: auth.OAuthExpiry,
34+
}
35+
}
36+
1937
func WorkspaceBuildParameters(params []database.WorkspaceBuildParameter) []codersdk.WorkspaceBuildParameter {
2038
out := make([]codersdk.WorkspaceBuildParameter, len(params))
2139
for i, p := range params {

coderd/database/dbauthz/dbauthz.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,13 @@ func (q *querier) DeleteCoordinator(ctx context.Context, id uuid.UUID) error {
740740
return q.db.DeleteCoordinator(ctx, id)
741741
}
742742

743+
func (q *querier) DeleteExternalAuthLink(ctx context.Context, arg database.DeleteExternalAuthLinkParams) error {
744+
return deleteQ(q.log, q.auth, func(ctx context.Context, arg database.DeleteExternalAuthLinkParams) (database.ExternalAuthLink, error) {
745+
//nolint:gosimple
746+
return q.db.GetExternalAuthLink(ctx, database.GetExternalAuthLinkParams{UserID: arg.UserID, ProviderID: arg.ProviderID})
747+
}, q.db.DeleteExternalAuthLink)(ctx, arg)
748+
}
749+
743750
func (q *querier) DeleteGitSSHKey(ctx context.Context, userID uuid.UUID) error {
744751
return deleteQ(q.log, q.auth, q.db.GetGitSSHKey, q.db.DeleteGitSSHKey)(ctx, userID)
745752
}
@@ -975,10 +982,7 @@ func (q *querier) GetExternalAuthLink(ctx context.Context, arg database.GetExter
975982
}
976983

977984
func (q *querier) GetExternalAuthLinksByUserID(ctx context.Context, userID uuid.UUID) ([]database.ExternalAuthLink, error) {
978-
if err := q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceSystem); err != nil {
979-
return nil, err
980-
}
981-
return q.db.GetExternalAuthLinksByUserID(ctx, userID)
985+
return fetchWithPostFilter(q.auth, q.db.GetExternalAuthLinksByUserID)(ctx, userID)
982986
}
983987

984988
func (q *querier) GetFileByHashAndCreator(ctx context.Context, arg database.GetFileByHashAndCreatorParams) (database.File, error) {

coderd/database/dbmem/dbmem.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,29 @@ func (*FakeQuerier) DeleteCoordinator(context.Context, uuid.UUID) error {
10191019
return ErrUnimplemented
10201020
}
10211021

1022+
func (q *FakeQuerier) DeleteExternalAuthLink(ctx context.Context, arg database.DeleteExternalAuthLinkParams) error {
1023+
err := validateDatabaseType(arg)
1024+
if err != nil {
1025+
return err
1026+
}
1027+
1028+
q.mutex.Lock()
1029+
defer q.mutex.Unlock()
1030+
1031+
for index, key := range q.externalAuthLinks {
1032+
if key.UserID != arg.UserID {
1033+
continue
1034+
}
1035+
if key.ProviderID != arg.ProviderID {
1036+
continue
1037+
}
1038+
q.externalAuthLinks[index] = q.externalAuthLinks[len(q.externalAuthLinks)-1]
1039+
q.externalAuthLinks = q.externalAuthLinks[:len(q.externalAuthLinks)-1]
1040+
return nil
1041+
}
1042+
return sql.ErrNoRows
1043+
}
1044+
10221045
func (q *FakeQuerier) DeleteGitSSHKey(_ context.Context, userID uuid.UUID) error {
10231046
q.mutex.Lock()
10241047
defer q.mutex.Unlock()

coderd/database/dbmetrics/dbmetrics.go

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

coderd/database/dbmock/dbmock.go

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

coderd/database/querier.go

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

coderd/database/queries.sql.go

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

coderd/database/queries/externalauth.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-- name: GetExternalAuthLink :one
22
SELECT * FROM external_auth_links WHERE provider_id = $1 AND user_id = $2;
33

4+
-- name: DeleteExternalAuthLink :exec
5+
DELETE FROM external_auth_links WHERE provider_id = $1 AND user_id = $2;
6+
47
-- name: GetExternalAuthLinksByUserID :many
58
SELECT * FROM external_auth_links WHERE user_id = $1;
69

0 commit comments

Comments
 (0)