screeley.com

Authentication Header and XMl

Jan.6

While working on the Django Alfresco integration I came across one of the most miserable bugs so far in the process. In order to make sure no one else has to go through it again, I'll write a little about it.

I was in the process of moving authentication out of the get parameters and into the header when I started getting the following error on POST or PUT.

Wrapped Exception (with status template): com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs.n at [row,col {unknown-source}]: [2,5]

Alfresco was not happy and I didn't really know why. Now best method to add an auth header is to use urllib's password manager, but that doesn't work because Alfresco accepts both username/password and a ticket for authentication. The ticket doesn't work with the password manager so we add the header manually.

The code looked something like this, where auth is user:password or a ticket.

class RESTRequest(urllib2.Request):
    def __init__(self, *args, **kwargs):
        self._method = kwargs.pop('method', 'GET')
        self._auth = kwargs.pop('auth', None)
        assert self._method in ['GET', 'POST', 'PUT', 'DELETE']
        urllib2.Request.__init__(self, *args, **kwargs)
        if self._auth:
            self.add_header('Authorization', 'Basic %s' 
            %  base64.encodestring(self._auth))

    def get_method(self):
        return self._method

Google wasn't that friendly at first, so I went through every line of Alfresco Restful services code to try to figure what was wrong with it. Obviously not the issue.

Then I found this post. Apparently base64.encodestring() adds a newline which makes the xml declaration the second line in the document and everything gets hosed up. Adding a strip() after the encoding fixes this.

self.add_header('Authorization', 'Basic %s' 
    %  base64.encodestring(self._auth).strip())

That's it. Enjoy

Comments

I wish you told us more about the django/alfresco integration ...

Thx

I'm a developer out of San Francisco CA working at a startup.

This space will deal with the work I've participated in using the Django framework to build applications for enterprise clients.

Finally, you should follow me on twitter.

Ruminations

  • "GobgoplebeM <a href=http://posterous.com/people/4SDzppk18fMR>сиалис цены</a> undilyday"
    at 3:24a.m. Sept. 6, 2010 | permalink

  • "generic z-pak <a href=http://sefsa.org>buy azithromycin</a>"
    at 7:53p.m. Aug. 27, 2010 | permalink

  • "How do i come up with cash from online gambling? <img>http://shrtn.info/smile/ref.php</img>"
    at 2:50a.m. Aug. 25, 2010 | permalink

  • "http://needman.ru замуж за иностранца <a href=http://needman.ru>знакомства с иностранцами</a>"
    at 12:59p.m. May 18, 2010 | permalink

  • "Yebhewjw <a href="http://yebhewjw.de">yebhewjw</a> http://yebhewjw.de yebhewjw http://yebhewjw.de"
    at 11:41p.m. April 29, 2010 | permalink

  • "Thanks for this, unbelievable our developer has a robots no follow tag on our site, no wonder it wasn't being found by the search engines ..."
    at 7:40a.m. March 2, 2010 | permalink

  • "maybe you are right. but how often robots.txt is actually accessed? and how much overhead there is? I'm curious - quantitatively - how big of ..."
    at 7:13p.m. Dec. 12, 2009 | permalink

  • "Lovely idea! Thanks for sharing. I'm gonna have a closer look at the patch for Django 1.2. This could help switching template engines a lot. ..."
    at 9:14a.m. Nov. 2, 2009 | permalink

  • "That was an inspiring post, I think Drupal is great! how could you hate it so much, Thanks for writing, most people don't bother."
    at 11:14a.m. Oct. 28, 2009 | permalink

  • "@Evgeniy. Yes at: http://code.google.com/p/django-alfresco/"
    at 10:42a.m. Oct. 22, 2009 | permalink

  • "Is this released as an open source project?"
    at 1:21a.m. Oct. 22, 2009 | permalink

  • "Interesting, thanks for the examples that you have shared, these are great... Anyway, thanks for the post"
    at 7:55a.m. Oct. 16, 2009 | permalink