public async static Task <HttpResponseMessage> GetByAccountAsync( [QueryParameter(Name = AccountPropertyName)] Guid accountId, Api.Azure.AzureApplication application, SessionToken security, MultipartResponseAsync <XIntegration> onContents, ReferencedDocumentNotFoundResponse <object> onAccountNotFound, 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(await onContents(integrations)); }
[Api.HttpPost] //(MatchAllBodyParameters = false)] public async static Task <HttpResponseMessage> CreateAsync( [Property(Name = AccountPropertyName)] Guid accountId, [Property(Name = AuthorizationPropertyName)] IRef <Authorization> authorizationRef, [Resource] AccountMapping accountMapping, Api.Azure.AzureApplication 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())); }
[Api.HttpPatch] //(MatchAllBodyParameters = false)] public async static Task <IHttpResponse> UpdateAsync( [Property(Name = IntegrationIdPropertyName)] IRef <XIntegration> integrationRef, [Property(Name = AuthorizationPropertyName)] IRef <Authorization> authorizationRef, Api.Azure.AzureApplication application, EastFive.Api.SessionToken security, ContentTypeResponse <XIntegration> onUpdated, NotFoundResponse onNotFound, ForbiddenResponse onForbidden, ReferencedDocumentDoesNotExistsResponse <Authorization> onAuthorizationDoesNotExist, UnauthorizedResponse onUnauthorized) { return(await integrationRef.StorageUpdateAsync( async (integration, saveAsync) => { var accountId = integration.accountId; if (!await application.CanAdministerCredentialAsync(accountId, security)) { return onUnauthorized(); } return await await authorizationRef.StorageGetAsync( async authorization => { if (!await application.ShouldAuthorizeIntegrationAsync(integration, authorization)) { return onForbidden().AddReason("Authorization is not accessable to this account."); } integration.Method = authorization.Method; // method is used in the .mappingId integration.authorization = authorizationRef.Optional(); integration.methodName = await Auth.Method.ById(authorization.Method, application, method => method.name, () => string.Empty); await saveAsync(integration); return onUpdated(integration); }, () => onAuthorizationDoesNotExist().AsTask()); }, () => onNotFound(), onModificationFailures: StorageConstraintUniqueAttribute.ModificationFailure( (XIntegration x) => x.authorization, () => { // TODO: Check if mapping is to this integration and reply already created. return onForbidden().AddReason("Authorization is already in use."); }).AsArray())); }
[Api.HttpGet] //(MatchAllBodyParameters = false)] public async static Task <HttpResponseMessage> GetByAccountAsync( [QueryParameter(Name = AccountPropertyName)] Guid accountId, Api.Azure.AzureApplication application, SessionToken security, MultipartResponseAsync <Integration> onContents, ReferencedDocumentNotFoundResponse <object> onAccountNotFound, UnauthorizedResponse onUnauthorized) { if (!await application.CanAdministerCredentialAsync(accountId, security)) { return(onUnauthorized()); } return(await await GetIntegrationsByAccountAsync(accountId, (kvps) => onContents(kvps.SelectKeys()), () => onAccountNotFound().AsTask())); }
[Api.HttpPatch] //(MatchAllBodyParameters = false)] public async static Task <HttpResponseMessage> UpdateAsync( [Property(Name = IntegrationIdPropertyName)] IRef <Integration> integrationRef, [Property(Name = AuthorizationPropertyName)] IRef <Authorization> authorizationRef, Api.Azure.AzureApplication application, EastFive.Api.SessionToken security, ContentTypeResponse <Integration> onUpdated, NotFoundResponse onNotFound, NotModifiedResponse onNotModified, ForbiddenResponse onForbidden, ReferencedDocumentDoesNotExistsResponse <Authorization> onAuthenticationDoesNotExist, UnauthorizedResponse onUnauthorized) { return(await integrationRef.StorageUpdateAsync( async (integration, saveAsync) => { var accountId = integration.accountId; if (!await application.CanAdministerCredentialAsync(accountId, security)) { return onUnauthorized(); } return await await authorizationRef.StorageGetAsync( async authorization => { // TODO? This // var accountIdDidMatch = await await authorization.ParseCredentailParameters( integration.Method = authorization.Method; // method is used in the .mappingId integration.authorization = authorizationRef.Optional(); return await await SaveAuthorizationLookupAsync(integration.integrationRef, authorization.authorizationRef, async() => { await saveAsync(integration); return await SaveAccountLookupAsync(accountId, integration, () => onUpdated(integration)); }, () => { // TODO: Check if mapping is to this integration and reply already created. return onForbidden().AddReason("Authorization is already in use.").AsTask(); }); }, () => { return onNotModified().AsTask(); }); }, () => onNotFound())); }
public static async Task <HttpResponseMessage> QueryByIntegrationAsync( [QueryParameter(Name = "integration")] IRef <Integration> integrationRef, Api.Azure.AzureApplication application, EastFive.Api.SessionToken security, MultipartResponseAsync <Method> onContent, UnauthorizedResponse onUnauthorized, ReferencedDocumentNotFoundResponse <Integration> 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); }) .Await() .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 await onContent(integrationProviders); }, () => onIntegrationNotFound().AsTask())); }
public async static Task <HttpResponseMessage> CreateAsync( [Property(Name = AccountPropertyName)] Guid accountId, [PropertyOptional(Name = AuthorizationPropertyName)] IRefOptional <Authorization> authorizationRefMaybe, [Resource] XIntegration integration, Api.Azure.AzureApplication 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()); })); }
[Api.HttpPost] //(MatchAllBodyParameters = false)] public async static Task <IHttpResponse> CreateAsync( [Property(Name = AccountPropertyName)] Guid accountId, [PropertyOptional(Name = AuthorizationPropertyName)] IRefOptional <Authorization> authorizationRefMaybe, [Resource] Integration integration, Api.Azure.AzureApplication application, EastFive.Api.SessionToken security, CreatedResponse onCreated, AlreadyExistsResponse onAlreadyExists, ForbiddenResponse forbidden, ReferencedDocumentDoesNotExistsResponse <Authorization> onAuthenticationDoesNotExist, GeneralConflictResponse onFailure) { if (!await application.CanAdministerCredentialAsync(accountId, security)) { return(forbidden()); } return(await await authorizationRefMaybe.StorageGetAsync( authorization => { // TODO? This // var accountIdDidMatch = await await authorization.ParseCredentailParameters( return CreateWithAuthorization(integration, authorization, accountId, () => onCreated(), () => onAlreadyExists(), (why) => onFailure(why)); }, async() => { return await await integration.StorageCreateAsync( discard => { return SaveAccountLookupAsync(accountId, integration, () => onCreated()); }, () => onAlreadyExists().AsTask()); })); }
public static async Task <HttpResponseMessage> DeleteAsync( [UpdateId(Name = IntegrationIdPropertyName)] IRef <Integration> integrationRef, Api.Azure.AzureApplication application, EastFive.Api.SessionToken security, NoContentResponse onDeleted, NotFoundResponse onNotFound, ForbiddenResponse onForbidden) { var integrationMaybe = await integrationRef.StorageGetAsync(i => i, () => default(Integration?)); if (!integrationMaybe.HasValue) { return(onNotFound()); } var integration = integrationMaybe.Value; if (!await application.CanAdministerCredentialAsync(integration.accountId, security)) { return(onForbidden()); } return(await DeleteInternalAsync(integrationRef, application, () => onDeleted(), () => onNotFound())); }