Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7992703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:38:36+00:00 2026-06-04T13:38:36+00:00

I’m trying to implement basic http authentication, but I need to return different HTTP

  • 0

I’m trying to implement basic http authentication, but I need to return different HTTP status codes based upon the status of the account beeing authenticated.
I’m using Sinatra to host the API, and this is what my application looks like:

require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'active_record'
require 'openssl'

# Define the paths that the application responds to, and include the authentication mechanism to provide
# basic HTTP authentication using the Rack middleware.
use Rack::Auth::Basic do |username, password|
  result = LoginApi::User.authenticate(username, password)

  # Check the status of the user we just authenticated. If he is banned or unverified then we
  # need to return special responses.
  throw :halt, [403, 'User is banned'] if LoginApi::AuthUser.user.banned?
  throw :halt, [412, 'User is not verified'] unless LoginApi::AuthUser.user.verified?

  # Return the result if no exceptions were raised in the meanwhile
  result
end

# This action responds on the root of the application and will return the basic authentication of a User
# in the headers. This is done by Rack::Auth, which requires the credentials to be sent in the initial
# call so they can be verified by the application
get '/' do
  headers \
    "X-UserID" => "#{LoginApi::AuthUser.user.id}|",
    "X-P1" => "#{LoginApi::AuthUser.p1}",
    "X-P2" => "#{LoginApi::AuthUser.p2}"
end

So basically, it returns a status 200 for ok authentication, 403 for a banned user, 412 for a not verified user and 400 if the authentication fails.

The problem is that I cant get my tests to properly work with this. The test for a normal authentication works fime, and passes, but the tests that need to check for the custom HTTP status reponses fail with the following error:

ArgumentError: uncaught throw :halt ./app.rb:23:in throw'
./app.rb:23:in
block in ‘
./features/step_definitions/when_steps.rb:8:in /^I make the call to
the API$/' ./features/banned.feature:8:in
When I make the call to the
API’

Skipped step

Can someone please explain what I am doing wrong or missing to get this to work as I want it to?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:38:37+00:00Added an answer on June 4, 2026 at 1:38 pm

    Moved the check for banned and unverified to the body of the action and just handle the result there.

    Shame I cannot do it in the Rack helper…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.