[Catalyst] How to return a 401 from Catalyst

Ian Docherty catalyst at iandocherty.com
Sun Jan 27 09:59:21 GMT 2008


Ian Docherty wrote:
> Related to my earlier thread about basic authentication, what do I 
> have to do to return a 401 error from within Catalyst?
>
> I have tried to understand the documentation at 
> http://www.ietf.org/rfc/rfc2617.txt but it does not help me in a 
> practical manner.
>
> My understanding is that when the Catalyst app refuses the credentials 
> given it must return a 401 with a 'realm' that is
> just a string used to identify the service.
>
> For example, when I create a test script for my Controller using LWP 
> as follows
>
> my $ua = LWP::UserAgent->new;
> $ua->credentials('linux:8001','some_realm_or_other','sage_1001','sage');
>
> Then my script fails because I have not set a 'realm' correctly in my 
> Catalyst controller. All I have done so far is as follows.
>
>    if ($login_success) ) {
>       # do something
>    }
>    else {
>        $c->response->status(401);
>        return 0;
>    }
>
> Obviously I need to do more to return the realm, but what exactly?
Answering my own question after a bit of digging around.

It seems that all that is needed is to return the appropriate header as 
follows.

        $c->response->header('WWW-Authenticate' => 'Basic realm="My 
Realm"');
        $c->response->status(401);
        return 0;

Simple really ;)
>
> Regards
> Ian
>
> p.s. I note that there is nothing in Test::WWW (that I can find) that 
> tests return status's such as 200, 401 etc.
>
>
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: 
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>




More information about the Catalyst mailing list