genericservices/httputils/Authentication/cauthentication.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // authentication.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32base.h>
       
    19 
       
    20 #include "cauthentication.h"
       
    21 
       
    22 
       
    23 /**
       
    24 	Creates a new instance of CAuthentication.
       
    25 
       
    26 	@param aName The new user name. A local copy of descriptor is created. Can be a null descriptor.
       
    27 	@param aPassword The new password. A local copy of descriptor is created. Can be a null descriptor.
       
    28 	@param aMethod the method type of either basic or digest. Defaults to digest.
       
    29 	@return A pointer to the newly created CAuthentication object.
       
    30 	@leave KErrNoMemory
       
    31  */
       
    32 EXPORT_C CAuthentication* CAuthentication::NewL(const TDesC8& aName, const TDesC8& aPassword, TMethod aMethod)
       
    33 	{
       
    34 	CAuthentication* me = new (ELeave) CAuthentication(aMethod);
       
    35 	CleanupStack::PushL(me);
       
    36 	me->ConstructL(aName, aPassword);
       
    37 	CleanupStack::Pop(me);
       
    38 	return me;
       
    39 	}
       
    40 
       
    41 /**
       
    42 	Creates a new instance of CAuthentication.
       
    43 
       
    44 	@param aUri The URI with a userinfo component.
       
    45 	@param aMethod the method type of either basic or digest. Defaults to digest.
       
    46 	@return A pointer to the newly created CAuthentication object.
       
    47 	@leave KErrNotFound If there is no userinfo component.
       
    48 	@leave KErrNoMemory
       
    49 */
       
    50 EXPORT_C CAuthentication* CAuthentication::NewL(const TUriC8& aUri, TMethod aMethod)
       
    51 	{
       
    52 	CAuthentication* me = new (ELeave) CAuthentication(aMethod);
       
    53 	CleanupStack::PushL(me);
       
    54 	me->ConstructL(aUri);
       
    55 	CleanupStack::Pop(me);
       
    56 	return me;
       
    57 	}
       
    58 
       
    59 /**
       
    60 	Constructor.
       
    61 	@param aMethod enum value of type TMethod.
       
    62  */
       
    63 CAuthentication::CAuthentication(TMethod aMethod):iMethodType(aMethod)
       
    64 	{
       
    65 	}
       
    66 
       
    67 /**
       
    68 	Second phase of two-phase construction method. Does any allocations required to fully construct 
       
    69 	the object.
       
    70 	
       
    71 	@param			aName A descriptor to be allocated initialised with.
       
    72 	@param			aPassword A descriptor to be allocated initialised with.
       
    73 	@pre 			First phase of construction is complete.
       
    74 	@post			The object is fully constructed and initialized.
       
    75  */
       
    76 void CAuthentication::ConstructL(const TDesC8& aName, const TDesC8& aPassword)
       
    77 	{
       
    78 	iName = aName.AllocL();
       
    79 	iPassword = aPassword.AllocL();
       
    80 	}
       
    81 
       
    82 /**
       
    83 	Second phase of two-phase construction method. Does any allocations required to fully construct 
       
    84 	the object. Must set both user name and password to at least an empty string.
       
    85 	
       
    86 	@param			aUri The URI with a userinfo component.
       
    87 	@pre 			First phase of construction is complete.
       
    88 	@post			The object is fully constructed and initialized.
       
    89  */
       
    90 void CAuthentication::ConstructL(const TUriC8& aUri)
       
    91 	{
       
    92 	// Check if user info component present.
       
    93 	if(!aUri.IsPresent(EUriUserinfo))
       
    94 		{
       
    95 		User::Leave(KErrNotFound);
       
    96 		}
       
    97 	// Set name and pwd. Note that these could be empty strings.
       
    98 	TPtrC8 userInfo(aUri.Extract(EUriUserinfo));
       
    99 	TInt colPos = userInfo.Locate(':');
       
   100 	if(KErrNotFound == colPos) // 'name@' or '@' case
       
   101 		{
       
   102 		iName = userInfo.AllocL();
       
   103 		iPassword = KNullDesC8().AllocL();
       
   104 		}	
       
   105 	else // ':' found
       
   106 		{
       
   107 		iName = userInfo.Left(colPos).AllocL();
       
   108 		iPassword = userInfo.Right(userInfo.Length()-colPos-1).AllocL();						
       
   109 		}
       
   110 	}
       
   111 
       
   112 /**
       
   113 	Destructor
       
   114  */
       
   115 EXPORT_C CAuthentication::~CAuthentication()
       
   116 	{
       
   117 	delete iName;
       
   118 	delete iPassword;
       
   119 	}
       
   120 
       
   121 /**
       
   122 	Gets the user name.
       
   123 
       
   124 	@return Reference to the user name descriptor.
       
   125  */
       
   126 EXPORT_C const TDesC8& CAuthentication::Name() const
       
   127 	{
       
   128 	return *iName;
       
   129 	}
       
   130 
       
   131 /**
       
   132 	Gets the password.
       
   133 
       
   134 	@return Reference to the password descriptor.
       
   135  */
       
   136 EXPORT_C const TDesC8& CAuthentication::Password() const
       
   137 	{
       
   138 	return *iPassword;
       
   139 	}
       
   140 
       
   141 /**
       
   142 	Gets the method type.
       
   143 
       
   144 	@return The authentication method type.
       
   145  */
       
   146 EXPORT_C CAuthentication::TMethod CAuthentication::Method() const
       
   147 	{
       
   148 	return iMethodType;
       
   149 	}
       
   150 
       
   151 /**
       
   152 	Sets the username.
       
   153 
       
   154 	@param aName Reference to the new username descriptor.
       
   155 	@leave KErrNoMemory
       
   156  */
       
   157 EXPORT_C void CAuthentication::SetNameL(const TDesC8& aName)
       
   158 	{
       
   159 	HBufC8* temp = iName;
       
   160 	iName = aName.AllocL();
       
   161 	delete temp;
       
   162 	}
       
   163 
       
   164 /**
       
   165 	Sets the password.
       
   166 
       
   167 	@param aPassword Reference to the new password descriptor.
       
   168 	@leave KErrNoMemory
       
   169  */
       
   170 EXPORT_C void CAuthentication::SetPasswordL(const TDesC8& aPassword)
       
   171 	{
       
   172 	HBufC8* temp = iPassword;
       
   173 	iPassword = aPassword.AllocL();
       
   174 	delete temp;
       
   175 	}
       
   176 
       
   177 /**
       
   178 	Sets the method type.
       
   179 
       
   180 	@param aMethod The new authentication method type.
       
   181  */
       
   182 EXPORT_C void CAuthentication::SetMethod(TMethod aMethod)
       
   183 	{
       
   184 	iMethodType = aMethod;
       
   185 	}