<commit_before>﻿using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security.OAuth;
using PathFinder.Security.DAL.Entities;
using PathFinder.Security.DAL.Managers;

namespace PathFinder.Security.WebApi.Providers
{
    public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
    {
        public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            await Task.Yield();

            context.Validated();
        }

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            if (context.UserName == null || context.Password == null)
            {
                context.SetError("invalid_credentials", "The user name or password is incorrect.");
                return;
            }
            var manager = context.OwinContext.GetUserManager<AppUserManager>();
            
            AppUser user = await manager.FindAsync(context.UserName, context.Password);
            if (user == null)
            {
                context.SetError("invalid_credentials", "The user name or password is incorrect.");
                return;
            }

            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            context.Validated(identity);
        }
    }
}<commit_msg>Small fixes of oauth provider<commit_after>﻿using System.Threading.Tasks;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security.OAuth;
using PathFinder.Security.DAL.Entities;
using PathFinder.Security.DAL.Managers;

namespace PathFinder.Security.WebApi.Providers
{
    public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
    {
        public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            // dont check clientId for now
            context.Validated();

            return Task.FromResult<object>(null);
        }

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            if (context.UserName == null || context.Password == null)
            {
                context.SetError("invalid_credentials", "The user name or password is incorrect.");
                return;
            }
            var manager = context.OwinContext.GetUserManager<AppUserManager>();
            
            AppUser user = await manager.FindAsync(context.UserName, context.Password);
            if (user == null)
            {
                context.SetError("invalid_credentials", "The user name or password is incorrect.");
                return;
            }

            var identity = await manager.CreateIdentityAsync(user, context.Options.AuthenticationType);

            context.Validated(identity);
        }
    }
}
{'fraction_non_alphanumeric': 0.07987763426240653, 'fraction_numerical': 0.0, 'mean_word_length': 2.478723404255319, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '6538338', 'n_tokens_mistral': 693, 'n_tokens_neox': 695, 'n_words': 151}