webservices/wsstar/wsstarplugin/src/wsstarvalidatehandler.cpp
changeset 0 62f9d29f7211
child 1 272b002df977
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2006-2006 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 "wsstarvalidatehandler.h"
       
    26 #include "wsstartrustclient.h"
       
    27 #include "sendebug.h"
       
    28 #include "senlogger.h"
       
    29 
       
    30 class CWSStarHandlerContext;
       
    31 
       
    32 //---------------------------------------------------------------------------
       
    33 // Create instance of concrete ECOM interface implementation
       
    34 //---------------------------------------------------------------------------
       
    35 //
       
    36 CWSStarValidateHandler* CWSStarValidateHandler::NewL(TAny* aHandlerCtx)
       
    37     {
       
    38     
       
    39     MSenHandlerContext* handlerCtx =
       
    40         reinterpret_cast<MSenHandlerContext*>(aHandlerCtx);
       
    41     CWSStarValidateHandler* self   = new (ELeave) CWSStarValidateHandler(*handlerCtx);
       
    42     CleanupStack::PushL (self);
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop(self);
       
    45     return self;
       
    46     }
       
    47 
       
    48 
       
    49 //---------------------------------------------------------------------------
       
    50 // Constructor
       
    51 //---------------------------------------------------------------------------
       
    52 //
       
    53 CWSStarValidateHandler::CWSStarValidateHandler(MSenHandlerContext& aCtx):CSenSessionHandler(aCtx)
       
    54     {
       
    55     }
       
    56 
       
    57 //---------------------------------------------------------------------------
       
    58 // Destructor
       
    59 //---------------------------------------------------------------------------
       
    60 //
       
    61 CWSStarValidateHandler::~CWSStarValidateHandler()
       
    62     {
       
    63     }
       
    64 
       
    65 //---------------------------------------------------------------------------
       
    66 // Second phase construction.
       
    67 //---------------------------------------------------------------------------
       
    68 //
       
    69 void CWSStarValidateHandler::ConstructL()
       
    70     {
       
    71     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KNormalLogLevel,(_L("CWSStarValidateHandler::ConstructL()")));
       
    72     const TDesC8* version = iHandlerContext.GetDesC8L(HandlerContextKey::KVersion);
       
    73     if (version && 
       
    74         (*version == WSTrust::KTrustNamespace200502 || *version == WSTrust::KTrustNamespace200404))
       
    75         {
       
    76         iVersion = version;
       
    77         }
       
    78     else
       
    79         {
       
    80         iVersion = &WSTrust::KTrustNamespace200502; //passport interface 2
       
    81         //&WSTrust::KTrustNamespace200404;
       
    82         }
       
    83     }
       
    84     
       
    85 
       
    86 //---------------------------------------------------------------------------
       
    87 // the most emergent method in message handler interface.
       
    88 // Each sub-class must implement this method thus providing message processing routine.
       
    89 //---------------------------------------------------------------------------
       
    90 //
       
    91 TInt CWSStarValidateHandler::InvokeL(MSenSessionContext& aCtx)
       
    92     {
       
    93 
       
    94 //getting data from input
       
    95     CWSStarServiceSession* pNewSession =
       
    96             (CWSStarServiceSession*)aCtx.GetSenRemoteServiceSessionL(
       
    97             WSStarContextKeys::KServiceSession());
       
    98     if (pNewSession->ProviderID() == KNullDesC8)
       
    99         {
       
   100         return KErrNone;
       
   101         }
       
   102 //try to find identity provider for curennt webService using its ProviderId
       
   103     CSenIdentityProvider* pIdentityProvider = IdentityProviderFromCoreL(
       
   104             pNewSession->ProviderID());
       
   105     if (!pIdentityProvider)
       
   106         {
       
   107         return KErrNotFound;
       
   108         }
       
   109     
       
   110     //in session context we have info
       
   111     const TBool* renewingPtr = ((MSenContext&)aCtx).GetIntL(WSStarContextKeys::KReAuthNeeded);
       
   112     TBool renewing;
       
   113     if (renewingPtr)
       
   114         {
       
   115         renewing = *renewingPtr;
       
   116         }
       
   117     else
       
   118         {
       
   119         renewing = EFalse;
       
   120         }
       
   121     const TBool* onlySharingPtr = ((MSenContext&)aCtx).GetIntL(WSStarContextKeys::KOnlySharing);
       
   122     TBool onlySharing;
       
   123     if (onlySharingPtr)
       
   124         {
       
   125         onlySharing = *onlySharingPtr;
       
   126         }
       
   127     else
       
   128         {
       
   129         onlySharing = ETrue;
       
   130         }
       
   131     
       
   132     CWSStarTrustClient* pSTSClient = STSClientL(pIdentityProvider);
       
   133     if (pSTSClient)
       
   134             {
       
   135             TInt error(KErrNone);
       
   136             pSTSClient->SetHandlerCtx(&iHandlerContext);
       
   137             pSTSClient->SetVersion(iVersion);
       
   138             //validate session of concrete WebService
       
   139             HBufC8* errorMessage = NULL;
       
   140              // fix for compiler warning #550-D reverted back
       
   141             TRAPD(lerror,error = pSTSClient->ValidateL(*pNewSession, renewing, errorMessage, onlySharing);)    
       
   142             lerror = 0; // not used in release builds
       
   143             ((MSenContext&)aCtx).Update(WSStarContextKeys::KReAuthNeeded, EFalse);
       
   144             if (errorMessage)
       
   145                 {
       
   146                 ((MSenContext&)aCtx).Update(WSStarContextKeys:: KErrMessage,  *errorMessage);
       
   147                 }
       
   148             delete errorMessage;
       
   149             errorMessage = NULL;
       
   150             //if credential expired session will use validator to obtain new
       
   151             
       
   152             // Core / XML DAO takes ownership of new session (will keep in array of SD):
       
   153             // - new session HAS
       
   154             //      - credential
       
   155             //      - context which keep for example key for signing message        
       
   156             // - if duplicate (equal primary keys) exists, it is deleted
       
   157             return error;
       
   158             }
       
   159        else
       
   160             {
       
   161             return KErrNotFound;
       
   162             }
       
   163     
       
   164     }
       
   165     
       
   166 SenHandler::THandlerDirection CWSStarValidateHandler::Direction() const
       
   167     {
       
   168         return SenHandler::EBoth;
       
   169     };
       
   170 SenHandler::THandlerPhase CWSStarValidateHandler::Phase()
       
   171     {
       
   172         return SenHandler::EValidate;
       
   173     };
       
   174     
       
   175 //---------------------------------------------------------------------------
       
   176 // Obtain or construct STS client
       
   177 //---------------------------------------------------------------------------
       
   178 //
       
   179   
       
   180 CWSStarTrustClient* CWSStarValidateHandler::STSClientL(CSenIdentityProvider*& aIdentityProvider)
       
   181 {
       
   182     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KNormalLogLevel,(_L("CWSStarPlugin::STSClientL")));
       
   183 
       
   184     CWSStarTrustClient* pSTSClient = NULL;
       
   185     //providerId from Identities.xml
       
   186     TPtrC8 providerId(KNullDesC8);
       
   187     if(aIdentityProvider)
       
   188         {
       
   189         providerId.Set(aIdentityProvider->ProviderID());
       
   190         }
       
   191 
       
   192     RPointerArray<CSenWSDescription> serviceDescriptions;
       
   193     CleanupClosePushL(serviceDescriptions);
       
   194         
       
   195     //try to find STS session in cache,some STS can be registered
       
   196     TInt retVal(KErrNone);
       
   197     
       
   198     TRAPD(error, retVal = iHandlerContext.GetSenCoreServiceManager()->ServiceDescriptionsL(serviceDescriptions, KWSStarSTSContract()));
       
   199     if(!retVal&&!error)
       
   200         {
       
   201         RPointerArray<CSenWSDescription> matches;
       
   202         CleanupClosePushL(matches);
       
   203         
       
   204         for(TInt i = 0; i < serviceDescriptions.Count(); i++)
       
   205             {
       
   206             //select XMLDescription from DAO where contract = STS, so we can cast
       
   207             pSTSClient = (CWSStarTrustClient*)serviceDescriptions[i];
       
   208             if(providerId.Length()>0)
       
   209                 {
       
   210                 TPtrC8 providerIDFromDescription = pSTSClient->ProviderID();
       
   211                 if(providerIDFromDescription == providerId)
       
   212                     {
       
   213                     matches.AppendL(pSTSClient);
       
   214                     }
       
   215                 }
       
   216             else
       
   217                 {
       
   218                 matches.AppendL(pSTSClient);
       
   219                 }
       
   220             }
       
   221         if(matches.Count())
       
   222             {
       
   223             pSTSClient =
       
   224                 reinterpret_cast<CWSStarTrustClient*>(matches[0]);
       
   225             pSTSClient->SetAccount(aIdentityProvider);
       
   226             }
       
   227         else
       
   228             {
       
   229             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KNormalLogLevel,(_L("CWSStarValidateHandler::STSClient - No matching STS description available!")));
       
   230             pSTSClient = NULL;
       
   231             }
       
   232     
       
   233         //if we find multi sts client (different only with lifetime, probably Secure Context Token lifetime)
       
   234         if(matches.Count() > 1)
       
   235             {
       
   236             // search for a instance with longest validity
       
   237             for(TInt i = matches.Count()-1; i > 0 ; i--)
       
   238                 {
       
   239                 CWSStarTrustClient* pNewerSTSClient =
       
   240                   reinterpret_cast<CWSStarTrustClient*>(matches[i]);
       
   241                 CWSStarServiceSession* newerSession = (CWSStarServiceSession*)pNewerSTSClient->ServiceSession();
       
   242                 CWSStarServiceSession* session = (CWSStarServiceSession*)pSTSClient->ServiceSession();
       
   243                 
       
   244 				if(newerSession && session) // STSClient Session was not set, if not validated 
       
   245 					{
       
   246 					if(newerSession->ValidUntilL() > session->ValidUntilL())
       
   247 					  	{
       
   248 					    pSTSClient = pNewerSTSClient;
       
   249 					    pSTSClient->SetAccount(aIdentityProvider);
       
   250 					    }
       
   251 					}
       
   252                 }
       
   253             }
       
   254             CleanupStack::PopAndDestroy(2, &serviceDescriptions);
       
   255         }
       
   256     else
       
   257         {
       
   258           CleanupStack::PopAndDestroy(&serviceDescriptions);
       
   259         }
       
   260     
       
   261     
       
   262     //no session, so we have create new STS client, temporary code 
       
   263     // architects says:- Yes... You must have AS registered into DAO (DB) sessions db
       
   264     /*if (!pSTSClient)
       
   265         {
       
   266         CSIF* sif = (CSIF*)iHandlerContext.GetAny(HandlerContextKey::KSIF());
       
   267         pSTSClient = CWSStarTrustClient::NewL(*sif,*Log());
       
   268         }*/
       
   269     return pSTSClient;
       
   270 }
       
   271 
       
   272 //---------------------------------------------------------------------------
       
   273 // Create IdentityProvider from CoreManager
       
   274 //---------------------------------------------------------------------------
       
   275 //
       
   276 CSenIdentityProvider* CWSStarValidateHandler::IdentityProviderFromCoreL(
       
   277                                             const TDesC8& aProviderID)
       
   278     {
       
   279     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KNormalLogLevel,"CWSStarValidateHandler::IdentityProviderLFromCore");
       
   280     CSenIdentityProvider* pIdentityProvider = NULL;
       
   281     if(aProviderID.Length() > 0)
       
   282         {
       
   283         CDesC8ArrayFlat* array = new (ELeave) CDesC8ArrayFlat(1);
       
   284         CleanupStack::PushL(array);
       
   285         array->AppendL(aProviderID);
       
   286         pIdentityProvider = iHandlerContext.GetSenCoreServiceManager()->
       
   287                 IdentityProviderL(*array, ETrue);
       
   288         CleanupStack::PopAndDestroy(array);
       
   289         }
       
   290     return pIdentityProvider;
       
   291     }
       
   292 
       
   293 //---------------------------------------------------------------------------
       
   294 // Init implementation
       
   295 //---------------------------------------------------------------------------
       
   296 //
       
   297 TInt CWSStarValidateHandler::InitL(MSenHandlerContext& aCtx)
       
   298     {
       
   299     iHandlerContext = aCtx;
       
   300     return KErrNone;
       
   301     }
       
   302 RFileLogger* CWSStarValidateHandler::Log() const
       
   303     {
       
   304     RFileLogger* pLog = NULL;
       
   305     TRAP_IGNORE( pLog = (RFileLogger*)iHandlerContext.GetAnyL(HandlerContextKey::KLogger); )
       
   306     return pLog;
       
   307     }
       
   308     
       
   309 // END OF FILE
       
   310