Advertisement
Guest User

Untitled

a guest
Dec 30th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System.Security.Claims;
  2. using System.Threading.Tasks;
  3. using Autofac;
  4. using Autofac.Core.Lifetime;
  5. using FluentValidation;
  6. using MediatR;
  7. using Microsoft.Owin.Security;
  8. using Microsoft.Owin.Security.OAuth;
  9. using Zee.Z5.Domain.Commands;
  10. using Zee.Z5.Domain.Commands.Validation;
  11.  
  12. namespace Zee.Z5.API.Setup.Auth
  13. {
  14.     public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
  15.     {
  16.         private const string DeviceCredentialsKey = "device_credentials";
  17.         private readonly ILifetimeScope _lifetimeScope;
  18.  
  19.         public AuthorizationServerProvider(ILifetimeScope lifetimeScope)
  20.         {
  21.             _lifetimeScope = lifetimeScope;
  22.         }
  23.  
  24.         public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
  25.         {
  26.             var deviceId = context.Parameters["deviceId"];
  27.             var deviceName = context.Parameters["deviceName"];
  28.             var devicePlatform = context.Parameters["devicePlatform"];
  29.  
  30.             var deviceCredentials = new DeviceCredentials(deviceId, deviceName, devicePlatform);
  31.             var validator = new DeviceCredentialsValidator();
  32.  
  33.             //TODO Create own extension method for rising ZeeApplicationException
  34.             await validator.ValidateAndThrowAsync(deviceCredentials);
  35.  
  36.             context.OwinContext.Set(DeviceCredentialsKey, deviceCredentials);
  37.             context.Validated();
  38.         }
  39.  
  40.         public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
  41.         {
  42.             var command = new UserLoginCommand()
  43.             {
  44.                 Username = context.UserName,
  45.                 Password = context.Password,
  46.                 Credentials = context.OwinContext.Get<DeviceCredentials>(DeviceCredentialsKey),
  47.                 AuthenticationType = context.Options.AuthenticationType
  48.             };
  49.            
  50.             ClaimsIdentity identity;
  51.             using (var scope = _lifetimeScope.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag))
  52.             {
  53.                 var mediator = scope.Resolve<IMediator>();
  54.                 identity = await mediator.SendAsync(command);
  55.             }
  56.  
  57.             var authProperties = new AuthenticationProperties();
  58.             var ticket = new AuthenticationTicket(identity, authProperties);
  59.             context.Validated(ticket);
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement