-
- {{ 'login.login.form.password.placeholder'|trans }}
-
-
diff --git a/src/Gist/Service/SaltGenerator.php b/src/Gist/Service/SaltGenerator.php
index f86a0e0..a3143c2 100644
--- a/src/Gist/Service/SaltGenerator.php
+++ b/src/Gist/Service/SaltGenerator.php
@@ -18,18 +18,30 @@ class SaltGenerator
*
* @return string
*/
- public function generate($length = 32)
+ public function generate($length = 32, $isApiKey = false)
{
if (!is_numeric($length)) {
throw new InvalidArgumentException('Paramter length must be a valid integer.');
}
if (function_exists('openssl_random_pseudo_bytes')) {
- return substr(base64_encode(openssl_random_pseudo_bytes($length)), 0, $length);
+ $string = base64_encode(openssl_random_pseudo_bytes(256));
}
if (function_exists('mcrypt_create_iv')) {
- return substr(base64_encode(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM)), 0, $length);
+ $string = base64_encode(mcrypt_create_iv(256, MCRYPT_DEV_URANDOM));
+ }
+
+ if (!empty($string)) {
+ if (true === $isApiKey) {
+ $string = str_replace(
+ array('+', '%', '/', '#', '&'),
+ '',
+ $string
+ );
+ }
+
+ return substr($string, 0, $length);
}
throw new RuntimeException('You must enable openssl or mcrypt modules.');
diff --git a/src/Gist/Service/UserProvider.php b/src/Gist/Service/UserProvider.php
index beab8e1..e887b50 100644
--- a/src/Gist/Service/UserProvider.php
+++ b/src/Gist/Service/UserProvider.php
@@ -126,6 +126,7 @@ class UserProvider implements UserProviderInterface
$user
->setRoles('ROLE_USER')
->setPassword($this->encoder->encodePassword($password, $user->getSalt()))
+ ->setApiKey($this->saltGenerator->generate(32, true))
->save();
return $user;
@@ -166,6 +167,20 @@ class UserProvider implements UserProviderInterface
return $user;
}
+ /**
+ * Loads a user by his api key.
+ *
+ * @param string $apiKey
+ *
+ * @return User
+ */
+ public function loadUserByApiKey($apiKey)
+ {
+ $user = UserQuery::create()->findOneByApiKey($apiKey);
+
+ return $user;
+ }
+
/*
* Checks if the given password is the current user password.
*
diff --git a/web/app/js/app.js b/web/app/js/app.js
index a4d64d4..671d102 100644
--- a/web/app/js/app.js
+++ b/web/app/js/app.js
@@ -98,6 +98,10 @@ var myEvents = function() {
$('#form-deletion form').submit();
}
});
+
+ $(document).on('change keyup keydown', '#form-api-key', function() {
+ $(this).val($(this).data('key'));
+ });
}
var mainEditorEvents = function() {