protected function generateApiKey()
    {
        // Load the hashing factory
        $factory = new CryptLib\Random\Factory;

        $meta = UserMetadata::model()->getPrototype('UserMetadata', array(
                'user_id' => $this->getUser()->id,
                'key' => 'api_key' . $this->app_name
            ), 
            array(
                'user_id' => $this->getUser()->id,
                'key' => 'api_key' . $this->app_name
        ));

        $meta->value   = $factory->getLowStrengthGenerator()->generateString(16);

        if ($meta->save())
            return $meta->value;

        throw new CHttpException(500, Yii::t('ciims.models.LoginForm', 'Unable to create API key, please try again.'));
    }