using System;
namespace CarpaNet.OAuth;
///
/// Base exception for OAuth errors.
///
public class OAuthException : Exception
{
///
/// The OAuth error code.
///
public string ErrorCode { get; }
///
/// The error description.
///
public string? ErrorDescription { get; }
///
/// Creates a new OAuth exception.
///
public OAuthException(string errorCode, string? errorDescription = null)
: base(FormatMessage(errorCode, errorDescription))
{
ErrorCode = errorCode;
ErrorDescription = errorDescription;
}
///
/// Creates a new OAuth exception with an inner exception.
///
public OAuthException(string errorCode, string? errorDescription, Exception innerException)
: base(FormatMessage(errorCode, errorDescription), innerException)
{
ErrorCode = errorCode;
ErrorDescription = errorDescription;
}
private static string FormatMessage(string errorCode, string? errorDescription)
{
return string.IsNullOrEmpty(errorDescription)
? errorCode
: $"{errorCode}: {errorDescription}";
}
}
///
/// Exception thrown when an OAuth callback contains an error.
///
public class OAuthCallbackException : OAuthException
{
///
/// The application state that was passed to the authorize call.
///
public string? AppState { get; }
///
/// Creates a new OAuth callback exception.
///
public OAuthCallbackException(string errorCode, string? errorDescription, string? appState)
: base(errorCode, errorDescription)
{
AppState = appState;
}
}
///
/// Exception thrown when token refresh fails.
///
public class TokenRefreshException : OAuthException
{
///
/// The subject (DID) of the session that failed to refresh.
///
public string? Sub { get; }
///
/// Creates a new token refresh exception.
///
public TokenRefreshException(string errorCode, string? errorDescription, string? sub)
: base(errorCode, errorDescription)
{
Sub = sub;
}
///
/// Creates a new token refresh exception with an inner exception.
///
public TokenRefreshException(string errorCode, string? errorDescription, string? sub, Exception innerException)
: base(errorCode, errorDescription, innerException)
{
Sub = sub;
}
}
///
/// Exception thrown when a DPoP nonce is required but not provided or invalid.
///
public class DPoPNonceException : OAuthException
{
///
/// The new nonce provided by the server.
///
public string? NewNonce { get; }
///
/// Creates a new DPoP nonce exception.
///
public DPoPNonceException(string? newNonce)
: base("use_dpop_nonce", "Server requires a DPoP nonce")
{
NewNonce = newNonce;
}
}