forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APP-4496: Add cli command to create end user auth applications (viamr…
- Loading branch information
Showing
10 changed files
with
334 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/urfave/cli/v2" | ||
apppb "go.viam.com/api/app/v1" | ||
) | ||
|
||
// RegisterAuthApplicationAction is the corresponding action for 'third-party-auth-app register'. | ||
func RegisterAuthApplicationAction(c *cli.Context) error { | ||
client, err := newViamClient(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return client.registerAuthApplicationAction(c) | ||
} | ||
|
||
func (c *viamClient) registerAuthApplicationAction(cCtx *cli.Context) error { | ||
if err := c.ensureLoggedIn(); err != nil { | ||
return err | ||
} | ||
|
||
orgID := cCtx.String(generalFlagOrgID) | ||
applicationName := cCtx.String(authApplicationFlagName) | ||
originURIs := cCtx.StringSlice(authApplicationFlagOriginURIs) | ||
redirectURIs := cCtx.StringSlice(authApplicationFlagRedirectURIs) | ||
logoutURI := cCtx.String(authApplicationFlagLogoutURI) | ||
|
||
req := &apppb.RegisterAuthApplicationRequest{ | ||
OrgId: orgID, | ||
ApplicationName: applicationName, | ||
OriginUris: originURIs, | ||
RedirectUris: redirectURIs, | ||
LogoutUri: logoutURI, | ||
} | ||
resp, err := c.endUserClient.RegisterAuthApplication(c.c.Context, req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
infof(cCtx.App.Writer, "Successfully registered auth application") | ||
formatOutput, err := json.MarshalIndent(resp, "", "\t") | ||
if err != nil { | ||
return err | ||
} | ||
printf(cCtx.App.Writer, "%s", formatOutput) | ||
warningf(cCtx.App.Writer, "Keep this information somewhere safe; "+ | ||
"it contains the secret to your auth application") | ||
return nil | ||
} | ||
|
||
// UpdateAuthApplicationAction is the corresponding action for 'third-party-auth-app update'. | ||
func UpdateAuthApplicationAction(c *cli.Context) error { | ||
client, err := newViamClient(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return client.updateAuthApplicationAction(c) | ||
} | ||
|
||
func (c *viamClient) updateAuthApplicationAction(cCtx *cli.Context) error { | ||
if err := c.ensureLoggedIn(); err != nil { | ||
return err | ||
} | ||
|
||
orgID := cCtx.String(generalFlagOrgID) | ||
applicationID := cCtx.String(authApplicationFlagApplicationID) | ||
applicationName := cCtx.String(authApplicationFlagName) | ||
originURIs := cCtx.StringSlice(authApplicationFlagOriginURIs) | ||
redirectURIs := cCtx.StringSlice(authApplicationFlagRedirectURIs) | ||
logoutURI := cCtx.String(authApplicationFlagLogoutURI) | ||
|
||
req := &apppb.UpdateAuthApplicationRequest{ | ||
OrgId: orgID, | ||
ApplicationId: applicationID, | ||
ApplicationName: applicationName, | ||
OriginUris: originURIs, | ||
RedirectUris: redirectURIs, | ||
LogoutUri: logoutURI, | ||
} | ||
resp, err := c.endUserClient.UpdateAuthApplication(c.c.Context, req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
infof(cCtx.App.Writer, "Successfully updated auth application") | ||
formatOutput, err := json.MarshalIndent(resp, "", "\t") | ||
if err != nil { | ||
return err | ||
} | ||
printf(cCtx.App.Writer, "%s", formatOutput) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
apppb "go.viam.com/api/app/v1" | ||
"go.viam.com/test" | ||
"google.golang.org/grpc" | ||
|
||
"go.viam.com/rdk/testutils/inject" | ||
) | ||
|
||
func TestRegisterAuthApplicationAction(t *testing.T) { | ||
registerAuthApplicationFunc := func(ctx context.Context, in *apppb.RegisterAuthApplicationRequest, | ||
opts ...grpc.CallOption, | ||
) (*apppb.RegisterAuthApplicationResponse, error) { | ||
return &apppb.RegisterAuthApplicationResponse{ | ||
ApplicationId: "c6215428-1b73-41c3-b44a-56db0631c8f1", | ||
ApplicationName: in.ApplicationName, | ||
Secret: "reallysecretsecret", | ||
}, nil | ||
} | ||
|
||
eusc := &inject.EndUserServiceClient{ | ||
RegisterAuthApplicationFunc: registerAuthApplicationFunc, | ||
} | ||
flags := make(map[string]any) | ||
flags[generalFlagOrgID] = "a757fe30-5648-4c5b-ab74-4ecd6bf06e4c" | ||
flags[authApplicationFlagName] = "pupper_app" | ||
flags[authApplicationFlagOriginURIs] = []string{"https://woof.com/login", "https://arf.com/"} | ||
flags[authApplicationFlagRedirectURIs] = []string{"https://woof.com/home", "https://arf.com/home"} | ||
flags[authApplicationFlagLogoutURI] = "https://woof.com/logout" | ||
|
||
cCtx, ac, out, errOut := setup(&inject.AppServiceClient{}, nil, nil, eusc, flags, "token") | ||
err := ac.registerAuthApplicationAction(cCtx) | ||
test.That(t, err, test.ShouldBeNil) | ||
test.That(t, len(errOut.messages), test.ShouldEqual, 0) | ||
test.That(t, len(out.messages), test.ShouldEqual, 5) | ||
} | ||
|
||
func TestUpdateAuthApplicationAction(t *testing.T) { | ||
updateAuthApplication := func(ctx context.Context, in *apppb.UpdateAuthApplicationRequest, | ||
opts ...grpc.CallOption, | ||
) (*apppb.UpdateAuthApplicationResponse, error) { | ||
return &apppb.UpdateAuthApplicationResponse{ | ||
ApplicationId: "c6215428-1b73-41c3-b44a-56db0631c8f1", | ||
ApplicationName: in.ApplicationName, | ||
}, nil | ||
} | ||
|
||
eusc := &inject.EndUserServiceClient{ | ||
UpdateAuthApplicationFunc: updateAuthApplication, | ||
} | ||
flags := make(map[string]any) | ||
flags[generalFlagOrgID] = "a757fe30-5648-4c5b-ab74-4ecd6bf06e4c" | ||
flags[authApplicationFlagApplicationID] = "a673022c-9916-4238-b8eb-4f7a89885909" | ||
flags[authApplicationFlagName] = "pupper_app" | ||
flags[authApplicationFlagOriginURIs] = []string{"https://woof.com/login", "https://arf.com/"} | ||
flags[authApplicationFlagRedirectURIs] = []string{"https://woof.com/home", "https://arf.com/home"} | ||
flags[authApplicationFlagLogoutURI] = "https://woof.com/logout" | ||
|
||
cCtx, ac, out, errOut := setup(&inject.AppServiceClient{}, nil, nil, eusc, flags, "token") | ||
err := ac.updateAuthApplicationAction(cCtx) | ||
test.That(t, err, test.ShouldBeNil) | ||
test.That(t, len(errOut.messages), test.ShouldEqual, 0) | ||
test.That(t, len(out.messages), test.ShouldEqual, 3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.