public async Task <PersonIdentityModel> GetPersonInformationAsync(string email)
        {
            // Because this code is used in the authentication middle ware we need to resolve dependencies in here.
            var identityProviders      = _container.GetAllInstances <IIdentityProvider>();
            var logger                 = _container.GetInstance <ILogger>();
            PersonIdentityModel person = null;

            foreach (var idProvider in identityProviders.OrderBy(x => x.Order))
            {
                var personCandidate = await idProvider.GetIdentity(email);

                if (personCandidate != null)
                {
                    person = personCandidate;
                    break;
                }
            }

            if (person == null)
            {
                await logger.LogErrorAsync($"Could not find a person with the provided email: {email}");

                throw new AuthenticationException("Could not find a person with provided email");
            }

            return(person);
        }
        private async Task <PersonIdentityModel> ValidateJwt(string clientToken, string service)
        {
            string email = string.Empty;
            PersonIdentityModel person = null;

            var handler   = new JwtSecurityTokenHandler();
            var jsonToken = handler.ReadToken(clientToken) as JwtSecurityToken;

            switch (service)
            {
            case "google":
                email = jsonToken.Claims.FirstOrDefault(x => x.Type == "email").Value;
                break;

            case "azure":
                var emailClaim = jsonToken.Claims.FirstOrDefault(x => x.Type == "unique_name");
                if (emailClaim != null)
                {
                    email = emailClaim.Value;
                }
                else
                {
                    email = jsonToken.Claims.FirstOrDefault(x => x.Type == "emails").Value;
                }
                break;

            case "hotmail":
                if (jsonToken.Claims.FirstOrDefault(x => x.Type == "email") != null)
                {
                    email = jsonToken.Claims.FirstOrDefault(x => x.Type == "email").Value;
                }
                else
                {
                    var emailClaimH = jsonToken.Claims.FirstOrDefault(x => x.Type == "unique_name");
                    if (emailClaimH != null)
                    {
                        email = emailClaimH.Value;
                    }
                    else
                    {
                        email = jsonToken.Claims.FirstOrDefault(x => x.Type == "emails").Value;
                    }
                }
                break;
            }

            if (string.IsNullOrEmpty(email))
            {
                return(null);
            }

            person = await _databaseIdentityProvider.GetPersonInformationAsync(email);

            if (person == null || person.Email.ToUpper() != email.ToUpper())
            {
                return(null);
            }

            return(person);
        }
        private List <Claim> GetClaims(PersonIdentityModel personInfo)
        {
            if (personInfo == null)
            {
                return(null);
            }

            var claims = new List <Claim> {
                new Claim(JwtRegisteredClaimNames.GivenName, personInfo.FirstName),
                new Claim(JwtRegisteredClaimNames.FamilyName, personInfo.LastSurname),
                new Claim(JwtRegisteredClaimNames.Email, personInfo.Email),

                // Add custom claims
                new Claim("person_usi", personInfo.Usi.ToString(), ClaimValueTypes.Integer),
                new Claim("person_type_id", personInfo.PersonTypeId.ToString(), ClaimValueTypes.Integer),
                new Claim("person_unique_id", personInfo.UniqueId.ToString(), ClaimValueTypes.String),
                new Claim("person_role", personInfo.PersonType, ClaimValueTypes.String),
                new Claim("email", personInfo.Email, ClaimValueTypes.String),
                new Claim("firstname", personInfo.FirstName, ClaimValueTypes.String),
                new Claim("lastsurname", personInfo.LastSurname, ClaimValueTypes.String),
                new Claim("person_identification_code", string.IsNullOrEmpty(personInfo.IdentificationCode) ? string.Empty: personInfo.IdentificationCode, ClaimValueTypes.String)
            };

            return(claims);
        }
Exemple #4
0
        public PersonDTO UpdatePerson(int id, PersonDTO person)
        {
            var identityModel = new PersonIdentityModel(id);
            var updateModel   = Mapper.Map <PersonUpdateModel>(person);

            Person res = PersonUpdateService.UpdatePerson(identityModel, updateModel);

            return(Mapper.Map <PersonDTO>(res));
        }
