forked from calebd/SimpleAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAuth.m
More file actions
133 lines (99 loc) · 3.75 KB
/
SimpleAuth.m
File metadata and controls
133 lines (99 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// SimpleAuth.m
// SimpleAuth
//
// Created by Caleb Davenport on 11/6/13.
// Copyright (c) 2013-2014 Byliner, Inc. All rights reserved.
//
#import "SimpleAuth.h"
#import "SimpleAuthProvider.h"
#import "SimpleAuthSingleSignOnProvider.h"
#import "NSObject+SimpleAuthAdditions.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
NSString * const SimpleAuthErrorDomain = @"SimpleAuthErrorDomain";
NSString * const SimpleAuthPresentInterfaceBlockKey = @"present_interface_block";
NSString * const SimpleAuthDismissInterfaceBlockKey = @"dismiss_interface_block";
NSString * const SimpleAuthRedirectURIKey = @"redirect_uri";
static SimpleAuthProvider *__currentProvider = nil;
static UIWindow *__window;
@implementation SimpleAuth
#pragma mark - NSObject
+ (void)initialize {
[self loadProviders];
}
#pragma mark - Public
+ (NSMutableDictionary *)configuration {
static dispatch_once_t token;
static NSMutableDictionary *configuration;
dispatch_once(&token, ^{
configuration = [NSMutableDictionary new];
});
return configuration;
}
+ (UIWindow *)window
{
return [[[UIApplication sharedApplication] delegate] window];
}
+ (UIViewController *)presentedViewController
{
UIViewController *controller = self.window.rootViewController;
while (controller.presentedViewController) {
controller = controller.presentedViewController;
}
return controller;
}
+ (void)authorize:(NSString *)type completion:(SimpleAuthRequestHandler)completion {
[self authorize:type options:nil completion:completion];
}
+ (void)authorize:(NSString *)type options:(NSDictionary *)options completion:(SimpleAuthRequestHandler)completion {
// Load the provider class
Class klass = [self providers][type];
NSAssert(klass, @"There is no class registered to handle %@ requests.", type);
// Create options dictionary
NSDictionary *defaultOptions = [klass defaultOptions];
NSDictionary *registeredOptions = [self configuration][type];
NSMutableDictionary *resolvedOptions = [NSMutableDictionary new];
[resolvedOptions addEntriesFromDictionary:defaultOptions];
[resolvedOptions addEntriesFromDictionary:registeredOptions];
[resolvedOptions addEntriesFromDictionary:options];
// Create the provider and run authorization
__currentProvider = [(SimpleAuthProvider *)[klass alloc] initWithOptions:resolvedOptions];
[__currentProvider authorizeWithCompletion:^(id responseObject, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(responseObject, error);
});
}];
}
+ (BOOL)handleCallback:(NSURL *)URL {
NSParameterAssert(URL != nil);
NSAssert(__currentProvider != nil, @"There is no provider waiting for single sign on callback.");
NSAssert([__currentProvider conformsToProtocol:@protocol(SimpleAuthSingleSignOnProvider)], @"The current provider does not handle single sign on.");
return [(id<SimpleAuthSingleSignOnProvider>)__currentProvider handleCallback:URL];
}
#pragma mark - Internal
+ (void)registerProviderClass:(Class)klass {
NSMutableDictionary *providers = [self providers];
NSString *type = [klass type];
if (providers[type]) {
NSLog(@"[SimpleAuth] Warning: multiple attempts to register provider for type: %@", type);
return;
}
if (type) {
providers[type] = klass;
}
}
#pragma mark - Private
+ (NSMutableDictionary *)providers {
static dispatch_once_t token;
static NSMutableDictionary *providers;
dispatch_once(&token, ^{
providers = [NSMutableDictionary new];
});
return providers;
}
+ (void)loadProviders {
[SimpleAuthProvider SimpleAuth_enumerateSubclassesWithBlock:^(Class klass, BOOL *stop) {
[self registerProviderClass:klass];
}];
}
@end