Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Security.Claims;
- using System.Threading.Tasks;
- using Autofac;
- using Autofac.Core.Lifetime;
- using FluentValidation;
- using MediatR;
- using Microsoft.Owin.Security;
- using Microsoft.Owin.Security.OAuth;
- using Zee.Z5.Domain.Commands;
- using Zee.Z5.Domain.Commands.Validation;
- namespace Zee.Z5.API.Setup.Auth
- {
- public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
- {
- private const string DeviceCredentialsKey = "device_credentials";
- private readonly ILifetimeScope _lifetimeScope;
- public AuthorizationServerProvider(ILifetimeScope lifetimeScope)
- {
- _lifetimeScope = lifetimeScope;
- }
- public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
- {
- var deviceId = context.Parameters["deviceId"];
- var deviceName = context.Parameters["deviceName"];
- var devicePlatform = context.Parameters["devicePlatform"];
- var deviceCredentials = new DeviceCredentials(deviceId, deviceName, devicePlatform);
- var validator = new DeviceCredentialsValidator();
- //TODO Create own extension method for rising ZeeApplicationException
- await validator.ValidateAndThrowAsync(deviceCredentials);
- context.OwinContext.Set(DeviceCredentialsKey, deviceCredentials);
- context.Validated();
- }
- public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
- {
- var command = new UserLoginCommand()
- {
- Username = context.UserName,
- Password = context.Password,
- Credentials = context.OwinContext.Get<DeviceCredentials>(DeviceCredentialsKey),
- AuthenticationType = context.Options.AuthenticationType
- };
- ClaimsIdentity identity;
- using (var scope = _lifetimeScope.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag))
- {
- var mediator = scope.Resolve<IMediator>();
- identity = await mediator.SendAsync(command);
- }
- var authProperties = new AuthenticationProperties();
- var ticket = new AuthenticationTicket(identity, authProperties);
- context.Validated(ticket);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement