OS X Keychain integration Keychain main.cpp

From CDOT Wiki
Revision as of 21:06, 8 February 2007 by Pcvitori (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
//Authors: Mohammad Tirtashi mo3b.com
//		   Phillip Vitorino philv.com
//Last changed: December 9th 2006
//main.cpp

#include "keychain.h"
// this is just a main to test the keychain class
int main () {

	int rcret = 0; //stores the return value when we a record is retrieved. "-25300" = no such item exists, "0" = a record already exists.
	int rcsav = 0; //0 = entry saved. anything else = uh oh!.
	int rcchg = 0; //0 = change successful.  anything else = huston we have a problem.
	SecKeychainItemRef itemRef = NULL; // an instance of a keychain item.
	char website[11] = "google.com"; // the domain attribute in the keychain item.
	website[10] = '\0';

	char account[4] = "moe"; // the username attribute.
	account[3] = '\0';
	
	
	void *retpassword = NULL; //stores the retrieved password from the keychain item
	UInt32 retpasswordlen = NULL;
	const void *savpassword = "1337"; //password to be saved 
	const void *savpassword2 = "pstdeni";
	
	char *tempretpass = NULL;
	
	keychain *test = keychain::getInstance(); //gets an instance of the keychain object.
	
	// attemps to retrieve the password for the first time. if an account exists then the password is retrieved.
    rcret = test->RetrieveInternetPassword(NULL,10,website,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&retpasswordlen,&retpassword,&itemRef);
	printf("Account exists: %d", rcret);
	tempretpass = new char[retpasswordlen];
	tempretpass = (char *)retpassword;
	printf("\nPassword is now: %s", retpassword);
	// if the account did not exist.
	if (rcret != 0) {
			// first we attemp to add a new keychain item.
			rcsav = test->AddInternetPassword(NULL,10,website, NULL, NULL, 3, account, NULL, NULL , 20, kSecProtocolTypeFTP, 1,4, savpassword, NULL);
			printf("\nAccount added: %d", rcsav);
			// if saving the keychain item was successful
			if (rcsav == 0){
				// retrieving password from the keychain item we just added
				rcret = test->RetrieveInternetPassword(NULL,10,website,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&retpasswordlen,&retpassword,&itemRef);
				printf("\nAccount exists: %d", rcret);
				
				// changing the password
				tempretpass = new char[retpasswordlen];
				tempretpass = (char *)retpassword;
				printf("\nPassword is now: %s", retpassword);
				// updating the keychain item with the new password
				rcchg = test->ChangeInternetPassword(itemRef, 3,account,7,savpassword2);
				printf("\nPassword changed: %d", rcret);
				
				// retrieving the password again to see if it changed.
				rcret = test->RetrieveInternetPassword(NULL,10,website,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&retpasswordlen,&retpassword,&itemRef);
				
				tempretpass = new char[retpasswordlen];
				tempretpass = (char *)retpassword;
				printf("\nPassword is now: %s", retpassword);
				
			}else{
				printf("\nFailed to add account: %d", rcsav);
			}
	}else{
		printf("\nAccount existed or : %d", rcret);
	}
	
	//SecKeychainItemFreeContent(NULL, savpassword);
	if (itemRef) CFRelease(itemRef);
	delete tempretpass;
	return 0;
 }