| 1 | package teams |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | ) |
| 7 | |
| 8 | // workflowURLValidator matches the Power Automate workflow webhook URL formats |
| 9 | // documented by Microsoft for the "When a Teams webhook request is received" |
| 10 | // trigger. |
| 11 | // |
| 12 | // Two host patterns are accepted: |
| 13 | // - Legacy Logic Apps: https://*.logic.azure.com/workflows/... |
| 14 | // - Current Power Platform: https://*.environment.api.powerplatform.com/powerautomate/automations/direct/.../workflows/... |
| 15 | // |
| 16 | // The Power Platform path may contain optional routing segments between |
| 17 | // "direct/" and "workflows/" (e.g. ".../direct/cu/04/workflows/..."), which |
| 18 | // Microsoft inserts when routing through the Power Platform ingress gateway. |
| 19 | // |
| 20 | // References: |
| 21 | // - https://learn.microsoft.com/en-us/troubleshoot/power-platform/power-automate/flow-run-issues/triggers-troubleshoot?tabs=new-designer#changes-to-http-or-teams-webhook-trigger-flows |
| 22 | // - https://learn.microsoft.com/en-us/power-automate/overview |
| 23 | var workflowURLValidator = regexp.MustCompile( |
| 24 | `^https://[a-zA-Z0-9][a-zA-Z0-9.-]*\.logic\.azure(?:\.[a-z]{2,})?(:\d+)?/(?:powerautomate/automations/direct/(?:[A-Za-z0-9-]+/)*)?workflows/|` + |
| 25 | `^https://[a-zA-Z0-9][a-zA-Z0-9.-]*\.environment\.api\.powerplatform\.com(:\d+)?/powerautomate/automations/direct/(?:[A-Za-z0-9-]+/)*workflows/`, |
| 26 | ) |
| 27 | |
| 28 | // ValidateWebhookURL ensures the webhook URL matches the Power Automate workflow pattern. |
| 29 | func ValidateWebhookURL(url string) error { |
| 30 | if !workflowURLValidator.MatchString(url) { |
| 31 | return fmt.Errorf("%w", ErrInvalidWebhookURL) |
| 32 | } |
| 33 | |
| 34 | return nil |
| 35 | } |