| 1 | // Copyright 2026 The Go MCP SDK Authors. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package oauthex |
| 6 | |
| 7 | import "errors" |
| 8 | |
| 9 | // ClientCredentials holds client authentication credentials for OAuth token requests. |
| 10 | // It supports multiple authentication methods, but only one method should be set at a time. |
| 11 | // Use the Validate method to ensure proper configuration. |
| 12 | type ClientCredentials struct { |
| 13 | // ClientID is the OAuth2 client identifier. |
| 14 | // REQUIRED for all authentication methods. |
| 15 | ClientID string |
| 16 | |
| 17 | // ClientSecretAuth configures client authentication using a client secret. |
| 18 | // This is the most common authentication method for confidential clients. |
| 19 | // OPTIONAL. If not provided, the client is treated as a public client. |
| 20 | ClientSecretAuth *ClientSecretAuth |
| 21 | } |
| 22 | |
| 23 | // ClientSecretAuth holds client secret authentication credentials. |
| 24 | // This authentication method supports both "client_secret_basic" and "client_secret_post" |
| 25 | // methods as defined in RFC 6749 Section 2.3.1. |
| 26 | type ClientSecretAuth struct { |
| 27 | // ClientSecret is the OAuth2 client secret for confidential clients. |
| 28 | // REQUIRED when using ClientSecretAuth. |
| 29 | ClientSecret string |
| 30 | } |
| 31 | |
| 32 | // Validate checks that the ClientCredentials are properly configured. |
| 33 | // It ensures that: |
| 34 | // - ClientID is not empty. |
| 35 | // - At most one authentication method is configured. |
| 36 | // - If ClientSecretAuth is set, ClientSecret is not empty. |
| 37 | func (c *ClientCredentials) Validate() error { |
| 38 | if c.ClientID == "" { |
| 39 | return errors.New("ClientID is required") |
| 40 | } |
| 41 | |
| 42 | // Count how many auth methods are configured. |
| 43 | authMethodCount := 0 |
| 44 | if c.ClientSecretAuth != nil { |
| 45 | authMethodCount++ |
| 46 | if c.ClientSecretAuth.ClientSecret == "" { |
| 47 | return errors.New("ClientSecret is required when using ClientSecretAuth") |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Allow zero auth methods (public client) or exactly one auth method. |
| 52 | if authMethodCount > 1 { |
| 53 | return errors.New("only one client authentication method can be configured") |
| 54 | } |
| 55 | |
| 56 | return nil |
| 57 | } |