using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Net; using System.Runtime.Serialization; namespace Octokit { /// /// /// #if !NETFX_CORE [Serializable] #endif [SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors", Justification = "These exceptions are specific to the GitHub API and not general purpose exceptions")] public class TwoFactorRequiredException : AuthorizationException { /// /// Constructs an instance of TwoFactorRequiredException. /// public TwoFactorRequiredException() : this(TwoFactorType.None) { } /// /// Constructs an instance of TwoFactorRequiredException. /// /// Expected 2FA response type public TwoFactorRequiredException(TwoFactorType twoFactorType) : base(HttpStatusCode.Unauthorized, null) { TwoFactorType = twoFactorType; } /// /// Constructs an instance of TwoFactorRequiredException. /// /// The HTTP payload from the server /// Expected 2FA response type public TwoFactorRequiredException(IResponse response, TwoFactorType twoFactorType) : base(response) { Debug.Assert(response != null && response.StatusCode == HttpStatusCode.Unauthorized, "TwoFactorRequiredException status code should be 401"); TwoFactorType = twoFactorType; } public override string Message { get { return ApiErrorMessageSafe ?? "Two-factor authentication code is required"; } } #if !NETFX_CORE /// /// 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 TwoFactorRequiredException(SerializationInfo info, StreamingContext context) : base(info, context) { if (info == null) return; TwoFactorType = (TwoFactorType) (info.GetInt32("TwoFactorType")); } public override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); info.AddValue("TwoFactorType", TwoFactorType); } #endif /// /// Expected 2FA response type /// public TwoFactorType TwoFactorType { get; private set; } } /// /// 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 } }