Exemple #1
0
        internal async Task <TResult> GetPasswordCredentialByActorAsync <TResult>(Guid actorId,
                                                                                  EastFive.Api.SessionToken security, AzureApplication application,
                                                                                  Func <PasswordCredential[], TResult> success,
                                                                                  Func <TResult> notFound,
                                                                                  Func <TResult> onUnauthorized,
                                                                                  Func <string, TResult> onServiceNotAvailable)
        {
            if (!await application.CanAdministerCredentialAsync(actorId, security))
            {
                return(onUnauthorized());
            }

            return(await await this.dataContext.PasswordCredentials.FindPasswordCredentialByActorAsync(actorId,
                                                                                                       async (credentials) =>
            {
                var pwCreds = await credentials.Select(
                    async credential =>
                {
                    return await managmentProvider.GetAuthorizationAsync(credential.loginId,
                                                                         (loginInfo) =>
                    {
                        return new PasswordCredential
                        {
                            id = credential.id,
                            displayName = loginInfo.displayName,
                            actorId = actorId,
                            userId = loginInfo.userName,
                            isEmail = loginInfo.isEmail,
                            forceChangePassword = loginInfo.forceChangePassword,
                            lastSent = credential.lastSent,
                        };
                    },
                                                                         () => default(PasswordCredential?), // TODO: Log this
                                                                         (why) => default(PasswordCredential?),
                                                                         () => default(PasswordCredential?),
                                                                         (why) => default(PasswordCredential?));
                })
                              .WhenAllAsync()
                              .SelectWhereHasValueAsync()
                              .ToArrayAsync();
                return success(pwCreds);
            },
                                                                                                       () => notFound().ToTask()));
        }
Exemple #2
0
 internal async Task <TResult> DeletePasswordCredentialAsync <TResult>(Guid passwordCredentialId,
                                                                       EastFive.Api.SessionToken security, AzureApplication application,
                                                                       Func <TResult> success,
                                                                       Func <TResult> onUnauthorized,
                                                                       Func <TResult> notFound,
                                                                       Func <string, TResult> onServiceNotAvailable)
 {
     return(await await this.dataContext.PasswordCredentials.DeletePasswordCredentialAsync(passwordCredentialId,
                                                                                           async (loginId) =>
     {
         if (!await application.CanAdministerCredentialAsync(loginId, security))
         {
             return onUnauthorized();
         }
         return await managmentProvider.DeleteAuthorizationAsync(loginId,
                                                                 success,
                                                                 onServiceNotAvailable,
                                                                 () => onServiceNotAvailable("Not supported"),
                                                                 onServiceNotAvailable);
     },
                                                                                           () => notFound().ToTask()));
 }
Exemple #3
0
        internal async Task <TResult> GetPasswordCredentialAsync <TResult>(Guid passwordCredentialId,
                                                                           EastFive.Api.SessionToken security, AzureApplication application,
                                                                           Func <PasswordCredential, TResult> success,
                                                                           Func <TResult> notFound,
                                                                           Func <TResult> onUnauthorized,
                                                                           Func <string, TResult> onServiceNotAvailable)
        {
            return(await await this.dataContext.PasswordCredentials.FindPasswordCredentialAsync(passwordCredentialId,
                                                                                                async (actorId, loginId, lastSent) =>
            {
                if (!await application.CanAdministerCredentialAsync(actorId, security))
                {
                    return onUnauthorized();
                }

                return await this.managmentProvider.GetAuthorizationAsync(loginId,
                                                                          (loginInfo) =>
                {
                    return success(new PasswordCredential
                    {
                        id = passwordCredentialId,
                        displayName = loginInfo.displayName,
                        actorId = actorId,
                        userId = loginInfo.userName,
                        isEmail = loginInfo.isEmail,
                        forceChangePassword = loginInfo.forceChangePassword,
                        lastSent = lastSent,
                    });
                },
                                                                          () => notFound(), // TODO: Log this
                                                                          (why) => onServiceNotAvailable(why),
                                                                          () => onServiceNotAvailable("not supported"),
                                                                          (why) => onServiceNotAvailable(why));
            },
                                                                                                () => notFound().ToTask()));
        }
