internal async Task <TResult> UpdateAsync <TResult>(Persistence.AuthenticationRequest authenticationRequest,
                                                            Guid sessionId, Guid stateId,
                                                            CredentialValidationMethodTypes method, IDictionary <string, string> extraParams,
                                                            Func <Guid, string, IDictionary <string, string>, Task> saveAuthRequest,
                                                            Func <Guid, Guid, string, string, AuthenticationActions, IDictionary <string, string>, Uri, TResult> onSuccess,
                                                            Func <string, TResult> onInvalidToken,
                                                            Func <string, TResult> onNotConfigured,
                                                            Func <string, TResult> onFailure)
        {
            if (!authenticationRequest.authorizationId.HasValue)
            {
                return(onFailure("The credential is corrupt"));
            }

            var authenticationId = authenticationRequest.authorizationId.Value;

            return(await await dataContext.Accesses.CreateAsync(authenticationRequest.id, authenticationId,
                                                                method, extraParams,
                                                                async() => await await context.Sessions.CreateSessionAsync(sessionId, authenticationId,
                                                                                                                           async(token, refreshToken) =>
            {
                await saveAuthRequest(authenticationId, token, extraParams);
                return onSuccess(stateId, authenticationId, token, refreshToken,
                                 AuthenticationActions.access, extraParams,
                                 authenticationRequest.redirect);
            },
                                                                                                                           onNotConfigured.AsAsyncFunc()),
                                                                () => onInvalidToken("Login is already mapped to an access.").ToTask()));
        }
 private static Session Convert(Persistence.AuthenticationRequest authenticationRequestStorage)
 {
     return(new Session
     {
         id = authenticationRequestStorage.id,
         method = authenticationRequestStorage.method,
         action = authenticationRequestStorage.action,
         token = authenticationRequestStorage.token,
         authorizationId = authenticationRequestStorage.authorizationId,
         extraParams = authenticationRequestStorage.extraParams,
         redirectUrl = authenticationRequestStorage.redirect,
     });
 }