Esempio n. 1
0
        public async static Task <IHttpResponse> GetByAccountAsync(
            [QueryParameter(Name = AccountPropertyName)] Guid accountId,
            IAuthApplication application, SessionToken security,
            MultipartAsyncResponse <XIntegration> onContents,
            UnauthorizedResponse onUnauthorized)
        {
            if (!await application.CanAdministerCredentialAsync(accountId, security))
            {
                return(onUnauthorized());
            }

            var integrations = GetIntegrationsByAccount(accountId)
                               .Select(
                kvp =>
            {
                var integration = kvp.Key;
                return(Auth.Method.ById(kvp.Value.Method,
                                        application,
                                        method =>
                {
                    integration.methodName = method.name;
                    return integration;
                },
                                        () => default(XIntegration?)));
            })
                               .Await()
                               .SelectWhereHasValue();

            return(onContents(integrations));
        }
Esempio n. 2
0
 public async static Task <IHttpResponse> CreateAsync(
     [Property(Name = AccountPropertyName)] Guid accountId,
     [Property(Name = AuthorizationPropertyName)] IRef <Authorization> authorizationRef,
     [Resource] AccountMapping accountMapping,
     IAuthApplication application, Api.SessionToken security,
     CreatedResponse onCreated,
     ForbiddenResponse onForbidden,
     UnauthorizedResponse onUnauthorized,
     ReferencedDocumentDoesNotExistsResponse <Authorization> onAuthenticationDoesNotExist,
     GeneralConflictResponse onFailure)
 {
     if (!await application.CanAdministerCredentialAsync(accountId, security))
     {
         return(onUnauthorized());
     }
     return(await await authorizationRef.StorageGetAsync(
                async authorization =>
     {
         accountMapping.Method = authorization.Method;     // method is used in the .mappingId
         var authorizationLookup = new AuthorizationLookup
         {
             accountMappingRef = accountMapping.mappingId,
             authorizationLookupRef = authorizationRef,
         };
         return await await authorizationLookup.StorageCreateAsync(
             async(idDiscard) =>
         {
             accountMapping.accountMappingLookup = await await authorization.ParseCredentailParameters(
                 application,
                 (accountKey, loginProvider) =>
             {
                 var lookup = new AccountMappingLookup()
                 {
                     accountkey = accountKey,
                     accountMappingId = accountMapping.mappingId,
                     Method = authorization.Method,
                 };
                 return lookup.StorageCreateAsync(
                     (discard) => new RefOptional <AccountMappingLookup>(
                         lookup.accountMappingLookupId),
                     () => new RefOptional <AccountMappingLookup>());
             },
                 (why) =>
             {
                 var amLookupMaybe = new RefOptional <AccountMappingLookup>();
                 return amLookupMaybe.AsTask();
             });
             return await accountMapping.StorageCreateAsync(
                 createdId =>
             {
                 return onCreated();
             },
                 () => onForbidden().AddReason("Account is already mapped to that authentication."));
         },
             () => onFailure("Authorization is already mapped to another account.").AsTask());
     },
                () => onAuthenticationDoesNotExist().AsTask()));
 }
Esempio n. 3
0
        public static async Task <IHttpResponse> QueryByIntegrationAsync(
            [QueryParameter(Name = "integration")] IRef <XIntegration> integrationRef,
            IAuthApplication application, EastFive.Api.SessionToken security,
            MultipartAsyncResponse <Method> onContent,
            UnauthorizedResponse onUnauthorized,
            ReferencedDocumentNotFoundResponse <XIntegration> onIntegrationNotFound)
        {
            return(await await integrationRef.StorageGetAsync(
                       async (integration) =>
            {
                var accountId = integration.accountId;
                if (!await application.CanAdministerCredentialAsync(accountId, security))
                {
                    return onUnauthorized();
                }

                var integrationProviders = application.LoginProviders
                                           .Where(loginProvider => loginProvider.Value.GetType().IsSubClassOfGeneric(typeof(IProvideIntegration)))
                                           .Select(
                    async loginProvider =>
                {
                    var integrationProvider = loginProvider.Value as IProvideIntegration;
                    var supportsIntegration = await integrationProvider.SupportsIntegrationAsync(accountId);
                    return supportsIntegration.PairWithValue(loginProvider);
                })
                                           .AsyncEnumerable()
                                           .Where(kvp => kvp.Key)
                                           .SelectValues()
                                           .Select(
                    (loginProvider) =>
                {
                    var integrationProvider = loginProvider.Value as IProvideIntegration;
                    return new Method
                    {
                        authenticationId = new Ref <Method>(loginProvider.Value.Id),
                        name = integrationProvider.GetDefaultName(new Dictionary <string, string>()),
                    };
                });
                return onContent(integrationProviders);
            },
                       () => onIntegrationNotFound().AsTask()));
        }
Esempio n. 4
0
        public async static Task <IHttpResponse> CreateAsync(
            [Property(Name = AccountPropertyName)] Guid accountId,
            [PropertyOptional(Name = AuthorizationPropertyName)] IRefOptional <Authorization> authorizationRefMaybe,
            [Resource] XIntegration integration,
            IAuthApplication application, EastFive.Api.SessionToken security,
            CreatedResponse onCreated,
            AlreadyExistsResponse onAlreadyExists,
            ForbiddenResponse onForbidden,
            ReferencedDocumentDoesNotExistsResponse <Authorization> onAuthorizationDoesNotExist,
            GeneralConflictResponse onFailure)
        {
            if (!await application.CanAdministerCredentialAsync(accountId, security))
            {
                return(onForbidden());
            }

            return(await await authorizationRefMaybe.StorageGetAsync(
                       async authorization =>
            {
                if (!await application.ShouldAuthorizeIntegrationAsync(integration, authorization))
                {
                    return onFailure("Authorization is not accessable to this account.");
                }

                return await CreateWithAuthorization(integration, authorization,
                                                     () => onCreated(),
                                                     () => onAlreadyExists(),
                                                     (why) => onFailure(why));
            },
                       async() =>
            {
                if (authorizationRefMaybe.HasValue)
                {
                    return onAuthorizationDoesNotExist();
                }
                return await integration.StorageCreateAsync(
                    (discard) => onCreated(),
                    () => onAlreadyExists());
            }));
        }
Esempio n. 5
0
        public static async Task <IHttpResponse> DeleteAsync(
            [UpdateId(Name = IntegrationIdPropertyName)] IRef <XIntegration> integrationRef,
            IAuthApplication application, EastFive.Api.SessionToken security,
            NoContentResponse onDeleted,
            NotFoundResponse onNotFound,
            ForbiddenResponse onForbidden)
        {
            var integrationMaybe = await integrationRef.StorageGetAsync(i => i, () => default(XIntegration?));

            if (!integrationMaybe.HasValue)
            {
                return(onNotFound());
            }

            var integration = integrationMaybe.Value;

            if (!await application.CanAdministerCredentialAsync(integration.accountId, security))
            {
                return(onForbidden());
            }

            return(await DeleteInternalAsync(integrationRef, () => onDeleted(), () => onNotFound()));
        }