Skip to content

Commit 81a3b36

Browse files
authored
feat: add endpoints to list all authed external apps (#10944)
* feat: add endpoints to list all authed external apps Listing the apps allows users to auth to external apps without going through the create workspace flow.
1 parent feaa989 commit 81a3b36

File tree

17 files changed

+565
-10
lines changed

17 files changed

+565
-10
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.listUserExternalAuths)
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
@@ -754,6 +754,13 @@ func (q *querier) DeleteCoordinator(ctx context.Context, id uuid.UUID) error {
754754
return q.db.DeleteCoordinator(ctx, id)
755755
}
756756

757+
func (q *querier) DeleteExternalAuthLink(ctx context.Context, arg database.DeleteExternalAuthLinkParams) error {
758+
return deleteQ(q.log, q.auth, func(ctx context.Context, arg database.DeleteExternalAuthLinkParams) (database.ExternalAuthLink, error) {
759+
//nolint:gosimple
760+
return q.db.GetExternalAuthLink(ctx, database.GetExternalAuthLinkParams{UserID: arg.UserID, ProviderID: arg.ProviderID})
761+
}, q.db.DeleteExternalAuthLink)(ctx, arg)
762+
}
763+
757764
func (q *querier) DeleteGitSSHKey(ctx context.Context, userID uuid.UUID) error {
758765
return deleteQ(q.log, q.auth, q.db.GetGitSSHKey, q.db.DeleteGitSSHKey)(ctx, userID)
759766
}
@@ -996,10 +1003,7 @@ func (q *querier) GetExternalAuthLink(ctx context.Context, arg database.GetExter
9961003
}
9971004

9981005
func (q *querier) GetExternalAuthLinksByUserID(ctx context.Context, userID uuid.UUID) ([]database.ExternalAuthLink, error) {
999-
if err := q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceSystem); err != nil {
1000-
return nil, err
1001-
}
1002-
return q.db.GetExternalAuthLinksByUserID(ctx, userID)
1006+
return fetchWithPostFilter(q.auth, q.db.GetExternalAuthLinksByUserID)(ctx, userID)
10031007
}
10041008

10051009
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
@@ -1027,6 +1027,29 @@ func (*FakeQuerier) DeleteCoordinator(context.Context, uuid.UUID) error {
10271027
return ErrUnimplemented
10281028
}
10291029

1030+
func (q *FakeQuerier) DeleteExternalAuthLink(_ context.Context, arg database.DeleteExternalAuthLinkParams) error {
1031+
err := validateDatabaseType(arg)
1032+
if err != nil {
1033+
return err
1034+
}
1035+
1036+
q.mutex.Lock()
1037+
defer q.mutex.Unlock()
1038+
1039+
for index, key := range q.externalAuthLinks {
1040+
if key.UserID != arg.UserID {
1041+
continue
1042+
}
1043+
if key.ProviderID != arg.ProviderID {
1044+
continue
1045+
}
1046+
q.externalAuthLinks[index] = q.externalAuthLinks[len(q.externalAuthLinks)-1]
1047+
q.externalAuthLinks = q.externalAuthLinks[:len(q.externalAuthLinks)-1]
1048+
return nil
1049+
}
1050+
return sql.ErrNoRows
1051+
}
1052+
10301053
func (q *FakeQuerier) DeleteGitSSHKey(_ context.Context, userID uuid.UUID) error {
10311054
q.mutex.Lock()
10321055
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)