diff --git a/core/modules/openid/lib/Drupal/openid/DatabaseOpenidStorage.php b/core/modules/openid/lib/Drupal/openid/DatabaseOpenidStorage.php
new file mode 100644
index 0000000..728fd2e
--- /dev/null
+++ b/core/modules/openid/lib/Drupal/openid/DatabaseOpenidStorage.php
@@ -0,0 +1,158 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\openid\DatabaseOpenidStorage.
+ */
+
+namespace Drupal\openid;
+
+use Drupal\Core\Database\Connection;
+use Drupal\Core\Database\DatabaseExceptionWrapper;
+
+/**
+ * Defines the database OpenID storage. This is the default Drupal backend.
+ */
+class DatabaseOpenidStorage implements OpenidStorageInterface {
+
+  /**
+   * The database connection used to store file usage information.
+   *
+   * @var Drupal\Core\Database\Connection
+   */
+  protected $connection;
+
+  /**
+   * Construct the DatabaseFileUsageBackend.
+   *
+   * @param Drupal\Core\Database\Connection $connection
+   *   The database connection which will be used to store the openid
+   *   information.
+   */
+  public function __construct(Connection $connection) {
+    $this->connection = $connection;
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::getIdentityUid().
+   */
+  public function getIdentityUid($identifier) {
+    return $this->connection->query("SELECT uid FROM {openid_identities} WHERE identifier = :identifier", array(':identifier' => $identifier))->fetchField();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::getIdentity().
+   */
+  public function getIdentity($uid, $aid) {
+    return $this->connection->query("SELECT identifier FROM {openid_identities} WHERE uid = :uid AND aid = :aid", array(':uid' => $uid, ':aid' => $aid))->fetchField();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::deleteIdentity().
+   */
+  public function deleteIdentity($uid, $aid = NULL) {
+    $query = $this->connection->delete('openid_identities')
+      ->condition('uid', $uid);
+    if ($aid) {
+      $query->condition('aid', $aid);
+    }
+    return $query->execute();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::insertIdentity().
+   */
+  public function insertIdentity($uid, $identifier) {
+    $this->connection->insert('openid_identities')
+      ->fields(array(
+        'uid' => $uid,
+        'identifier' => $identifier,
+      ))
+      ->execute();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::deleteExpiredAssociation().
+   */
+  public function deleteExpiredAssociation() {
+    $this->connection->delete('openid_association')
+      ->where('created + expires_in < :request_time', array(':request_time' => REQUEST_TIME))
+      ->execute();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::getAssociationHandle().
+   */
+  public function getAssociationHandle($op_endpoint) {
+    return $this->connection->query("SELECT assoc_handle FROM {openid_association} WHERE idp_endpoint_uri = :endpoint", array(':endpoint' => $op_endpoint))->fetchField();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::insertAssociation().
+   */
+  public function insertAssociation($op_endpoint, $assoc_response) {
+    $this->connection->insert('openid_association')
+      ->fields(array(
+        'idp_endpoint_uri' => $op_endpoint,
+        'session_type' => $assoc_response['session_type'],
+        'assoc_handle' => $assoc_response['assoc_handle'],
+        'assoc_type' => $assoc_response['assoc_type'],
+        'expires_in' => $assoc_response['expires_in'],
+        'mac_key' => $assoc_response['mac_key'],
+        'created' => REQUEST_TIME,
+      ))
+      ->execute();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::getAssociation().
+   */
+  public function getAssociation($handle) {
+    return $this->connection->query("SELECT * FROM {openid_association} WHERE assoc_handle = :assoc_handle", array(':assoc_handle' => $handle))->fetchObject();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::deleteAssociation().
+   */
+  public function deleteAssociation($handle) {
+    $this->connection->delete('openid_association')
+      ->condition('assoc_handle', $handle)
+      ->execute();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::deleteNonce().
+   */
+  public function deleteNonce() {
+    $this->connection->delete('openid_nonce')
+      ->condition('expires', REQUEST_TIME, '<')
+      ->execute();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::insertNonce().
+   */
+  public function insertNonce($uri, $nonce, $expiries) {
+    $this->connection->insert('openid_nonce')
+      ->fields(array(
+        'idp_endpoint_uri' => $uri,
+        'nonce' => $nonce,
+        'expires' => $expiries,
+      ))
+      ->execute();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::getNonceUsage().
+   */
+  public function getNonceUsage($nonce, $uri) {
+    return $this->connection->query("SELECT COUNT(*) FROM {openid_nonce} WHERE nonce = :nonce AND idp_endpoint_uri = :idp_endpoint_uri", array(':nonce' => $nonce, ':idp_endpoint_uri' => $uri))->fetchField();
+  }
+
+  /**
+   * Implements Drupal\openid\OpenidStorageInterface::getUserIdentities().
+   */
+  public function getUserIdentities($uid) {
+    return $this->connection->query("SELECT * FROM {openid_identities} WHERE uid = :uid", array(':uid' => $uid));
+  }
+}
diff --git a/core/modules/openid/lib/Drupal/openid/OpenidBundle.php b/core/modules/openid/lib/Drupal/openid/OpenidBundle.php
new file mode 100644
index 0000000..ffeb15c
--- /dev/null
+++ b/core/modules/openid/lib/Drupal/openid/OpenidBundle.php
@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\openid\OpenidBundle.
+ */
+
+namespace Drupal\openid;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+class OpenidBundle extends Bundle {
+  public function build(ContainerBuilder $container) {
+    $container->register('OpenID.storage', 'Drupal\openid\DatabaseOpenidStorage')
+      ->addArgument(new Reference('database'));
+  }
+}
diff --git a/core/modules/openid/lib/Drupal/openid/OpenidStorageInterface.php b/core/modules/openid/lib/Drupal/openid/OpenidStorageInterface.php
new file mode 100644
index 0000000..71db928
--- /dev/null
+++ b/core/modules/openid/lib/Drupal/openid/OpenidStorageInterface.php
@@ -0,0 +1,126 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\openid\OpenidStorageInterface.
+ */
+
+namespace Drupal\openid;
+
+/**
+ * OpenID storage interface.
+ */
+interface OpenidStorageInterface {
+
+  /**
+   * Delete an identity from OpenID.
+   *
+   * @param int $uid
+   *   User id.
+   * @param int $aid
+   *   (optional) authentication id. Defaults to NULL.
+   */
+  public function deleteIdentity($uid, $aid = NULL);
+
+  /**
+   * Retrieves a uid for a given OpenID identifier.
+   *
+   * @param string $identifier
+   *   OpenID identifer.
+   */
+  public function getIdentityUid($identifier);
+
+  /**
+   * Retrieves an OpenID identifier.
+   *
+   * @param int $uid
+   *   User id.
+   * @param int $aid
+   *   Authentication id.
+   */
+  public function getIdentity($uid, $aid);
+
+  /**
+   * Store an OpenID identity.
+   *
+   * @param int $uid
+   *   User id.
+   * @param string $identifier
+   *   OpenID identifer.
+   */
+  public function insertIdentity($uid, $identifier);
+
+  /**
+   * Delete expired OpenID associations.
+   */
+  public function deleteExpiredAssociation();
+
+  /**
+   * Retrieve the handle of an OpenID association by endpoint.
+   *
+   * @param string $op_endpoint
+   *   OpenID endpoint.
+   */
+  public function getAssociationHandle($op_endpoint);
+
+  /**
+   * Store an OpenID association.
+   *
+   * @param string $op_endpoint
+   *   OpenID endpoint.
+   * @param array $assoc_response
+   *   OpenID response.
+   */
+  public function insertAssociation($op_endpoint, $assoc_response);
+
+  /**
+   * Retrieve an OpenID association by handle.
+   *
+   * @param string $handle
+   *   OpenID handle.
+   */
+  public function getAssociation($handle);
+
+  /**
+   * Delete an OpenID association by handle.
+   *
+   * @param string $handle
+   *   OpenID handle.
+   */
+  public function deleteAssociation($handle);
+
+  /**
+   * Delete expired OpenID nonce.
+   */
+  public function deleteNonce();
+
+  /**
+   * Store an OpenID nonce.
+   *
+   * @param string $uri
+   *   URI the nonce is associated with.
+   * @param string $nonce
+   *   OpenID nonce.
+   * @param int $expiries
+   *   Timestamp for when the nonce expires.
+   */
+  public function insertNonce($uri, $nonce, $expiries);
+
+  /**
+   * Get OpenID nonce usage.
+   *
+   * @param string $nonce
+   *   OpenID nonce.
+   * @param string $uri
+   *   URI the nonce is associated with.
+   */
+  public function getNonceUsage($nonce, $uri);
+
+  /**
+   * Get OpenID identities for a given user.
+   *
+   * @param int $uid
+   *   User id.
+   */
+  public function getUserIdentities($uid);
+}
diff --git a/core/modules/openid/openid.module b/core/modules/openid/openid.module
index 426c8ae..fc1e8ae 100644
--- a/core/modules/openid/openid.module
+++ b/core/modules/openid/openid.module
@@ -5,6 +5,9 @@
  * Implement OpenID Relying Party support for Drupal
  */
 
+use Drupal\openid\OpenidStorageInterface;
+use Drupal\openid\DatabaseOpenidStorage;
+
 /**
  * Implements hook_menu().
  */
@@ -97,7 +100,7 @@ function openid_help($path, $arg) {
  *   A fully-loaded user object if the user is found or FALSE if not found.
  */
 function openid_external_load($identifier) {
-  $uid = db_query("SELECT uid FROM {openid_identities} WHERE identifier = :identifier", array(':identifier' => $identifier))->fetchField();
+  $uid = drupal_container()->get('OpenID.storage')->getIdentityUid($identifier);
   if ($uid) {
     return user_load($uid);
   }
@@ -108,9 +111,7 @@ function openid_external_load($identifier) {
  * Implements hook_user_delete().
  */
 function openid_user_delete($account) {
-  db_delete('openid_identities')
-    ->condition('uid', $account->uid)
-    ->execute();
+  drupal_container()->get('OpenID.storage')->deleteIdentity($account->uid);
 }
 
 /**
@@ -122,12 +123,7 @@ function openid_user_insert($account) {
     if (config('user.settings')->get('verify_mail')) {
       drupal_set_message(t('Once you have verified your e-mail address, you may log in via OpenID.'));
     }
-    db_insert('openid_identities')
-      ->fields(array(
-        'uid' => $account->uid,
-        'identifier' => $account->openid_claimed_id,
-      ))
-      ->execute();
+    drupal_container()->get('OpenID.storage')->insertIdentity($account->uid, $account->openid_claimed_id);
     unset($_SESSION['openid']);
     unset($account->openid_claimed_id);
   }
@@ -688,14 +684,13 @@ function openid_openid_normalization_method_info() {
  */
 function openid_association($op_endpoint) {
   module_load_include('inc', 'openid');
+  $openid_storage = drupal_container()->get('OpenID.storage');
 
   // Remove Old Associations:
-  db_delete('openid_association')
-    ->where('created + expires_in < :request_time', array(':request_time' => REQUEST_TIME))
-    ->execute();
+  $openid_storage->deleteExpiredAssociation();
 
   // Check to see if we have an association for this IdP already
-  $assoc_handle = db_query("SELECT assoc_handle FROM {openid_association} WHERE idp_endpoint_uri = :endpoint", array(':endpoint' => $op_endpoint))->fetchField();
+  $assoc_handle = $openid_storage->getAssociationHandle($op_endpoint);
   if (empty($assoc_handle)) {
     $mod = OPENID_DH_DEFAULT_MOD;
     $gen = OPENID_DH_DEFAULT_GEN;
@@ -727,17 +722,7 @@ function openid_association($op_endpoint) {
       $shared = _openid_math_powmod($spub, $private, $mod);
       $assoc_response['mac_key'] = base64_encode(_openid_dh_xorsecret($shared, $enc_mac_key));
     }
-    db_insert('openid_association')
-      ->fields(array(
-        'idp_endpoint_uri' => $op_endpoint,
-        'session_type' => $assoc_response['session_type'],
-        'assoc_handle' => $assoc_response['assoc_handle'],
-        'assoc_type' => $assoc_response['assoc_type'],
-        'expires_in' => $assoc_response['expires_in'],
-        'mac_key' => $assoc_response['mac_key'],
-        'created' => REQUEST_TIME,
-      ))
-      ->execute();
+    $openid_storage->insertAssociation($op_endpoint, $assoc_response);
     $assoc_handle = $assoc_response['assoc_handle'];
   }
   return $assoc_handle;
@@ -928,7 +913,7 @@ function openid_verify_assertion($service, $response) {
   // direct verification: ignore the openid.assoc_handle, even if present.
   // See http://openid.net/specs/openid-authentication-2_0.html#rfc.section.11.4.1
   if (!empty($response['openid.assoc_handle']) && empty($response['openid.invalidate_handle'])) {
-    $association = db_query("SELECT * FROM {openid_association} WHERE assoc_handle = :assoc_handle", array(':assoc_handle' => $response['openid.assoc_handle']))->fetchObject();
+    $association = drupal_container()->get('OpenID.storage')->getAssociation($response['openid.assoc_handle']);
   }
 
   if ($association && isset($association->session_type)) {
@@ -959,9 +944,7 @@ function openid_verify_assertion($service, $response) {
           // This association handle has expired on the OP side, remove it from the
           // database to avoid reusing it again on a subsequent authentication request.
           // See http://openid.net/specs/openid-authentication-2_0.html#rfc.section.11.4.2.2
-          db_delete('openid_association')
-            ->condition('assoc_handle', $response['invalidate_handle'])
-            ->execute();
+          drupal_container()->get('OpenID.storage')->deleteAssociation($response['invalidate_handle']);
         }
       }
       else {
@@ -1047,20 +1030,12 @@ function openid_verify_assertion_nonce($service, $response) {
     return FALSE;
   }
 
+  $openid_storage = drupal_container()->get('OpenID.storage');
   // Record that this nonce was used.
-  db_insert('openid_nonce')
-    ->fields(array(
-      'idp_endpoint_uri' => $service['uri'],
-      'nonce' => $response['openid.response_nonce'],
-      'expires' => $nonce_timestamp + $expiry,
-    ))
-    ->execute();
+  $openid_storage->insertNonce($service['uri'], $response['openid.response_nonce'], $nonce_timestamp + $expiry);
 
   // Count the number of times this nonce was used.
-  $count_used = db_query("SELECT COUNT(*) FROM {openid_nonce} WHERE nonce = :nonce AND idp_endpoint_uri = :idp_endpoint_uri", array(
-    ':nonce' => $response['openid.response_nonce'],
-    ':idp_endpoint_uri' => $service['uri'],
-  ))->fetchField();
+  $count_used = $openid_storage->getNonceUsage($response['openid.response_nonce'], $service['uri']);
 
   if ($count_used == 1) {
     return TRUE;
@@ -1116,9 +1091,7 @@ function openid_verify_assertion_return_url($service, $response) {
  * Implements hook_cron().
  */
 function openid_cron() {
-  db_delete('openid_nonce')
-    ->condition('expires', REQUEST_TIME, '<')
-    ->execute();
+  drupal_container()->get('OpenID.storage')->deleteNonce();
 }
 
 /**
diff --git a/core/modules/openid/openid.pages.inc b/core/modules/openid/openid.pages.inc
index 81a5905..2740dac 100644
--- a/core/modules/openid/openid.pages.inc
+++ b/core/modules/openid/openid.pages.inc
@@ -34,12 +34,7 @@ function openid_user_identities($account) {
   $response = openid_complete();
   if ($response['status'] == 'success') {
     $identity = $response['openid.claimed_id'];
-    $query = db_insert('openid_identities')
-      ->fields(array(
-        'uid' => $account->uid,
-        'identifier' => $identity,
-      ))
-      ->execute();
+    drupal_container()->get('OpenID.storage')->insertIdentity($account->uid, $identity);
     drupal_set_message(t('Successfully added %identity', array('%identity' => $identity)));
     // Let other modules act on OpenID authentication.
     module_invoke_all('openid_response', $response, $account);
@@ -48,7 +43,7 @@ function openid_user_identities($account) {
   $header = array(t('OpenID'), t('Operations'));
   $rows = array();
 
-  $result = db_query("SELECT * FROM {openid_identities} WHERE uid=:uid", array(':uid' => $account->uid));
+  $result = drupal_container()->get('OpenID.storage')->getUserIdentities($account->uid);
   foreach ($result as $identity) {
     $row = array();
     $row[] = check_plain($identity->identifier);
@@ -95,7 +90,7 @@ function openid_user_add() {
 function openid_user_add_validate($form, &$form_state) {
   // Check for existing entries.
   $claimed_id = openid_normalize($form_state['values']['openid_identifier']);
-  if (db_query("SELECT identifier FROM {openid_identities} WHERE identifier = :identifier", array(':identifier' => $claimed_id))->fetchField()) {
+  if (drupal_container()->get('OpenID.storage')->getIdentityUid($claimed_id)) {
     form_set_error('openid_identifier', t('That OpenID is already in use on this site.'));
   }
 }
@@ -109,19 +104,12 @@ function openid_user_add_submit($form, &$form_state) {
  * Menu callback; Delete the specified OpenID identity from the system.
  */
 function openid_user_delete_form($form, $form_state, $account, $aid = 0) {
-  $identifier = db_query("SELECT identifier FROM {openid_identities} WHERE uid = :uid AND aid = :aid", array(
-    ':uid' => $account->uid,
-    ':aid' => $aid,
-  ))
-  ->fetchField();
+  $identifier = drupal_container()->get('OpenID.storage')->getIdentity($account->uid, $aid);
   return confirm_form(array(), t('Are you sure you want to delete the OpenID %identifier for %user?', array('%identifier' => $identifier, '%user' => $account->name)), 'user/' . $account->uid . '/openid');
 }
 
 function openid_user_delete_form_submit($form, &$form_state) {
-  $query = db_delete('openid_identities')
-    ->condition('uid', $form_state['build_info']['args'][0]->uid)
-    ->condition('aid', $form_state['build_info']['args'][1])
-    ->execute();
+  $query = drupal_container()->get('OpenID.storage')->deleteIdentity($form_state['build_info']['args'][0]->uid, $form_state['build_info']['args'][1]);
   if ($query) {
     drupal_set_message(t('OpenID deleted.'));
   }
