Ejemplo n.º 1
0
        internal IClientApplication CreateClientApplication(string name)
        {
            Guard.NotNullOrEmpty(() => name, name);

            // Get the app
            IEnumerable <IClientApplication> apps = Storage.Find(Storage.BuildQuery(
                                                                     Reflector <ClientApplication> .GetPropertyName(x => x.Name), QueryOperator.EQ, name));

            if (apps.Any())
            {
                throw LogicErrorThrower.ResourceConflict(
                          Resources.ClientApplicationsManager_ApplicationByNameExists,
                          name);
            }

            // Construct a new identifier and a secret
            var app = new ClientApplication
            {
                Name             = name,
                ClientIdentifier = Guid.NewGuid().ToString("D"),
                ClientSecret     = Guid.NewGuid().ToString("D"),
            };

            return(RegisterClientApplication(app));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Handle the request to authorize the request
        /// </summary>
        /// <param name="request">The request to handle</param>
        /// <param name="jsonRequest">The parameters of the call, if JSON request</param>
        public DnoaAuthZResponse HandleTokenRequest(IRequest request, CreateAccessToken jsonRequest)
        {
            OutgoingWebResponse response = GetResponse(request, jsonRequest);

            var responseBody = response.Body.FromJson <Dictionary <string, string> >();

            if (response.Status == HttpStatusCode.OK)
            {
                return(new DnoaAuthZResponse
                {
                    AccessToken = responseBody[@"access_token"],
                    RefreshToken = responseBody[@"refresh_token"],
                    ExpiresIn = responseBody[@"expires_in"],
                    TokenType = responseBody[@"token_type"],
                    Scope = responseBody[@"scope"],
                });
            }

            string error   = (responseBody.ContainsKey(@"error")) ? responseBody[@"error"] : string.Empty;
            string message = (responseBody.ContainsKey(@"error_description"))
                ? responseBody[@"error_description"]
                : error;

            throw LogicErrorThrower.RuleViolation(message);
        }
Ejemplo n.º 3
0
        public IClientApplication GetClientApplication(string id)
        {
            Guard.NotNullOrEmpty(() => id, id);

            // Get the app
            IClientApplication app = Storage.Get(id);

            if (app == null)
            {
                throw LogicErrorThrower.ResourceNotFound();
            }

            return(app);
        }
Ejemplo n.º 4
0
        internal IUserAccount CreateUserAccount(string currentUsername, string username, string passwordHash,
                                                string forenames, string surname, string email, string mobilePhone, Address address, string roles = null)
        {
            Guard.NotNull(() => currentUsername, currentUsername);
            Guard.NotNullOrEmpty(() => forenames, forenames);
            Guard.NotNullOrEmpty(() => surname, surname);
            Guard.NotNullOrEmpty(() => email, email);
            Guard.NotNull(() => address, address);

            // Check email not already registered
            if (FindUserAccount(x => x.Email, email))
            {
                throw LogicErrorThrower.ResourceConflict(Resources.UserAccountsManager_UserAccountExistsByEmail, email);
            }

            // Check username not already used
            if (CredentialsProvided(username, passwordHash) && FindUserAccount(x => x.Username, username))
            {
                throw LogicErrorThrower.ResourceConflict(Resources.UserAccountsManager_UserAccountExistsByUsername,
                                                         username);
            }

            // Can't specify role if no credentials
            if (!CredentialsProvided(username, passwordHash) && roles.HasValue())
            {
                throw new RuleViolationException(Resources.UserAccountsManager_NoRolesForParticipant);
            }

            var newAccount = new UserAccount
            {
                Username     = (CredentialsProvided(username, passwordHash)) ? username : email,
                PasswordHash = (CredentialsProvided(username, passwordHash)) ? passwordHash : null,
                Forenames    = forenames,
                Surname      = surname,
                MobilePhone  = mobilePhone,
                Address      = address,
                Email        = email,
                Roles        = CalculateRoles(username, passwordHash, roles),
                IsRegistered = CredentialsProvided(username, passwordHash),
            };

            string accountId = Storage.Add(newAccount);

            newAccount.Id = accountId;

            //TODO: Audit the creation of the user account

            return(newAccount);
        }
Ejemplo n.º 5
0
        public IUserAccount GetUserAccount(string currentUsername, string id)
        {
            Guard.NotNull(() => currentUsername, currentUsername);
            Guard.NotNull(() => id, id);

            // Get the account
            IUserAccount account = Storage.Get(id);

            if (account == null)
            {
                throw LogicErrorThrower.ResourceNotFound();
            }

            return(account);
        }
Ejemplo n.º 6
0
        public IClientApplication GetClientApplicationByClientIdentifier(string clientIdentifier)
        {
            Guard.NotNullOrEmpty(() => clientIdentifier, clientIdentifier);

            // Get the app
            IEnumerable <IClientApplication> apps = Storage.Find(Storage.BuildQuery(
                                                                     Reflector <ClientApplication> .GetPropertyName(x => x.ClientIdentifier),
                                                                     QueryOperator.EQ, clientIdentifier));

            if (!apps.Any())
            {
                throw LogicErrorThrower.ResourceNotFound();
            }

            return(apps.FirstOrDefault());
        }
Ejemplo n.º 7
0
        internal void DeleteClientApplication(string id)
        {
            Guard.NotNullOrEmpty(() => id, id);

            // Get the app
            IClientApplication app = Storage.Get(id);

            if (app == null)
            {
                throw LogicErrorThrower.ResourceNotFound();
            }

            Storage.Delete(id);

            //TODO: Audit the deletion of the client application
        }
Ejemplo n.º 8
0
        internal void DeleteUserAccount(string currentUsername, string id)
        {
            Guard.NotNull(() => currentUsername, currentUsername);
            Guard.NotNull(() => id, id);

            // Get the account
            IUserAccount account = Storage.Get(id);

            if (account == null)
            {
                throw LogicErrorThrower.ResourceNotFound();
            }

            // Delete the account
            Storage.Delete(id);

            //TODO: Audit the deletion of the user account
        }
Ejemplo n.º 9
0
        internal IUserAccount UpdateUserAccount(string currentUsername, string id, string oldPasswordHash,
                                                string newPasswordHash,
                                                string forename, string surname, string email, string mobilePhone, Address address)
        {
            Guard.NotNull(() => currentUsername, currentUsername);
            Guard.NotNull(() => id, id);

            // Get the account
            IUserAccount accountToUpdate = Storage.Get(id);

            if (accountToUpdate == null)
            {
                throw LogicErrorThrower.ResourceNotFound();
            }
            accountToUpdate.Address = accountToUpdate.Address ?? new Address();

            //Verify password hashes
            if (newPasswordHash.HasValue())
            {
                if (!accountToUpdate.PasswordHash.EqualsOrdinal(oldPasswordHash))
                {
                    throw LogicErrorThrower.RuleViolation(Resources.UserAccountsManager_PasswordsDontMatch);
                }

                accountToUpdate.PasswordHash = newPasswordHash;
            }

            //Update (allowable) account properties
            accountToUpdate.Forenames   = forename;
            accountToUpdate.Surname     = surname;
            accountToUpdate.Email       = email;
            accountToUpdate.MobilePhone = mobilePhone;
            if (address != null)
            {
                accountToUpdate.Address.PopulateWithNonDefaultValues(address);
            }

            IUserAccount newAccount = Storage.Update(id, accountToUpdate);

            // TODO: Audit the update of the account

            return(newAccount);
        }