Exemple #4
0
        public async Task <TResult> CreatePasswordCredentialsAsync <TResult>(Guid passwordCredentialId, Guid actorId,
                                                                             string displayName, string username, bool isEmail, string token, bool forceChange,
                                                                             DateTime?emailLastSent, Uri callbackUrl,
                                                                             EastFive.Api.SessionToken security, AzureApplication application,
                                                                             Func <TResult> onSuccess,
                                                                             Func <TResult> credentialAlreadyExists,
                                                                             Func <Guid, TResult> onUsernameAlreadyInUse,
                                                                             Func <TResult> onPasswordInsufficent,
                                                                             Func <TResult> onRelationshipAlreadyExists,
                                                                             Func <TResult> onLoginAlreadyUsed,
                                                                             Func <TResult> onUnathorized,
                                                                             Func <TResult> onServiceNotAvailable,
                                                                             Func <string, TResult> onFailure)
        {
            if (!await application.CanAdministerCredentialAsync(actorId, security))
            {
                return(onUnathorized());
            }

            return(await await application.InstantiateAll <IProvideLoginManagement>()
                   .FirstAsync(
                       async managmentProvider =>
            {
                if (string.IsNullOrWhiteSpace(displayName))
                {
                    displayName = "User";
                }

                return await await managmentProvider.CreateAuthorizationAsync(displayName,
                                                                              username, isEmail, token, forceChange,
                                                                              async loginId =>
                {
                    var result = await await dataContext.PasswordCredentials.CreatePasswordCredentialAsync(
                        passwordCredentialId, actorId, loginId, emailLastSent,
                        async() =>
                    {
                        if (!isEmail || !emailLastSent.HasValue)
                        {
                            return onSuccess();
                        }

                        return await Web.Configuration.Settings.GetUri(EastFive.Security.SessionServer.Configuration.AppSettings.LandingPage,
                                                                       async(landingPage) =>
                        {
                            var method = CredentialValidationMethodTypes.Password.ToString();
                            return await await context.Sessions.CreateLoginAsync(Guid.NewGuid(),
                                                                                 method, landingPage, landingPage,
                                                                                 (type) => callbackUrl,
                                                                                 async(authenticationRequest) =>
                            {
                                var loginUrl = authenticationRequest.loginUrl;
                                var resultMail = await SendInvitePasswordAsync(username, username, token, loginUrl,
                                                                               onSuccess, onServiceNotAvailable, onFailure);
                                return resultMail;
                            },
                                                                                 "GUID not unique".AsFunctionException <Task <TResult> >(),
                                                                                 "Password system said not available while in use".AsFunctionException <Task <TResult> >(),
                                                                                 "Password system said not initialized while in use".AsFunctionException <string, Task <TResult> >(),
                                                                                 onFailure.AsAsyncFunc());
                        },
                                                                       onFailure.AsAsyncFunc());
                    },
                        async() =>
                        await managmentProvider.DeleteAuthorizationAsync(loginId,
                                                                         () => credentialAlreadyExists(),
                                                                         (why) => onFailure(why),
                                                                         () => onFailure("Service became unsupported after credentialAlreadyExists"),
                                                                         (why) => onFailure(why)),
                        async() =>
                        await managmentProvider.DeleteAuthorizationAsync(loginId,
                                                                         () => onRelationshipAlreadyExists(),
                                                                         (why) => onFailure(why),
                                                                         () => onFailure("Service became unsupported after onRelationshipAlreadyExists"),
                                                                         (why) => onFailure(why)),
                        async() =>
                        await managmentProvider.DeleteAuthorizationAsync(loginId,
                                                                         () => onRelationshipAlreadyExists(),
                                                                         (why) => onFailure(why),
                                                                         () => onFailure("Service became unsupported after onLoginAlreadyUsed"),
                                                                         (why) => onFailure(why)));
                    return result;
                },
                                                                              (loginId) => onUsernameAlreadyInUse(loginId).ToTask(),
                                                                              () => onPasswordInsufficent().ToTask(),
                                                                              (why) => onFailure(why).ToTask(),
                                                                              () => onFailure("Service not supported").ToTask(),
                                                                              (why) => onFailure(why).ToTask());
            },
                       onServiceNotAvailable.AsAsyncFunc()));
        }
