ajkavanagh / pyramid_jwtauth

JSON Web Token (JWT) Auth plugin for Pyramid
12 stars 9 forks source link

Example Documentation #3

Closed vcatalano closed 9 years ago

vcatalano commented 9 years ago

Could you include an example configuration as part of the documentation? I've been looking through the tests and it's not entirely clear how I would configure this plugin for an existing project.

ajkavanagh commented 9 years ago

Do you mean something like this? (this is from my production app using it):

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    # Need to change this as I want to read from a ENV variable
    settings = expandvars_dict(settings)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(settings=settings)
    config.include('pyramid_mako')
    config.include("cornice")
    config.include("pyramid_jwtauth")
    config.include("wsauthapp.utils.auth_utils")  # gets the JWT signing code
    config.add_static_view(name=settings["static_assets"],
                           path='wsauthapp:static',
                           cache_max_age=3600)
    config.add_route('login', '/login')
    config.add_route('redirect', '/redirect')
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()

and the config:

###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
###

[app:main]
use = egg:ws-authapp

pyramid.reload_templates = false
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes =
    pyramid_tm

jwtauth.private_key_file = /home/ubuntu/current_version/conf/jwt/authapp_jwt_rsa
jwtauth.public_key_file = /home/ubuntu/current_version/conf/jwt/authapp_jwt_rsa.pub
jwtauth.algorithm = RS256

...
vcatalano commented 9 years ago

Actually, I was confused how to actually create the token. I ended up figuring it out.

        policy = request.registry.queryUtility(IAuthenticationPolicy)

        claims = {
            'sub': cred.email,
        }

       # JSON data returned on login
        return {
            'token': policy.encode_jwt(None, claims=claims),
            'user': {
                'id': cred.user.id,
            }
        }
ajkavanagh commented 9 years ago

Okay, no worries. I'll close this for now.