Пример #1
0
        //private static Task<TResult> UnmappedCredentialAsync<TResult>(
        //        string subject, IDictionary<string, string> extraParams,
        //        EastFive.Azure.Auth.Method authentication, Authorization authorization,
        //        IProvideAuthorization authorizationProvider,
        //        IAuthApplication application,
        //        Uri baseUri,
        //    Func<Guid, TResult> createMapping,
        //    Func<TResult> onAllowSelfServe,
        //    Func<Uri, TResult> onInterceptProcess,
        //    Func<string, TResult> onFailure,
        //    TelemetryClient telemetry)
        //{
        //    return application.OnUnmappedUserAsync(subject, extraParams,
        //            authentication, authorization, authorizationProvider, baseUri,
        //        (accountId) => createMapping(accountId),
        //        () => onAllowSelfServe(),
        //        (callback) => onInterceptProcess(callback),
        //        () =>
        //        {
        //            var message = "Token is not connected to a user in this system";
        //            telemetry.TrackException(new ResponseException(message));
        //            return onFailure(message);
        //        });
        //}

        public static async Task <TResult> MapAccountAsync <TResult>(Authorization authorization,
                                                                     Guid internalAccountId, string externalAccountKey,
                                                                     IInvokeApplication endpoints,
                                                                     Uri baseUri,
                                                                     AzureApplication application, IHttpRequest request,
                                                                     Func <Uri, TResult> onRedirect,
                                                                     Func <string, TResult> onFailure,
                                                                     TelemetryClient telemetry)
        {
            // applies when intercept process is used
            if (authorization.accountIdMaybe.IsDefault())
            {
                authorization = await authorization.authorizationRef.StorageUpdateAsync(
                    async (a, saveAsync) =>
                {
                    a.accountIdMaybe = internalAccountId;
                    await saveAsync(a);
                    return(a);
                });
            }

            return(await await AccountMapping.CreateByMethodAndKeyAsync(authorization, externalAccountKey,
                                                                        internalAccountId,
                                                                        ProcessAsync,
                                                                        ProcessAsync));

            async Task <TResult> ProcessAsync()
            {
                return(await await Method.ById(authorization.Method, application,
                                               async method =>
                {
                    return await await method.GetLoginProviderAsync(application,
                                                                    (loginProviderMethodName, loginProvider) =>
                    {
                        return CreateLoginResponseAsync(
                            internalAccountId, authorization.parameters,
                            method, authorization,
                            application, request, endpoints, baseUri, loginProvider,
                            (url, modifier) => onRedirect(url),
                            onFailure,
                            telemetry);
                    },
                                                                    () =>
                    {
                        return onFailure("Login provider is no longer supported by the system").AsTask();
                    });
                },
                                               () => onFailure("Method no longer suppored.").AsTask()));
            }
        }
 public static async Task <TResult> MapAccountAsync <TResult>(Authorization authorization,
                                                              Guid internalAccountId, string externalAccountKey,
                                                              Guid requestId,
                                                              Uri baseUri,
                                                              AzureApplication application,
                                                              Func <Uri, TResult> onRedirect,
                                                              Func <string, TResult> onFailure,
                                                              TelemetryClient telemetry)
 {
     return(await await AccountMapping.CreateByMethodAndKeyAsync(authorization, externalAccountKey,
                                                                 internalAccountId,
                                                                 async() =>
     {
         return await await Method.ById(authorization.Method, application,
                                        async method =>
         {
             return await await method.GetLoginProviderAsync(application,
                                                             (loginProviderMethodName, loginProvider) =>
             {
                 return CreateLoginResponseAsync(requestId,
                                                 internalAccountId, authorization.parameters,
                                                 method, authorization,
                                                 baseUri,
                                                 application, loginProvider,
                                                 (url) => onRedirect(url),
                                                 onFailure,
                                                 telemetry);
             },
                                                             () =>
             {
                 return onFailure("Login provider is no longer supported by the system").AsTask();
             });
         },
                                        () => onFailure("Method no longer suppored.").AsTask());
     },
                                                                 (why) => onFailure(why).AsTask()));
 }
        public static async Task <TResult> ProcessAsync <TResult>(Authorization authorization,
                                                                  Func <Authorization, Task> saveAsync,
                                                                  Method authentication, string externalAccountKey,
                                                                  IDictionary <string, string> extraParams,
                                                                  Guid requestId, Uri baseUri, AzureApplication application, IProvideLogin loginProvider,
                                                                  Func <Uri, TResult> onRedirect,
                                                                  Func <string, TResult> onGeneralFailure,
                                                                  TelemetryClient telemetry)
        {
            authorization.authorized             = true;
            authorization.LocationAuthentication = null;
            var result = await await AccountMapping.FindByMethodAndKeyAsync(authentication.authenticationId, externalAccountKey,
                                                                            authorization,

                                                                            // Found
                                                                            async internalAccountId =>
            {
                authorization.parameters     = extraParams;
                authorization.accountIdMaybe = internalAccountId;
                await saveAsync(authorization);
                return(await CreateLoginResponseAsync(requestId,
                                                      internalAccountId, extraParams,
                                                      authentication, authorization,
                                                      baseUri,
                                                      application, loginProvider,
                                                      onRedirect,
                                                      onGeneralFailure,
                                                      telemetry));
            },

                                                                            // Not found
                                                                            async() =>
            {
                return(await await UnmappedCredentialAsync(externalAccountKey, extraParams,
                                                           authentication, authorization,
                                                           loginProvider, application, baseUri,

                                                           // Create mapping
                                                           async(internalAccountId) =>
                {
                    authorization.parameters = extraParams;
                    authorization.accountIdMaybe = internalAccountId;
                    await saveAsync(authorization);
                    return await await AccountMapping.CreateByMethodAndKeyAsync(authorization, externalAccountKey,
                                                                                internalAccountId,
                                                                                () =>
                    {
                        return CreateLoginResponseAsync(requestId,
                                                        internalAccountId, extraParams,
                                                        authentication, authorization,
                                                        baseUri,
                                                        application, loginProvider,
                                                        onRedirect,
                                                        onGeneralFailure,
                                                        telemetry);
                    },
                                                                                (why) => onGeneralFailure(why).AsTask());
                },

                                                           // Allow self serve
                                                           async() =>
                {
                    authorization.parameters = extraParams;
                    await saveAsync(authorization);
                    return await CreateLoginResponseAsync(requestId,
                                                          default(Guid?), extraParams,
                                                          authentication, authorization,
                                                          baseUri,
                                                          application, loginProvider,
                                                          onRedirect,
                                                          onGeneralFailure,
                                                          telemetry);
                },

                                                           // Intercept process
                                                           async(interceptionUrl) =>
                {
                    authorization.parameters = extraParams;
                    await saveAsync(authorization);
                    return onRedirect(interceptionUrl);
                },

                                                           // Failure
                                                           async(why) =>
                {
                    // Save params so they can be used later
                    authorization.parameters = extraParams;
                    await saveAsync(authorization);
                    return onGeneralFailure(why);
                },
                                                           telemetry));
            });

            return(result);
        }