FRANotificationHandler.m revision a0f7bc4425f8e8f863d60ccd0cd909d341bb4f9e
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell/*
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * The contents of this file are subject to the terms of the Common Development and
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * Distribution License (the License). You may not use this file except in compliance with the
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * License.
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell *
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * specific language governing permission and limitations under the License.
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell *
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * When distributing Covered Software, include this CDDL Header Notice in each file and include
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * Header, with the fields enclosed by brackets [] replaced by your own identifying
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * information: "Portions copyright [year] [name of copyright owner]".
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell *
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * Copyright 2016 ForgeRock AS.
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell */
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell#import "FRABlockAlertView.h"
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell#import "FRAIdentity.h"
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import "FRAIdentityModel.h"
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell#import "FRAPushMechanism.h"
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell#import "FRANotification.h"
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell#import "FRANotificationHandler.h"
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell#import "FRANotificationViewController.h"
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings#import "FRAMessageUtils.h"
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings#import <JWT.h>
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell/*!
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell * Private interface.
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell */
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell@interface FRANotificationHandler ()
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell/*!
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell * The identity model.
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell */
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell@property (nonatomic, strong, readonly) FRAIdentityModel *identityModel;
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell/*!
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell * The database to which notifications should be persisted.
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell */
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell@property (strong, nonatomic) FRAIdentityDatabase *database;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell@end
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell@implementation FRANotificationHandler
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbingsstatic NSString * const TTL_KEY = @"t";
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbingsstatic NSString * const MESSAGE_ID_KEY_PATH = @"aps.messageId";
a0f7bc4425f8e8f863d60ccd0cd909d341bb4f9eKen Stubbingsstatic NSString * const MESSAGE_DATA_PATH = @"aps.data";
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbingsstatic NSString * const CHALLENGE_KEY = @"c";
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbingsstatic NSString * const MECHANISM_UID_KEY = @"u";
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (instancetype)initWithDatabase:(FRAIdentityDatabase *)database identityModel:(FRAIdentityModel *)identityModel {
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell self = [super init];
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell if (self) {
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell _database = database;
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell _identityModel = identityModel;
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell }
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell return self;
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell}
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell+ (instancetype)handlerWithDatabase:(FRAIdentityDatabase *)database identityModel:(FRAIdentityModel *)identityModel {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell return [[FRANotificationHandler alloc] initWithDatabase:database identityModel:identityModel];
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell}
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)messageData {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell FRANotification *notification = [self notificationFromRemoteNotification:messageData];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell if (!notification || !notification.pending) {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // if the notification is nil then there was a problem looking up the mechanism
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // if the notification is not pending, then the notification timed out or was dealt with by opening the app
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // from the homescreen and navigating to the notification; either way, there's nothing further to do here
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell return;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell }
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell [self showNotification:notification ifForegroundApplication:application];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell}
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell- (FRANotification *)notificationFromRemoteNotification:(NSDictionary *)messageData {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings // Decode JWT from modessage
a0f7bc4425f8e8f863d60ccd0cd909d341bb4f9eKen Stubbings NSString* alertJwt = [messageData valueForKeyPath:MESSAGE_DATA_PATH];
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings NSDictionary *payload = [FRAMessageUtils extractJTWBodyFromString:alertJwt];
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings // makec mechanism from data
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings NSLog(@"message data: %@", payload);
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // Lookup push mechanism to which the notification should be added
1d464fc27913cfdeb6e09c8ab5d06ada64f94d55Ken Stubbings
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings FRAPushMechanism *mechanism = [self pushMechanismTargetForRemoteNotification:payload];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell if (!mechanism) {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell return nil;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell }
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // Try to look up an existing notification (in case this message has already been handled)
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings FRANotification *notification = [mechanism notificationWithMessageId:[messageData valueForKeyPath:MESSAGE_ID_KEY_PATH]];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell if (notification) {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell return notification;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell }
1d464fc27913cfdeb6e09c8ab5d06ada64f94d55Ken Stubbings
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // otherwise, create the notification from the message and add it to the mechanism
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings NSTimeInterval timeToLive = [[payload objectForKey:TTL_KEY] doubleValue];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell notification = [[FRANotification alloc] initWithDatabase:self.database
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings messageId:[messageData valueForKeyPath:MESSAGE_ID_KEY_PATH]
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings challenge:[payload objectForKey:CHALLENGE_KEY]
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell timeReceived:[NSDate date]
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell timeToLive:timeToLive];
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings // TODO: Handle error
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott @autoreleasepool {
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott NSError* error;
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [mechanism addNotification:notification error:&error];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott }
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell return notification;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell}
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell- (FRAPushMechanism *)pushMechanismTargetForRemoteNotification:(NSDictionary *)messageData {
a1e92b2783be4bfeb0c7e267223cc7779a6f324cKen Stubbings NSString *mechanismId = [messageData objectForKey:MECHANISM_UID_KEY];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell FRAMechanism *mechanism = [self.identityModel mechanismWithId:mechanismId];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell if ([mechanism isKindOfClass:[FRAPushMechanism class]]) {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell return (FRAPushMechanism *)mechanism;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell } else {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell return nil;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell }
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell}
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell- (void)showNotification:(FRANotification *)notification ifForegroundApplication:(UIApplication *)application {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // jump to the notification if the app was in the foreground when the notification arrived or has
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // been opened by clicking on the notification
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell void (^showNotification)() = ^{
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell UIViewController *rootViewController = application.delegate.window.rootViewController;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell UIStoryboard *storyboard = rootViewController.storyboard;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell FRANotificationViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:FRANotificationViewControllerStoryboardIdentifer];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell viewController.notification = notification;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell [rootViewController presentViewController:viewController animated:YES completion:NULL];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell };
1d464fc27913cfdeb6e09c8ab5d06ada64f94d55Ken Stubbings
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell switch (application.applicationState) {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell case UIApplicationStateBackground: {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // don't show notification if app is running in background
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell }
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell break;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell case UIApplicationStateActive: {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // the notification arrived while the app was in the foreground
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell FRABlockAlertView *alertView = [[FRABlockAlertView alloc] initWithTitle:@"Authentication Request Received"
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell message:nil
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell delegate:nil
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell cancelButtonTitle:nil
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell otherButtonTitles:@"OK", nil];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell alertView.callback = ^(NSInteger offset) {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell const NSInteger okButton = 0;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell if (offset == okButton) {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell showNotification();
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell }
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell };
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell [alertView show];
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell }
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell break;
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell case UIApplicationStateInactive: {
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell // the application is being opened from the notification
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell showNotification();
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell }
916ef74523ecddc8140815c084ab6971ee366bcfCraig McDonnell break;
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell }
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell}
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell
6c1420dd55f69d09f39dd213ee6c97ba901b8d92Craig McDonnell@end