Skip to content

Commit 22c51b2

Browse files
committed
feat(oauth): wire up api
1 parent 25ce13c commit 22c51b2

File tree

6 files changed

+137
-11
lines changed

6 files changed

+137
-11
lines changed

src/extension/host-binary/cmd/main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"os"
8-
"os/signal"
9-
"syscall"
10-
secretsapi "github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/secrets"
117
"github.com/docker/labs-ai-tools-for-devs/pkg/client"
8+
secretsapi "github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/secrets"
129
"github.com/docker/labs-ai-tools-for-devs/pkg/paths"
1310
"github.com/spf13/cobra"
11+
"os"
12+
"os/signal"
13+
"syscall"
1414
)
1515

1616
func main() {
@@ -45,6 +45,14 @@ func newApiClient() (client.ApiClient, error) {
4545
return client.NewApiClient(p), nil
4646
}
4747

48+
func newOAuthApiClient() (client.OAuthApiClient, error) {
49+
p, err := paths.GetToolsApiSocketPath()
50+
if err != nil {
51+
return nil, err
52+
}
53+
return client.NewOAuthApiClient(p), nil
54+
}
55+
4856
type addOptions struct {
4957
Name string
5058
Value string
@@ -87,7 +95,7 @@ func ListSecrets(ctx context.Context) *cobra.Command {
8795
func DeleteSecret(ctx context.Context) *cobra.Command {
8896
opts := &deleteOptions{}
8997
cmd := &cobra.Command{
90-
Use: "delete",
98+
Use: "delete",
9199
Short: "Delete a secret",
92100
Args: cobra.NoArgs,
93101
RunE: func(*cobra.Command, []string) error {

src/extension/host-binary/pkg/client/client.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ func NewApiClient(socketPath string) ApiClient {
3636
}
3737
}
3838

39-
type Secret struct {
40-
Name string
41-
Value string
42-
Policies []string
43-
}
44-
4539
func (d apiClientImpl) SetSecret(ctx context.Context, s secretsapi.Secret) error {
4640
apiReq := d.SecretsApi.SetJfsSecret(ctx)
4741
req := secretsapi.NewSecret(s.Name, s.Value, s.Policies)

src/extension/host-binary/pkg/client/conn.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"context"
55
"github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/secrets"
6+
oauthapi "github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/tools"
67
"net"
78
"net/http"
89
)
@@ -25,3 +26,17 @@ func GetConfiguration(addr string) *secretsapi.Configuration {
2526
}
2627
return cfg
2728
}
29+
30+
func GetOAuthConfiguration(addr string) *oauthapi.Configuration {
31+
cfg := oauthapi.NewConfiguration()
32+
cfg.Scheme = HTTPProtocol
33+
cfg.Host = DummyHost
34+
cfg.HTTPClient = &http.Client{
35+
Transport: &http.Transport{
36+
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
37+
return DialSocket(addr)
38+
},
39+
},
40+
}
41+
return cfg
42+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package client
2+
3+
import (
4+
"context"
5+
oauthapi "github.com/docker/labs-ai-tools-for-devs/pkg/generated/go/client/tools"
6+
)
7+
8+
type OAuthApiClient interface {
9+
// DeleteOAuthApp Unauthorizes an app.
10+
DeleteOAuthApp(ctx context.Context, app string) error
11+
// DeleteOAuthAppTool Remove a tool from an app.
12+
DeleteOAuthAppTool(ctx context.Context, app, tool string) error
13+
// GetOAuthApp Returns an app object.
14+
GetOAuthApp(ctx context.Context, app string) (oauthapi.OAuthApp, error)
15+
// ListOAuthApps Lists all app objects.
16+
ListOAuthApps(ctx context.Context) ([]oauthapi.OAuthApp, error)
17+
// PostOAuthApp Authorize an app.
18+
PostOAuthApp(ctx context.Context, app, scopes string) (oauthapi.AuthResponse, error)
19+
// PostOAuthAppTool Add a tool to an app.
20+
PostOAuthAppTool(ctx context.Context, app, tool string) error
21+
}
22+
23+
var _ OAuthApiClient = &oauthApiClientImpl{}
24+
25+
type oauthApiClientImpl struct {
26+
*oauthapi.APIClient
27+
}
28+
29+
func NewOAuthApiClient(socketPath string) OAuthApiClient {
30+
return &oauthApiClientImpl{
31+
APIClient: oauthapi.NewAPIClient(GetOAuthConfiguration(socketPath)),
32+
}
33+
}
34+
35+
func (o oauthApiClientImpl) DeleteOAuthApp(ctx context.Context, app string) error {
36+
apiReq := o.ToolsApi.DeleteOAuthApp(ctx, app)
37+
_, err := apiReq.Execute()
38+
return err
39+
}
40+
41+
func (o oauthApiClientImpl) DeleteOAuthAppTool(ctx context.Context, app, tool string) error {
42+
apiReq := o.ToolsApi.DeleteOAuthAppTool(ctx, app, tool)
43+
_, err := apiReq.Execute()
44+
return err
45+
}
46+
47+
func (o oauthApiClientImpl) GetOAuthApp(ctx context.Context, app string) (oauthapi.OAuthApp, error) {
48+
apiReq := o.ToolsApi.GetOAuthApp(ctx, app)
49+
res, _, err := apiReq.Execute()
50+
if err != nil {
51+
return oauthapi.OAuthApp{}, err
52+
}
53+
return *res, nil
54+
}
55+
56+
func (o oauthApiClientImpl) ListOAuthApps(ctx context.Context) ([]oauthapi.OAuthApp, error) {
57+
apiReq := o.ToolsApi.ListOAuthApps(ctx)
58+
res, _, err := apiReq.Execute()
59+
if err != nil {
60+
return nil, err
61+
}
62+
return res, nil
63+
}
64+
65+
func (o oauthApiClientImpl) PostOAuthApp(ctx context.Context, app, scopes string) (oauthapi.AuthResponse, error) {
66+
apiReq := o.ToolsApi.PostOAuthApp(ctx, app)
67+
res, _, err := apiReq.Scopes(scopes).Execute()
68+
if err != nil {
69+
return oauthapi.AuthResponse{}, err
70+
}
71+
return *res, nil
72+
}
73+
74+
func (o oauthApiClientImpl) PostOAuthAppTool(ctx context.Context, app, tool string) error {
75+
apiReq := o.ToolsApi.PostOAuthAppTool(ctx, app, tool)
76+
_, err := apiReq.Execute()
77+
return err
78+
}
79+
80+
type OAuthApp struct {
81+
App string `json:"app"`
82+
Authorized bool `json:"authorized"`
83+
Provider string `json:"provider"`
84+
Scopes []OAuthScopes `json:"scopes,omitempty"`
85+
Tools []string `json:"tools"`
86+
}
87+
88+
type OAuthScopes struct {
89+
Description string `json:"description,omitempty"`
90+
Metadata []string `json:"metadata,omitempty"`
91+
Name string `json:"name,omitempty"`
92+
}
93+
94+
type AuthResponse struct {
95+
AuthType string `json:"authType,omitempty"`
96+
BrowserUrl string `json:"browserUrl,omitempty"`
97+
}

src/extension/host-binary/pkg/paths/paths_unix.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ func GetSecretsApiSocketPath() (string, error) {
1111
}
1212
return filepath.Join(dir, "jfs.sock"), nil
1313
}
14+
15+
func GetToolsApiSocketPath() (string, error) {
16+
dir, err := dockerDesktopSocketDir()
17+
if err != nil {
18+
return "", err
19+
}
20+
return filepath.Join(dir, "tools.sock"), nil
21+
}

src/extension/host-binary/pkg/paths/paths_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ package paths
55
func GetSecretsApiSocketPath() (string, error) {
66
return `//./pipe/dockerJfs`, nil
77
}
8+
9+
func GetSecretsApiSocketPath() (string, error) {
10+
return `//./pipe/dockerTools`, nil
11+
}

0 commit comments

Comments
 (0)