<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NathanHJones.com &#187; enterprise</title>
	<atom:link href="http://nathanhjones.com/tag/enterprise/feed/" rel="self" type="application/rss+xml" />
	<link>http://nathanhjones.com</link>
	<description>just thinking outloud</description>
	<lastBuildDate>Fri, 11 Nov 2011 06:55:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Single Sign-on Pattern for Enterprise iOS Applications</title>
		<link>http://nathanhjones.com/2011/11/11/a-single-sign-on-pattern-for-enterprise-ios-applications/</link>
		<comments>http://nathanhjones.com/2011/11/11/a-single-sign-on-pattern-for-enterprise-ios-applications/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 06:30:32 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[business]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[enterprise]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iphone sdk]]></category>
		<category><![CDATA[mSSO]]></category>
		<category><![CDATA[single sign-on]]></category>
		<category><![CDATA[SSO]]></category>

		<guid isPermaLink="false">http://nathanhjones.com/?p=341</guid>
		<description><![CDATA[In conversations with clients, we continue to hear how important single sign-on is to their enterprise mobile application strategy. According to a study earlier this year by Kelton Research 250 IT Managers, 21% of respondents indicated that they plan to deploy 20, or more, enterprise applications to their organization this year. In order for enterprises [...]]]></description>
			<content:encoded><![CDATA[<p>
In conversations with clients, we continue to hear how important single sign-on is to their enterprise mobile application strategy.  According to a <a href="http://www.echannelline.com/usa/story.cfm?item=26463">study earlier this year by Kelton Research 250 IT Managers</a>, 21% of respondents indicated that they plan to deploy 20, or more, enterprise applications to their organization this year.
</p>
<p>
In order for enterprises to realize the productivity gains factored into the investment decisions for those applications, IT Managers must explore options for enabling single sign-on for their users. In this post, we&#8217;ll outline a mobile single sign-on (mSSO) pattern for enterprise iOS applications.
</p>
<h2>Assumptions</h2>
<ol>
<li>Your company has a centralized single sign-on service exposed on the network &#8211; LDAP, Novell Access Manager, etc. This approach will work with a number of identity management architectures, but you will need to tailor credential management accordingly. For this demo, our service calls are simulated with hardcoded tokens.</li>
<li>Your company has a standardized credential timeout period or a way of dynamically distributing that value to applications. For this demo, we&#8217;ll use a static 30 minute timeout period.</li>
<li>You are able to sign and distribute builds to a device. This is important because keychain activities do not function in the simulator.</li>
</ol>
<h2>Implementation</h2>
<h4>Approach</h4>
<p>
Our approach will consist of creating three applications &#8211; two mock business applications and one logout application. Each application may contain its own logout functionality, but a single logout application makes the user&#8217;s activity of killing a session intuitive.  In practice, the mSSO pattern would be implemented as a stand-alone library which could be easily dropped into each of your various enterprise applications.
</p>
<p>
The business applications retrieve a unique authentication token which is included with each service request.  This token will be stored in the device&#8217;s <a href="http://developer.apple.com/library/ios/#documentation/Security/Reference/keychainservices/Reference/reference.html">keychain</a>, which will be shared across our suite of applications.  We will also maintain an &#8216;expiration date&#8217; within the keychain so that subsequent requests from our various apps can (if required) preemptively prompt the user to authenticate.
</p>
<p>
Authentication for this demo may be simplistic, but <a href="http://blogs.captechconsulting.com/blog/jack-cox/managing-complex-mobile-transactions">existing patterns</a> cover implementing authentication in a service-based mobile application.  For example, the &#8216;store credentials&#8217; logic presented in this example would live somewhere in that pattern&#8217;s &#8220;authenticateOperation&#8221;.
</p>
<p>
One possible extension of the mSSO pattern is to confirm that your credentials have not expired before issuing a call to the service.  You would use the mSSO pattern to sign your network requests, but rely on the service and response handlers to inform you that credentials have expired.  Checking credential expiration prior to making a service call limits unnecessary network traffic.
</p>
<h4>Configuration</h4>
<p>
There are two key steps that must be completed in order for your applications to share keychain access.</p>
<ol>
<li>Each application included in your mSSO effort must share a Bundle Seed ID, which allows shared keychain access between our suite of applications. This is configured within the <a href="http://developer.apple.com/ios/manage/overview/index.action">iOS management portal</a>. We&#8217;ll configure our applications using the default &#8216;Team ID&#8217; selection.</li>
<p><a href="http://nathanhjones.com/wp-content/uploads/2011/11/app1.png"><img src="http://nathanhjones.com/wp-content/uploads/2011/11/app1-300x136.png" alt="" title="app1" width="300" height="136" class="aligncenter size-medium wp-image-354" /></a><br />
<a href="http://nathanhjones.com/wp-content/uploads/2011/11/end-result.png"><img src="http://nathanhjones.com/wp-content/uploads/2011/11/end-result-300x61.png" alt="" title="end-result" width="300" height="61" class="aligncenter size-medium wp-image-355" /></a></p>
<li>We also need to enable and add an entitlements file that specifies that this application should be able to access the shared keychain. <span style="font-weight:bold;">This step is done after the Xcode project has been created.</span></li>
<ul>
<li>Select your app target in Xcode and choose the &#8216;Summary&#8217; tab.</li>
<li>Choose &#8216;Enable Entitlements&#8217; at the bottom.</li>
<li>Set the Entitlements File name to &#8220;mSSO&#8221; and hit return. Select &#8216;Create&#8217; when prompted.</li>
<li>Unless needed for your application, remove iCloud configuration settings.</li>
<li>Add a keychain value titled &#8220;mSSO&#8221;, our Bundle Seed ID is prepended to this value for us.</li>
<p><a href="http://nathanhjones.com/wp-content/uploads/2011/11/app-target-entitlements.png"><img src="http://nathanhjones.com/wp-content/uploads/2011/11/app-target-entitlements-300x137.png" alt="" title="app-target-entitlements" width="300" height="137" class="aligncenter size-medium wp-image-360" /></a><br />
<a href="http://nathanhjones.com/wp-content/uploads/2011/11/mSSO-entitlements-file.png"><img src="http://nathanhjones.com/wp-content/uploads/2011/11/mSSO-entitlements-file-300x35.png" alt="" title="mSSO-entitlements-file" width="300" height="35" class="aligncenter size-medium wp-image-361" /></a>
</ul>
</ol>
<h4>Development</h4>
<p>
We&#8217;ll walk through how to create App1 in detail, and then let you work through App2 and the Logout application. Let&#8217;s start by opening Xcode and creating a single view application.  Before we get started, add the Security.framework and create your entitlement files as outlined above.  <a href="http://useyourloaf.com/blog/2010/3/29/simple-iphone-keychain-access.html" target="_blank">Here</a> is a good primer for interacting with the keychain.
</p>
<ol>
<li>Add the custom <code>mSSOUtils</code> and <code>DateUtils</code> classes as outlined below.  Make sure that you import accordingly.  Due to changes in iOS related to <a href="http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iOS5.html#//apple_ref/doc/uid/TP30915195-SW4">ARC</a> (developer account required), you will need to disable ARC for the <code>mSSOUtils</code> class.  You can do so by selecting your target in Xcode and viewing the <span style="font-weight:bold;">Build Phases</span> tab. Expand the <span style="font-weight:bold;">Compile Sources</span> section and double click the <code>mSSOUtils</code> class to add the <code>-fno-objc-arc</code> compiler flag.  You may need to clean and build.<br />
<span style="font-weight:bold;">mSSOUtils:</span></p>

<div class="wp_codebox"><table><tr id="p3418"><td class="code" id="p341code8"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kmSSOKeychainGroup @&quot;3Q4M6DQ9WM.mSSO&quot;</span>
<span style="color: #6e371a;">#define kAuthenticationServiceName @&quot;com.captechconsulting.msso&quot;</span>
<span style="color: #6e371a;">#define kCredentialToken @&quot;mSSOAuthenticationToken&quot;</span>
<span style="color: #6e371a;">#define kCredentialExpiration @&quot;mSSOCredentialsExpirationDate&quot;</span>
<span style="color: #6e371a;">#define kExpirationTimeout 60.0 * 30    // 30 minute timeout</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// *** PRIVATE METHODS DEF *** //</span>
<span style="color: #a61390;">@interface</span> mSSOUtils <span style="color: #002200;">&#40;</span>Private<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> keychainSearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>identifier;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getValueForIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>identifer;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value forIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>identifier;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> deleteValueForIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>identifier;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> mSSOUtils<span style="color: #002200;">:</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> authenticateWithUsername<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>username andPassword<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>password <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// for testing purposes, each call to this method authenticates successfully</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// set the token - app specific - change this in your App2 implementation</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self setValue<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;TokenSetFromApp1&quot;</span> forIdentifier<span style="color: #002200;">:</span>kCredentialToken<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// token set, now set the credential expiration</span>
        <span style="color: #002200;">&#91;</span>self extendCredentials<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unable to set token.&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> logout <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// destroy token AND expiration date</span>
    <span style="color: #002200;">&#91;</span>self deleteValueForIdentifier<span style="color: #002200;">:</span>kCredentialToken<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self deleteValueForIdentifier<span style="color: #002200;">:</span>kCredentialExpiration<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// credential management</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> extendCredentials <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>newExpireDate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>DateUtils dateWithTimeout<span style="color: #002200;">:</span>kExpirationTimeout<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>newExpireString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>DateUtils stringFromDate<span style="color: #002200;">:</span>newExpireDate withFormat<span style="color: #002200;">:</span>kDateFormat<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">BOOL</span> success <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self setValue<span style="color: #002200;">:</span>newExpireString forIdentifier<span style="color: #002200;">:</span>kCredentialExpiration<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>success<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unable to extend credentials.&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> credentialsExpired <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// if no token exists, call credentials expired</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self credentialToken<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>expirationDate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self credentialExpirationDate<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>expirationDate<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// check for expiration</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>DateUtils dateInPast<span style="color: #002200;">:</span>expirationDate<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// if there is no expiration date, default to 'expired'</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #11740a; font-style: italic;">// sign the request with current credentials - we'll add the token as an HTTP header field</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> signRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>request <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>token <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self credentialToken<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>token<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>request addValue<span style="color: #002200;">:</span>token forHTTPHeaderField<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;auth-token&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> request;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// methods to retrieve credential information</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> credentialToken <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>token <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self getValueForIdentifier<span style="color: #002200;">:</span>kCredentialToken<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> token;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> credentialExpirationDate <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>expirationDateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self getValueForIdentifier<span style="color: #002200;">:</span>kCredentialExpiration<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>expirationDateString<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// convert to date</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>DateUtils dateFromString<span style="color: #002200;">:</span>expirationDateString withFormat<span style="color: #002200;">:</span>kDateFormat<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> displayAuthenticateView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIViewController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>vc <span style="color: #002200;">&#123;</span>
    authenticateViewController <span style="color: #002200;">*</span>authenticateView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>authenticateViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;authenticateViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    UINavigationController <span style="color: #002200;">*</span>nc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationController alloc<span style="color: #002200;">&#93;</span> initWithRootViewController<span style="color: #002200;">:</span>authenticateView<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>vc presentModalViewController<span style="color: #002200;">:</span>nc animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark PRIVATE METHODS</span>
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> keychainSearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>identifier <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>keychainSearch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>keychainSearch setObject<span style="color: #002200;">:</span>kmSSOKeychainGroup forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecAttrAccessGroup<span style="color: #002200;">&#93;</span>;   <span style="color: #11740a; font-style: italic;">// inform the search that we're using the shared keychain</span>
    <span style="color: #002200;">&#91;</span>keychainSearch setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecClassGenericPassword forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecClass<span style="color: #002200;">&#93;</span>;   <span style="color: #11740a; font-style: italic;">// set the type to generic password - other options are certification, internet password, etc</span>
&nbsp;
    <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>encodedIdentifier <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>identifier dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>keychainSearch setObject<span style="color: #002200;">:</span>encodedIdentifier forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecAttrGeneric<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>keychainSearch setObject<span style="color: #002200;">:</span>encodedIdentifier forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecAttrAccount<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>keychainSearch setObject<span style="color: #002200;">:</span>kAuthenticationServiceName forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecAttrService<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> keychainSearch;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> getValueForIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>identifier <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>search <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self keychainSearch<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>search setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecMatchLimitOne forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecMatchLimit<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// limit it to the first result</span>
    <span style="color: #002200;">&#91;</span>search setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kCFBooleanTrue forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecReturnData<span style="color: #002200;">&#93;</span>;    <span style="color: #11740a; font-style: italic;">// return data vs a dictionary of attributes</span>
&nbsp;
    <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>value <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    OSStatus status <span style="color: #002200;">=</span> SecItemCopyMatching<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFDictionaryRef<span style="color: #002200;">&#41;</span>search,
                                          <span style="color: #002200;">&#40;</span>CFTypeRef <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>value<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>status <span style="color: #002200;">==</span> noErr<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithUTF8String<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>value bytes<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value forIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>identifier <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// check if value exists</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>existingValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self getValueForIdentifier<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>existingValue<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>existingValue isEqualToString<span style="color: #002200;">:</span>value<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// update value</span>
            <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>search <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self keychainSearch<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;
&nbsp;
            <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>valueData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>value dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
            <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>update <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>valueData, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecValueData, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
            OSStatus status <span style="color: #002200;">=</span> SecItemUpdate<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFDictionaryRef<span style="color: #002200;">&#41;</span>search,
                                            <span style="color: #002200;">&#40;</span>CFDictionaryRef<span style="color: #002200;">&#41;</span>update<span style="color: #002200;">&#41;</span>;
&nbsp;
            <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>status <span style="color: #002200;">==</span> errSecSuccess<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
            <span style="color: #002200;">&#125;</span>
            <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
        <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
&nbsp;
        <span style="color: #11740a; font-style: italic;">// create new entry</span>
        <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>add <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self keychainSearch<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>valueData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>value dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>add setObject<span style="color: #002200;">:</span>valueData forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecValueData<span style="color: #002200;">&#93;</span>;
&nbsp;
        OSStatus status <span style="color: #002200;">=</span> SecItemAdd<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFDictionaryRef<span style="color: #002200;">&#41;</span>add,<span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
&nbsp;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>status <span style="color: #002200;">==</span> errSecSuccess<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
&nbsp;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> deleteValueForIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>identifier <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>search <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self keychainSearch<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;
    SecItemDelete<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFDictionaryRef<span style="color: #002200;">&#41;</span>search<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p><span style="font-weight:bold;">DateUtils:</span></p>

<div class="wp_codebox"><table><tr id="p3419"><td class="code" id="p341code9"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> dateFromString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #a61390;">string</span> withFormat<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>format <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormatter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>dateFormatter setDateFormat<span style="color: #002200;">:</span>format<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormatter dateFromString<span style="color: #002200;">:</span><span style="color: #a61390;">string</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> date;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> stringFromDate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date withFormat<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>format; <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormatter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>dateFormatter setDateFormat<span style="color: #002200;">:</span>format<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormatter stringFromDate<span style="color: #002200;">:</span>date<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> dateString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> dateWithTimeout<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSTimeInterval<span style="color: #002200;">&#41;</span>timeout <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>now <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>timeoutDate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>now dateByAddingTimeInterval<span style="color: #002200;">:</span>timeout<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> timeoutDate;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> dateInPast<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>date compare<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> NSOrderedAscending<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

</li>
<li>Within the generated <code>ViewController</code>, add two <code>UILabel</code> outlets/properties &#8211; token and expiration date &#8211; and a &#8220;Logout&#8221; button.  You&#8217;ll need to add two custom methods: logout and a selector to handle the foreground notification we register to receive.

<div class="wp_codebox"><table><tr id="p34110"><td class="code" id="p341code10"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> logout<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>mSSOUtils logout<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>mSSOUtils displayAuthenticateView<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> enterForeground<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// reset labels as we've entered the foreground</span>
    self.token.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>mSSOUtils credentialToken<span style="color: #002200;">&#93;</span>;
    self.expiration.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>DateUtils stringFromDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>mSSOUtils credentialExpirationDate<span style="color: #002200;">&#93;</span> withFormat<span style="color: #002200;">:</span>kDateFormat<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

</li>
<li>You should only need to update two view lifecycle methods within <code>ViewController</code> &#8211; <code>viewDidLoad</code> and <code>viewWillAppear</code>.  Within <code>viewDidLoad</code>, we register to receive a notification when the app is brought to the foreground that triggers our UI updates.  The additions to <code>viewWillAppear</code> simply update our labels if there is data in the keychain.

<div class="wp_codebox"><table><tr id="p34111"><td class="code" id="p341code11"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// register for enter foreground notification to update labels</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
                                             selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>enterForeground<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
                                                 name<span style="color: #002200;">:</span>UIApplicationWillEnterForegroundNotification
                                               object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewWillAppear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
&nbsp;
    self.token.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>mSSOUtils credentialToken<span style="color: #002200;">&#93;</span>;
    self.expiration.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>DateUtils stringFromDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>mSSOUtils credentialExpirationDate<span style="color: #002200;">&#93;</span> withFormat<span style="color: #002200;">:</span>kDateFormat<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

</li>
<li>Add an <code>authenticateViewController</code> to your project. This will be displayed modally when the users credentials need to be re-challenged. This example simply has a login button, but this is where you would include typical login fields.

<div class="wp_codebox"><table><tr id="p34112"><td class="code" id="p341code12"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> authenticate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// if authentication is successful, dismiss the view</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>mSSOUtils authenticateWithUsername<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;username&quot;</span> andPassword<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hashedPassword&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>   
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

</li>
<li>Last up, we&#8217;ll need to update our application delegate to confirm our credentials are valid when the app launches or is brought back from the background.  You&#8217;ll need to update the <code>didFinishLaunchingWithOptions</code> and <code>applicationWillEnterForeground</code> methods as noted below:

<div class="wp_codebox"><table><tr id="p34113"><td class="code" id="p341code13"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span>
    self.window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch.</span>
    self.viewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    self.window.rootViewController <span style="color: #002200;">=</span> self.viewController;
    <span style="color: #002200;">&#91;</span>self.window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// credentials don't exist or are expired - display the authenticate view</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>mSSOUtils credentialsExpired<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>mSSOUtils displayAuthenticateView<span style="color: #002200;">:</span>self.viewController<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationWillEnterForeground<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// credentials don't exist or are expired - display the authenticate view</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>mSSOUtils credentialsExpired<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>mSSOUtils displayAuthenticateView<span style="color: #002200;">:</span>self.viewController<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

</li>
<li>Now, rinse and repeat for App2.  You can follow the same steps for the Logout application, but you really just need a single view that calls <code>[mSSOUtils logout]</code> on launch and informs the user their credentials have been terminated.

<div class="wp_codebox"><table><tr id="p34114"><td class="code" id="p341code14"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span>
    self.window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch.</span>
    self.viewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    self.window.rootViewController <span style="color: #002200;">=</span> self.viewController;
    <span style="color: #002200;">&#91;</span>self.window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// logout to kill credentials</span>
    <span style="color: #002200;">&#91;</span>mSSOUtils logout<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationWillEnterForeground<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>   
    <span style="color: #11740a; font-style: italic;">// logout to kill credentials</span>
    <span style="color: #002200;">&#91;</span>mSSOUtils logout<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

</li>
</ol>
<p>
Here are a couple helpful hints while developing your solution:</p>
<ul>
<li>We&#8217;ve disabled ARC for the <code>mSSOUtils</code> class, which means you need to handle memory management yourself.</li>
<li>When building your application, if you get a Mach-O Linker error, ensure that you&#8217;ve added the Security.framework.</li>
</ul>
<h4>Testing</h4>
<p>
Our testing won&#8217;t get too crazy, but at this point you should be all set to install our suite of apps on your device.
</p>
<p>
We&#8217;ll start by opening App1 and simulating an authentication call. Once the modal view is dismissed, our token and expiration date should be updated &#8211; expiration being now + 30 minutes.  From here, jump to App2 where you should see the App1 token/expiration. Logout and re-authenticate within App2, which will update our token and expiration date.  Now, we&#8217;ll wait 30 minutes and test whether our token expires. After 30 minutes, open App1, you should be prompted with an authentication view.  Authenticate and then open the Logout app.  Enter App2 from the multi-task tray, you should be prompted to authenticate once again.
</p>
<p>
The test sequence above has been captured in the screenshots below.  Note: for brevity, I&#8217;ve excluded screenshots of each authentication view except for the final step.<br />
<a href="http://nathanhjones.com/wp-content/uploads/2011/11/mSSO-Testing.jpg"><img src="http://nathanhjones.com/wp-content/uploads/2011/11/mSSO-Testing-300x143.jpg" alt="" title="mSSO Testing" width="300" height="143" class="aligncenter size-medium wp-image-374" /></a>
</p>
<h4>Closing</h4>
<p>
This post presents a pattern for implementing single sign-on for your enterprise iOS applications. It should give you the foundation needed to begin implementing single sign-on in your applications. I&#8217;ve attached the source for App1. If you&#8217;ve got questions or are interested in additional source files, I&#8217;m <a href="http://twitter.com/nathanhjones">@nathanhjones</a> on Twitter.
</p>
<div style="border: 1px solid #cdcdcd; background-color: #ffffff; padding: 10px 0 10px 10px;">
<span style="font-weight:bold;">Project files:</span> <a href="http://nathanhjones.com/wp-content/uploads/2011/11/mssoapp1.zip">mssoapp1.zip</a>
</div>
]]></content:encoded>
			<wfw:commentRss>http://nathanhjones.com/2011/11/11/a-single-sign-on-pattern-for-enterprise-ios-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Amazon Brings Enterprise Databases to the Cloud</title>
		<link>http://nathanhjones.com/2008/10/05/amazon-brings-enterprise-databases-to-the-cloud/</link>
		<comments>http://nathanhjones.com/2008/10/05/amazon-brings-enterprise-databases-to-the-cloud/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 02:28:56 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[amazon web service]]></category>
		<category><![CDATA[aws]]></category>
		<category><![CDATA[cloud computing]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[enterprise]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://nathanhjones.com/?p=97</guid>
		<description><![CDATA[The people over at Amazon Web Services have been busy at work the last couple weeks. The Elastic Compute Cloud (EC2) team has rolled out two significant computing options over that will really help pave the way for more enterprise adoption of their services. Last Tuesday, Amazon announced that it plans to unveil the ability [...]]]></description>
			<content:encoded><![CDATA[<p>The people over at Amazon Web Services have been busy at work the last couple weeks.  The Elastic Compute Cloud (EC2) team has rolled out two significant computing options over that will really help pave the way for more enterprise adoption of their services.</p>
<p>Last Tuesday, Amazon announced that it plans to unveil the ability to <a href="http://aws.amazon.com/windows/">run Microsoft Windows Server and Microsoft SQL Server on the EC2 platform</a> later this fall.  This announcement is a huge step and will allow customers to <a href="http://www.allthingsdistributed.com/2008/09/amazon_ec2_with_microsoft_wind.html">deploy ASP.NET applications</a> using a host of database options as well as other Windows-based applications such as Windows Media transcoding.  This advancement in cloud computing will really ease the transition for potential enterprise clients which still tend to be very Windows heavy &#8211; ASP.NET portals still seem to be rather popular in my experience.</p>
<p>Last week, in an email to developers, they also announced that Oracle has certified EC2 as “the first cloud computing platform that has been authorized to run supported Oracle databases” which also increases the database options for enterprises and developers alike.  According to the announcement, <a href="http://aws.amazon.com/about-aws/whats-new/2008/09/22/oracle-and-aws/">customers will be able to run Oracle 11g</a>, Oracle Fusion Middleware and the Oracle Enterprise Manager on AWS.  Oracle is really taking an interest in the project and has delivered a set of free images to help make deployment easier.</p>
<p>I don&#8217;t claim to know how many &#8216;enterprise&#8217; clients Amazon Web Services has on its roster but I think this is really a step in the right direction.  I know enterprise security executives are still wary about throwing their proprietary data in the &#8216;cloud&#8217; but who wouldn&#8217;t enjoy a little extra cushion for those PR heavy days when your site gets overloaded.  I can only imagine how busy the Bear Sterns intranet was the day their employees found out it was over.</p>
<p>This will also continue to lower the barrier of entry for web-based start-ups.  The language of choice these days tends to be PHP, but that isn&#8217;t normally a part of the university curriculum.  Now, students can continue to expand on those class projects and turn them into successful (hopefully&#8230;) start-ups.  Of course, that&#8217;s only for those few that aren&#8217;t programming before they hit high school.</p>
]]></content:encoded>
			<wfw:commentRss>http://nathanhjones.com/2008/10/05/amazon-brings-enterprise-databases-to-the-cloud/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

