Authentication
How to authenticate to an atServer
C
1. Fetch your atServer's address from the production atDirectory
First, include atclient_utils.h
and #include <atclient/constants.h>
at the top of you program.
Next, initialize variables that will hold the host and port of our atServer. We will pass pointers to these variables to the function that will populate these values for us.
Lastly, use the atclient_utils_find_atserver_address
function to populate our atserver_host
and atserver_port
variables. This function returns an int
for error handling. A non-zero exit code indicates an error.
Don't forget to free the atserver_host
variable! It is mentioned in the documentation of the atclient_utils_find_atserver_address
function that it is the responsibility of the caller to free this variable.
2. Load your atSign's atKeys
First, include atkeys.h
.
Next, allocate memory for the atclient_atkeys
struct. In this scenario, we are statically allocating. We need to call the atclient_atkeys_init
function and pass a pointer to our atclient_atkeys
struct. You will find this to be a common pattern when working with C structs. This is our method of mimicking object-oriented methodology in our C SDK.
Finally, call the atclient_utils_populate_atkeys_from_homedir
function which will look for your ATSIGN
's keys in your $HOME/.atsign/keys/
directory.
For example, if my atSign was @alice
, I would have my @alice
keys set up such that this file exists: $HOME/.atsign/keys/@alice_key.atKeys.
This function returns an int
for error handling purposes. A non-zero exit code indicates that an error has occurred.
Don't forget to run atclient_atkeys_free
at the end of your application. You will see it as a common pattern that for every *_init
function that we call, we must have a corresponding *_free
function.
3. PKAM Authenticate
If your atSign's keys exist and your atSign's atServer is already activated, then this is known as pkam authentication.
First include atclient.h
at the top of your application.
Secondly, we will create our atclient
object. This variable holds data necessary for doing various operations on our atServer later on (such as CRUD or Events).
Thirdly, call the atclient_pkam_authenticate
function.
This function connects to the atServer (if necessary) and uses your atSign's atKeys file to generate a special pkam command by signing a challenge sent from the atServer. Once pkam authentication is successful, your connection will be authenticated and your client will be free to make various operations on the atServer.
This function will return an int
for error handling, in which a non-zero exit code indicates an error.
Lastly, do not forget to call atclient_free
at the end of your application.
Example Application
Here is an example application that authenticates my atSign @jeremy_0
.
Last updated