This repository was archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathSimpleAuthSingleSignOnFacebookProvider.m
More file actions
115 lines (84 loc) · 3.42 KB
/
SimpleAuthSingleSignOnFacebookProvider.m
File metadata and controls
115 lines (84 loc) · 3.42 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
//
// SimpleAuthSingleSignOnFacebookProvider.m
// SimpleAuth
//
// Created by Julien Seren-Rosso on 10/02/2014.
// Copyright (c) 2014 Byliner, Inc. All rights reserved.
//
#import "SimpleAuthSingleSignOnFacebookProvider.h"
#import "SimpleAuth.h"
NSString *const SimpleAuthSingleSignOnFacebookProviderDomain = @"io.simpleauth.sso.facebook";
@interface SimpleAuthSingleSignOnFacebookProvider ()
@property (nonatomic, strong) SimpleAuthRequestHandler completion;
@end
@implementation SimpleAuthSingleSignOnFacebookProvider
#pragma mark - SimpleAuthProvider
+ (NSString *)type {
return @"facebook-sso";
}
+ (NSDictionary *)defaultOptions {
return @{
@"permissions" : @[ @"basic_info" ]
};
}
- (void)authorizeWithCompletion:(SimpleAuthRequestHandler)completion
{
self.completion = completion;
[FBSession openActiveSessionWithReadPermissions:self.options[@"permission"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
[self sessionStateChanged:session state:status error:error];
// Immediately close the session (if successfully opened) in order to retrieve a new token next time
if ([session isOpen]) {
[FBSession.activeSession closeAndClearTokenInformation];
}
}];
}
#pragma mark - Facebook SSO
- (BOOL)handleCallback:(NSURL *)url
{
return [FBSession.activeSession handleOpenURL:url];
}
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
// If the session was opened successfully
if (!error && state == FBSessionStateOpen) {
NSLog(@"Session opened");
// Retrieve the information from the sesssion
self.completion([self dictionaryWithFBSession:session], nil);
return;
}
if (state == FBSessionStateClosed) {
NSLog(@"Session closed");
// The session was closed, do nothing
return;
}
if (state == FBSessionStateClosedLoginFailed) {
NSLog(@"Login failed");
// Something wrong happened
NSError *error = [NSError errorWithDomain:SimpleAuthSingleSignOnFacebookProviderDomain
code:SimpleAuthSingleSignOnFacebookProviderLoginFailed
userInfo:@{ NSLocalizedDescriptionKey: @"Something went wrong, try again." }];
self.completion(nil, error);
return;
}
// Handle errors
if (error) {
NSLog(@"Facebook error");
NSError *ssoError = [NSError errorWithDomain:SimpleAuthSingleSignOnFacebookProviderDomain
code:SimpleAuthSingleSignOnFacebookProviderFacebookError
userInfo:@{
NSLocalizedDescriptionKey: @"Something went wrong, try again.",
NSUnderlyingErrorKey: error
}];
self.completion(nil, ssoError);
}
}
- (NSDictionary *)dictionaryWithFBSession:(FBSession *)session
{
NSMutableDictionary *dictionary = [NSMutableDictionary new];
// Provider
dictionary[@"provider"] = [[self class] type];
// Credentials
dictionary[@"credentials"] = @{ @"token" : session.accessTokenData.accessToken };
return dictionary;
}
@end