Exemple #5
0
        public void Delete(PersonIdentityModel id)
        {
            var personToDelete = this.ApplicationContext.Person.Where(p => p.Id == id.Id).First();

            this.ApplicationContext.Attach(personToDelete);
            this.ApplicationContext.Remove(personToDelete);

            this.ApplicationContext.SaveChanges();
        }
Exemple #6
0
        public Parking.Person Update(PersonIdentityModel id, PersonUpdateModel person)
        {
            var existing = this.ApplicationContext.Person.Where(p => p.Id == id.Id).First();

            var result = this.Mapper.Map(person, existing);

            this.ApplicationContext.Update(result);

            this.ApplicationContext.SaveChanges();

            return(this.Mapper.Map <Parking.Person>(result));
        }
        public async Task <IHttpActionResult> ExchangeToken(OAuthTokenExchangeRequest request)
        {
            if (request.Grant_type != "client_credentials")
            {
                return(BadRequest($"grant_type ({request.Grant_type}) not supported"));
            }

            bool   token_signed = false;
            string access_token = string.Empty;

            if (request.Service == "facebook" && !string.IsNullOrEmpty(request.Code))
            {
                access_token = ValidateSignatureOther(request.Code, request.Service);
                token_signed = !string.IsNullOrEmpty(access_token);
            }
            else
            {
                token_signed = ValidateSignatureJwt(request.Id_token, request.Service);
            }

            if (!token_signed)
            {
                return(Unauthorized());
            }

            PersonIdentityModel person = null;

            if (!string.IsNullOrEmpty(access_token))
            {
                person = await GetPersonInformationByService(access_token, request.Service);
            }
            else
            {
                person = await ValidateJwt(request.Id_token, request.Service);
            }

            if (person == null)
            {
                return(Unauthorized());
            }

            var key              = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSetting("Jwt:Key")));
            var creds            = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var expiresInMinutes = Convert.ToInt32(_config.GetSetting("Jwt:ExpiresInMinutes"));

            var token = new JwtSecurityToken(
                _config.GetSetting("Jwt:Issuer"),
                _config.GetSetting("Jwt:Audience"),
                expires: DateTime.Now.AddMinutes(expiresInMinutes),
                claims: GetClaims(person),
                signingCredentials: creds
                );

            var model = new OAuthResponse
            {
                Access_token = await Task.Run(() => new JwtSecurityTokenHandler().WriteToken(token)),
                Expires_in   = expiresInMinutes * 60,
                Token_type   = "Bearer"
            };

            return(Ok(model));
        }
Exemple #8
0
 public List <Parking.ParkingPlace> GetPersonPlaces(PersonIdentityModel id)
 {
     return(this.ApplicationContext.ParkingPlaces.Where(place => place.PersonId == id.Id).ToList().Select(x => this.Mapper.Map <Parking.ParkingPlace>(x)).ToList());
 }
 public void DeletePerson(PersonIdentityModel id)
 {
     this.PersonDataAccess.Delete(id);
 }
Exemple #10
0
        public void DeletePerson(int id)
        {
            var identityModel = new PersonIdentityModel(id);

            PersonDeleteService.DeletePerson(identityModel);
        }
Exemple #11
0
 public Person GetPerson(PersonIdentityModel id)
 {
     return(PersonDataAccess.Get(id));
 }
Exemple #12
0
        public Parking.Person Get(PersonIdentityModel id)
        {
            var result = this.ApplicationContext.Person.Where(p => p.Id == id.Id).First();

            return(this.Mapper.Map <Parking.Person>(result));
        }
 public List <ParkingPlace> GetPersonsPlaces(PersonIdentityModel personId)
 {
     return(this.ParkingPlaceDataAccess.GetPersonPlaces(personId));
 }
Exemple #14
0
 public Person UpdatePerson(PersonIdentityModel id, PersonUpdateModel person)
 {
     PersonGetService.ValidatePerson(person);
     return(this.PersonDataAccess.Update(id, person));
 }