using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Net; using System.Runtime.Serialization; using System.Security; namespace Octokit { /// /// Represents a failed 2FA challenge from the API /// [Serializable] [SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors", Justification = "These exceptions are specific to the GitHub API and not general purpose exceptions")] public abstract class TwoFactorAuthorizationException : AuthorizationException { /// /// Constructs an instance of TwoFactorRequiredException. /// /// Expected 2FA response type /// The inner exception protected TwoFactorAuthorizationException(TwoFactorType twoFactorType, Exception innerException) : base(HttpStatusCode.Unauthorized, innerException) { TwoFactorType = twoFactorType; } /// /// Constructs an instance of TwoFactorRequiredException. /// /// The HTTP payload from the server /// Expected 2FA response type protected TwoFactorAuthorizationException(IResponse response, TwoFactorType twoFactorType) : base(response) { Debug.Assert(response != null && response.StatusCode == HttpStatusCode.Unauthorized, "TwoFactorRequiredException status code should be 401"); TwoFactorType = twoFactorType; } /// /// Constructs an instance of TwoFactorRequiredException. /// /// The HTTP payload from the server /// Expected 2FA response type /// The inner exception protected TwoFactorAuthorizationException(IResponse response, TwoFactorType twoFactorType, Exception innerException) : base(response, innerException) { Debug.Assert(response != null && response.StatusCode == HttpStatusCode.Unauthorized, "TwoFactorRequiredException status code should be 401"); TwoFactorType = twoFactorType; } /// /// Expected 2FA response type /// public TwoFactorType TwoFactorType { get; private set; } /// /// Constructs an instance of TwoFactorRequiredException. /// /// /// The that holds the /// serialized object data about the exception being thrown. /// /// /// The that contains /// contextual information about the source or destination. /// protected TwoFactorAuthorizationException(SerializationInfo info, StreamingContext context) : base(info, context) { if (info == null) return; TwoFactorType = (TwoFactorType)info.GetInt32("TwoFactorType"); } [SecurityCritical] public override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); info.AddValue("TwoFactorType", TwoFactorType); } } /// /// Methods for receiving 2FA authentication codes /// public enum TwoFactorType { /// /// No method configured /// None, /// /// Unknown method /// Unknown, /// /// Receive via SMS /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Sms")] Sms, /// /// Receive via application /// AuthenticatorApp } }