public AccessToken GetAccessTokenImplicit(string encryptedUsername, string encryptedPassword)
        {
            if (string.IsNullOrEmpty(encryptedUsername) ||
                string.IsNullOrEmpty(encryptedPassword))
            {
                throw new ApplicationException("Username or Password is empty.");
            }

            string usernameDecrypted = SymmetricEncryption.Decrypt(encryptedUsername);
            string passwordDecrypted = SymmetricEncryption.Decrypt(encryptedPassword);

            User user = GetUser(usernameDecrypted, passwordDecrypted);

            if (user == null)
            {
                throw new ApplicationException("Could not find user matching Username and Password.");
            }

            AccessEnity accessEntity = CreateAccess(user.Id, new string[0], user.UserName);

            PersistAccess(accessEntity);
            AccessToken accessToken = CreateAccessToken(accessEntity);

            return(accessToken);
        }
        public AccessToken GetAccessToken(AccessRequest accessRequest)
        {
            OrganisationKeySerDes organisationKey = ExtractOrganisationKey(accessRequest.Key);

            if (organisationKey == null)
            {
                throw new ApplicationException("Extract Organisation-Key process returned a null key.");
            }
            CheckKeyIsValid(organisationKey.Name, organisationKey.OKey);

            AuthorisationEntity authorisation = GetAuthorisation(accessRequest.AuthenticationCode);

            if (authorisation == null)
            {
                throw new ApplicationException("Could not find Authorisation entry in the database.");
            }

            AccessEnity access = CreateAccess(authorisation.UserId, accessRequest.Scope,
                                              organisationKey.Name);

            PersistAccess(access);
            DeleteAuthorisation(authorisation);

            AccessToken accessToken = CreateAccessToken(access);

            return(accessToken);
        }
        private void PersistAccess(AccessEnity access)
        {
            IAccessRepository accessRepo = AccessRepoFactory.GetAuthorisationRepository(DatabaseOption.DatabaseEngine,
                                                                                        DatabaseOption.DbConnectionString);

            accessRepo.InsertAccess(access);
        }
        private AccessToken CreateAccessToken(AccessEnity access)
        {
            string      encrptedOrganisationName = SymmetricEncryption.Encrypt(access.Organisation);
            AccessToken accessToken = new AccessToken
            {
                Organisation = encrptedOrganisationName,
                Token        = access.Token,
                StartTime    = access.StartTime,
                EndTime      = access.EndTime,
                Scope        = access.Scope,
            };

            return(accessToken);
        }
        private AccessEnity CreateAccess(long userId, string[] scope, string organisationName)
        {
            //string encrptedOrganisationName = SymmetricEncryption.Encrypt(organisationName);
            DateTime    currentDateTime = DateTime.Now;
            AccessEnity access          = new AccessEnity
            {
                Organisation = organisationName,
                Token        = Guid.NewGuid().ToString(),
                UserId       = userId,
                StartTime    = currentDateTime,
                EndTime      = DateTime.Now.AddYears(100),
                Scope        = scope,
            };

            return(access);
        }