Skip to content

Commit

Permalink
fix: get auth providers in client sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza committed Feb 18, 2025
1 parent 1576b73 commit 7b24f97
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 15 deletions.
6 changes: 3 additions & 3 deletions config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ func (c *AuthConfig) AddOidcProvider(name string, issuerUrl string, clientId str
if strings.HasPrefix(err.Message, fmt.Sprintf("auth.providers.%d.name", newProviderIndex)) {
// This function allows the adding of internal auth providers which can start with 'keel_'
if !strings.Contains(err.Message, "Cannot start with 'keel_'") {
return fmt.Errorf(err.Message)
return err
}
}
if strings.HasPrefix(err.Message, fmt.Sprintf("auth.providers.%d.issuerUrl", newProviderIndex)) {
return fmt.Errorf(err.Message)
return err
}
if strings.HasPrefix(err.Message, fmt.Sprintf("auth.providers.%d.clientId", newProviderIndex)) {
return fmt.Errorf(err.Message)
return err
}
}

Expand Down
18 changes: 18 additions & 0 deletions integration/testdata/client_auth/keelconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
auth:
providers:
- type: oidc
name: myOidcProvider
issuerUrl: "https://auth.example.com/"
clientId: foo_1
- type: google
name: googleProvider
clientId: foo_3
- type: slack
name: slackProvider
clientId: foo_4
- type: facebook
name: facebookProvider
clientId: foo_5
- type: gitlab
name: Gitlab_Provider
clientId: foo_6
42 changes: 42 additions & 0 deletions integration/testdata/client_auth/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,45 @@ test("authentication - not authenticated and no permissions", async () => {
expect(response.data?.results).toHaveLength(1);
expect(response.error).toBeUndefined();
});

test("authentication - get providers", async () => {
const provs = await client.auth.providers();
console.log(provs);

expect(provs.data?.[0]).toEqual({
name: "myOidcProvider",
type: "oidc",
authorizeUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/authorize/myoidcprovider",
callbackUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/callback/myoidcprovider",
});

expect(provs.data?.[1]).toEqual({
name: "googleProvider",
type: "google",
authorizeUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/authorize/googleprovider",
callbackUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/callback/googleprovider",
});

expect(provs.data?.[2]).toEqual({
name: "slackProvider",
type: "slack",
authorizeUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/authorize/slackprovider",
callbackUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/callback/slackprovider",
});

expect(provs.data?.[3]).toEqual({
name: "facebookProvider",
type: "facebook",
authorizeUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/authorize/facebookprovider",
callbackUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/callback/facebookprovider",
});

expect(provs.data?.[4]).toEqual({
name: "Gitlab_Provider",
type: "gitlab",
authorizeUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/authorize/gitlab_provider",
callbackUrl: process.env.KEEL_TESTING_AUTH_API_URL + "/callback/gitlab_provider",
});

expect(provs.data?.length).toEqual(5);
});
2 changes: 1 addition & 1 deletion node/templates/client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class Core {
});

if (result.ok) {
return await result.json();
return { data: await result.json() };
} else {
return {
error: {
Expand Down
10 changes: 2 additions & 8 deletions runtime/apis/authapi/authorize_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,13 @@ func CallbackHandler(schema *proto.Schema) common.HandlerFunc {
return common.InternalServerErrorResponse(ctx, err)
}

// Establishes new OIDC provider. This will call the providers discovery endpoint
oidcProvider, err := oidc.NewProvider(ctx, provider.IssuerUrl)
if err != nil {
return common.InternalServerErrorResponse(ctx, err)
}

// ClientSecret is required for token exchange
oauthConfig := &oauth2.Config{
ClientID: provider.ClientId,
ClientSecret: secret,
Endpoint: oauth2.Endpoint{
AuthURL: oidcProvider.Endpoint().AuthURL,
TokenURL: oidcProvider.Endpoint().TokenURL,
AuthURL: oidcProv.Endpoint().AuthURL,
TokenURL: oidcProv.Endpoint().TokenURL,
},
RedirectURL: callbackUrl.String(),
}
Expand Down
3 changes: 0 additions & 3 deletions runtime/apis/authapi/providers_endpoint.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package authapi

import (
"fmt"
"net/http"
"strings"

Expand All @@ -25,8 +24,6 @@ func ProvidersHandler(schema *proto.Schema) common.HandlerFunc {

config, err := runtimectx.GetOAuthConfig(ctx)
if err != nil {
fmt.Println(err)

return common.InternalServerErrorResponse(ctx, err)
}

Expand Down
2 changes: 2 additions & 0 deletions testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ func Run(ctx context.Context, opts *RunnerOpts) error {
os.Setenv(key, value)
}

os.Setenv("KEEL_API_URL", fmt.Sprintf("http://localhost:%s", runtimePort))

// Server to handle receiving HTTP requests from the ActionExecutor, JobExecutor and SubscriberExecutor.
runtimeServer := http.Server{
Addr: fmt.Sprintf(":%s", runtimePort),
Expand Down

0 comments on commit 7b24f97

Please sign in to comment.