<commit_before>﻿using System;
using System.Data.Entity;
using System.Linq;
using Infopulse.EDemocracy.Data.Repositories;
using Microsoft.Owin.Security.OAuth;
using System.Security.Claims;
using System.Threading.Tasks;
using Infopulse.EDemocracy.Common.Extensions;
using Infopulse.EDemocracy.Model.Enum;

namespace Infopulse.EDemocracy.Web.Auth.Providers
{
	public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
	{
		public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
		{
			context.Validated();
		}

		public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
		{
			context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

			using (AuthRepository _repo = new AuthRepository())
			{
				var user = await _repo.FindUser(context.UserName, context.Password);

				if (user == null)
				{
					context.SetError("invalid_grant", "The user name or password is incorrect.");
					return;
				}
			}

			var identity = new ClaimsIdentity(context.Options.AuthenticationType);
			identity.AddClaim(new Claim(ClaimTypes.Email, context.UserName));
			AddRoleClaim(identity, context.UserName);

			context.Validated(identity);
		}

		private void AddRoleClaim(ClaimsIdentity identity, string userEmail)
		{
			var roleClaim = GetUserRoleClaim(userEmail);
			if (roleClaim != null)
			{
				identity.AddClaim(roleClaim);
			}
		}

		private Claim GetUserRoleClaim(string userEmail)
		{
			var roleClaimValue = this.GetUserRolesAsString(userEmail);
			return new Claim(ClaimTypes.Role, roleClaimValue);
		}<commit_msg>Fix issue with no role assigned.<commit_after>﻿using System;
using System.Data.Entity;
using System.Linq;
using Infopulse.EDemocracy.Data.Repositories;
using Microsoft.Owin.Security.OAuth;
using System.Security.Claims;
using System.Threading.Tasks;
using Infopulse.EDemocracy.Common.Extensions;
using Infopulse.EDemocracy.Model.Enum;

namespace Infopulse.EDemocracy.Web.Auth.Providers
{
	public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
	{
		public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
		{
			context.Validated();
		}

		public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
		{
			context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

			using (AuthRepository _repo = new AuthRepository())
			{
				var user = await _repo.FindUser(context.UserName, context.Password);

				if (user == null)
				{
					context.SetError("invalid_grant", "The user name or password is incorrect.");
					return;
				}
			}

			var identity = new ClaimsIdentity(context.Options.AuthenticationType);
			identity.AddClaim(new Claim(ClaimTypes.Email, context.UserName));
			AddRoleClaim(identity, context.UserName);

			context.Validated(identity);
		}

		private void AddRoleClaim(ClaimsIdentity identity, string userEmail)
		{
			var roleClaim = GetUserRoleClaim(userEmail);
			if (roleClaim != null)
			{
				identity.AddClaim(roleClaim);
			}
		}

		private Claim GetUserRoleClaim(string userEmail)
		{
			var roleClaimValue = this.GetUserRolesAsString(userEmail);
			return new Claim(ClaimTypes.Role, roleClaimValue ?? string.Empty);
		}
{'fraction_non_alphanumeric': 0.09605692262081234, 'fraction_numerical': 0.0, 'mean_word_length': 5.00355871886121, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3837749', 'n_tokens_mistral': 1118, 'n_tokens_neox': 966, 'n_words': 196}