|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using contentstack.model.generator.Model; |
| 5 | + |
| 6 | +namespace Contentstack.Model.Generator.Model |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Represents the response from the OAuth app authorization API. |
| 10 | + /// </summary> |
| 11 | + public class OAuthAppAuthorizationResponse |
| 12 | + { |
| 13 | + |
| 14 | + [JsonProperty("data")] |
| 15 | + public OAuthAppAuthorizationData[] Data { get; set; } |
| 16 | + } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Represents OAuth app authorization data. |
| 20 | + /// </summary> |
| 21 | + public class OAuthAppAuthorizationData |
| 22 | + { |
| 23 | + |
| 24 | + [JsonProperty("authorization_uid")] |
| 25 | + public string AuthorizationUid { get; set; } |
| 26 | + |
| 27 | + |
| 28 | + [JsonProperty("user")] |
| 29 | + public OAuthUser User { get; set; } |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + public class OAuthUser |
| 34 | + { |
| 35 | + |
| 36 | + [JsonProperty("uid")] |
| 37 | + public string Uid { get; set; } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Configuration options for OAuth authentication. |
| 42 | + /// </summary> |
| 43 | + public class OAuthOptions |
| 44 | + { |
| 45 | + /// <summary> |
| 46 | + /// The OAuth application ID. Defaults to the Contentstack app ID. |
| 47 | + /// </summary> |
| 48 | + public string AppId { get; set; } = "6400aa06db64de001a31c8a9"; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// The OAuth client ID. Defaults to the Contentstack client ID. |
| 52 | + /// </summary> |
| 53 | + public string ClientId { get; set; } = "Ie0FEfTzlfAHL4xM"; |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// The redirect URI for OAuth callbacks. Defaults to localhost:8184. |
| 57 | + /// </summary> |
| 58 | + public string RedirectUri { get; set; } = "http://localhost:8184"; |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// The OAuth client secret. If provided, PKCE flow will be skipped. |
| 62 | + /// If null or empty, PKCE flow will be used for enhanced security. |
| 63 | + /// </summary> |
| 64 | + public string ClientSecret { get; set; } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// The OAuth response type. Defaults to "code" for authorization code flow. |
| 68 | + /// </summary> |
| 69 | + public string ResponseType { get; set; } = "code"; |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// The OAuth scopes to request. Optional array of permission scopes. |
| 73 | + /// </summary> |
| 74 | + public string[] Scope { get; set; } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Indicates whether PKCE (Proof Key for Code Exchange) flow should be used. |
| 78 | + /// This is automatically determined based on whether ClientSecret is provided. |
| 79 | + /// </summary> |
| 80 | + public bool UsePkce => string.IsNullOrEmpty(ClientSecret); |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Validates the OAuth options configuration. |
| 84 | + /// </summary> |
| 85 | + /// <returns>True if the configuration is valid, false otherwise.</returns> |
| 86 | + public bool IsValid() |
| 87 | + { |
| 88 | + return IsValid(out _); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Validates the OAuth options configuration and provides detailed error information. |
| 93 | + /// </summary> |
| 94 | + /// <param name="errorMessage">The validation error message if validation fails.</param> |
| 95 | + /// <returns>True if the configuration is valid, false otherwise.</returns> |
| 96 | + public bool IsValid(out string errorMessage) |
| 97 | + { |
| 98 | + errorMessage = null; |
| 99 | + |
| 100 | + if (string.IsNullOrWhiteSpace(AppId)) |
| 101 | + { |
| 102 | + errorMessage = "AppId is required for OAuth configuration."; |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + if (string.IsNullOrWhiteSpace(ClientId)) |
| 107 | + { |
| 108 | + errorMessage = "ClientId is required for OAuth configuration."; |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + if (string.IsNullOrWhiteSpace(RedirectUri)) |
| 113 | + { |
| 114 | + errorMessage = "RedirectUri is required for OAuth configuration."; |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + if (!Uri.TryCreate(RedirectUri, UriKind.Absolute, out var redirectUri)) |
| 119 | + { |
| 120 | + errorMessage = "RedirectUri must be a valid absolute URI."; |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + if (redirectUri.Scheme != "http" && redirectUri.Scheme != "https") |
| 125 | + { |
| 126 | + errorMessage = "RedirectUri must use http or https scheme."; |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + if (string.IsNullOrWhiteSpace(ResponseType)) |
| 131 | + { |
| 132 | + errorMessage = "ResponseType is required for OAuth configuration."; |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + if (ResponseType != "code") |
| 137 | + { |
| 138 | + errorMessage = "ResponseType must be 'code' for authorization code flow."; |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + // For traditional OAuth flow (non-PKCE), client secret is required |
| 143 | + if (!UsePkce && string.IsNullOrWhiteSpace(ClientSecret)) |
| 144 | + { |
| 145 | + errorMessage = "ClientSecret is required for traditional OAuth flow. Use PKCE flow (leave ClientSecret empty) for public clients."; |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Validates the OAuth options configuration and throws an exception if invalid. |
| 154 | + /// </summary> |
| 155 | + /// <exception cref="OAuthConfigurationException">Thrown when the configuration is invalid.</exception> |
| 156 | + public void Validate() |
| 157 | + { |
| 158 | + if (!IsValid(out var errorMessage)) |
| 159 | + { |
| 160 | + throw new Exceptions.OAuthConfigurationException(errorMessage); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Gets a string representation of the OAuth options for debugging. |
| 166 | + /// </summary> |
| 167 | + /// <returns>A string representation of the OAuth options.</returns> |
| 168 | + public override string ToString() |
| 169 | + { |
| 170 | + return $"OAuthOptions: AppId={AppId}, ClientId={ClientId}, RedirectUri={RedirectUri}, " + |
| 171 | + $"ResponseType={ResponseType}, UsePkce={UsePkce}, HasScope={Scope?.Length > 0}"; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Represents the response from OAuth token exchange operations. |
| 177 | + /// </summary> |
| 178 | + public class OAuthResponse |
| 179 | + { |
| 180 | + |
| 181 | + [JsonProperty("access_token")] |
| 182 | + public string AccessToken { get; set; } |
| 183 | + |
| 184 | + |
| 185 | + [JsonProperty("refresh_token")] |
| 186 | + public string RefreshToken { get; set; } |
| 187 | + |
| 188 | + |
| 189 | + [JsonProperty("expires_in")] |
| 190 | + public int ExpiresIn { get; set; } |
| 191 | + |
| 192 | + |
| 193 | + [JsonProperty("organization_uid")] |
| 194 | + public string OrganizationUid { get; set; } |
| 195 | + |
| 196 | + |
| 197 | + [JsonProperty("user_uid")] |
| 198 | + public string UserUid { get; set; } |
| 199 | + } |
| 200 | + /// <summary> |
| 201 | + /// Represents OAuth tokens stored in memory for cross-SDK access. |
| 202 | + /// This class enables sharing OAuth tokens between the Management SDK and other SDKs |
| 203 | + /// </summary> |
| 204 | + public class OAuthTokens |
| 205 | + { |
| 206 | + |
| 207 | + public string AccessToken { get; set; } |
| 208 | + |
| 209 | + public string RefreshToken { get; set; } |
| 210 | + |
| 211 | + public DateTime ExpiresAt { get; set; } |
| 212 | + |
| 213 | + public string OrganizationUid { get; set; } |
| 214 | + |
| 215 | + public string UserUid { get; set; } |
| 216 | + |
| 217 | + public string ClientId { get; set; } |
| 218 | + |
| 219 | + public string AppId { get; set; } |
| 220 | + |
| 221 | + public bool IsExpired => ExpiresAt == DateTime.MinValue || DateTime.UtcNow >= ExpiresAt; |
| 222 | + |
| 223 | + public bool NeedsRefresh |
| 224 | + { |
| 225 | + get |
| 226 | + { |
| 227 | + // If ExpiresAt is not set or is MinValue, consider it expired |
| 228 | + if (ExpiresAt == DateTime.MinValue) |
| 229 | + return true; |
| 230 | + |
| 231 | + try |
| 232 | + { |
| 233 | + // Check if we need to refresh (5 minutes before expiration) |
| 234 | + var refreshTime = ExpiresAt.AddMinutes(-5); |
| 235 | + return DateTime.UtcNow >= refreshTime || IsExpired; |
| 236 | + } |
| 237 | + catch (ArgumentOutOfRangeException) |
| 238 | + { |
| 239 | + // If the calculation results in an unrepresentable DateTime, consider it expired |
| 240 | + return true; |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + public bool IsValid => !string.IsNullOrEmpty(AccessToken) && !IsExpired; |
| 246 | + } |
| 247 | + |
| 248 | +} |
0 commit comments