Working with Sessions and Nodes

This chapter provides sample code that shows how to work with nodes. Opening a session, finding a node, and authenticating a user to the node are fundamental Open Directory tasks.

Listing a Session’s Registered Nodes

The sample code in Listing 2-1 demonstrates how to initialize a session object and retrieve a list of all registered nodes. Corresponding C API for this and all other Cocoa examples in this document can be found in Open Directory Reference.

Listing 2-1  Listing registered nodes

ODSession *mySession = [ODSession defaultSession];
NSError *err;
NSArray *nodeNames = [mySession nodeNamesAndReturnError:&err];

Finding a Node

Open Directory nodes can be found by type or by name. Listing 2-2 demonstrates how to find the authentication node of a session by its type.

Listing 2-2  Finding a node by type

ODNode *myNode = [ODNode nodeWithSession:mySession type:kODNodeTypeAuthentication error:&err];

The sample code in Listing 2-3 demonstrates how to find the node for a specific pathname.

Listing 2-3  Finding the node for a pathname

NSString *nodeName = @"/LDAPv3/127.0.0.1";
ODNode *myNode = [ODNode nodeWithSession:mySession name:nodeName error:&err];

Authenticating a User to a Node

To authenticate itself to the Open Directory for the purposes of reading, writing, or making changes to a node, an Open Directory client application calls one of the three following methods of an ODNode object: