]> git.saurik.com Git - apple/security.git/blob - SOSCCAuthPlugin/SOSCCAuthPlugin.m
Security-57336.1.9.tar.gz
[apple/security.git] / SOSCCAuthPlugin / SOSCCAuthPlugin.m
1 //
2 // SOSCCAuthPlugin.m
3 // Security
4 //
5 // Created by Christian Schmidt on 7/8/15.
6 // Copyright 2015 Apple, Inc. All rights reserved.
7 //
8
9 #import <SOSCCAuthPlugin.h>
10 #import <Foundation/Foundation.h>
11 #import <Accounts/Accounts.h>
12 #import <Accounts/Accounts_Private.h>
13 #import <Accounts/ACLogging.h>
14 #import <AccountsDaemon/ACDAccountStore.h>
15 #import <AppleAccount/ACAccount+AppleAccount.h>
16 #import <AppleAccount/ACAccountStore+AppleAccount.h>
17 #import <Security/SOSCloudCircle.h>
18 #include "utilities/SecCFRelease.h"
19
20
21 @implementation SOSCCAuthPlugin
22
23 - (void) didReceiveAuthenticationResponseParameters: (NSDictionary *) parameters
24 accountStore: (ACDAccountStore *) store
25 account: (ACAccount *) account
26 completion: (dispatch_block_t) completion
27 {
28 BOOL do_auth = NO;
29 ACLogNotice(@"parameters %@", parameters);
30 ACLogNotice(@"account %@", account);
31
32 if ([account.accountType.identifier isEqualToString:ACAccountTypeIdentifierIdentityServices]) {
33 ACAccount *icloud = [store aa_primaryAppleAccount];
34 NSString *dsid = [parameters[@"com.apple.private.ids"][@"service-data"][@"profile-id"] substringFromIndex:2]; // remove "D:" prefix
35 ACLogNotice(@"IDS account: iCloud %@ (personID %@)", icloud, icloud.aa_personID);
36 do_auth = icloud && icloud.aa_personID && [icloud.aa_personID isEqualToString:dsid];
37 } else if ([account.accountType.identifier isEqualToString:ACAccountTypeIdentifierAppleAccount]) {
38 ACLogNotice(@"AppleID account: primary %@", @([account aa_isPrimaryAccount]));
39 do_auth = [account aa_isPrimaryAccount];
40 }
41
42 ACLogNotice(@"do_auth %@", do_auth ? @"YES" : @"NO" );
43
44 if (do_auth) {
45 CFErrorRef authError = NULL;
46 NSString *rawPassword = [account _aa_rawPassword];
47
48 if (rawPassword != NULL) {
49 const char *password = [rawPassword cStringUsingEncoding:NSUTF8StringEncoding];
50 CFDataRef passwordData = CFDataCreate(kCFAllocatorDefault, (const uint8_t *) password, strlen(password));
51 if (passwordData) {
52 ACLogNotice(@"Performing SOS circle credential set for account %@: %@", account.identifier, account.username);
53 NSString *dsid = [account aa_personID];
54 if (!SOSCCSetUserCredentialsAndDSID((__bridge CFStringRef) account.username, passwordData, (__bridge CFStringRef) dsid, &authError)) {
55 ACLogError(@"Unable to set SOS circle credentials for account %@: %@", account.identifier, authError);
56 CFReleaseNull(authError);
57 }
58
59 CFRelease(passwordData);
60 }
61 } else {
62 if (!SOSCCCanAuthenticate(&authError)) {
63 ACLogError(@"Account %@ did not present a password and we could not authenticate the SOS circle: %@", account.identifier, authError);
64 CFReleaseNull(authError); // CFReleaseSafe?
65 }
66 }
67 } else {
68 ACLogNotice(@"NOT performing SOS circle credential set for account %@: %@", account.identifier, account.username);
69 }
70
71 completion();
72 }
73
74 @end