Coder Social home page Coder Social logo

mcsmkeychainitem's Introduction

MCSMKeychainItem

MCSMGenericKeychainItem

MCSMKeychainItem (or more specifically MCSMGenericKeychainItem) allows you to create, fetch and remove Keychain Items from the Keychain on iOS and OS X using the same simple Objective-C API.

Generic Keychain Item allows you to store an Account (typically a username) and Password for a given Service. Doing this with MCSMGenericKeychainItem is easy:

// First Check to see if a Generic Keychain Item for the Service and Username already exists

MCSMGenericKeychainItem *genericKeychainItem = nil;

genericKeychainItem = [MCSMGenericKeychainItem genericKeychainItemForService:service
                                                                     account:account 
                                                                  attributes:attributes
                                                                       error:NULL];

// If a Generic Keychain Item already exists remove it to prevent duplicates
if(genericKeychainItem)
{
  [genericKeychainItem removeFromKeychainWithError:NULL];
}

//Add the new Generic Keychain Item                             
[MCSMGenericKeychainItem genericKeychainItemWithService:service 
                                                account:account 
                                             attributes:attributes 
                                               password:password
                                                  error:NULL];

When you want to fetch the Generic Keychain Item for a Service and Account you need to do:

MCSMGenericKeychainItem *genericKeychainItem = [MCSMGenericKeychainItem genericKeychainItemForService:service 
                                                                                              account:account 
                                                                                           attributes:attributes
                                                                                                error:NULL];

You can then get the password using the password property:

NSString *password = genericKeychainItem.password;

And to remove it from the Keychain just call removeFromKeychainWithError:

[genericKeychainItem removeFromKeychainWithError:NULL];

MCSMInternetKeychainItem

MCSMInternetKeychainItem allows you to create, fetch and remove Internet Keychain Items from the Keychain on iOS and OS X using the same simple Objective-C API.

Internet Keychain Item allows you to store a Server, Security Domain, Account (typically a username), Path, Port, Protocol, Authentication Type and Password.

The Protocols and Authentication Types are defined in Security/SecItem.h, the current list is:

Protocols

kSecAttrProtocolFTP
kSecAttrProtocolFTPAccount
kSecAttrProtocolHTTP
kSecAttrProtocolIRC
kSecAttrProtocolNNTP
kSecAttrProtocolPOP3
kSecAttrProtocolSMTP
kSecAttrProtocolSOCKS
kSecAttrProtocolIMAP
kSecAttrProtocolLDAP
kSecAttrProtocolAppleTalk
kSecAttrProtocolAFP
kSecAttrProtocolTelnet
kSecAttrProtocolSSH
kSecAttrProtocolFTPS
kSecAttrProtocolHTTPS
kSecAttrProtocolHTTPProxy
kSecAttrProtocolHTTPSProxy
kSecAttrProtocolFTPProxy
kSecAttrProtocolSMB
kSecAttrProtocolRTSP
kSecAttrProtocolRTSPProxy
kSecAttrProtocolDAAP
kSecAttrProtocolEPPC
kSecAttrProtocolIPP
kSecAttrProtocolNNTPS
kSecAttrProtocolLDAPS
kSecAttrProtocolTelnetS
kSecAttrProtocolIMAPS
kSecAttrProtocolIRCS
kSecAttrProtocolPOP3S

Authentication Types

kSecAttrAuthenticationTypeNTLM
kSecAttrAuthenticationTypeMSN
kSecAttrAuthenticationTypeDPA
kSecAttrAuthenticationTypeRPA
kSecAttrAuthenticationTypeHTTPBasic
kSecAttrAuthenticationTypeHTTPDigest
kSecAttrAuthenticationTypeHTMLForm
kSecAttrAuthenticationTypeDefault

The recommended way to create a MCSMInternetKeychainItem is:

// Fetch existing Keychain Item
MCSMInternetKeychainItem *internetKeychainItem = nil;

internetKeychainItem = [MCSMInternetKeychainItem internetKeychainItemForServer:server
                                                                securityDomain:securityDomain
                                                                       account:account
                                                                          path:path
                                                                          port:port
                                                                      protocol:protocol
                                                            authenticationType:authenticationType
                                                                    attributes:attributes
                                                                         error:NULL];

// If a Internet Keychain Item already exists remove it to prevent duplicates
if(internetKeychainItem)
{
  [internetKeychainItem removeFromKeychainWithError:NULL];
}

