API authorization without decorators

30 views
Skip to first unread message

Francesco S

unread,
Nov 20, 2014, 8:40:29 AM11/20/14
to web...@googlegroups.com
I am novice in web2py. I am developing an angularjs+web2py web application. 
In addition to using web2py to write the APIs, at least at the first stage, 
I would like to use the web2py authentication class (i.e., no API approach for authentication)
and to have it working even when calling the APIs using curl.
Anyhow, I do not want to use decorators to force authentication for the APIs but I need to 
handle it manually (e.g., the some APIs should return different results if the user is   
logged or not logged but the APIs must always return a result).

To clarify, I would like to write something like that:

auth.settings.allow_basic_login = True
@request.restful()
@auth.requires(True, requires_login=False)   # ===> WORKAROUND
def api_auth_test():
    response
.view = 'generic.json'
   
import gluon.contrib.simplejson
   
def GET():
       
if auth.is_logged_in():
           
return gluon.contrib.simplejson.dumps(dict(results="I am logged"))
       
else:  
           
return gluon.contrib.simplejson.dumps(dict(results="I am NOT logged"))
   
return locals()                                                    


But it seems that the authorization works only if I include an authorization decorator 
which I do not want to add. As a workaround I added the fake decorator
@auth.requires(True, requires_login=False)
and it seems to work but I wonder if I am missing something and if there is a clean
way to achieve the result. Thanks.

Francesco

Massimo Di Pierro

unread,
Nov 21, 2014, 5:52:33 PM11/21/14
to web...@googlegroups.com
You should be able to do 

if not auth.user: do_whatever_you_like()

instead of using the decorator.
Reply all
Reply to author
Forward
0 new messages