Imported Upstream version 17.23.2
[platform/upstream/libzypp.git] / zypp / media / CredentialManager.cc
index 38c5489..639bd6c 100644 (file)
@@ -6,7 +6,7 @@
 |                         /_____||_| |_| |_|                           |
 |                                                                      |
 \---------------------------------------------------------------------*/
-/** \file zypp/media/CredentialManager.h
+/** \file zypp/media/CredentialManager.cc
  *
  */
 #include <iostream>
 using namespace std;
 
 //////////////////////////////////////////////////////////////////////
-namespace zypp 
+namespace zypp
 { ////////////////////////////////////////////////////////////////////
   //////////////////////////////////////////////////////////////////////
   namespace media
   { ////////////////////////////////////////////////////////////////////
 
+  //////////////////////////////////////////////////////////////////////
+  //
+  // CLASS NAME : AuthDataComparator
+  //
+  //////////////////////////////////////////////////////////////////////
+
+  bool AuthDataComparator::operator()( const AuthData_Ptr & lhs, const AuthData_Ptr & rhs ) const
+  {
+    static const url::ViewOption vopt = url::ViewOption::DEFAULTS
+                                     - url::ViewOption::WITH_USERNAME
+                                     - url::ViewOption::WITH_PASSWORD
+                                     - url::ViewOption::WITH_QUERY_STR;
+    // std::less semantic!
+    int cmp = lhs->url().asString(vopt).compare( rhs->url().asString(vopt) );
+    if ( ! cmp )
+      cmp = lhs->username().compare( rhs->username() );
+    return( cmp < 0 );
+  }
 
   //////////////////////////////////////////////////////////////////////
   //
-  // CLASS NAME : CredManagerOptions 
+  // CLASS NAME : CredManagerOptions
   //
   //////////////////////////////////////////////////////////////////////
 
@@ -52,7 +70,7 @@ namespace zypp
 
   //////////////////////////////////////////////////////////////////////
   //
-  // CLASS NAME : CredentialManager::Impl 
+  // CLASS NAME : CredentialManager::Impl
   //
   struct CredentialManager::Impl
   {
@@ -60,7 +78,7 @@ namespace zypp
 
     ~Impl()
     {}
-    
+
     void init_globalCredentials();
     void init_userCredentials();
 
@@ -86,7 +104,7 @@ namespace zypp
 
   //////////////////////////////////////////////////////////////////////
   //
-  // CLASS NAME : CredentialManager::Impl 
+  // CLASS NAME : CredentialManager::Impl
   //
   //////////////////////////////////////////////////////////////////////
 
@@ -204,7 +222,7 @@ namespace zypp
   AuthData_Ptr CredentialManager::Impl::getCredFromFile(const Pathname & file)
   {
     AuthData_Ptr result;
-    
+
     Pathname credfile;
     if (file.absolute())
       // get from that file
@@ -262,7 +280,7 @@ namespace zypp
 
   //////////////////////////////////////////////////////////////////////
   //
-  // CLASS NAME : CredentialManager 
+  // CLASS NAME : CredentialManager
   //
   //////////////////////////////////////////////////////////////////////
 
@@ -286,8 +304,12 @@ namespace zypp
 
   void CredentialManager::addCred(const AuthData & cred)
   {
-#warning addCred(const AuthData & cred) not implemented
-    // add with user callbacks
+    Pathname credfile = cred.url().getQueryParam("credentials");
+    if (credfile.empty())
+      //! \todo ask user where to store these creds. saving to user creds for now
+      addUserCred(cred);
+    else
+      saveInFile(cred, credfile);
   }
 
 
@@ -295,8 +317,15 @@ namespace zypp
   {
     AuthData_Ptr c_ptr;
     c_ptr.reset(new AuthData(cred)); // FIX for child classes if needed
-    if (_pimpl->_credsGlobal.insert(c_ptr).second)
+    pair<CredentialIterator, bool> ret = _pimpl->_credsGlobal.insert(c_ptr);
+    if (ret.second)
+      _pimpl->_globalDirty = true;
+    else if ((*ret.first)->password() != cred.password())
+    {
+      _pimpl->_credsGlobal.erase(ret.first);
+      _pimpl->_credsGlobal.insert(c_ptr);
       _pimpl->_globalDirty = true;
+    }
   }
 
 
@@ -304,8 +333,15 @@ namespace zypp
   {
     AuthData_Ptr c_ptr;
     c_ptr.reset(new AuthData(cred)); // FIX for child classes if needed
-    if (_pimpl->_credsUser.insert(c_ptr).second)
+    pair<CredentialIterator, bool> ret = _pimpl->_credsUser.insert(c_ptr);
+    if (ret.second)
+      _pimpl->_userDirty = true;
+    else if ((*ret.first)->password() != cred.password())
+    {
+      _pimpl->_credsUser.erase(ret.first);
+      _pimpl->_credsUser.insert(c_ptr);
       _pimpl->_userDirty = true;
+    }
   }
 
 
@@ -338,6 +374,7 @@ namespace zypp
   {
     AuthData_Ptr c_ptr;
     c_ptr.reset(new AuthData(cred)); // FIX for child classes if needed
+    c_ptr->setUrl(Url()); // don't save url in custom creds file
     CredentialManager::CredentialSet creds;
     creds.insert(c_ptr);