//Add the new Internet Keychain Item                             
[MCSMInternetKeychainItem internetKeychainItemForServer:server
                                         securityDomain:securityDomain
                                                account:account
                                                   path:path
                                                   port:port
                                               protocol:protocol
                                     authenticationType:authenticationType
                                             attributes:attributes
                                               password:password
                                                  error:NULL];

When you want to fetch the Internet Keychain Item you need to do:

MCSMInternetKeychainItem *internetKeychainItem = nil;

internetKeychainItem = [MCSMInternetKeychainItem internetKeychainItemForServer:server
                                                                securityDomain:securityDomain
                                                                       account:account
                                                                          path:path
                                                                          port:port
                                                                      protocol:protocol
                                                            authenticationType:authenticationType
                                                                    attributes:attributes
                                                                         error:NULL];

You can then get the password using the password property:

NSString *password = internetKeychainItem.password;

And to remove it from the Keychain just call removeFromKeychainWithError:

[internetKeychainItem removeFromKeychainWithError:NULL];

MCSMApplicationUUIDKeychainItem

Apple have deprecated the unique identifier API that returns the UDID for a device in iOS 5.0.

You can however generate a UUID using the CoreFoundation's CFUUIDCreateString API

The important thing to realize about CFUUIDCreateString is that (by design) it generates a different UUID everytime you call it. This means that you cannot uniquely identify a device by simply replacing [UIDevice uniqueIdentifier] with CFUUIDCreateString.

MCSMApplicationUUIDKeychainItem's approach is to generate a UUID on first call of the API and then store the UUID into the device's keychain. This means that the UUID will not change between launches of the application and storing it in the keychain means it persists between reinstalls of the application. However the UUID will be deleted if the user restores their device, along with every other keychain item.

If all you need is a replacement for UIDevice's uniqueIdentifier API, then all you need to do replace it with MCSMApplicationUUIDKeychainItem's applicationUUID API.

NSString *UUID = [MCSMApplicationUUIDKeychainItem applicationUUID];

The first time applicationUUID is called it with generate a UUID and store it in the keychain. Subsequent calls will return the previously generated UUID.

If you need more control you can retrieve the Application UUID Keychain Item using:

MCSMApplicationUUIDKeychainItem *applicationUUIDKeychainItem = [MCSMApplicationUUIDKeychainItem applicationUUIDKeychainItem];

Then you can get the UUID using the UUID property:

NSString *UUID = applicationUUIDKeychainItem.UUID

If one doesn't exist you can generate one:

MCSMApplicationUUIDKeychainItem *applicationUUIDKeychainItem = [MCSMApplicationUUIDKeychainItem generateApplicationUUIDKeychainItem];

And you can remove it using removeFromKeychainWithError:

[applicationUUIDKeychainItem removeFromKeychainWithError:NULL];

mcsmkeychainitem's People

Contributors

dlinsin avatar objcolumnist avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mcsmkeychainitem's Issues

The 1.1 pod spec has not been published to Cocoapods

We are currently using the older 1.0.2 version of MCSMKeychainItem. Xcode 6 is complaining about some ARC related issues. It appears that you have resolved these issued with the recent 1.1 changes. Could you please publish the 1.1 pod spec and add a 1.1 tag so we can take advantage of these changes?

Error stored

I cant store my item

NSString *serviceAccount = @"My.account";
NSString *account = @"[email protected]";
NSDictionary *attributes = [NSDictionary dictionaryWithObject:@"Myattr" forKey:@"nameAttrib"];
MCSMGenericKeychainItem *genericKeychainItemAccount = nil;
    genericKeychainItemAccount = [MCSMGenericKeychainItem
                  genericKeychainItemForService:serviceAccount                                                                                      
                  account:account                                                                          
                  attributes:attributes                                                                        
                  error:NULL];
    if(genericKeychainItemAccount)
    {
        [genericKeychainItemAccount removeFromKeychainWithError:NULL];
    }
    [MCSMGenericKeychainItem genericKeychainItemWithService:serviceAccount
                                                    account:account
                                                 attributes:attributes                                                 password:@"188823773"
                                                      error:NULL];

When i try debaging, i think so have error in there - https://github.com/ObjColumnist/MCSMKeychainItem/blob/master/MCSMKeychainItem.m#L332 returnStatus -50:

