Esempio n. 1
0
        /// <summary>
        /// Used by Admin users to create a new account bound to the client specified by <c>clientId</c>.
        /// </summary>
        /// <param name="clientId">The account to be modified.</param>
        /// <param name="userCredentialsCallback">A delegate that, if invoked, returns the User's credentials.</param>
        /// <returns>The starting balance for the new account.</returns>
        ///
        public async Task <string> CreateAccount(string clientId, UserCredentialsCallback userCredentialsCallback)
        {
            string accountId;

            var accessToken = await GetCurrentAccessToken(userCredentialsCallback);

            using (var accountsApi = new AccountsApiClient(_accountsApiUrl, accessToken))
            {
                accountId = await accountsApi.CreateAccount(clientId);
            }

            return(accountId);
        }
Esempio n. 2
0
        /// <summary>
        /// Get the history of an account from the Accounts API, on behalf of a User.
        /// </summary>
        /// <param name="accountNumber">The account to be queried.</param>
        /// <param name="userCredentialsCallback">A delegate that, if invoked, returns the User's credentials.</param>
        /// <returns>The account history as list of signed doubles.</returns>
        ///
        public async Task <AccountHistory> GetAccountHistory(string accountNumber, UserCredentialsCallback userCredentialsCallback)
        {
            AccountHistory history;

            var accessToken = await GetCurrentAccessToken(userCredentialsCallback);

            using (var accountsApi = new AccountsApiClient(_accountsApiUrl, accessToken))
            {
                history = await accountsApi.GetAccountHistory(accountNumber);
            }

            return(history);
        }
Esempio n. 3
0
        /// <summary>
        /// Get the balance of an account from the Accounts API, on behalf of a User.
        /// </summary>
        /// <param name="accountNumber">The account to be queried.</param>
        /// <param name="userCredentialsCallback">A delegate that, if invoked, returns the User's credentials.</param>
        /// <returns>The account balance as a double.</returns>
        ///
        public async Task <double> GetAccountBalance(string accountNumber, UserCredentialsCallback userCredentialsCallback)
        {
            double balance;

            var accessToken = await GetCurrentAccessToken(userCredentialsCallback);

            using (var accountsApi = new AccountsApiClient(_accountsApiUrl, accessToken))
            {
                balance = await accountsApi.GetAccountBalance(accountNumber);
            }

            return(balance);
        }
Esempio n. 4
0
        /// <summary>
        /// Makes a call to the specified endpoint of Accounts API, using the specified Invocation (Get, Put or Post).
        /// This call only supports requests that can be fully encoded in the URL.
        /// </summary>
        /// <param name="invocation">A delegate that will pass the </param>
        /// <param name="requestUrn">The URN to be appended to the Accounts API URL in order to
        /// obtain the full request URL.</param>
        /// <returns>An object representing the response by the Accounts API.</returns>
        /// <example>
        /// var response = await InvokeApiAsync(Put, "accounts/credit/1234?sum=100");
        ///
        /// Where "1234" is an account number and "100" is the sum to be credited to that account.
        /// </example>
        private async Task <HttpResponseMessage> InvokeApiAsync(HttpClientHelpers.EndPointInvocation invocation, string requestUrn)
        {
            HttpResponseMessage response;

            var accessToken = TokenManager.GetAccessTokens().AccessToken;

            using (var apiClient = new AccountsApiClient(_accountsApiUrl, accessToken))
            {
                response = await apiClient.Invoke(invocation, requestUrn);
            }

            return(response);
        }
Esempio n. 5
0
        /// <summary>
        /// Create a Client with and bind it to the specified username.  This request will fail if the User
        /// specified by <c>username</c> is already bound to a Client.
        /// </summary>
        /// <param name="username">The username of the User to bind to the new Client.</param>
        /// <param name="firstName">The client's first/given name.</param>
        /// <param name="lastName">The client's last/family name.</param>
        /// <param name="userCredentialsCallback"></param>
        /// <returns></returns>
        public async Task <string> CreateClient(string username,
                                                string firstName,
                                                string lastName,
                                                UserCredentialsCallback userCredentialsCallback)
        {
            var accessToken = await GetCurrentAccessToken(userCredentialsCallback);

            using (var accountsApi = new AccountsApiClient(_accountsApiUrl, accessToken))
            {
                var clientId = await accountsApi.CreateClient(username, firstName, lastName);

                return(clientId);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Call the Accounts API, on behalf of a User, to debit an account by some amount.
        /// Note that <c>amount</c> should be unsigned, and that only it's absolute value
        /// will be taken in any event.
        /// </summary>
        /// <param name="accountNumber">The account to be modified.</param>
        /// <param name="amount">The (unsigned) amount to be debited from the account.</param>
        /// <param name="memo">A short note to be attached to this transaction.</param>
        /// <param name="userCredentialsCallback">A delegate that, if invoked, returns the User's credentials.</param>
        /// <returns>The new balance for the modified account.</returns>
        ///
        public async Task <double> DebitAccount(string accountNumber, double amount, string memo,
                                                UserCredentialsCallback userCredentialsCallback)
        {
            double balance;

            var accessToken = await GetCurrentAccessToken(userCredentialsCallback);

            using (var accountsApi = new AccountsApiClient(_accountsApiUrl, accessToken))
            {
                balance = await accountsApi.DebitAccount(accountNumber, amount, memo);
            }

            return(balance);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a new Identity with the specified username, password, and name.  Also creates a corresponding
        /// User on the Accounts system and links it to the new Identity via the <see cref="ApplicationClaimTypes.ACCOUNTS_USERNAME"/>
        /// claim.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="isAdmin"></param>
        /// <param name="userCredentialsCallback"></param>
        /// <returns></returns>
        public async Task CreateUser(string username,
                                     string password,
                                     string firstName,
                                     string lastName,
                                     bool isAdmin,
                                     UserCredentialsCallback userCredentialsCallback)
        {
            var accessToken = await GetCurrentAccessToken(userCredentialsCallback);

            var newUser     = new AltSourceNewUserDto(username, password, firstName, lastName, isAdmin);
            var jsonNewUser = JsonConvert.SerializeObject(newUser);
            var content     = new StringContent(jsonNewUser, Encoding.UTF8, "application/json");

            using (var identityApi = new HttpClient {
                BaseAddress = _identityServerUrl
            })
            {
                identityApi.SetBearerToken(accessToken);

                var requestUrl = (isAdmin ? "api/manage/admin/create" : "api/manage/user/create");
                var result     = await identityApi.PutAsync(requestUrl, content);

                if (!result.IsSuccessStatusCode)
                {
                    var msg = result.Content.ReadAsStringAsync();

                    throw new HttpRequestException($"{result.ReasonPhrase} - {msg.Result}");
                }
            }

            using (var accountsApi = new AccountsApiClient(_accountsApiUrl, accessToken))
            {
                if (!await accountsApi.CreateUser(username))
                {
                    throw new HttpRequestException($"Failed to create user '{username}' on the Accounts system.");
                }
            }
        }