Introduction
In this tutorial, we will build a simple application to
demonstrate how to sign up a new user for a SmartVault
account. Because you only need client privileges to sign
up a user, we will only be using the client protocol.
Here’s What You’ll Need
To perform autonomous authentication, you’ll first need to generate a RSA key pair. Visit Generate a Key Pair to view the steps.
If you have not yet created a developer account or client id, click here to do so:
Create a Client ID. (For OAuth Support, select Autonomous since this is how we will be authenticating and copy and paste the contents of the public key you just created into the Public Key field.)
Installing the Library
We could form individual REST API calls and communicate with SmartVault that way, but why reinvent the wheel right? Our Java API wrapper library allows you to perform actions with SmartVault without writing individual REST API requests.
This wrapper library can be downloaded down below.
There is one important dependency you will need in order to use our library: protobuf-java 2.4.1.
For this tutorial, we will also make use of two other libraries: commons-io and bcprov-jdk16.
All of these extra libraries can be obtained using Maven. How you set them up will depend on the environment you are using. If you are using an IDE, check its documentation if you are unsure about how to do this.
Setting Up
Let’s create a new class called SmartVault to handle communication with the SmartVault API. We will need to include the SmartVault library.
import com.smartvault.proto.*; import com.smartvault.rest.*; public class SmartVault { . . . }
Opening the Connection
Let’s add an instance variables to our class to handle communication with SmartVault:
private ClientProtocol _protocol;
The protocol object created here will handle tasks on behalf of the client. We can use this to do a variety administrative tasks.
Let’s create a constructor to set up an initial connection with SmartVault. This will initialize the _protocol object and authenticate using the private key.
public SmartVault() throws Exception { String clientId = "JavaTutorial"; // Change this to your client ID String privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" + "MIIEowIBAAKCAQEAuyF+b+lfhw6kXpf6/F6P3YnU7WirZJnwIFwlC4weje+VI3JS\n" + "qXUFOmMMVAHTNcofsOnYp5E4UYufkXtR10HqnVGslpsSYS9XxKEK5dkWNXYSaqrf\n" + "Q+iuX0kwnysnyomGxTnOqM6Vva/d07IjWCWR1byLrmF1Lyp/JOc+hXPbLJqbwhlf\n" + "lWxMhbKoYeYjL3ZdrOlclRX4nttxAwjQZKVxkeEAA1PymldHumBY3OTeASWQMeAg\n" + "EDrOiXDEA6kMkDoKz58aK11Xi2FwmFKQEU5rOD0x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Z7HmNuuSAydHbwJk/4AUCC8RQTpDa12Wb9f38IC8oI/m8+b2HBOoo\n" + "DmvkVblFCbHuLvnNzYAGrh0/6gj4h80po6azhXwlG8yJVbK1XJXGsXQYeMfW3DgW\n" + "m6PfnQKBgG2x8MQehdLQ0A9kGCmTlVu2oU0PTg21cHjCnVghXi6G79bsradlTFpD\n" + "kCUyj/DttTWSY8MoXL/4tk1uIGygRXFyNG+FCuIKq2UQwT4hLmVyYIPxEPLA3vJt\n" + "Q7SZvekEgOEP8Epz2Y/EtWEC7P6a3nzap5p06KMlLH2sn+IV9DPi\n" + "-----END RSA PRIVATE KEY-----\n"; Security.addProvider(new BouncyCastleProvider()); StringReader reader = new StringReader(privateKey); KeyPair keyPair = (KeyPair) new PEMReader(reader).readObject(); ServerCfg cfg = new ServerCfg("https://rest.smartvault.com", clientId, keyPair.getPrivate()); Authentication auth = new Authentication(cfg); _protocol = auth.createClientProtocol(); }
NOTE: You will need to change the Client ID to the one you registered earlier using the SmartVault Portal.
You should also never include the private key in your code like this in a production application, as it is a huge security risk. We are only doing this to simplify things for this basic example.
For production applications, you should take every step possible to ensure the security of your private key. For example, Java has its own key store designed for storing cryptographic keys. Windows also has key containers that you can use if you are running Windows. You should avoid storing the private key in plain-text, since this would allow anybody to have access to it.
Signing Up a User
Let’s create a method called signUpUser to handle creating a user account in SmartVault. This method can take in a strings containing the first name, last name, phone number, account name, vault name, and email of the user.
public void signUpUser(String firstName, String lastName, String phoneNumber, String accountName, String vaultName, String userEmail) throws Exception { . . . }
First let’s build a SingUpNewUserRequest in order to sign up the user. This will store the user’s information (name, phone number, etc.).
public void signUpUser(String firstName, String lastName, String phoneNumber, String accountName, String vaultName, String userEmail) throws Exception { System.out.println("Creating user..."); SignUpNewUserProto.SignUpNewUserRequest.Builder builder = SignUpNewUserProto.SignUpNewUserRequest.newBuilder(); builder.setFirstName(firstName); builder.setLastName(lastName); builder.setPhoneNumber(phoneNumber); builder.setAccountName(accountName); builder.setVaultName(vaultName); builder.setEmail(userEmail); // only set this to true if your client program has the ability to // validate the user has posession of this email account builder.setEmailValidated(true); _protocol.signUpNewUser(builder.build()); }
Next, we need to actually sign up the user by using a library call that takes in the request.
public void signUpUser(String firstName, String lastName, String phoneNumber, String accountName, String vaultName, String userEmail) throws Exception { System.out.println("Creating user..."); SignUpNewUserProto.SignUpNewUserRequest.Builder builder = SignUpNewUserProto.SignUpNewUserRequest.newBuilder(); builder.setFirstName(firstName); builder.setLastName(lastName); builder.setPhoneNumber(phoneNumber); builder.setAccountName(accountName); builder.setVaultName(vaultName); builder.setEmail(userEmail); // only set this to true if your client program has the ability to // validate the user has posession of this email account builder.setEmailValidated(true); _protocol.signUpNewUser(builder.build()); }
Trying It Out
Setting It Up
Now that we’ve got a nice wrapper class to handle some basic operations, let’s try it out! We’re going to make a simple console program that asks the user to input the user information into the console and then creates an account for that user in SmartVault.
Let’s start with a simple main method.
public static void main(String[] args) { . . . }
Opening the Connection
All we need to do is create an instance of our SmartVault class to open a connection.
public static void main(String[] args) { try { SmartVault smartVault = new SmartVault(); . . . } catch (Exception e) { e.printStackTrace(); } }
Signing Up the User
Before we can sign up the user, we need some of their information: the first name, last name, phone number, account name, vault name, and email. Let’s ask the user to input this information into the console.
public static void main(String[] args) { try { SmartVault smartVault = new SmartVault(); System.out.println("Sign Up User"); System.out.println("First Name:"); String firstName = keyboard.nextLine(); System.out.println("Last Name:"); String lastName = keyboard.nextLine(); System.out.println("Phone Number:"); String phoneNumber = keyboard.nextLine(); System.out.println("Account Name:"); String accountName = keyboard.nextLine(); System.out.println("Vault Name:"); String vaultName = keyboard.nextLine(); System.out.println("User Email:"); String email = keyboard.nextLine(); . . . } catch (Exception e) { e.printStackTrace(); } }
Finally, we can actually sign the user up using the signUpUser method we created earlier in SmartVault.cs. Let’s do this in the main method.
public static void main(String[] args) { try { SmartVault smartVault = new SmartVault(); System.out.println("Sign Up User"); System.out.println("First Name:"); String firstName = keyboard.nextLine(); System.out.println("Last Name:"); String lastName = keyboard.nextLine(); System.out.println("Phone Number:"); String phoneNumber = keyboard.nextLine(); System.out.println("Account Name:"); String accountName = keyboard.nextLine(); System.out.println("Vault Name:"); String vaultName = keyboard.nextLine(); System.out.println("User Email:"); String email = keyboard.nextLine(); smartVault.signUpUser(firstName, lastName, phoneNumber, accountName, vaultName, email); System.out.println("User account has been created."); } catch (Exception e) { e.printStackTrace(); } }
Finishing It Up
Since console applications generally quit after they finish, let’s prevent that by adding a quick prompt to have the user press the enter key to exit. This allows us to see the end of the program’s output easily if it is not launched from a command line window. (It is not necessary to do this if you do not want it.)
public static void main(String[] args) { try { SmartVault smartVault = new SmartVault(); System.out.println("Sign Up User"); System.out.println("First Name:"); String firstName = keyboard.nextLine(); System.out.println("Last Name:"); String lastName = keyboard.nextLine(); System.out.println("Phone Number:"); String phoneNumber = keyboard.nextLine(); System.out.println("Account Name:"); String accountName = keyboard.nextLine(); System.out.println("Vault Name:"); String vaultName = keyboard.nextLine(); System.out.println("User Email:"); String email = keyboard.nextLine(); smartVault.signUpUser(firstName, lastName, phoneNumber, accountName, vaultName, email); System.out.println("User account has been created."); } catch (Exception e) { e.printStackTrace(); } System.out.println("\nPress 'Enter' key to exit"); keyboard.nextLine(); }
Once you’ve signed up the user, they will receive an email with a link to validate their account.
Putting It Together
SmartVault.java
import com.smartvault.proto.*; import com.smartvault.rest.*; import java.io.StringReader; import java.security.KeyPair; import java.security.Security; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.openssl.PEMReader; public class SmartVault { private ClientProtocol _protocol; private DelegatedProtocol _delegation; public SmartVault() throws Exception { String clientId = "JavaTutorial"; // Change this to your client ID String privateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" + "MIIEogIBAAKCAQEA0+UlqixNqRoNrZhRj90MP14SavZkMPxFhJEN0p23fxIlOIY0\r\n" + "4SW3IO5iyT5PUsOtK6Gigp2zn3uEMqAVI2MwR9fXBI7iVuOThIDTt+DygBHHWc52\r\n" + "6Fcd5aU8lxt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Jts+OF42qbRM+wzBHqPfjZYNjlYWot2ER12yKxygTyXXFCfauZLzZUn3yTny5h1\r\n" + "mNdvfujfkTawbIOi7lpF2wItM4/CLATTUj0KrjsiibUx251t+K9T4X29cICDV0NO\r\n" + "qRDg3YAuP5I9ng64wrbpSQKBgEb9aL7doWKNgZrb4Vjy+CRYq4u18KvSUcpf/qv/\r\n" + "6InYQ/CMMQVExNknvesE9e2ymIcgJRY8kfaA+R/VBEd5ixcQBAMg7HqbEIO+dgcV\r\n" + "U9brdRvhXIzMMVdSJo10a/s7eOGR8mxPsmSPCee0Pt6omik8gykN+eEwTUz22aqo\r\n" + "jWH5AoGAYWd+y1wcI4ZkqdnoGbFapo2BjeZmFEV5KUhXWW7eJCmh6iD6hWr5b1ZR\r\n" + "6aQQyUNITq40JvIkTpgNO7bJ49+yL0SbCQXiJLoH2nvLK7sGLYOq0GQxD2sn3eLW\r\n" + "/qKTFd4LAK6dCMklQzr1Ev9cWMFm0DLUdOnKNv6zvme67Gs9xPE=\r\n" + "-----END RSA PRIVATE KEY-----\r\n"; Security.addProvider(new BouncyCastleProvider()); StringReader reader = new StringReader(privateKey); KeyPair keyPair = (KeyPair) new PEMReader(reader).readObject(); ServerCfg cfg = new ServerCfg("https://rest.smartvault.com", clientId, keyPair.getPrivate()); Authentication auth = new Authentication(cfg); _protocol = auth.createClientProtocol(); } public void signUpUser(String firstName, String lastName, String phoneNumber, String accountName, String vaultName, String userEmail) throws Exception { System.out.println("Creating user..."); SignUpNewUserProto.SignUpNewUserRequest.Builder builder = SignUpNewUserProto.SignUpNewUserRequest.newBuilder(); builder.setFirstName(firstName); builder.setLastName(lastName); builder.setPhoneNumber(phoneNumber); builder.setAccountName(accountName); builder.setVaultName(vaultName); builder.setEmail(userEmail); // only set this to true if your client program has the ability to // validate the user has posession of this email account builder.setEmailValidated(true); _protocol.signUpNewUser(builder.build()); } }
Program.java
import java.awt.*; import java.net.URI; import java.util.Scanner; public class Program { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { try { SmartVault smartVault = new SmartVault(); System.out.println("Sign Up User"); System.out.println("First Name:"); String firstName = keyboard.nextLine(); System.out.println("Last Name:"); String lastName = keyboard.nextLine(); System.out.println("Phone Number:"); String phoneNumber = keyboard.nextLine(); System.out.println("Account Name:"); String accountName = keyboard.nextLine(); System.out.println("Vault Name:"); String vaultName = keyboard.nextLine(); System.out.println("User Email:"); String email = keyboard.nextLine(); smartVault.signUpUser(firstName, lastName, phoneNumber, accountName, vaultName, email); System.out.println("User account has been created."); } catch (Exception e) { e.printStackTrace(); } System.out.println("\nPress 'Enter' key to exit"); keyboard.nextLine(); } }
Download HelloWorldSVRest-SignUpUser.zip.
Here’s What You’ll Need
To perform autonomous authentication, you’ll first need to generate a RSA key pair. Visit Generate a Key Pair to view the steps.
If you have not yet created a developer account or client ID, click here to do so:
Create a client ID. (For OAuth Support, select Autonomous since this is how we will be authenticating and copy and paste the contents of the public key you just created into the Public Key field.)
Setting Up
Let’s begin by creating some global variables that we will be using throughout this tutorial.
static $client_id = "PHPTutorial"; static $endpoint = "https://rest.smartvault.com"; static $email= "phpexample@smartvault.com"; $private_key = "qqwkHMTo+/gFeUtafHP3bRBAztIz6wmiLXaSV/ WhmZZoUGJqDXOnZwIdWDIs39BeJU5wqUu7yJzFn/JoZs4qH3YhSP8auPtbnu eNnYU8sLA0pkEd14PRZIAQeoE5USEtNGV3t3L6uaTC0NYemWnqcGHXsJ8qLX 9DYw50LCT/JlO19t4NRcnJNxwkbBDZ0lQZt3gE/xY1Ut3LmE9UHOV6usU8Ou /NriOOhWHCS+yaN+JeefXkq9jZWYPhr53ZTtNd3j39CSL+fbXJ2dDrsx2NJe RJ2lm8OBw+F14L3a30I/GA5gobZ/ny+K4qtCpvhE+GuhM6sEJ+XgguHleP8T CnZQ==AQAB y8lNtyFth443JHdl 5nr/Vt2U6H1GoSEqExpTTM/eghJ/UZBo0Uu59NzCmrSCDIHbars9S8fu3ud4 YRfJdYHjWurfY7oC+VfdQL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isUNgc2vJKpSpfYwoxzpWFXkoQ== ";
NOTE: You will need to change the client_id and email to the ones you used to register on the SmartVault Portal. In addition, change private_key to the private key you generated earlier.
Also note that it is not recommended to provide the private key this way, as it is a huge security risk. This implementation is purely for example purposes and production code should take great care to protect this private key. For example, Java has its own key store designed for storing cryptographic keys. Windows also has key containers that you can use if you are running Windows. You should avoid storing the private key in plain-text, since this would allow anybody to have access to it.
Authenticating
Requesting a Nonce
A SmartVault nonce will be used to begin the authentication process with the SmartVault service.
To request a nonce from SmartVault, we will setup an HttpWebRequest that interacts with this URI: http://rest.smartvault.com/auto/auth/nonce/1.
function request_nonce( &$callback ) { global $endpoint; $uri = $endpoint . "/auto/auth/nonce/1"; . . . }
Let’s build the message we will be sending and convert it to JSON format. All you will need for this request is your client Id.
function request_nonce( &$callback ) { global $endpoint; $uri = $endpoint . "/auto/auth/nonce/1"; global $client_id; $http_body = new http\Message\Body(); $http_body->append( json_encode( array( "client_id" => $client_id ) ) ); . . . }
We can now set up a connection with SmartVault. We’ll need to indicate the type of request (POST), the URI (http://rest.smartvault.com/auto/auth/nonce/1), the format of the request and the response (JSON), and the body of the message we are sending. After sending the request, SmartVault will respond by sending us a nonce.
function request_nonce( &$callback ) { global $endpoint; global $client_id; $uri = $endpoint . "/auto/auth/nonce/1"; $http_body = new http\Message\Body(); $http_body->append( json_encode( array( "client_id" => $client_id ) ) ); $headers = array( "accept" => "application/json" ); $http_client = new http\Client(); $http_request = new http\Client\Request( "POST", $uri, $headers, $http_body ); $http_request->setContentType( "application/json" ); $http_client->enqueue( $http_request, $callback ); return $http_client->send(); }
Creating a Self-Signed Token
Now that we have a nonce, we can construct a self-signed token, which will uniquely identify your application so that the SmartVault service can recognize it. The self-signed token will be created using the nonce you just received, your client ID, your private key, and a prefix (“SLF00”). It can later be exchanged for a client token that can log on as your client program.
function generate_self_token( &$nonce ) { global $client_id; global $private_key; $prefix = "SLF00"; . . . }
To construct the self-signed token, we will have to combine the prefix, the length of the client ID, the client ID, the length of the nonce, and the nonce and save this data in a blob.
function generate_self_token( &$nonce ) { global $client_id; global $private_key; $prefix = "SLF00"; $data = array(); $temp = unpack( "C*", $prefix ); $data = array_merge( $data, $temp ); $data[] = strlen( $client_id ); $temp = unpack( "C*", $client_id ); $data = array_merge( $data, $temp ); $data[] = strlen( $nonce ); $temp = unpack( "C*", $nonce ); $data = array_merge( $data, $temp ); print json_encode( $data ) . "\n"; $blob = join ( "", array_map( function ( &$i ) { return chr( $i ); }, $data ) ); . . . }
Next, let’s first use openssl_sign() to generate a digital signature and then do a base 64 encoding of the blob and signature combined. And voila! We just created our own self-encoded token.
function generate_self_token( &$nonce ) { global $client_id; global $private_key; $prefix = "SLF00"; $data = array(); $temp = unpack( "C*", $prefix ); $data = array_merge( $data, $temp ); $data[] = strlen( $client_id ); $temp = unpack( "C*", $client_id ); $data = array_merge( $data, $temp ); $data[] = strlen( $nonce ); $temp = unpack( "C*", $nonce ); $data = array_merge( $data, $temp ); print json_encode( $data ) . "\n"; $blob = join ( "", array_map( function ( &$i ) { return chr( $i ); }, $data ) ); $signature = ""; openssl_sign( $blob, $signature, $private_key, OPENSSL_ALGO_SHA256 ); print "blob: " . bin2hex( $blob ) . "\n"; print "signature: " . bin2hex( $signature ) . "\n"; $token = base64_encode( $blob . $signature ); print "token: " . $token . "\n"; return $token; }
Requesting a Client Token
Using your self-signed token, your application can now request a client token from the SmartVault service, which you can use to perform administrative tasks or to exchange for a delegation token.
function request_client_token( &$self_token, &$callback ) { global $endpoint; $uri = $endpoint . "/auto/auth/ctoken/1"; . . . }
Let’s create a request to for a client token. The body of the message we will be sending will only consist of our self-signed token. Let’s convert it to JSON format.
function request_client_token( &$self_token, &$callback ) { global $endpoint; $uri = $endpoint . "/auto/auth/ctoken/1"; $http_body = new http\Message\Body(); $http_body->append( json_encode( array( "token" => $self_token ) ) ); . . . }
Now we can begin the request process. Indicate the method type (POST), the URI (http://rest.smartvault.com/auto/auth/ctoken/1), the request and response formats (JSON), and the message body.
function request_client_token( &$self_token, &$callback ) { global $endpoint; $uri = $endpoint . "/auto/auth/ctoken/1"; $http_body = new http\Message\Body(); $http_body->append( json_encode( array( "token" => $self_token ) ) ); $headers = array( "accept" => "application/json" ); $http_client = new http\Client(); $http_request = new http\Client\Request("POST", $uri, $headers, $http_body); $http_request->setContentType( "application/json" ); . . . }
We will need to create a function that will read the body of the response, check to see if the request is a success, extract the token, and stat the user to make sure that the user holds a valid account. Finally, send the request.
function request_client_token( &$self_token, &$callback ) { global $endpoint; $uri = $endpoint . "/auto/auth/ctoken/1"; $http_body = new http\Message\Body(); $http_body->append( json_encode( array( "token" => $self_token ) ) ); $headers = array( "accept" => "application/json" ); $http_client = new http\Client(); $http_request = new http\Client\Request( "POST", $uri, $headers, $http_body ); $http_request->setContentType( "application/json" ); $action = function ( &$response ) use ( $callback ) { print $response->toString() . "\n"; $response_code = $response->getTransferInfo()->response_code; $body = $response->getBody(); $envelope = json_decode( $body ); if ( 200 == $response_code && true == $envelope->error->success ) { print "Do something with the client token\n"; $client_token = $envelope->message->token; stat_user( $client_token, $callback ); } }; $http_client->enqueue( $http_request, $action ); return $http_client->send(); }
Stat User
Let’s create the stat_user method we used after requesting a client token. Stat user is used to determine if a user account is activated and if they have authorized your application to act on their behalf. The response from the call provides a URL for the user to authorize your application if they have not.
In order to stat a user, we will need the client token, the endpoint, the client ID, and the email. The path of the requested resource will be http://rest.smartvault.com/auto/usr/phpexample@smartvault.com.
function stat_user( &$client_token, &$callback ) { global $endpoint; global $client_id; global $email; $uri = $endpoint . "/auto/usr/" . $email; . . . }
Set the method (GET), the URI we will be interacting with (http://rest.smartvault.com/auto/usr/phpexample@smartvault.com), the request and response formats (JSON), and the message body.
function stat_user( &$client_token, &$callback ) { global $endpoint; global $client_id; global $email; $uri = $endpoint . "/auto/usr/" . $email; $headers = array( "accept" => "application/json" ); $auth_basic = 1; $http_client = new http\Client(); $http_client->setOptions( array( "httpauthtype" => $auth_basic, "httpauth" => $client_id . ":" . $client_token ) ); $http_request = new http\Client\Request( "GET", $uri, $headers ); $http_request->setContentType( "application/json" ); . . . }
Now let’s create a function that will check if the user’s status. First check if the user has activated their account. If they have not, let the user know to activate their account. Next, check if the user has authorized your client ID. If they have not, extract the authorization URI that that SmartVault provided you in the response message and redirect them to this site so that they can sign in to SmartVault and authorize your application. If the user passes both of these, you are ready to call the method to request a delegation token.
function stat_user( &$client_token, &$callback ) { global $endpoint; global $client_id; global $email; $uri = $endpoint . "/auto/usr/" . $email; $headers = array( "accept" => "application/json" ); $auth_basic = 1; $http_client = new http\Client(); $http_client->setOptions( array( "httpauthtype" => $auth_basic, "httpauth" => $client_id . ":" . $client_token ) ); $http_request = new http\Client\Request( "GET", $uri, $headers ); $http_request->setContentType( "application/json" ); $action = function ( &$response ) use ( $client_token, $callback ) { print $response->toString() . "\n"; $response_code = $response->getTransferInfo()->response_code; $body = $response->getBody(); $envelope = json_decode( $body ); if ( 200 == $response_code && true == $envelope->error->success ) { print "User statted correctly\n"; if ( !$envelope->message->activated ) { print "User needs to activate their account"; } elseif ( !$envelope->message->authorized ) { print "User needs to authorize their account for your client:\n "; print $envelope->message->authorization_uri . "\n"; /* You can add a redirect_uri=some_url as a URL parameter to that link to send the user back to your page after they authorize. */ } else { request_delegation_token( $client_token, $callback ); } } }; $http_client->enqueue( $http_request, $action ); return $http_client->send(); }
Requesting a Delegation Token
We can now use the client token to request a delegation token. Delegation tokens are used to perform operations on behalf of a SmartVault user.
For this step, we will need the endpoint, the client ID, and the email. The path we will be interacting with will be the following: http://rest.smartvault.com/auto/auth/dtoken/1.
function request_delegation_token( &$client_token, &$callback ) { global $endpoint; global $client_id; global $email; $uri = $endpoint . "/auto/auth/dtoken/1"; . . . }
The body of the message we will be sending will consist of the user’s email. Let’s convert it to JSON format.
function request_delegation _token( &$client_token, &$callback ) { global $endpoint; global $client_id; global $email; $uri = $endpoint . "/auto/auth/dtoken/1"; $http_body = new http\Message\Body(); $http_body->append( json_encode( array( "user_email" => $email ) ) ); . . . }
Indicate the method (POST), the URI (http://rest.smartvault.com/auto/auth/nonce/1), the request and response formats (JSON), and the message body.
function request_delegation_token( &$client_token, &$callback ) { global $endpoint; global $client_id; global $email; $uri = $endpoint . "/auto/auth/dtoken/1"; $http_body = new http\Message\Body(); $http_body->append( json_encode( array( "user_email" => $email ) ) ); $headers = array( "accept" => "application/json" ); $auth_basic = 1; // pulled from docs $http_client = new http\Client(); $http_client->setOptions( array( "httpauthtype" => $auth_basic, "httpauth" => $client_id . ":" . $client_token ) ); print "Client token: " . $client_token . "\n"; $http_request = new http\Client\Request( "POST", $uri, $headers, $http_body ); $http_request->setContentType( "application/json" ); . . . }
Let’s create function to check if the delegation token request was a success. (A response code of 200 indicates that a request succeeded and that the requested resource has been returned in the message body.). Finally, let’s return the delegation token.
function request_delegation_token( &$client_token, &$callback ) { global $endpoint; global $client_id; global $email; $uri = $endpoint . "/auto/auth/dtoken/1"; $http_body = new http\Message\Body(); $http_body->append( json_encode( array( "user_email" => $email ) ) ); $headers = array( "accept" => "application/json" ); $auth_basic = 1; // pulled from docs $http_client = new http\Client(); $http_client->setOptions( array( "httpauthtype" => $auth_basic, "httpauth" => $client_id . ":" . $client_token ) ); print "Client token: " . $client_token . "\n"; $http_request = new http\Client\Request( "POST", $uri, $headers, $http_body ); $http_request->setContentType( "application/json" ); $action = function ( &$response ) use ( $callback ) { print $response->toString() . "\n"; $response_code = $response->getTransferInfo()->response_code; $body = $response->getBody(); $envelope = json_decode( $body ); if ( 200 == $response_code && true == $envelope->error->success ) { $callback( $envelope->message->token ); } }; $http_client->enqueue( $http_request, $action ); return $http_client->send(); }
Authenticating
It’s time to begin the SmartVault authentication process.
Let’s start by looking at the response from the nonce request. If the request was successful, get the nonce. Then call the function to generate a self-signed token, and lastly, request a client token.
function authenticate( &$callback ) { $nonce_callback = function ( &$response ) use ( $callback ) { print $response->toString() . "\n"; $response_code = $response->getTransferInfo()->response_code; $body = $response->getBody(); $envelope = json_decode( $body ); if ( 200 == $response_code && true == $envelope->error->success ) { $nonce = $envelope->message->code; $self_token = generate_self_token( $nonce ); request_client_token( $self_token, $callback ); } }; $http_client = request_nonce( $nonce_callback ); }
Putting It Together
Download AutonomousAuth-PHP.zip
Here’s What You’ll Need
If you have not yet created a developer account or client ID, visit Creating a Developer Account to view the steps on how to create them. (For OAuth Support, select Autonomous since this is how we will be authenticating.)
Installing the Library
We could form individual REST API calls and communicate with SmartVault that way, but why do this when this work has already been done for you? Our C# API wrapper library allows you to perform actions with SmartVault without writing individual REST API requests. This is available as a NuGet package called SmartVault.Rest and can be obtained from the standard nuget.org repository:
Now that we have the library installed, we can start writing some code.
Setting Up
Let’s create a new class called SmartVault to handle communication with the SmartVault API. First we’ll need to include the SmartVault library:
using SmartVault.Core; using SmartVault.Proto; using SmartVault.Rest; class SmartVault { ... }
Opening the Connection
Let’s add an instance variable to our class to handle communication with SmartVault:
private ClientProtocol _protocol;
The protocol object created here will handle tasks on behalf of the client. We can use this to perform administrative tasks.
Let’s create a constructor to set up an initial connection with SmartVault. This will initialize the _protocol object and authenticate using the private key.
public SmartVault() { string clientId = "CSharpTutorial"; // Change this to your client ID string privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" + "MIIEpAIBAAKCAQEAxmh2LZYY5SzT+BMmSNoGsjyI7O6fzmozR+mviyOiAIk0bBXa\n" + "U/kLGnaTYe4WE8DyQHweBQq1C5ovUA3L5rqKshoxmhrOtJ04YvMXmEaPBxxfxpLH\n" + "ogqg+7kkjDMs+Mb+EpYwguHKGXhFF80AWMZmfVVquTCuhG6bXUHFmdcVSg4imiE2\n" + "Ah75matYfD/X9aQecLC32gdgUAx7+6JflmQp+xAhr4s+LUORwCLT6v6znzpA89Qy\n" + "/8cc8FS3RrOZtxpjPcY9xK60uY94huPXjuW9Yj9nBgYnqE1hNPZGZCKcwxSInEtC\n" + "GjsAx+bvRiNYdNwtOZz6z6oAN/QX4mEQkE2wlQIDAQABAoIBAHkpD4n1EAzbXDIw\n" + "139DjTVI3ED706EEn345y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eDK+hzyGslVg\n" + "/kG5UwKBgQCG4U6kaT3BjF8Rr+S65JfPKEdZfO6pngva8ySMwB5xdzuX37nMqVZr\n" + "TGc654yj1FCa80UMwUo2ptUE0oCKh3VdZElinG9+wNpO/3nIS/b404A5EdF8QJ6w\n" + "1lHO1FsptGDOpb69WIjqoUzTiMfMEeOCNKlvFI2ZamzDdR6l39J8Cw==\n" + "-----END RSA PRIVATE KEY-----\n"; StringReader reader = new StringReader(privateKey); RSAKeyPair keyPair = (RSAKeyPair)new Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject(); ServerCfg cfg = new ServerCfg(clientId, keyPair.Private); Authentication auth = new Authentication(cfg); _protocol = auth.CreateClientProtocol(); }
NOTE: You will need to change the Client ID and the private key to the actual ones you registered earlier using the SmartVault Portal.
You should also never include the private key in your code like this in a production application, as it is a huge security risk. We are only doing this to simplify things for this basic example.
For production applications, you should take every step possible to ensure the security of your private key. For example, Java has its own key store designed for storing cryptographic keys. Windows also has key containers that you can use if you are running Windows. You should avoid storing the private key in plain-text, since this would allow anybody to have access to it.
Signing Up a User
Let’s create a method called SingUpUser to handle signing up a user. This method will take in strings containing the first name, last name, phone number, account name, vault name, and user email.
public void SignUpUser(string firstName, string lastName, string phoneNumber, string accountName, string vaultName, string userEmail) { . . . }
Now we need to build a SignUpNewUserRequest in order to sign up a user. This will contain the user’s information.
public void SignUpUser(string firstName, string lastName, string phoneNumber, string accountName, string vaultName, string userEmail) { SignUpNewUserRequest.Builder builder = SignUpNewUserRequest.CreateBuilder(); builder.FirstName = firstName; builder.LastName = lastName; builder.PhoneNumber = phoneNumber; builder.AccountName = accountName; builder.VaultName = vaultName; builder.CreateVault = true; builder.Email = userEmail; // only set this to true if your client program has the ability to // validate the user has possession of this email account builder.EmailValidated = true; ... }
Next, we need to actually sign up the user using a library call that takes in the request.
public void SignUpUser(string firstName, string lastName, string phoneNumber, string accountName, string vaultName, string userEmail) { SignUpNewUserRequest.Builder builder = SignUpNewUserRequest.CreateBuilder(); builder.FirstName = firstName; builder.LastName = lastName; builder.PhoneNumber = phoneNumber; builder.AccountName = accountName; builder.VaultName = vaultName; builder.CreateVault = true; builder.Email = userEmail; // only set this to true if your client program has the ability to // validate the user has possession of this email account builder.EmailValidated = true; _protocol.SignUpNewUser(builder.Build()); }
Trying It Out
Setting It Up
We’re now ready to test our SignUpUser method. Let’s begin with a simple Main method.
class Program { static void Main(string[] args) { . . . } }
Opening the Connection
All we need to do is create an instance of our SmartVault class to open a connection.
static void Main(string[] args) { try { SmartVault smartVault = new SmartVault(); . . . } catch (Exception e) { Console.WriteLine(e.Message); } }
Signing Up the User
Before we can sign a user up, we need the user’s information: first name, last name, phone number, account name, vault name, and email. Let’s ask the user to input this information into the console.
static void Main(string[] args) { try { SmartVault smartVault = new SmartVault(); Console.WriteLine("Sign Up User"); Console.WriteLine("First Name:"); string firstName = Console.ReadLine(); Console.WriteLine("Last Name:"); string lastName = Console.ReadLine(); Console.WriteLine("Phone Number:"); string phoneNumber = Console.ReadLine(); Console.WriteLine("Account Name:"); string accountName = Console.ReadLine(); Console.WriteLine("Vault Name:"); string vaultName = Console.ReadLine(); Console.WriteLine("User Email:"); string email = Console.ReadLine(); smartVault.SignUpUser(firstName, lastName, phoneNumber, accountName, vaultName, email); . . . } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } }
Finally, we can actually sign up the user using the SignUpUser method we created earlier. Let’s do this in the Main method.
static void Main(string[] args) { try { SmartVault smartVault = new SmartVault(); Console.WriteLine("Sign Up User"); Console.WriteLine("First Name:"); string firstName = Console.ReadLine(); Console.WriteLine("Last Name:"); string lastName = Console.ReadLine(); Console.WriteLine("Phone Number:"); string phoneNumber = Console.ReadLine(); Console.WriteLine("Account Name:"); string accountName = Console.ReadLine(); Console.WriteLine("Vault Name:"); string vaultName = Console.ReadLine(); Console.WriteLine("User Email:"); string email = Console.ReadLine(); smartVault.SignUpUser(firstName, lastName, phoneNumber, accountName, vaultName, email); Console.WriteLine("User has been created."); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } Console.WriteLine("\nPress any key to exit"); Console.ReadKey(); }
Finishing It Up
Since console applications generally quit after they finish, let’s prevent that by adding a quick prompt to have the user press the enter key to exit. This allows us to see the end of the program’s output easily if it is not launched from a command line window. It is not necessary to do this if you do not want it.
static void Main(string[] args) { try { SmartVault smartVault = new SmartVault(); Console.WriteLine("Sign Up User"); Console.WriteLine("First Name:"); string firstName = Console.ReadLine(); Console.WriteLine("Last Name:"); string lastName = Console.ReadLine(); Console.WriteLine("Phone Number:"); string phoneNumber = Console.ReadLine(); Console.WriteLine("Account Name:"); string accountName = Console.ReadLine(); Console.WriteLine("Vault Name:"); string vaultName = Console.ReadLine(); Console.WriteLine("User Email:"); string email = Console.ReadLine(); smartVault.SignUpUser(firstName, lastName, phoneNumber, accountName, vaultName, email); Console.WriteLine("User has been created."); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } Console.WriteLine("\nPress any key to exit"); Console.ReadKey(); }
Once you’ve signed up the user, they will receive an email with a link to validate their account.
Putting It Together
SmartVault.cs
using System; using SmartVault.Core; using SmartVault.Proto; using SmartVault.Rest; using System.IO; namespace HelloWorldSVRest { class SmartVault { private ClientProtocol _protocol; public SmartVault() { string clientId = "CSharpTutorial"; // Change this to your client ID string privateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" + "MIIEogIBAAKCAQEA0+UlqixNqRoNrZhRj90MP14SavZkMPxFhJEN0p23fxIlOIY0\r\n" + "4SW3IO5iyT5PUsOtK6Gigp2zn3uEMqAVI2MwR9fXBI7iVuOThIDTt+DygBHHWc52\r\n" + "6Fcd5aU8lxtg02SGJ5HZgKWI/8pHfQbd+pFPrEF50saVb/XPfN74OWx5pWsJbQz9\r\n" + "B8XodDswGzKmyqVeicPefSIn9qa2AkbLNhANbEgx+VE1p+L1X3r3z4sZ/Cks/5nr\r\n" + "2fO5AayhNzwTbN71s82M2Q5WgvxbM+ah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L7doWKNgZrb4Vjy+CRYq4u18KvSUcpf/qv/\r\n" + "6InYQ/CMMQVExNknvesE9e2ymIcgJRY8kfaA+R/VBEd5ixcQBAMg7HqbEIO+dgcV\r\n" + "U9brdRvhXIzMMVdSJo10a/s7eOGR8mxPsmSPCee0Pt6omik8gykN+eEwTUz22aqo\r\n" + "jWH5AoGAYWd+y1wcI4ZkqdnoGbFapo2BjeZmFEV5KUhXWW7eJCmh6iD6hWr5b1ZR\r\n" + "6aQQyUNITq40JvIkTpgNO7bJ49+yL0SbCQXiJLoH2nvLK7sGLYOq0GQxD2sn3eLW\r\n" + "/qKTFd4LAK6dCMklQzr1Ev9cWMFm0DLUdOnKNv6zvme67Gs9xPE=\r\n" + "-----END RSA PRIVATE KEY-----\r\n"; StringReader reader = new StringReader(privateKey); RSAKeyPair keyPair = RSAPrivateKey2.KeyPairFromPem(privateKey); ServerCfg cfg = new ServerCfg(clientId, keyPair.Private); Authentication auth = new Authentication(cfg); _protocol = auth.CreateClientProtocol(); } public void SignUpUser(string firstName, string lastName, string phoneNumber, string accountName, string vaultName, string userEmail) { Console.WriteLine("Creating user..."); SignUpNewUserRequest.Builder builder = SignUpNewUserRequest.CreateBuilder(); builder.FirstName = firstName; builder.LastName = lastName; builder.PhoneNumber = phoneNumber; builder.AccountName = accountName; builder.VaultName = vaultName; builder.CreateVault = true; builder.Email = userEmail; // only set this to true if your client program has the ability to // validate the user has posession of this email account builder.EmailValidated = true; _protocol.SignUpNewUser(builder.Build()); } } }
Program.cs
using System; using System.Diagnostics; using System.IO; namespace HelloWorldSVRest { class Program { static void Main(string[] args) { try { SmartVault smartVault = new SmartVault(); Console.WriteLine("Sign Up User"); Console.WriteLine("First Name:"); string firstName = Console.ReadLine(); Console.WriteLine("Last Name:"); string lastName = Console.ReadLine(); Console.WriteLine("Phone Number:"); string phoneNumber = Console.ReadLine(); Console.WriteLine("Account Name:"); string accountName = Console.ReadLine(); Console.WriteLine("Vault Name:"); string vaultName = Console.ReadLine(); Console.WriteLine("User Email:"); string email = Console.ReadLine(); smartVault.SignUpUser(firstName, lastName, phoneNumber, accountName, vaultName, email); Console.WriteLine("User has been created."); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } Console.WriteLine("\nPress any key to exit"); Console.ReadKey(); } } }
Leave A Comment?