Exemple #5
0
        internal async Task <TResult> UpdatePasswordCredentialAsync <TResult>(Guid passwordCredentialId,
                                                                              string password, bool forceChange, DateTime?emailLastSent,
                                                                              EastFive.Api.SessionToken security, AzureApplication application,
                                                                              Func <TResult> onSuccess,
                                                                              Func <TResult> onNotFound,
                                                                              Func <TResult> onUnathorized,
                                                                              Func <TResult> onServiceNotAvailable,
                                                                              Func <string, TResult> onFailure)
        {
            if (!security.accountIdMaybe.HasValue)
            {
                return(onUnathorized());
            }
            var resultUpdatePassword = await dataContext.PasswordCredentials.UpdatePasswordCredentialAsync(passwordCredentialId,
                                                                                                           async (actorId, loginId, emailLastSentCurrent, updateEmailLastSentAsync) =>
            {
                DiscriminatedDelegate <Guid, TResult, Task <TResult> > resultSuccess =
                    (success, fail) => success(loginId);
                DiscriminatedDelegate <Guid, TResult, Task <TResult> > resultNotFound =
                    (success, fail) => fail(onNotFound());
                var failureMessage = "";
                DiscriminatedDelegate <Guid, TResult, Task <TResult> > resultFailure =
                    (success, fail) => fail(onFailure(failureMessage));

                if (!await application.CanAdministerCredentialAsync(actorId, security))
                {
                    return((success, fail) => fail(onUnathorized()));
                }

                if (emailLastSent.HasValue &&
                    (!emailLastSentCurrent.HasValue ||
                     emailLastSent.Value > emailLastSentCurrent.Value))
                {
                    var resultGetLogin = await await managmentProvider.GetAuthorizationAsync(loginId,
                                                                                             async(loginInfo) =>
                    {
                        var email = await Library.configurationManager.GetActorAdministrationEmailAsync(actorId,
                                                                                                        security.accountIdMaybe.Value, security.claims,
                                                                                                        address => address,
                                                                                                        () => string.Empty,
                                                                                                        () => string.Empty,
                                                                                                        (why) => string.Empty);
                        if (email.IsNullOrWhiteSpace())
                        {
                            email = loginInfo.GetEmail(
                                address => address,
                                () => string.Empty);
                        }

                        if (string.IsNullOrWhiteSpace(email))
                        {
                            failureMessage = "No email address found for user.";
                            return(resultFailure);
                        }

                        return(await Web.Configuration.Settings.GetUri(EastFive.Security.SessionServer.Configuration.AppSettings.LandingPage,
                                                                       async(landingPage) =>
                        {
                            if (string.IsNullOrWhiteSpace(password))
                            {
                                password = Membership.GeneratePassword(8, 2);
                            }
                            // TODO: the purpose of the next line is to send the password.
                            // If we don't want it sent, don't update the last sent value!!!
                            return await await SendInvitePasswordAsync(email, loginInfo.userName, password, landingPage,
                                                                       async() =>
                            {
                                await updateEmailLastSentAsync(emailLastSent.Value);
                                return resultSuccess;
                            },
                                                                       () =>
                            {
                                DiscriminatedDelegate <Guid, TResult, Task <TResult> > resultServiceUnavailable =
                                    (success, fail) => fail(onServiceNotAvailable());
                                return resultServiceUnavailable.ToTask();
                            },
                                                                       (why) =>
                            {
                                failureMessage = why;
                                return resultFailure.ToTask();
                            });
                        },
                                                                       (why) =>
                        {
                            DiscriminatedDelegate <Guid, TResult, Task <TResult> > resultFailureConfig =
                                (success, fail) => fail(onFailure(why));
                            return resultFailureConfig.ToTask();
                        }));
                    },
                                                                                             () => resultNotFound.ToTask(),
                                                                                             (why) => resultFailure.ToTask(),
                                                                                             () => resultFailure.ToTask(),
                                                                                             (why) => resultFailure.ToTask());
                    return(resultGetLogin);
                }
                return(resultSuccess);
            },
                                                                                                           () =>
            {
                DiscriminatedDelegate <Guid, TResult, Task <TResult> > result =
                    (success, fail) => fail(onNotFound());
                return(result);
            });

            return(await resultUpdatePassword(
                       async (loginId) =>
            {
                if (string.IsNullOrWhiteSpace(password))
                {
                    return onSuccess();
                }

                return await managmentProvider.UpdateAuthorizationAsync(loginId, password, forceChange,
                                                                        () => onSuccess(),
                                                                        (why) => onFailure(why),
                                                                        () => onServiceNotAvailable(),
                                                                        (why) => onFailure(why));
            },
                       (r) => r.ToTask()));
        }