r3200 - in trunk: libmsip/include/libmsip libmsip/source libmsip/source/dialogs minisip/minisip/gui/textui

mikma at minisip.org mikma at minisip.org
Sat Feb 10 16:25:27 CET 2007


Author: mikma
Date: 2007-02-10 16:25:26 +0100 (Sat, 10 Feb 2007)
New Revision: 3200

Modified:
   trunk/libmsip/include/libmsip/SipAuthenticationDigest.h
   trunk/libmsip/include/libmsip/SipDialog.h
   trunk/libmsip/include/libmsip/SipDialogConfig.h
   trunk/libmsip/source/SipAuthenticationDigest.cxx
   trunk/libmsip/source/SipDialogConfig.cxx
   trunk/libmsip/source/dialogs/SipDialog.cxx
   trunk/libmsip/source/dialogs/SipDialogRegister.cxx
   trunk/minisip/minisip/gui/textui/MinisipTextUI.cxx
Log:
Fix ask_password dialog and improve library support for setting credentials.


Modified: trunk/libmsip/include/libmsip/SipAuthenticationDigest.h
===================================================================
--- trunk/libmsip/include/libmsip/SipAuthenticationDigest.h	2007-02-08 17:14:11 UTC (rev 3199)
+++ trunk/libmsip/include/libmsip/SipAuthenticationDigest.h	2007-02-10 15:25:26 UTC (rev 3200)
@@ -63,17 +63,18 @@
 		/**
 		 * Set credential used when creating authorization headers.
 		 */
-		void setCredential(const std::string &username, const std::string &password);
-
+		void setCredential( MRef<SipCredential*> credential );
 		/**
-		 * Set credential used when creating authorization headers.
+		 * @return SIP credential or NULL
 		 */
-		void setCredential( MRef<SipCredential*> credential );
+		MRef<SipCredential*> getCredential() const;
 
 		static std::string md5ToString(unsigned char *md5);
 
 	private:
 		std::string calcResponse( MRef<SipRequest*> req ) const;
+		const std::string &getUsername() const;
+		const std::string &getPassword() const;
 
 		int type;
 		std::string realm;
@@ -84,8 +85,7 @@
 		std::string algorithm;
 		std::string qop;
 
-		std::string username;
-		std::string password;
+		MRef<SipCredential*> cred;
 
 		static std::string nullStr;
 };

Modified: trunk/libmsip/include/libmsip/SipDialog.h
===================================================================
--- trunk/libmsip/include/libmsip/SipDialog.h	2007-02-08 17:14:11 UTC (rev 3199)
+++ trunk/libmsip/include/libmsip/SipDialog.h	2007-02-10 15:25:26 UTC (rev 3200)
@@ -220,6 +220,15 @@
 		 */
 		void addAuthorizations( MRef<SipRequest*> req );
 
+		/**
+		 * Find realm of WWW-Authenticate or Proxy-Authenticate
+		 * header without valid credential.
+		 * Returns "" if there are none
+		 */
+		const std::string &findUnauthenticatedRealm() const;
+
+		bool addCredential( MRef<SipCredential*> credential );
+
 		std::string getDialogDebugString();
 
 	protected:

Modified: trunk/libmsip/include/libmsip/SipDialogConfig.h
===================================================================
--- trunk/libmsip/include/libmsip/SipDialogConfig.h	2007-02-08 17:14:11 UTC (rev 3199)
+++ trunk/libmsip/include/libmsip/SipDialogConfig.h	2007-02-10 15:25:26 UTC (rev 3200)
@@ -64,9 +64,9 @@
 			       const std::string &password,
 			       const std::string &realm = "" );
 
