Spyne support user authentication?

616 Views Asked by At

I'm new to Sypne, would like to build a webservice, but not open to all, only to specified users. Any support documentation, example or any other packages needed? I'm using Django as the web framework. Thanks!

1

There are 1 best solutions below

0
On

For information, here is the link to the spyne documentation. http://spyne.io/docs/2.10/

You can handle authentication with the decorators as written in the documentation. http://spyne.io/docs/2.10/manual/04_usermanager.html#decorators-and-rpc.

from decorator import decorator

def _check_authentication(func, *args, **kw):
    print ("before call")
    ctx = args[0] # Get the ctx

    # Check if the token is in the header
    token = ctx.transport.req.get("HTTP_TOKEN")
    if token is None or token != "the_token_needed":
        return "Authentication failed"

    # Call the function
    result = func(*args, **kw)

    print ("after call")
    return result

def check_authentication(f):
    return decorator(_check_authentication, f)

class MyService(ServiceBase):

    @rpc(String, _returns=String)
    @check_authentication
    def service_function(ctx, order_id):
        # Here is your logic if the user is authenticated 

Don't forget to test your system authentication.