## - email capabilities ## - authentication (registration, login, logout, ... ) ## - authorization (role based authorization) ## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss) ## - old style crud actions ## (more options discussed in gluon/tools.py) ######################################################################### import datetime import utilities from gluon.tools import Auth, AuthJWT, PluginManager, Service from stopstalk_constants import * auth = Auth(db) auth_jwt = AuthJWT(auth, secret_key=current.jwt_secret, user_param="email") service = Service() plugins = PluginManager() all_countries = { u'Canada': u'CA', u'Moldova (Republic of)': u'MD', u'Sao Tome and Principe': u'ST', u'Guinea-Bissau': u'GW', u'United States of America': u'US', u'Lithuania': u'LT', u'Cambodia': u'KH', u'Saint Helena, Ascension and Tristan da Cunha': u'SH', u'Switzerland': u'CH', u'Ethiopia': u'ET', u'Aruba': u'AW',
# ------------------------------------------------------------------------- # JWT # ———————————————————————————————————— def secure_payload(payload): keys = ['registration_id', 'reset_password_key', 'registration_key'] for key in keys: if key in payload['user']: del payload['user'][key] #remove keys from payload return payload authJWT = AuthJWT(auth, secret_key=configuration.get('jwt.secret_key'), additional_payload=secure_payload) # ------------------------------------------------------------------------- # configure email # ------------------------------------------------------------------------- mail = auth.settings.mailer mail.settings.server = 'logging' if request.is_local else configuration.get( 'smtp.server') mail.settings.sender = configuration.get('smtp.sender') mail.settings.login = configuration.get('smtp.login') mail.settings.tls = configuration.get('smtp.tls') or False mail.settings.ssl = configuration.get('smtp.ssl') or False # ------------------------------------------------------------------------- # configure auth policy # -------------------------------------------------------------------------
# - authorization (role based authorization) # - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss) # - old style crud actions # (more options discussed in gluon/tools.py) # ------------------------------------------------------------------------- # host names must be a list of allowed host names (glob syntax allowed) auth = Auth(db, host_names=configuration.get('host.names')) # ------------------------------------------------------------------------- # create all tables needed by auth, maybe add a list of extra fields # ------------------------------------------------------------------------- auth.settings.extra_fields['auth_user'] = [] auth.define_tables(username=False, signature=False) auth_jwt = AuthJWT(auth, secret_key='secret', expiration=60 * 30) # ------------------------------------------------------------------------- # configure email # ------------------------------------------------------------------------- mail = auth.settings.mailer mail.settings.server = 'logging' if request.is_local else configuration.get( 'smtp.server') mail.settings.sender = configuration.get('smtp.sender') mail.settings.login = configuration.get('smtp.login') mail.settings.tls = configuration.get('smtp.tls') or False mail.settings.ssl = configuration.get('smtp.ssl') or False # ------------------------------------------------------------------------- # configure auth policy # -------------------------------------------------------------------------
import json from gluon.tools import AuthJWT, AuthAPI import requests import hashlib from gluon.contrib.appconfig import AppConfig from gluon.tools import Mail import uuid myconf = AppConfig() myjwt = AuthJWT(auth, secret_key='secret') mail = Mail() mail.settings.server = myconf.take('smtp.server') mail.settings.sender = myconf.take('smtp.sender') mail.settings.login = myconf.take('smtp.login') def api_requires_extension(func): def wrapper(*args): if request.extension != 'html': response.view = 'generic.' + request.extension else: response.view = 'generic.json' return func(*args) return wrapper @api_requires_extension @myjwt.allows_jwt() def api_requires_login(func):
return authentication else: authentication['token'] = None return authentication else: authentication['token'] = None return authentication def logout(): authentication = AuthAPI(db).logout(next=None) return authentication # this one receives the credentials and gives you a token refer to gluon/tools.py 1132 line myjwt = AuthJWT(auth, secret_key='secretsddfsdfsd') # this one receives the credentials and gives you a token refer to gluon/tools.py 1132 line def token(): return myjwt.jwt_token_manager() @myjwt.allows_jwt() @auth.allows_jwt() def protected(): return '%s$%s' % (request.now, auth.user_id)