Skip to content

Commit fca870f

Browse files
authored
Outbound app crud (#578)
* Outbound app crud +tests fixes descope/etc#11062 * CR fixes
1 parent 7492f6f commit fca870f

File tree

8 files changed

+486
-3
lines changed

8 files changed

+486
-3
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,36 @@ err = descopeClient.DescopeClient().Management.ThirdPartyApplication().DeleteCon
17031703

17041704
```
17051705

1706+
### Manage Outbound Applications
1707+
1708+
You can create, update, delete, or load outbound applications:
1709+
1710+
```go
1711+
// Create an outbound application
1712+
app, err := descopeClient.Management.OutboundApplication().CreateApplication(context.Background(), &descope.OutboundApp{
1713+
Name: "My Outbound App",
1714+
Description: "Description",
1715+
// ... other fields ...
1716+
})
1717+
1718+
// Update an outbound application
1719+
// Leave secret as nil, to not update it
1720+
app, err = descopeClient.Management.OutboundApplication().UpdateApplication(context.Background(), &descope.OutboundApp{
1721+
ID: "app-id",
1722+
Name: "Updated Name",
1723+
// ... other fields ...
1724+
}, &secret)
1725+
1726+
// Delete an outbound application
1727+
err := descopeClient.Management.OutboundApplication().DeleteApplication(context.Background(), "app-id")
1728+
1729+
// Load an outbound application by id
1730+
app, err := descopeClient.Management.OutboundApplication().LoadApplication(context.Background(), "app-id")
1731+
1732+
// Load all outbound applications
1733+
apps, err := descopeClient.Management.OutboundApplication().LoadAllApplications(context.Background())
1734+
```
1735+
17061736
## Code Examples
17071737

17081738
You can find various usage examples in the [examples folder](https://github.com/descope/go-sdk/blob/main/examples).

descope/api/client.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,18 @@ var (
220220
fgaSearchMappableResources: "mgmt/fga/mappable/resources",
221221
fgaResourcesLoad: "mgmt/fga/resources/load",
222222
fgaResourcesSave: "mgmt/fga/resources/save",
223+
outboundApplicationCreate: "mgmt/outbound/app/create",
224+
outboundApplicationUpdate: "mgmt/outbound/app/update",
225+
outboundApplicationDelete: "mgmt/outbound/app/delete",
226+
outboundApplicationLoad: "mgmt/outbound/app",
227+
outboundApplicationLoadAll: "mgmt/outbound/apps",
223228
thirdPartyApplicationCreate: "mgmt/thirdparty/app/create",
224229
thirdPartyApplicationUpdate: "mgmt/thirdparty/app/update",
225230
thirdPartyApplicationPatch: "mgmt/thirdparty/app/patch",
226231
thirdPartyApplicationDelete: "mgmt/thirdparty/app/delete",
227232
thirdPartyApplicationLoad: "mgmt/thirdparty/app/load",
228233
thirdPartyApplicationLoadAll: "mgmt/thirdparty/apps/load",
229-
thidPartyApplicationSecret: "mgmt/thirdparty/app/secret",
234+
thirdPartyApplicationSecret: "mgmt/thirdparty/app/secret",
230235
thirdPartyApplicationRotate: "mgmt/thirdparty/app/rotate",
231236
thirdPartyApplicationConsentDelete: "mgmt/thirdparty/consents/delete",
232237
thirdPartyApplicationTenantConsentDelete: "mgmt/thirdparty/consents/delete/tenant",
@@ -466,13 +471,19 @@ type mgmtEndpoints struct {
466471
fgaResourcesLoad string
467472
fgaResourcesSave string
468473

474+
outboundApplicationCreate string
475+
outboundApplicationUpdate string
476+
outboundApplicationDelete string
477+
outboundApplicationLoad string
478+
outboundApplicationLoadAll string
479+
469480
thirdPartyApplicationCreate string
470481
thirdPartyApplicationUpdate string
471482
thirdPartyApplicationPatch string
472483
thirdPartyApplicationDelete string
473484
thirdPartyApplicationLoad string
474485
thirdPartyApplicationLoadAll string
475-
thidPartyApplicationSecret string
486+
thirdPartyApplicationSecret string
476487
thirdPartyApplicationRotate string
477488
thirdPartyApplicationConsentDelete string
478489
thirdPartyApplicationTenantConsentDelete string
@@ -1243,6 +1254,26 @@ func (e *endpoints) ManagementFGAResourcesSave() string {
12431254
return path.Join(e.version, e.mgmt.fgaResourcesSave)
12441255
}
12451256

1257+
func (e *endpoints) ManagementOutboundApplicationCreate() string {
1258+
return path.Join(e.version, e.mgmt.outboundApplicationCreate)
1259+
}
1260+
1261+
func (e *endpoints) ManagementOutboundApplicationUpdate() string {
1262+
return path.Join(e.version, e.mgmt.outboundApplicationUpdate)
1263+
}
1264+
1265+
func (e *endpoints) ManagementOutboundApplicationDelete() string {
1266+
return path.Join(e.version, e.mgmt.outboundApplicationDelete)
1267+
}
1268+
1269+
func (e *endpoints) ManagementOutboundApplicationLoad() string {
1270+
return path.Join(e.version, e.mgmt.outboundApplicationLoad)
1271+
}
1272+
1273+
func (e *endpoints) ManagementOutboundApplicationLoadAll() string {
1274+
return path.Join(e.version, e.mgmt.outboundApplicationLoadAll)
1275+
}
1276+
12461277
func (e *endpoints) ManagementThirdPartyApplicationCreate() string {
12471278
return path.Join(e.version, e.mgmt.thirdPartyApplicationCreate)
12481279
}
@@ -1268,7 +1299,7 @@ func (e *endpoints) ManagementThirdPartyApplicationPatch() string {
12681299
}
12691300

12701301
func (e *endpoints) ManagementThirdPartyApplicationSecret() string {
1271-
return path.Join(e.version, e.mgmt.thidPartyApplicationSecret)
1302+
return path.Join(e.version, e.mgmt.thirdPartyApplicationSecret)
12721303
}
12731304

12741305
func (e *endpoints) ManagementThirdPartyApplicationRotate() string {

descope/internal/mgmt/mgmt.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type managementService struct {
3838
authz sdk.Authz
3939
fga sdk.FGA
4040
thirdPartyApplication sdk.ThirdPartyApplication
41+
outboundApplication sdk.OutboundApplication
4142
}
4243

4344
func NewManagement(conf ManagementParams, provider *auth.Provider, c *api.Client) *managementService {
@@ -46,6 +47,7 @@ func NewManagement(conf ManagementParams, provider *auth.Provider, c *api.Client
4647
service.tenant = &tenant{managementBase: base}
4748
service.ssoApplication = &ssoApplication{managementBase: base}
4849
service.thirdPartyApplication = &thirdPartyApplication{managementBase: base}
50+
service.outboundApplication = &outboundApplication{managementBase: base}
4951
service.user = &user{managementBase: base}
5052
service.accessKey = &accessKey{managementBase: base}
5153
service.sso = &sso{managementBase: base}
@@ -142,6 +144,11 @@ func (mgmt *managementService) ThirdPartyApplication() sdk.ThirdPartyApplication
142144
return mgmt.thirdPartyApplication
143145
}
144146

147+
func (mgmt *managementService) OutboundApplication() sdk.OutboundApplication {
148+
mgmt.ensureManagementKey()
149+
return mgmt.outboundApplication
150+
}
151+
145152
func (mgmt *managementService) ensureManagementKey() {
146153
if mgmt.conf.ManagementKey == "" {
147154
logger.LogInfo("Management key is missing, make sure to add it in the Config struct or the environment variable \"%s\"", descope.EnvironmentVariableManagementKey) // notest
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package mgmt
2+
3+
import (
4+
"context"
5+
6+
"github.com/descope/go-sdk/descope"
7+
"github.com/descope/go-sdk/descope/api"
8+
"github.com/descope/go-sdk/descope/internal/utils"
9+
"github.com/descope/go-sdk/descope/sdk"
10+
)
11+
12+
type outboundApplication struct {
13+
managementBase
14+
}
15+
16+
var _ sdk.OutboundApplication = &outboundApplication{}
17+
18+
func (s *outboundApplication) CreateApplication(ctx context.Context, appRequest *descope.CreateOutboundAppRequest) (app *descope.OutboundApp, err error) {
19+
if appRequest == nil {
20+
return nil, utils.NewInvalidArgumentError("appRequest")
21+
}
22+
if appRequest.Name == "" {
23+
return nil, utils.NewInvalidArgumentError("appRequest.Name")
24+
}
25+
26+
req := makeCreateUpdateOutboundApplicationRequest(&appRequest.OutboundApp)
27+
req["clientSecret"] = appRequest.ClientSecret
28+
httpRes, err := s.client.DoPostRequest(ctx, api.Routes.ManagementOutboundApplicationCreate(), req, nil, s.conf.ManagementKey)
29+
if err != nil {
30+
return nil, err
31+
}
32+
return s.unmarshalAppResponse(httpRes)
33+
}
34+
35+
func (s *outboundApplication) UpdateApplication(ctx context.Context, appRequest *descope.OutboundApp, clientSecret *string) (app *descope.OutboundApp, err error) {
36+
if appRequest == nil {
37+
return nil, utils.NewInvalidArgumentError("appRequest")
38+
}
39+
if appRequest.ID == "" {
40+
return nil, utils.NewInvalidArgumentError("appRequest.id")
41+
}
42+
if appRequest.Name == "" {
43+
return nil, utils.NewInvalidArgumentError("appRequest.Name")
44+
}
45+
46+
req := makeCreateUpdateOutboundApplicationRequest(appRequest)
47+
if clientSecret != nil {
48+
req["clientSecret"] = *clientSecret
49+
}
50+
httpRes, err := s.client.DoPostRequest(ctx, api.Routes.ManagementOutboundApplicationUpdate(), map[string]any{
51+
"app": req,
52+
}, nil, s.conf.ManagementKey)
53+
if err != nil {
54+
return nil, err
55+
}
56+
return s.unmarshalAppResponse(httpRes)
57+
}
58+
59+
func (s *outboundApplication) DeleteApplication(ctx context.Context, id string) error {
60+
if id == "" {
61+
return utils.NewInvalidArgumentError("id")
62+
}
63+
req := map[string]any{"id": id}
64+
_, err := s.client.DoPostRequest(ctx, api.Routes.ManagementOutboundApplicationDelete(), req, nil, s.conf.ManagementKey)
65+
return err
66+
}
67+
68+
func (s *outboundApplication) LoadApplication(ctx context.Context, id string) (*descope.OutboundApp, error) {
69+
if id == "" {
70+
return nil, utils.NewInvalidArgumentError("id")
71+
}
72+
res, err := s.client.DoGetRequest(ctx, api.Routes.ManagementOutboundApplicationLoad()+"/"+id, nil, s.conf.ManagementKey)
73+
if err != nil {
74+
return nil, err
75+
}
76+
return s.unmarshalAppResponse(res)
77+
}
78+
79+
func (s *outboundApplication) LoadAllApplications(ctx context.Context) ([]*descope.OutboundApp, error) {
80+
res, err := s.client.DoGetRequest(ctx, api.Routes.ManagementOutboundApplicationLoadAll(), nil, s.conf.ManagementKey)
81+
if err != nil {
82+
return nil, err
83+
}
84+
tmp := &struct {
85+
Apps []*descope.OutboundApp `json:"apps"`
86+
}{}
87+
if err = utils.Unmarshal([]byte(res.BodyStr), tmp); err != nil {
88+
return nil, err
89+
}
90+
return tmp.Apps, nil
91+
}
92+
93+
func makeCreateUpdateOutboundApplicationRequest(app *descope.OutboundApp) map[string]any {
94+
return map[string]any{
95+
"id": app.ID,
96+
"name": app.Name,
97+
"description": app.Description,
98+
"templateId": app.TemplateID,
99+
"clientId": app.ClientID,
100+
"logo": app.Logo,
101+
"discoveryUrl": app.DiscoveryURL,
102+
"authorizationUrl": app.AuthorizationURL,
103+
"authorizationUrlParams": app.AuthorizationURLParams,
104+
"tokenUrl": app.TokenURL,
105+
"tokenUrlParams": app.TokenURLParams,
106+
"revocationUrl": app.RevocationURL,
107+
"defaultScopes": app.DefaultScopes,
108+
"defaultRedirectUrl": app.DefaultRedirectURL,
109+
"callbackDomain": app.CallbackDomain,
110+
"pkce": app.Pkce,
111+
"accessType": app.AccessType,
112+
"prompt": app.Prompt,
113+
}
114+
}
115+
116+
func (s *outboundApplication) unmarshalAppResponse(httpRes *api.HTTPResponse) (*descope.OutboundApp, error) {
117+
var err error
118+
res := &struct {
119+
App *descope.OutboundApp `json:"app"`
120+
}{}
121+
if err = utils.Unmarshal([]byte(httpRes.BodyStr), res); err != nil {
122+
return nil, err
123+
}
124+
return res.App, nil
125+
}

0 commit comments

Comments
 (0)