Example #1
0
        public string login([QueryField] string username, [QueryField] string password)
        {
            if (username == null || password == null)
            {
                throw HttpException.BadRequest();
            }

            var user = server.client.database
                       .GetCollection <AccountModel>(AccountModel.collectionName)
                       .Find(x => x.username == username.ToLower());

            if (!user.Any())
            {
                throw HttpException.Unauthorized();
            }

            if (!user.First().checkPassword(password))
            {
                throw HttpException.Unauthorized();
            }

            return(JWT.Encode(new AuthorizationPayload {
                fullName = username,
                validUntil = DateTime.Now.AddHours(1), // Expire in 1 hour
                permissions = user.First().permissions
            }, Encoding.UTF8.GetBytes(server.config.jwtSecret), JwsAlgorithm.HS256));
        }
Example #2
0
        protected override async Task OnRequestAsync(IHttpContext context)
        {
            if (server.config.jwtSecret == null)
            {
                await module.HandleRequestAsync(context);

                return;
            }

            var authorizationHeader = context.Request.Headers["Authorization"];

            if (!tokenIsValid(authorizationHeader))
            {
                throw HttpException.Unauthorized("Token is invalid.");
            }

            // Extract the incoming JWT Token from the header
            var token = authorizationHeader[bearerPrefix.Length..];
        /// <inheritdoc />
        protected sealed override async Task OnRequestAsync(IHttpContext context)
        {
            async Task <bool> IsAuthenticatedAsync()
            {
                try
                {
                    var(userName, password) = GetCredentials(context.Request);
                    return(await VerifyCredentialsAsync(context.RequestedPath, userName, password, context.CancellationToken)
                           .ConfigureAwait(false));
                }
                catch (FormatException)
                {
                    // Credentials were not formatted correctly.
                    return(false);
                }
            }

            if (!await IsAuthenticatedAsync().ConfigureAwait(false))
            {
                throw HttpException.Unauthorized();
            }

            context.Response.Headers.Set(HttpHeaderNames.WWWAuthenticate, _wwwAuthenticateHeaderValue);
        }
Example #4
0
 public void GetUnauthorized() =>
 throw HttpException.Unauthorized();
Example #5
0
        /// <summary>
        /// Rejects a authentication challenge.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="error">The error.</param>
        public static void Rejected(this IHttpContext context, object?error = null)
        {
            context.Response.Headers.Add(HttpHeaderNames.WWWAuthenticate, "Bearer");

            throw HttpException.Unauthorized(data: error);
        }
Example #6
0
 /// <summary>
 /// <para>Throws a <see cref="HttpException"/> with a response code of <c>401 Unauthorized</c>.</para>
 /// </summary>
 /// <param name="context">An <see cref="IHttpContext"/> interface representing the context of the request.</param>
 /// <param name="module">The authentication module that called the handler.</param>
 /// <returns>This method never returns; it throws an exception instead..</returns>
 public static Task Unauthorized(IHttpContext context, AuthenticationModuleBase module)
 => throw HttpException.Unauthorized();
 /// <summary>
 /// <para>Throws a <see cref="HttpException"/> with a response code of <c>401 Unauthorized</c>
 /// and, optionally, a custom message and data.</para>
 /// </summary>
 /// <param name="message">A message to include in the response.</param>
 /// <param name="data">The data object to include in the response.</param>
 /// <returns>This method never returns; it throws an exception instead..</returns>
 public static AuthenticationHandlerCallback Unauthorized(string message = null, object data = null)
 {
     return((context, module) => throw HttpException.Unauthorized(message, data));
 }
        /// <summary>
        /// <para>Throws a <see cref="HttpException"/> with a response code of <c>401 Unauthorized</c>.</para>
        /// </summary>
        /// <param name="context">An <see cref="IHttpContext"/> interface representing the context of the request.</param>
        /// <param name="module">The authentication module that called the handler.</param>
        /// <returns>This method never returns; it throws an exception instead..</returns>
#pragma warning disable CA1801  // Remove unused parameter
        public static Task Unauthorized(IHttpContext context, AuthenticationModuleBase module)
#pragma warning restore CA1801  // Remove unused parameter
        => throw HttpException.Unauthorized();
Example #9
0
 /// <summary>
 /// <para>Unconditionally sends a <c>403 Unauthorized</c> response.</para>
 /// </summary>
 /// <param name="context">A <see cref="IHttpContext"/> interface representing the context of the request.</param>
 /// <param name="info">If the requested path has been successfully mapped to a resource (file or directory), the result of the mapping;
 /// otherwise, <see langword="null"/>.</param>
 /// <returns>This method never returns; it throws a <see cref="HttpException"/> instead.</returns>
 public static Task ThrowUnauthorized(IHttpContext context, MappedResourceInfo info)
 => throw HttpException.Unauthorized();