models/db.py

from gluon.tools import Auth,Crud, Service, prettydate
from gluon.contrib.login_methods.basic_auth import basic_auth
import datetime
crud, service = Crud(db), Service(), 
auth = Auth(db)
auth.define_tables(username=True)
auth.settings.allow_basic_login = True
auth.settings.hmac_key = 'sha512:a-pass-phrase'
auth.settings.password_min_length = 4
auth.settings.login_after_registration = True
auth.settings.create_user_groups = False
auth.settings.login_email_validate = False

auth.settings.login_methods = [basic_auth('http://127.0.0.1:8000')]

...

session 
<https://marsrover-dev.eng.yorku.ca/examples/global/vars/session>.connect(request
 <https://marsrover-dev.eng.yorku.ca/examples/global/vars/request>, response 
<https://marsrover-dev.eng.yorku.ca/examples/global/vars/response>, db) #VERY 
IMPORTANT


My only concern is the line with basic_auth above, right now its pointed to 
127.0.0.1:8000, but it seems to be working on my webserver so maybe that should 
be just blank.


controllers/default.py
@service.jsonrpc
def count():
    session.counter = (session.counter or 0) + 1
    return dict(counter=session.counter, now=request.now)

def user():
    return dict(form=auth())

def download():
    return response.download(request, db)

@auth.requires_login()
def call():
    #session.forget(response) #uncomment if you do NOT want sessions
    return service()

def tester():
    from gluon.contrib.simplejsonrpc import ServerProxy
    URL = "http://user:pass@127.0.0.1:8000/app/default/call/jsonrpc";
    service = ServerProxy(URL, verbose=True)
    
    return dict(count=service.count())


The tester you can output on a webpage or you can do it in the terminal.

Now remember even though you have authentication if you want to manage a 
session you will need to manually send HTTP headers since the simplejsonrpc 
does not store the session cookie, and I have not found an implementation 
in Python that does. So I just use the requests library to build the 
message and then the count session variable increases by 1 and is stored. 
You can either store it as a static file or on the database(my case).

Good luck.

On Monday, February 4, 2013 1:44:31 AM UTC-5, Anthony wrote:
>
> But that text is preceded by:
>
> Some times you want to implement your own logic and do "manual" user 
> login. This can also be done by calling the function:
>
> user = auth.login_bare(username,password)
>
>
> To me, that says you can do a manual user login by calling 
> auth.login_bare() (the method name is kind of a giveaway as well). What 
> else should it say?
>
> Anthony
>
> On Sunday, February 3, 2013 10:05:16 PM UTC-5, encompass wrote:
>>
>> I copied the text from the web2py book online.  It should say that they 
>> user is then logged in.  At least for me it needed to be more descriptive.
>> BR,
>> Jason
>>
>> On Saturday, January 19, 2013 5:22:11 PM UTC+2, Massimo Di Pierro wrote:
>>>
>>> Where do you read that comment? login_bare does login the user and 
>>> returns the user record.
>>>
>>> On Saturday, 19 January 2013 00:31:00 UTC-6, encompass wrote:
>>>>
>>>> The last link was the one I was looking at first.  Before this post.  
>>>> But 
>>>> """
>>>> login_bare returns user if the user exists and the password is valid, 
>>>> else it returns False. username is the email if the "auth_user" table 
>>>> does not have a "username" field.
>>>> """
>>>> doesn't sound like I logged in a user.  It just returns the user  and 
>>>> returns false.  But I will give it a try.
>>>> BR,
>>>> Jason Brower
>>>>
>>>> On Friday, January 18, 2013 11:39:35 PM UTC+2, Alan Etkin wrote:
>>>>>
>>>>> My current app runs with only JSON calls is there a way to make the 
>>>>>> login occure with JSON?
>>>>>>
>>>>>
>>>>> Did you check this?
>>>>>
>>>>>
>>>>> http://www.web2py.com/books/default/chapter/29/10#Services-and-Authentication
>>>>>
>>>>> http://www.web2py.com/books/default/chapter/29/09#Access-Control-and-Basic-Authentication
>>>>> http://www.web2py.com/books/default/chapter/29/09#Manual-Authentication
>>>>>
>>>>>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to