コード例 #1
0
        public AuthenticationModule(IServiceAuthenticatorHost serviceAuthenticatorHost,
                                    IAuthenticationService authenticationService,
                                    IUserService userService,
                                    IJwtTokenHandler jwtTokenHandler,
                                    ICommandHandler <SignIn> signInHandler,
                                    ICommandHandler <RefreshUserSession> refreshSessionHandler)
            : base(requireAuthentication: false)
        {
            _authenticationService = authenticationService;
            _userService           = userService;
            _jwtTokenHandler       = jwtTokenHandler;

            Post("authenticate", args =>
            {
                var credentials = BindRequest <Credentials>();
                var token       = serviceAuthenticatorHost.CreateToken(credentials);
                if (token.HasNoValue)
                {
                    return(HttpStatusCode.Unauthorized);
                }

                return(token.Value);
            });

            Post("sign-in", async args =>
            {
                var command = BindRequest <SignIn>();
                await signInHandler.HandleAsync(command);
                var session = await HandleSessionAsync(command.SessionId);
                if (session.HasNoValue)
                {
                    return(HttpStatusCode.Unauthorized);
                }

                return(session.Value);
            });

            Post("sessions", async args =>
            {
                var command = BindRequest <RefreshUserSession>();
                await refreshSessionHandler.HandleAsync(command);
                var session = await HandleSessionAsync(command.NewSessionId);
                if (session.HasNoValue)
                {
                    return(HttpStatusCode.Forbidden);
                }

                return(session.Value);
            });
        }
コード例 #2
0
        public AuthenticationModule(IServiceAuthenticatorHost serviceAuthenticatorHost)
            : base(requireAuthentication: false)
        {
            Post("authenticate", args =>
            {
                var credentials = this.BindRequest <Credentials>();
                var token       = serviceAuthenticatorHost.CreateToken(credentials);
                if (token.HasNoValue)
                {
                    return(HttpStatusCode.Unauthorized);
                }

                return(token.Value);
            });
        }