-		std::string getRealm() const;
-		std::string getUsername() const;
-		std::string getPassword() const;
+		const std::string &getRealm() const;
+		const std::string &getUsername() const;
+		const std::string &getPassword() const;
 
 		void set( const std::string &username,
 			  const std::string &password,

Modified: trunk/libmsip/source/SipAuthenticationDigest.cxx
===================================================================
--- trunk/libmsip/source/SipAuthenticationDigest.cxx	2007-02-08 17:14:11 UTC (rev 3199)
+++ trunk/libmsip/source/SipAuthenticationDigest.cxx	2007-02-10 15:25:26 UTC (rev 3200)
@@ -99,7 +99,7 @@
 	unsigned char digest[16];
 	MD5Context context;
 	MD5Init(&context);
-	string u_r_p(username+":"+realm+":"+password);
+	string u_r_p(getUsername()+":"+realm+":"+getPassword());
 	MD5Update(&context, (const unsigned char *)u_r_p.c_str(), (unsigned int)u_r_p.length() );
 	MD5Final(digest,&context);
 	string md5_u_r_p = md5ToString(digest);
@@ -131,7 +131,7 @@
 
 	if( type == SIP_HEADER_TYPE_WWWAUTHENTICATE ){
 		authorization = new SipHeaderValueAuthorization(
-			username,
+			getUsername(),
 			realm,
 			nonce,
 			opaque == nullStr ? "" : opaque,
@@ -142,7 +142,7 @@
 	}
 	else {
 		authorization = new SipHeaderValueProxyAuthorization(
-			username,
+			getUsername(),
 			realm,
 			nonce,
 			opaque == nullStr ? "" : opaque,
@@ -155,18 +155,28 @@
 	return authorization;
 }
 
-void SipAuthenticationDigest::setCredential(const std::string &username, const std::string &password){
-	this->username = username;
-	this->password = password;
+void SipAuthenticationDigest::setCredential( MRef<SipCredential*> credential ){
+	cred = credential;
 }
 
-void SipAuthenticationDigest::setCredential( MRef<SipCredential*> credential ){
-	if( credential.isNull() ){
-		username = "anonymous";
-		password = "";
-		return;
-	}
+MRef<SipCredential*> SipAuthenticationDigest::getCredential() const{
+	return cred;
+}
 
-	this->username = credential->getUsername();
-	this->password = credential->getPassword();
+const string &SipAuthenticationDigest::getUsername() const{
+	static string anonymous = "anonymous";
+
+	if( cred.isNull() )
+		return anonymous;
+	else
+		return cred->getUsername();
 }
+
+const string &SipAuthenticationDigest::getPassword() const{
+	static string empty = "";
+
+	if( cred.isNull() )
+		return empty;
+	else
+		return cred->getPassword();
+}

Modified: trunk/libmsip/source/SipDialogConfig.cxx
===================================================================
--- trunk/libmsip/source/SipDialogConfig.cxx	2007-02-08 17:14:11 UTC (rev 3199)
+++ trunk/libmsip/source/SipDialogConfig.cxx	2007-02-10 15:25:26 UTC (rev 3200)
@@ -47,15 +47,15 @@
 {
 }
 
-std::string SipCredential::getRealm() const{
+const std::string &SipCredential::getRealm() const{
 	return realm;
 }
 
-std::string SipCredential::getUsername() const{
+const std::string &SipCredential::getUsername() const{
 	return username;
 }
 
-std::string SipCredential::getPassword() const{
+const std::string &SipCredential::getPassword() const{
 	return password;
 }
 

Modified: trunk/libmsip/source/dialogs/SipDialog.cxx
===================================================================
--- trunk/libmsip/source/dialogs/SipDialog.cxx	2007-02-08 17:14:11 UTC (rev 3199)
+++ trunk/libmsip/source/dialogs/SipDialog.cxx	2007-02-10 15:25:26 UTC (rev 3200)
@@ -297,16 +297,26 @@
 		if( item->getRealm() == challenge->getRealm() ){
 			item->update( *auth );
 
-			if( item->getStale() )
+			if( item->getStale() ){
 				changed = true;
+			}
+			else{
+				// Clear invalid credential
+				item->setCredential( NULL );
+			}
+
 			found = true;
+			break;
 		}
 	}
-		
+
 	if( !found ){
 		dialogState.auths.push_back( challenge );
 
-		challenge->setCredential( getDialogConfig()->sipIdentity->getCredential()  );
+		MRef<SipCredential*> cred =
+			getDialogConfig()->sipIdentity->getCredential();
+
+		challenge->setCredential( cred );
 		changed = true;
 	}
 
@@ -352,6 +362,45 @@
 	}
 }
 
+const string &SipDialog::findUnauthenticatedRealm() const{
+	static const string empty;
+	list<MRef<SipAuthenticationDigest*> >::const_iterator j;
+
+	for ( j = dialogState.auths.begin();
+	      j != dialogState.auths.end(); j++ ){
+		MRef<SipAuthenticationDigest*> digest = *j;
+
+		if( digest->getCredential().isNull() ){
+			return digest->getRealm();
+		}
+	}
+
+	return empty;
+}
+
+bool SipDialog::addCredential( MRef<SipCredential*> credential ){
+	bool found = false;
+	list<MRef<SipAuthenticationDigest*> >::iterator j;
+
+	for ( j = dialogState.auths.begin();
+	      j != dialogState.auths.end(); j++ ){
+		MRef<SipAuthenticationDigest*> digest = *j;
+
+		if( digest->getCredential().isNull() ){
+			// Needs update
+			if( credential->getRealm() == "" ||
+			    credential->getRealm() == digest->getRealm() ){
+				digest->setCredential( credential );
+				found = true;
+			}
+		}
+	}
+
+	return found;
+}
+
+
+
 /*
 Establish a dialog acting as a UAS (receive a request)
 - routeSet = Record-Route header list, direct order

Modified: trunk/libmsip/source/dialogs/SipDialogRegister.cxx
===================================================================
--- trunk/libmsip/source/dialogs/SipDialogRegister.cxx	2007-02-08 17:14:11 UTC (rev 3199)
+++ trunk/libmsip/source/dialogs/SipDialogRegister.cxx	2007-02-10 15:25:26 UTC (rev 3200)
@@ -215,6 +215,8 @@
 				SipSMCommand::transaction_layer, 
 				SipSMCommand::dialog_layer, 
 				"401\n407")){
+
+		string realm = findUnauthenticatedRealm();
 		
 		//TODO: Ask password
 		CommandString cmdstr( 
@@ -222,6 +224,8 @@
 			SipCommandString::ask_password, 
 			getDialogConfig()->sipIdentity->getSipRegistrar()->getUri().getIp());
 		cmdstr["identityId"] = getDialogConfig()->sipIdentity->getId();
+		cmdstr["realm"] = realm;
+
 		getSipStack()->getCallback()->handleCommand("gui", cmdstr );
 		//authentication info from received response is extracted in a2
 		return true;
@@ -243,25 +247,14 @@
 			//We store the new credentials for this dialogs
 			//configuration. Note that it is not saved for the
 			//next time minisip is started.
-		MRef<SipCredential*> cred;
-		cred = getDialogConfig()->sipIdentity->getCredential();
+		const string &user = command.getCommandString().getParam();
+		const string &pass = command.getCommandString().getParam2() ;
 
-		cred->set( command.getCommandString().getParam(),
-			   command.getCommandString().getParam2() );
+		MRef<SipCredential*> cred =
+			new SipCredential( user, pass, realm );
 
-			//Update the set of credentials with the new (and hopefully
-			//correct) information.
-		list<MRef<SipAuthenticationDigest*> >::iterator j;
-		for ( j = dialogState.auths.begin();
-				j != dialogState.auths.end(); j++ ){
-			MRef<SipAuthenticationDigest*> digest = *j;
-			if (digest->getRealm()==realm){
-				digest->setCredential(command.getCommandString().getParam(), 
-						command.getCommandString().getParam2());
-			}
-		}
+		addCredential( cred );
 
-
 		++dialogState.seqNo;
 
 		send_register(/*trans->getBranch()*/"");

Modified: trunk/minisip/minisip/gui/textui/MinisipTextUI.cxx
===================================================================
--- trunk/minisip/minisip/gui/textui/MinisipTextUI.cxx	2007-02-08 17:14:11 UTC (rev 3199)
+++ trunk/minisip/minisip/gui/textui/MinisipTextUI.cxx	2007-02-10 15:25:26 UTC (rev 3200)
@@ -156,9 +156,9 @@
 	if (cmd.getOp()==SipCommandString::ask_password){
 		MRef<QuestionDialog*> d= new QuestionDialog;
 		d->questionId = cmd.getDestinationId();
-		d->questionId2 = cmd.getParam();
-		d->questions.push_back("Enter USER NAME for realm <"+cmd.getParam()+">");
-		d->questions.push_back("Enter PASSWORD for realm <"+cmd.getParam()+">");
+		d->questionId2 = cmd.get("realm");
+		d->questions.push_back("Enter USER NAME for realm <"+d->questionId2+">");
+		d->questions.push_back("Enter PASSWORD for realm <"+d->questionId2+">");
 		showQuestionDialog(d);
 	}
 



More information about the Minisip-devel mailing list