github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/MobileLibrary/iOS/SampleApps/TunneledWebView/External/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.h (about)

     1  /*
     2   File: JAHPAuthenticatingHTTPProtocol.h
     3   Abstract: An NSURLProtocol subclass that overrides the built-in HTTP/HTTPS protocol.
     4   Version: 1.1
     5   
     6   Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
     7   Inc. ("Apple") in consideration of your agreement to the following
     8   terms, and your use, installation, modification or redistribution of
     9   this Apple software constitutes acceptance of these terms.  If you do
    10   not agree with these terms, please do not use, install, modify or
    11   redistribute this Apple software.
    12   
    13   In consideration of your agreement to abide by the following terms, and
    14   subject to these terms, Apple grants you a personal, non-exclusive
    15   license, under Apple's copyrights in this original Apple software (the
    16   "Apple Software"), to use, reproduce, modify and redistribute the Apple
    17   Software, with or without modifications, in source and/or binary forms;
    18   provided that if you redistribute the Apple Software in its entirety and
    19   without modifications, you must retain this notice and the following
    20   text and disclaimers in all such redistributions of the Apple Software.
    21   Neither the name, trademarks, service marks or logos of Apple Inc. may
    22   be used to endorse or promote products derived from the Apple Software
    23   without specific prior written permission from Apple.  Except as
    24   expressly stated in this notice, no other rights or licenses, express or
    25   implied, are granted by Apple herein, including but not limited to any
    26   patent rights that may be infringed by your derivative works or by other
    27   works in which the Apple Software may be incorporated.
    28   
    29   The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
    30   MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
    31   THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
    32   FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
    33   OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
    34   
    35   IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
    36   OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    37   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    38   INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
    39   MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
    40   AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
    41   STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
    42   POSSIBILITY OF SUCH DAMAGE.
    43   
    44   Copyright (C) 2014 Apple Inc. All Rights Reserved.
    45   
    46   */
    47  
    48  @import Foundation;
    49  
    50  @protocol JAHPAuthenticatingHTTPProtocolDelegate;
    51  
    52  /*! An NSURLProtocol subclass that overrides the built-in HTTP/HTTPS protocol to intercept
    53   *  authentication challenges for subsystems, ilke UIWebView, that don't otherwise allow it.
    54   *  To use this class you should set up your delegate (+setDelegate:) and then call +start.
    55   *  If you don't call +start the class is completely benign.
    56   *
    57   *  The really tricky stuff here is related to the authentication challenge delegate
    58   *  callbacks; see the docs for JAHPAuthenticatingHTTPProtocolDelegate for the details.
    59   */
    60  
    61  @interface JAHPAuthenticatingHTTPProtocol : NSURLProtocol
    62  
    63  /*! Call this to start the module.  Prior to this the module is just dormant, and
    64   *  all HTTP requests proceed as normal.  After this all HTTP and HTTPS requests
    65   *  go through this module.
    66   */
    67  
    68  + (void)start;
    69  
    70  /*! 
    71   Unregisters the protocol
    72   */
    73  + (void)stop;
    74  
    75  /*! Sets the delegate for the class.
    76   *  \details Note that there's one delegate for the entire class, not one per
    77   *  instance of the class as is more normal.  The delegate is not retained in general,
    78   *  but is retained for the duration of any given call.  Once you set the delegate to nil
    79   *  you can be assured that it won't be called unretained (that is, by the time that
    80   *  -setDelegate: returns, we've already done all possible retains on the delegate).
    81   *
    82   *  The delegate is weakly referenced, so there's no risk of a crash if you don't
    83   *  explicitly nil it.
    84   *  \param newValue The new delegate to use; may be nil.
    85   */
    86  
    87  + (void)setDelegate:(nullable id<JAHPAuthenticatingHTTPProtocolDelegate>)newValue;
    88  
    89  /*! Returns the class delegate.
    90   */
    91  
    92  + (nullable id<JAHPAuthenticatingHTTPProtocolDelegate>)delegate;
    93  
    94  /*! Sets the user agent token
    95   *  \details This token is appended to the system default user agent.
    96   */
    97  + (void)setUserAgentToken:(nullable NSString *)userAgentToken;
    98  
    99  /*! Returns the user agent token.
   100   */
   101  + (nullable NSString *)userAgentToken;
   102  
   103  + (void)resetSharedDemux;
   104  
   105  @property (atomic, strong, readonly)  NSURLAuthenticationChallenge * __nullable     pendingChallenge;   ///< The current authentication challenge; it's only safe to access this from the main thread.
   106  
   107  /*! Call this method to resolve an authentication challeng.  This must be called on the main thread.
   108   *  \param challenge The challenge to resolve. This must match the pendingChallenge property.
   109   *  \param credential The credential to use, or nil to continue without a credential.
   110   */
   111  
   112  - (void)resolvePendingAuthenticationChallengeWithCredential:(nonnull NSURLCredential *)credential;
   113  - (void)cancelPendingAuthenticationChallenge;
   114  
   115  @end
   116  
   117  /*! The delegate for the JAHPAuthenticatingHTTPProtocol class (not its instances).
   118   *  \details The delegate handles two different types of callbacks:
   119   *
   120   *  - authentication challenges
   121   *
   122   *  - logging
   123   *
   124   *  The latter is very simple.  The former is quite tricky.  The basic idea is that each JAHPAuthenticatingHTTPProtocol
   125   *  instance sends the delegate a serialised stream of authentication challenges, each of which it is
   126   *  expected to resolve.  The sequence is as follows:
   127   *
   128   *  -# It calls -authenticatingHTTPProtocol:canAuthenticateAgainstProtectionSpace: to determine if the delegate
   129   *     can handle the challenge.  This can be call on an arbitrary background thread.
   130   *
   131   *  -# If the delegate returns YES, it calls -authenticatingHTTPProtocol:didReceiveAuthenticationChallenge: to
   132   *     actually process the challenge.  This is always called on the main thread.  The delegate can resolve
   133   *     the challenge synchronously (that is, before returning from the call) or it can return from the call
   134   *     and then, later on, resolve the challenge.  Resolving the challenge involves calling
   135   *     -[JAHPAuthenticatingHTTPProtocol resolveAuthenticationChallenge:withCredential:], which also must be called
   136   *     on the main thread.  Between the calls to -authenticatingHTTPProtocol:didReceiveAuthenticationChallenge:
   137   *     and -[JAHPAuthenticatingHTTPProtocol resolveAuthenticationChallenge:withCredential:], the protocol's
   138   *     pendingChallenge property will contain the challenge.
   139   *
   140   *  -# While there is a pending challenge, the protocol may call -authenticatingHTTPProtocol:didCancelAuthenticationChallenge:
   141   *     to cancel the challenge.  This is always called on the main thread.
   142   *
   143   *  Note that this design follows the original NSURLConnection model, not the newer NSURLConnection model
   144   *  (introduced in OS X 10.7 / iOS 5) or the NSURLSession model, because of my concerns about performance.
   145   *  Specifically, -authenticatingHTTPProtocol:canAuthenticateAgainstProtectionSpace: can be called on any thread
   146   *  but -authenticatingHTTPProtocol:didReceiveAuthenticationChallenge: is called on the main thread.  If I unified
   147   *  them I'd end up calling the resulting single routine on the main thread, which meanings a lot more
   148   *  bouncing between threads, much of which would be pointless in the common case where you don't want to
   149   *  customise the default behaviour.  Alternatively I could call the unified routine on an arbitrary thread,
   150   *  but that would make it harder for clients and require a major rework of my implementation.
   151   */
   152  
   153  typedef void (^JAHPDidCancelAuthenticationChallengeHandler)(JAHPAuthenticatingHTTPProtocol * __nonnull authenticatingHTTPProtocol, NSURLAuthenticationChallenge * __nonnull challenge);
   154  
   155  @protocol JAHPAuthenticatingHTTPProtocolDelegate <NSObject>
   156  
   157  @optional
   158  
   159  /*! Called by an JAHPAuthenticatingHTTPProtocol instance to ask the delegate whether it's prepared to handle
   160   *  a particular authentication challenge.  Can be called on any thread.
   161   *  \param protocol The protocol instance itself; will not be nil.
   162   *  \param protectionSpace The protection space for the authentication challenge; will not be nil.
   163   *  \returns Return YES if you want the -authenticatingHTTPProtocol:didReceiveAuthenticationChallenge: delegate
   164   *  callback, or NO for the challenge to be handled in the default way.
   165   */
   166  
   167  - (BOOL)authenticatingHTTPProtocol:(nonnull JAHPAuthenticatingHTTPProtocol *)authenticatingHTTPProtocol canAuthenticateAgainstProtectionSpace:(nonnull NSURLProtectionSpace *)protectionSpace;
   168  
   169  /*! Called by an JAHPAuthenticatingHTTPProtocol instance to request that the delegate process on authentication
   170   *  challenge. Will be called on the main thread. Unless the challenge is cancelled (see below)
   171   *  the delegate must eventually resolve it by calling -resolveAuthenticationChallenge:withCredential:.
   172   *  \param protocol The protocol instance itself; will not be nil.
   173   *  \param challenge The authentication challenge; will not be nil.
   174   *  \returns an optional JAHPDidCancelAuthenticationChallengeHandler that will be called when the
   175   *  JAHPAuthenticatingHTTPProtocol instance cancels the authentication challenge. Just like
   176   *  -authenticatingHTTPProtocol:didCancelAuthenticationChallenge:. If this is returned, there is usually no need 
   177   *  to implement -authenticatingHTTPProtocol:didCancelAuthenticationChallenge:. This block will be called before
   178   *  -authenticatingHTTPProtocol:didCancelAuthenticationChallenge:.
   179   */
   180  
   181  - (nullable JAHPDidCancelAuthenticationChallengeHandler)authenticatingHTTPProtocol:(nonnull JAHPAuthenticatingHTTPProtocol *)authenticatingHTTPProtocol didReceiveAuthenticationChallenge:(nonnull NSURLAuthenticationChallenge *)challenge;
   182  
   183  /*! Called by an JAHPAuthenticatingHTTPProtocol instance to cancel an issued authentication challenge.
   184   *  Will be called on the main thread.
   185   *  \param protocol The protocol instance itself; will not be nil.
   186   *  \param challenge The authentication challenge; will not be nil; will match the challenge
   187   *  previously issued by -authenticatingHTTPProtocol:canAuthenticateAgainstProtectionSpace:.
   188   */
   189  
   190  - (void)authenticatingHTTPProtocol:(nonnull JAHPAuthenticatingHTTPProtocol *)authenticatingHTTPProtocol didCancelAuthenticationChallenge:(nonnull NSURLAuthenticationChallenge *)challenge;
   191  
   192  /*! Called by the JAHPAuthenticatingHTTPProtocol to log various bits of information.
   193   *  Can be called on any thread.
   194   *  \param protocol The protocol instance itself; nil to indicate log messages from the class itself.
   195   *  \param format A standard NSString-style format string; will not be nil.
   196   *  \param arguments Arguments for that format string.
   197   */
   198  
   199  - (void)authenticatingHTTPProtocol:(nullable JAHPAuthenticatingHTTPProtocol *)authenticatingHTTPProtocol logWithFormat:(nonnull NSString *)format
   200  // clang's static analyzer doesn't know that a va_list can't have an nullability annotation.
   201  #pragma clang diagnostic push
   202  #pragma clang diagnostic ignored "-Wnullability-completeness"
   203                           arguments:(va_list)arguments;
   204  #pragma clang diagnostic pop
   205  
   206  /*! Called by the JAHPAuthenticatingHTTPProtocol to log various bits of information. Use this if implementing in Swift. Swift doesn't like
   207   * -authenticatingHTTPProtocol:logWithFormat:arguments: because 
   208   * `Method cannot be marked @objc because the type of the parameter 3 cannot be represented in Objective-C`
   209   *  I assume this is a problem with Swift not understanding that CVAListPointer should become va_list.
   210   *  Can be called on any thread.
   211   *  \param protocol The protocol instance itself; nil to indicate log messages from the class itself.
   212   *  \param message A message to log
   213   */
   214  
   215  - (void)authenticatingHTTPProtocol:(nullable JAHPAuthenticatingHTTPProtocol *)authenticatingHTTPProtocol logMessage:(nonnull NSString *)message;
   216  
   217  @end