webservices/idwsfplugin/src/idwsfauthenticationserviceclient.cpp
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:        
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 // INCLUDE FILES
       
    26 #include "idwsfauthenticationserviceclient.h"
       
    27 
       
    28 #include <s32mem.h>
       
    29 #include <f32file.h>
       
    30 #include <s32file.h>
       
    31 
       
    32 #include <MSenServiceDescription.h>
       
    33 #include <SenServiceConnection.h>
       
    34 #include <SenXmlUtils.h>
       
    35 
       
    36 #include "idwsfsaslmessage.h"
       
    37 #include "idwsfmessage.h"  // for response parsing and request building
       
    38 #include "idwsfdsqueryresponse.h"
       
    39 #include "idwsfdiscoveryserviceclient.h"
       
    40 #include "idwsfservicesession.h"
       
    41 
       
    42 #include "msencoreservicemanager.h"
       
    43 #include "sendebug.h" // internal Utils\inc - filelogging MACROs
       
    44 #include "senlogger.h"
       
    45 
       
    46 CIdWsfAuthenticationServiceClient* CIdWsfAuthenticationServiceClient::NewL(
       
    47                                                         CSIF& aSIF)//,
       
    48                                                         //RFileLogger& aLogger)
       
    49     {
       
    50     CIdWsfAuthenticationServiceClient* pNew = NewLC(aSIF);//, aLogger);
       
    51     CleanupStack::Pop();
       
    52     return pNew;
       
    53     }
       
    54 
       
    55 CIdWsfAuthenticationServiceClient* CIdWsfAuthenticationServiceClient::NewLC(
       
    56                                                         CSIF& aSIF)//,
       
    57                                                         //RFileLogger& aLogger)
       
    58     {
       
    59     CIdWsfAuthenticationServiceClient* pNew =
       
    60         new (ELeave) CIdWsfAuthenticationServiceClient(
       
    61                     aSIF,
       
    62                     MSenServiceDescription::EIdWsfAuthenticationServiceClient);//,
       
    63                     //aLogger);
       
    64 
       
    65     CleanupStack::PushL(pNew);
       
    66     pNew->BaseConstructL();
       
    67     return pNew;
       
    68     }
       
    69 
       
    70 
       
    71 CIdWsfAuthenticationServiceClient::CIdWsfAuthenticationServiceClient(
       
    72                                                 CSIF& aSIF,
       
    73                                                 TDescriptionClassType aType)//,
       
    74                                                 //RFileLogger& aLogger)
       
    75     : CIdWsfDiscoveryServiceClient(aSIF, aType),// aLogger),
       
    76     iAuthenticator(NULL),
       
    77     iDsSession(NULL),
       
    78     iAttempts(0),
       
    79     iMaxAttempts(1)
       
    80     {
       
    81     }
       
    82 
       
    83 void CIdWsfAuthenticationServiceClient::BaseConstructL()
       
    84     {
       
    85     CIdWsfDiscoveryServiceClient::BaseConstructL();
       
    86 		TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("CIdWsfAuthenticationServiceClient::ConstructL()")));
       
    87     }
       
    88 
       
    89 CIdWsfAuthenticationServiceClient::~CIdWsfAuthenticationServiceClient()
       
    90     {
       
    91     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,
       
    92     "CIdWsfAuthenticationServiceClient::~CIdWsfAuthenticationServiceClient");
       
    93     delete iAuthenticator;
       
    94     }
       
    95 
       
    96 TInt CIdWsfAuthenticationServiceClient::ValidateL(
       
    97                                         CIdWsfServiceSession& aSession,
       
    98                                         MSenRemoteServiceConsumer& aRemoteConsumer)
       
    99     {
       
   100     CSLOG_L(aRemoteConsumer.ConnectionId(), KMinLogLevel ,"CIdWsfAuthenticationServiceClient::ValidateL()");
       
   101     return CIdWsfAuthenticationServiceClient::ValidateL(aSession, 
       
   102                                                         KNullDesC8(), 
       
   103                                                         aRemoteConsumer);
       
   104     }
       
   105 
       
   106 
       
   107 TInt CIdWsfAuthenticationServiceClient::ValidateL(
       
   108                                     CIdWsfServiceSession &aSession,
       
   109                                     const TDesC8& /*authnContext*/,
       
   110                                     MSenRemoteServiceConsumer& aRemoteConsumer)
       
   111     {
       
   112     CSLOG_L(aRemoteConsumer.ConnectionId(), KMinLogLevel ,"CIdWsfAuthenticationServiceClient::ValidateL(..)");
       
   113     // keep this as first line
       
   114     iServedConsumer = &aRemoteConsumer;
       
   115 
       
   116     if(!ipAccount)
       
   117         {
       
   118         return KErrSenNoEndpoint; 
       
   119         }
       
   120 
       
   121     TInt retVal(KErrNone);          // for returning error codes
       
   122     iCState = EAuthServiceClient;   // set state
       
   123     iDsSession = &aSession;         // save session
       
   124     CSLOG_L(aRemoteConsumer.ConnectionId(), KMinLogLevel ,"- starting transaction.");
       
   125     StartTransaction();
       
   126 
       
   127     TPtrC8 mechanismNames =
       
   128                         Framework().Manager().SenSecurityMechanismNames8L();
       
   129 
       
   130     CIdWsfSaslMessage* pInitMessage =
       
   131         CIdWsfSaslMessage::NewL(Framework().Manager(),
       
   132                                 KSaslXmlns,
       
   133                                 KSaslRequestName,
       
   134                                 KSaslRequestQName);
       
   135     CleanupStack::PushL(pInitMessage); // push pInitMessage
       
   136 
       
   137     CSLOG_L(aRemoteConsumer.ConnectionId(), KMinLogLevel ,"- constructing initial request.");
       
   138     pInitMessage->ConstructInitRequestFromL(mechanismNames, *ipAccount);
       
   139 
       
   140     HBufC8* pInitRequest8 = pInitMessage->AsXmlL();
       
   141     CleanupStack::PushL(pInitRequest8); // push pInitRequest
       
   142 
       
   143     if(iService)
       
   144         {
       
   145         HBufC8* pResponse8 = NULL;
       
   146 
       
   147         HBufC8* pNextMessage = NULL;
       
   148 
       
   149 #ifdef _SENDEBUG
       
   150         TPtr8 initRequest = pInitRequest8->Des();
       
   151 //wslog        FILELOGALL(_L("SenCoreServiceManager"), _L("AS_init_req.xml"), initRequest);
       
   152 #endif // _SENDEBUG
       
   153 
       
   154         retVal = iService->SubmitL(*pInitRequest8, KNullDesC8, *this, pResponse8);
       
   155         aRemoteConsumer.SetDataTrafficDetails(iAuthDetails); 
       
   156         CleanupStack::PushL(pResponse8); // pResponse8
       
   157 
       
   158         if(retVal == KErrNone && pResponse8)
       
   159             {
       
   160 #ifdef _SENDEBUG
       
   161             TPtr8 initResponse = pResponse8->Des();
       
   162 //wslog            FILELOGALL(_L("SenCoreServiceManager"), _L("AS_init_rsp.xml"), initResponse);
       
   163 #endif // _SENDEBUG
       
   164             retVal = ParseResponseL(*pResponse8, pNextMessage);
       
   165             }
       
   166 #ifdef _SENDEBUG
       
   167         else
       
   168             {
       
   169             CSLOG_L(aRemoteConsumer.ConnectionId(), KMinLogLevel ,"CIdWsfAuthenticationServiceClient::ValidateL:");
       
   170             CSLOG_FORMAT((aRemoteConsumer.ConnectionId(), KMinLogLevel , _L8("- SubmitL failed: %d"), retVal));
       
   171             }
       
   172 #endif // _SENDEBUG
       
   173 
       
   174         CleanupStack::PopAndDestroy(1); // pResponse8
       
   175 
       
   176         while ( pNextMessage )
       
   177             {
       
   178 #ifdef _SENDEBUG
       
   179             TPtr8 authRequest = pNextMessage->Des();
       
   180 //wslog            FILELOGALL(_L("SenCoreServiceManager"), _L("AS_req.xml"), authRequest);
       
   181 #endif // _SENDEBUG
       
   182             CleanupStack::PushL(pNextMessage); // pNextMessage
       
   183             retVal = iService->SubmitL(*pNextMessage, KNullDesC8, *this, pResponse8); 
       
   184             aRemoteConsumer.SetDataTrafficDetails(iAuthDetails); 	
       
   185             CleanupStack::PopAndDestroy(); // pNextMessage
       
   186             pNextMessage = NULL;
       
   187 
       
   188             if (retVal == KErrNone && pResponse8)
       
   189                 {
       
   190                 CleanupStack::PushL(pResponse8); // pResponse8
       
   191 #ifdef _SENDEBUG
       
   192                 TPtr8 authResponse = pResponse8->Des();
       
   193 //wslog                FILELOGALL(_L("WsLog"), _L("AS_rsp.xml"), authResponse);
       
   194 #endif // _SENDEBUG
       
   195                 retVal = ParseResponseL(*pResponse8, pNextMessage);
       
   196                 CleanupStack::PopAndDestroy(); // pResponse8
       
   197                 }
       
   198             }
       
   199         }
       
   200     CleanupStack::PopAndDestroy(2); // pInitRequest8, pInitMessage
       
   201     
       
   202     if ((retVal == KErrNone) && iDsSession->IsReadyL())
       
   203         {
       
   204          CSLOG_L(aRemoteConsumer.ConnectionId(), KMinLogLevel ,"- starting transaction.");
       
   205         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"- setting validator for DS session.");
       
   206 
       
   207         iDsSession->SetValidator(this);
       
   208         }
       
   209 
       
   210     return retVal;
       
   211     }
       
   212 
       
   213 
       
   214 TInt CIdWsfAuthenticationServiceClient::ParseResponseL(TDesC8& aMessage,
       
   215                                                        HBufC8*& aParsedMessage)
       
   216     {
       
   217     TInt retVal(KErrNone); 
       
   218 
       
   219     HBufC8* pNextMessage = NULL;
       
   220 
       
   221     CSenSecurityMechanism* pAuthenticator = NULL;
       
   222     CIdWsfSaslMessage* pSaslResponse =
       
   223                             CIdWsfSaslMessage::NewL(Framework().Manager());
       
   224     CleanupStack::PushL(pSaslResponse);
       
   225 
       
   226     pSaslResponse->SetReader(*Framework().Manager().XMLReader());
       
   227     pSaslResponse->BuildFrom(aMessage);
       
   228 
       
   229     if(pSaslResponse->IsOk())
       
   230         {
       
   231         iCState = EHasSession;
       
   232         TransactionCompleted();
       
   233         iSession = iDsSession;  // The "stateObject" in Java is
       
   234                                 // a "iSession" in here
       
   235         delete iAuthenticator;
       
   236         iAuthenticator = NULL;
       
   237         }
       
   238 
       
   239 
       
   240     if(iCState == EAuthServiceClient)
       
   241         {
       
   242         // expect a SASLResponse and look for the Status,
       
   243         // if "continue" instantiate a SASLMechanism
       
   244         // and let it deal with the message
       
   245         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("Parse Response - EAuthServiceClient")));
       
   246 
       
   247         TPtrC8 mechName = pSaslResponse->Mechanism();
       
   248 
       
   249         if(pSaslResponse->IsContinue())
       
   250             {
       
   251             pAuthenticator = Framework().Manager().MechanismNamedL(mechName);
       
   252             if(pAuthenticator)
       
   253                 {
       
   254                 // mechanism ECOM plugin found...
       
   255 
       
   256                 pAuthenticator->SetTransforms(pSaslResponse->Transforms());
       
   257                 
       
   258                 if (ipAccount->Password() == KNullDesC8)
       
   259                     {
       
   260                     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("Password will be asked from user.")));
       
   261                     iAttempts++;
       
   262                     }
       
   263                 
       
   264                 // This may invoke username/password UI prompt
       
   265                 pAuthenticator->SetIdentity(ipAccount, &Framework().Manager());
       
   266 
       
   267                 //if (pAuthenticator->IsPasswordFromUser() &&
       
   268                 //  pAuthenticator->MaxAttempts()>0)
       
   269                 if (pAuthenticator->MaxAttempts()>0)
       
   270                     {
       
   271                     //iAttempts++;
       
   272                     iMaxAttempts = pAuthenticator->MaxAttempts();
       
   273                     }
       
   274                 delete iAuthenticator;
       
   275                 iAuthenticator = NULL;
       
   276                 iAuthenticator = pAuthenticator;
       
   277                 iCState = ESaslMechanism;
       
   278                 }
       
   279             else
       
   280                 {
       
   281                 retVal = KErrNotFound;
       
   282                 }
       
   283             }
       
   284         if(pSaslResponse->IsAbort())
       
   285             {
       
   286             // if response is "abort" that means there is no matching
       
   287             // SASLMechanism and we should quite the whole process
       
   288             TransactionCompleted();
       
   289             pNextMessage = NULL;
       
   290             }
       
   291         }
       
   292 
       
   293     if(iCState == ESaslMechanism)
       
   294         {
       
   295         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("Parse Response - ESaslMechanism")));
       
   296         pAuthenticator = iAuthenticator;
       
   297 
       
   298         if (pSaslResponse->IsAbort())
       
   299             {
       
   300             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("SASL Response: Abort")));
       
   301             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KNormalLogLevel  ,_L("      Attempts : %d, Max Attempts : %d"),
       
   302                                 iAttempts, iMaxAttempts));
       
   303             // we could retry if allowed, otherwise quit the process
       
   304             if (iAttempts < iMaxAttempts)
       
   305                 {
       
   306                 pAuthenticator->ClearPasswordL();
       
   307                 iAttempts++;    // even if we cannot get a new password
       
   308                                 // we count this as an attempt!
       
   309 
       
   310                 TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("ESaslMechanism - Create a new init request")));
       
   311 
       
   312                 CIdWsfSaslMessage* pInitMessage =
       
   313                     CIdWsfSaslMessage::NewL(Framework().Manager(),
       
   314                                             KSaslXmlns,
       
   315                                             KSaslRequestName,
       
   316                                             KSaslRequestQName);
       
   317                 CleanupStack::PushL(pInitMessage); // push pInitMessage
       
   318                 pInitMessage->ConstructInitRequestFromL(iAuthenticator->Name(),
       
   319                                                         *ipAccount);
       
   320                 pNextMessage = pInitMessage->AsXmlL();
       
   321                 CleanupStack::PopAndDestroy(); // pInitMessage
       
   322                 CleanupStack::PushL(pNextMessage);
       
   323                 }
       
   324             else
       
   325                 {
       
   326                 TransactionCompleted();
       
   327                 }
       
   328             }
       
   329         else if (pSaslResponse->IsContinue())
       
   330             {
       
   331             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("SASL Response: Continue")));
       
   332             TPtrC8 mechanism = pSaslResponse->Mechanism();
       
   333             TPtrC8 authName = pAuthenticator->Name();
       
   334             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,
       
   335                 _L8(" Mechanism : %S, Authenticator Name : %S"), &mechanism,
       
   336                                                                  &authName
       
   337                 ));
       
   338 
       
   339             if(mechanism != authName)
       
   340                 {
       
   341                 TLSLOG(KSenCoreServiceManagerLogChannelBase  , KNormalLogLevel ,(
       
   342                 _L("pSaslResponse->Mechanism() != pAuthenticator->Name()")));
       
   343                 //switch to another mechanism
       
   344                 pAuthenticator = Framework().Manager().MechanismNamedL(
       
   345                                                 pSaslResponse->Mechanism());
       
   346                 if(pAuthenticator)
       
   347                     {
       
   348                     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("Delete iAuthenticator")));
       
   349                     delete iAuthenticator; // free memory
       
   350 
       
   351                     // no need to delete after initialize from is in use,
       
   352                     // because iAuthenticator == pAuthenticator !!
       
   353                     iAuthenticator = pAuthenticator;
       
   354                     }
       
   355                 else
       
   356                     {
       
   357                     retVal = KErrNotFound;
       
   358                     }
       
   359                 }
       
   360             if(pAuthenticator)
       
   361                 {
       
   362                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,
       
   363                 "ESaslMechanism - Handle response and create a new request");
       
   364                 CIdWsfSaslMessage* pSaslRequest =
       
   365                     CIdWsfSaslMessage::NewL(Framework().Manager(),
       
   366                                             KSaslXmlns,
       
   367                                             KSaslRequestName,
       
   368                                             KSaslRequestQName);
       
   369 
       
   370                 CleanupStack::PushL(pSaslRequest); // pSaslRequest
       
   371 
       
   372                 retVal = pAuthenticator->HandleResponseL(*pSaslResponse,
       
   373                                                             *pSaslRequest);
       
   374                 pNextMessage = pSaslRequest->AsXmlL();
       
   375                 CleanupStack::PopAndDestroy();  // pSaslRequest
       
   376                 CleanupStack::PushL(pNextMessage);
       
   377                 }
       
   378             else
       
   379                 {
       
   380                 retVal = KErrNotFound;
       
   381                 }
       
   382             } // end IsContinue()
       
   383         }
       
   384 
       
   385     if(iCState == EHasSession)
       
   386         {
       
   387         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("Parse Response - EHasSession")));
       
   388         // expect a DSQueryResponse with the ResourceOffering for the DS
       
   389         retVal = CIdWsfDiscoveryServiceClient::ProcessResponseL(pSaslResponse);
       
   390 
       
   391         if (iService)
       
   392             {
       
   393             iDsSession->SetClientServerIntervalL(
       
   394                                             iService->ClientServerInterval());// codescannerwarnings
       
   395             }
       
   396 
       
   397         if ( iService && iDsSession && iDsSession->IsReadyL())
       
   398             {
       
   399             TPtrC8 providerId = iService->ProviderId();
       
   400             iDsSession->SetTrustAnchorL(providerId);
       
   401             iCState = ENotInitialized;  // check if iSession should
       
   402                                         // be deleted here!?
       
   403             }
       
   404         }
       
   405 
       
   406     if(pNextMessage)
       
   407         {
       
   408         CleanupStack::Pop(); // pNextMessage
       
   409         }
       
   410 
       
   411     CleanupStack::PopAndDestroy(); // pSaslResponse
       
   412     aParsedMessage = pNextMessage;  // assign data pointer
       
   413 
       
   414     return retVal;
       
   415     }
       
   416 
       
   417 
       
   418 void CIdWsfAuthenticationServiceClient::SetStatusL(const TInt /*aStatus */)
       
   419     {
       
   420     // No implementation in Java
       
   421     }
       
   422 
       
   423 void CIdWsfAuthenticationServiceClient::SetAccount(
       
   424                                     CSenIdentityProvider* aIdentityProvicer)
       
   425     {
       
   426     ipAccount = aIdentityProvicer;
       
   427     }
       
   428 
       
   429 
       
   430 TBool CIdWsfAuthenticationServiceClient::HasSuperClass( TDescriptionClassType aType )
       
   431     {
       
   432     if( aType == MSenServiceDescription::EIdWsfDiscoveryServiceClient ) // direct superclass!    
       
   433         {
       
   434         // If asked type is the know *direct* father/mother, return true:
       
   435         return ETrue; 
       
   436         } 
       
   437     else
       
   438         {
       
   439         // Otherwise, ask from superclass (chain, recursively)
       
   440         return CIdWsfDiscoveryServiceClient::HasSuperClass( aType ); 
       
   441         }
       
   442     }
       
   443 
       
   444 void CIdWsfAuthenticationServiceClient::SetDataTrafficDetails( TSenDataTrafficDetails& aDetails) 
       
   445 	{
       
   446 	iAuthDetails = aDetails;
       
   447 	}
       
   448 
       
   449 // End of file
       
   450 
       
   451