Skip to content

Commit b1f3d1e

Browse files
authored
refactor: dcr integration with ims-go after moving logics (#106)
1 parent 0aec7a4 commit b1f3d1e

6 files changed

Lines changed: 63 additions & 86 deletions

File tree

cmd/dcr.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func registerCmd(imsConfig *ims.Config) *cobra.Command {
3838
RunE: func(cmd *cobra.Command, args []string) error {
3939
cmd.SilenceUsage = true
4040

41-
resp, err := imsConfig.Register()
41+
resp, err := imsConfig.DCRRegister()
4242
if err != nil {
4343
return fmt.Errorf("error during client registration: %w", err)
4444
}
@@ -50,6 +50,7 @@ func registerCmd(imsConfig *ims.Config) *cobra.Command {
5050

5151
cmd.Flags().StringVarP(&imsConfig.ClientName, "clientName", "n", "", "Client application name.")
5252
cmd.Flags().StringSliceVarP(&imsConfig.RedirectURIs, "redirectURIs", "r", []string{}, "Redirect URIs (comma-separated or multiple flags).")
53+
cmd.Flags().StringSliceVarP(&imsConfig.Scopes, "scopes", "s", []string{}, "Requested scopes (comma-separated or multiple flags).")
5354

5455
return cmd
5556
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ go 1.23.0
1515
toolchain go1.26.2
1616

1717
require (
18-
github.com/adobe/ims-go v0.21.0
18+
github.com/adobe/ims-go v0.22.0
1919
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
2020
github.com/spf13/cobra v1.10.2
2121
github.com/spf13/viper v1.21.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/adobe/ims-go v0.21.0 h1:Y0uBT0Fho54ZXhVY0wvT6FUGrM3OEg4J4aJb0fUQdKQ=
2-
github.com/adobe/ims-go v0.21.0/go.mod h1:zGpx0ylsumBjkgd8fYgzJ8+Ci/zFABiBTAxbCscsyR8=
1+
github.com/adobe/ims-go v0.22.0 h1:fN9sUoET8ytkv58aLEDCaUUTyXwsIpRjoSNy87EkbwU=
2+
github.com/adobe/ims-go v0.22.0/go.mod h1:zGpx0ylsumBjkgd8fYgzJ8+Ci/zFABiBTAxbCscsyR8=
33
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

ims/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func TestValidateAuthorizeClientCredentialsConfig(t *testing.T) {
295295
}
296296
}
297297

298-
func TestValidateRegisterConfig(t *testing.T) {
298+
func TestValidateDCRConfig(t *testing.T) {
299299
tests := []struct {
300300
name string
301301
config Config
@@ -308,7 +308,7 @@ func TestValidateRegisterConfig(t *testing.T) {
308308
}
309309
for _, tt := range tests {
310310
t.Run(tt.name, func(t *testing.T) {
311-
err := tt.config.validateRegisterConfig()
311+
err := tt.config.validateDCRConfig()
312312
assertError(t, err, tt.wantErr)
313313
})
314314
}

ims/dcr.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2026 Adobe. All rights reserved.
2+
// This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License. You may obtain a copy
4+
// of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
//
6+
// Unless required by applicable law or agreed to in writing, software distributed under
7+
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8+
// OF ANY KIND, either express or implied. See the License for the specific language
9+
// governing permissions and limitations under the License.
10+
11+
// Dynamic Client Registration (DCR): POST JSON to IMS /ims/register.
12+
13+
package ims
14+
15+
import (
16+
"fmt"
17+
18+
"github.com/adobe/ims-go/ims"
19+
)
20+
21+
func (i Config) validateDCRConfig() error {
22+
switch {
23+
case i.URL == "":
24+
return fmt.Errorf("missing IMS base URL parameter")
25+
case !validateURL(i.URL):
26+
return fmt.Errorf("invalid IMS base URL parameter")
27+
case i.ClientName == "":
28+
return fmt.Errorf("missing client name parameter")
29+
case len(i.RedirectURIs) == 0:
30+
return fmt.Errorf("missing redirect URIs parameter")
31+
default:
32+
return nil
33+
}
34+
}
35+
36+
func (i Config) DCRRegister() (string, error) {
37+
if err := i.validateDCRConfig(); err != nil {
38+
return "", fmt.Errorf("invalid parameters for client registration: %w", err)
39+
}
40+
41+
c, err := i.newIMSClient()
42+
if err != nil {
43+
return "", fmt.Errorf("error creating the IMS client: %w", err)
44+
}
45+
46+
resp, err := c.DCR(&ims.DCRRequest{
47+
ClientName: i.ClientName,
48+
RedirectURIs: i.RedirectURIs,
49+
Scopes: i.Scopes,
50+
})
51+
if err != nil {
52+
return "", fmt.Errorf("error during client registration: %w", err)
53+
}
54+
55+
return string(resp.Body), nil
56+
}

ims/register.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)