[query setObject:[password dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id<NSCopying>)(kSecValueData)];
    OSStatus returnStatus = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
    MCSMGenericKeychainItem *genericKeychainItem = nil;
    if (returnStatus)
    {
        if(error == NULL)
        {
            NSError *newError __autoreleasing = nil;
            error = &newError;
        }
        NSDictionary *userInfo = @{ MCSMKeychainItemQueryKey : query };
        *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:returnStatus userInfo:userInfo];
    }

It is error dump:

error   NSError **  error: summary string parsing error 0xb0114ad0
*error  NSError *   nil 0x00000000
[1] NSError *   nil 0x00000000
[2] NSError *   domain: <read memory from 0xffffffda failed (0 of 4 bytes read)> - code: <read memory from 0xffffffd6 failed (0 of 4 bytes read)>   0xffffffce
NSObject    NSObject        
_reserved   void *  NULL    
_code   NSInteger       
_domain NSString *  nil 
_userInfo   NSDictionary *  nil 
[3] __NSDictionaryM *   5 key/value pairs   0x08bb4dc0
[0] (null)  @"class" : @"genp"  
[1] (null)  @"nameAttrib" : @"Myattr"   
[2] (null)  @"v_Data" : 9 bytes 
[3] (null)  @"svce" : @"My.account" 
[4] (null)  @"acct" : @"[email protected]"  
[4] NSError *   domain: 5 key/value pairs - code: -50   0xb0114ad0
NSObject    NSObject        
_reserved   void *  NULL    0x00000000
_code   NSInteger   -50 -50
_domain __NSDictionaryM *   5 key/value pairs   0x08bb4dc0
[0] (null)  @"class" : @"genp"  
[1] (null)  @"nameAttrib" : @"Myattr"   
[2] (null)  @"v_Data" : 9 bytes 
[3] (null)  @"svce" : @"My.account" 
[4] (null)  @"acct" : @"[email protected]"  
_userInfo   NSDictionary *  0xb0114ad0  0xb0114ad0
query   __NSDictionaryM *   5 key/value pairs   0x08bb4dc0
[0] (null)  @"class" : @"genp"  
[1] (null)  @"nameAttrib" : @"Myattr"   
[2] (null)  @"v_Data" : 9 bytes 
[3] (null)  @"svce" : @"My.account" 
[4] (null)  @"acct" : @"[email protected]"  
returnStatus    OSStatus    -50 -50
userInfo    __NSDictionaryI *   1 key/value pair    0x08a32840
[0] (null)  @"MCSMKeychainItemQueryKey" : 5 key/value pairs 
key __NSCFConstantString *  @"MCSMKeychainItemQueryKey" 0x0001b120
[0] id      
value   __NSDictionaryM *   5 key/value pairs   0x08bb4dc0
[0] (null)  @"class" : @"genp"  
[1] (null)  @"nameAttrib" : @"Myattr"   
[2] (null)  @"v_Data" : 9 bytes 
[3] (null)  @"svce" : @"My.account" 
[4] (null)  @"acct" : @"[email protected]"  
MCSMKeychainItemQueryKey    NSString *const @"MCSMKeychainItemQueryKey" 0x0001b120
NSObject    NSObject        
isa Class   __NSCFConstantString    0x019e1350

And i can not store to keychain
Is you help me?

Q: How to store token

I want use token-based authentication,
please, help me, how i can store auth token?
So, i want store:

  • service
  • account
  • password
  • token

Tell me way to store this
Thank you!

Same Unique Id for 2 different iPads

Hi,
i have an issue where [MCSMApplicationUUIDKeychainItem applicationUUID] give me same Unique Id for 2 different iPads. Can anybody tell me in which condition i can get same Unique Id for different iPads? It is urgent if anybody also get this issue, please share your thoughts with me.

Thanks

Does not compile on iOS

The code contains several pre compiler directives like this:

#if TARGET_OS_MAC  && !TARGET_IPHONE_SIMULATOR

However, on an iOS device, TARGET_OS_MAC is 1 and TARGET_IPHONE_SIMULATOR is 0. This allows it to pass the test and the Mac code is included, causing a compiler error. This can be fixed by adding the check !TARGET_OS_IPHONE. See this Stack Overflow question for more details: http://stackoverflow.com/questions/4798205/can-preprocessor-directives-be-used-to-import-different-header-files-for-mac-and.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.