|
| 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 | +} |
0 commit comments