Tuesday, September 16, 2014

OAM Authentication iOS App


Oracle Mobile and Social Access Service is a fully integrated, server-based solution designed to secure mobile access to applications leveraging the enterprise’s existing OAM 11g infrastructure.  In this blog I am going to show how to use the Oracle’s iOS SDK to demonstrate the login/logout feature. This article assumes you have OAM 11g as well as the Mobile and Social component installed and configured.

Create new Single View Application

I called it OAMLogin

Add Navigation Controller to the main storyboard Main_iPhone.storyboard

Delete the Table View Controller – Root from the storyboard and move the starting point (arrow) to the Navigation Controller:

Control Click on Navigation Controller and drag to OAM View Controller and set it as root view:
Add Login button to the Navigation:
Update the oamViewController header and implementation as follows. After this we will have just a shell with login/logout behavior but no real authentication happening.
oamViewController.h
#import
@interface oamViewController : UIViewController {    bool isAuthenticated;    NSString *username; } @property (strong, nonatomic) IBOutlet UILabel *welcomeLabel; @property (nonatomic,assign) bool isAuthenticated; @end 
oamViewController.m
#import "oamViewController.h"@interface oamViewController () @end@implementation oamViewController @synthesize isAuthenticated; - (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    if(!isAuthenticated)    {        self.navigationItem.prompt = nil;        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                        initWithTitle:@"Login"                                        style:UIBarButtonItemStyleBordered                                        target:self                                        action:@selector(doLogin:)];        [self.navigationItem setRightBarButtonItem:rightButton                                          animated:YES];    }    else    {        self.navigationItem.prompt = nil;        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                        initWithTitle:@"Logout"                                        style:UIBarButtonItemStyleBordered                                        target:self                                        action:@selector(doLogout:)];        [self.navigationItem setRightBarButtonItem:rightButton                                          animated:YES];    } }- (IBAction)doLogin:(id)object {    [self doLogin]; }- (void) doLogin {    isAuthenticated =YES;    self.navigationItem.prompt = nil;    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                    initWithTitle:@"Logout"                                    style:UIBarButtonItemStyleBordered                                    target:self                                    action:@selector(doLogout:)];    [self.navigationItem setRightBarButtonItem:rightButton                                      animated:YES];    [self.welcomeLabel setText:[NSString stringWithFormat:@"Welcome %@", @""]]; }- (IBAction)doLogout:(id)object {    [self doLogout]; }- (void) doLogout {    isAuthenticated =YES;    self.navigationItem.prompt = nil;    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                    initWithTitle:@"Login"                                    style:UIBarButtonItemStyleBordered                                    target:self                                    action:@selector(doLogin:)];    [self.navigationItem setRightBarButtonItem:rightButton                                      animated:YES];    [self.welcomeLabel setText:[NSString stringWithFormat:@"Welcome Guest"]]; }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated. }@end


Add OAM functionality:
This document assumes you have already followed the Oracle Documentation and configured the Mobile and Social component of OAM)
Download the iOS SDK (oamms_sdk_for_ios.zip) from OTN website, extract the files and drag & drop the library file and the folders containing headers (libIDMMobileSDK.a, PublicHeaders and PublicResources) into the Xcode project.
Click on the project name to see the project properties and under general scroll down to Linked Frameworks and Libraries. Add the following libraries:


Security.Framework
SystemConfiguration.Framework
CoreLocation.Framework
Click on the “Build Settings” and scroll down to Linking. For the setting “Other Linker Flags” add: -ObjC “-all_load” (without the double quotes of course).
Add a new header file called constants.h to hold our OAM related constants:
constants.h:
#ifndef OAMLogin_constants_h#define OAMLogin_constants_h #define NUM_AUTH_RETRIES        3 #define OIC_URL @"http://oracle-access-server.company.com:14100" //your OIC REST URL#define OIC_SERVICE_DOMAIN  @"MobileServiceDomain"   //Defined during M&S configuration#define OIC_APP_NAME        @"OAMAuthTest"           //Defined during M&S configuration #endif


Modify the oamViewController h and m files as follows to implement OAM authentication.
oamViewController.h
#import
#import "IDMMobileSDK.h" @interface oamViewController : UIViewController
{            bool isAuthenticated;    OMMobileSecurityService *_mobileServices;    NSString *username;} @property (strong, nonatomic) IBOutlet UILabel *welcomeLabel;@property (strong, nonatomic) IBOutlet UIButton *authenticatedSegue;@property (nonatomic,assign) bool isAuthenticated;@property (nonatomic,retain) OMMobileSecurityService *mobileServices;
@end

 oamViewController.m
