Git Product home page Git Product logo

nsmutableurlrequest-basicauth's Introduction

NSMutableURLRequest+BasicAuth

An iOS Objective-C category for performing HTTP Basic Access Authentication, aka Basic auth.

Why?

Most solutions for performing Basic auth on iOS involve the use of 3rd party Base64 libraries. Apple has provided native functions within the CFNetworking framework, removing the need for 3rd party libraries. This category wraps the solution into one clean and reusable method.

Changelog (1.0.2)

  • Thanks to Mike Abdullah for this update.
  • Handled exception for username's that contain colons (e.g. :)
  • Updated README

Installation

Cocoapods Installation

pod 'NSMutableURLRequest-BasicAuth'

Manual Installation

  • Add the NSMutableURLRequest+BasicAuth folder into your project
  • Import NSMutableURLRequest+BasicAuth.h into your class(es).
  • Import Apple's CFNetworking framework.

Usage

  • Create an NSMutableURLRequest and make sure to set the following properties:
    • URL
    • HTTPMethod
  • Then, call the basicAuthForRequest:withUsername:andPassword method with your request.
  • Afterwards, initialize your NSURLConnection and load your Basic auth request.

Interface

+ (void)basicAuthForRequest:(NSMutableURLRequest *)request withUsername:(NSString *)username andPassword:(NSString *)password;

Implementation

    // Cast username and password as CFStringRefs via Toll-Free Bridging
    CFStringRef usernameRef = (__bridge CFStringRef)username;
    CFStringRef passwordRef = (__bridge CFStringRef)password;
    
    // Reference properties of the NSMutableURLRequest
    CFHTTPMessageRef authoriztionMessageRef = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (__bridge CFStringRef)[request HTTPMethod], (__bridge CFURLRef)[request URL], kCFHTTPVersion1_1);
    
    // Encodes usernameRef and passwordRef in Base64
    CFHTTPMessageAddAuthentication(authoriztionMessageRef, nil, usernameRef, passwordRef, kCFHTTPAuthenticationSchemeBasic, FALSE);
    
    // Creates the 'Basic - <encoded_username_and_password>' string for the HTTP header
    CFStringRef authorizationStringRef = CFHTTPMessageCopyHeaderFieldValue(authoriztionMessageRef, CFSTR("Authorization"));
    
    // Add authorizationStringRef as value for 'Authorization' HTTP header
    [request setValue:(__bridge NSString *)authorizationStringRef forHTTPHeaderField:@"Authorization"];
    
    // Cleanup
    CFRelease(authorizationStringRef);
    CFRelease(authoriztionMessageRef);

Important Note

An exception is thrown if the username contains a colon (e.g. :).

Created and maintained by

Created by Arthur Ariel Sabintsev for ID.me, Inc.

nsmutableurlrequest-basicauth's People

Contributors

artsabintsev avatar mikeabdullah avatar

Watchers

谢福才 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.