#import "oamViewController.h"  
#import "constants.h"
@interface oamViewController ()@end @implementation oamViewController @synthesize isAuthenticated; - (void)viewDidLoad{    [super viewDidLoad];    [self connectToOICServerAndSetup];    if(!isAuthenticated)    {        self.navigationItem.prompt = nil;        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                        initWithTitle:@"Login"                                        style:UIBarButtonItemStyleBordered                                        target:self                                        action:@selector(doLogin:)];        [self.navigationItem setRightBarButtonItem:rightButton                                          animated:YES];        self.authenticatedSegue.hidden = YES;    }    else    {        self.navigationItem.prompt = nil;        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                        initWithTitle:@"Logout"                                        style:UIBarButtonItemStyleBordered                                        target:self                                        action:@selector(doLogout:)];        [self.navigationItem setRightBarButtonItem:rightButton                                          animated:YES];        self.authenticatedSegue.hidden = NO;    }} - (IBAction)doLogin:(id)object{    [self doLogin];} - (void) doLogin{    if (self.mobileServices.applicationProfile == nil)    {        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"                                                            message:@"OAM server settings is not available."                                                           delegate:nil                                                  cancelButtonTitle:@"OK"                                                  otherButtonTitles:nil];        [alertView show];        [self performSegueWithIdentifier:@"ShowError" sender:self];        return;    }    NSError *error = nil;    error = [self.mobileServices startAuthenticationProcess:nil                                    presenterViewController:self]; } - (IBAction)doLogout:(id)object{    OMAuthenticationContext *ac = [self.mobileServices authenticationContext:TRUE];    if (ac != nil)    {        [self.mobileServices logout:FALSE];    }  
    [self didFinishLogout];} - (void)didFinishLogout{      isAuthenticated = NO;      self.navigationItem.prompt = nil;    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                    initWithTitle:@"Login"                                    style:UIBarButtonItemStyleBordered                                    target:self                                    action:@selector(doLogin:)];      [self.navigationItem setRightBarButtonItem:rightButton                                      animated:YES];    [self.welcomeLabel setText:[NSString stringWithFormat:@"Welcome Guest"]];    self.authenticatedSegue.hidden = YES;} - (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}  //OAM Related Stuff- (void)connectToOICServerAndSetup{    self.mobileServices = nil;  
    NSMutableDictionary *sdkProps = [[NSMutableDictionary alloc] init];    [sdkProps setObject:OM_PROP_AUTHSERVER_OAMMS forKey:OM_PROP_AUTHSERVER_TYPE];    [sdkProps setObject:OIC_URL forKey:OM_PROP_OAMMS_URL];    [sdkProps setObject:OIC_APP_NAME forKey:OM_PROP_APPNAME];    [sdkProps setObject:OIC_SERVICE_DOMAIN forKey:OM_PROP_OAMMS_SERVICE_DOMAIN];  
    OMMobileSecurityService *mss = [[OMMobileSecurityService alloc]                                    initWithProperties:sdkProps                                    delegate:self];    self.mobileServices = mss;  
    UIActivityIndicatorView *av = [[UIActivityIndicatorView alloc]                                   initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:av];    [self.navigationItem setRightBarButtonItem:rightButton];    [av startAnimating];    [self.mobileServices setup];} - (void)didReceiveApplicationProfile:(NSDictionary *)applicationProfile                               error:(NSError *)error{    NSLog(@"Downloaded application profile: %@", applicationProfile);    if (error)    {        NSString *msg = [[NSString alloc] initWithFormat:@"%@-%d: %@",                         [error domain], [error code],                         [error localizedDescription]];        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Application Initialization Failed"                                                            message:msg                                                           delegate:self                                                  cancelButtonTitle:@"OK"                                                  otherButtonTitles:nil];        [alertView show];        [self performSegueWithIdentifier:@"ShowError" sender:self];    }    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                    initWithTitle:@"Login"                                    style:UIBarButtonItemStyleBordered                                    target:self                                    action:@selector(doLogin:)];      [self.navigationItem setRightBarButtonItem:rightButton                                      animated:YES];} - (void)didFinishAuthentication:(OMAuthenticationContext *)context                          error:(NSError *)error{    NSLog(@"Got Authenticated: %d: %@",error.code, error.description);    if (context == nil || error != nil)    {        NSString *msg = [[NSString alloc] initWithFormat:@"%@-%d: %@", [error domain],                         [error code], [error localizedDescription]];        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Authentication Status"                                                            message:msg                                                           delegate:self                                                  cancelButtonTitle:@"OK"                                                  otherButtonTitles:nil];        [alertView show];        return;    }  
    username = context.userName;    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Successfully Authenticated!"                                                        message:[NSString stringWithFormat:@"Username: %@", username]                                                       delegate:self                                              cancelButtonTitle:@"OK"                                              otherButtonTitles:nil];    [alertView show];    isAuthenticated =YES;    self.navigationItem.prompt = nil;    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]                                    initWithTitle:@"Logout"                                    style:UIBarButtonItemStyleBordered                                    target:self                                    action:@selector(doLogout:)];    [self.navigationItem setRightBarButtonItem:rightButton                                      animated:YES];    self.authenticatedSegue.hidden = NO;    [self.welcomeLabel setText:[NSString stringWithFormat:@"Welcome %@", username]];  
} @end 
That’s it. This little app demonstrates just the authentication feature of OAM Mobile and Social iOS SDK. This can be used as a starter app for building your corporate iOS app. In the next article I will show how to make calls to REST API services protected my OAM 11g webgate using the SDK.