Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Nameko Auth Toy
from nameko.standalone.rpc import ClusterRpcProxy
config = {
'AMQP_URI': 'amqp://guest:guest@localhost:5672/'
with ClusterRpcProxy(config) as rpc:
session_token = rpc.auth.login("admin", "secret")
with ClusterRpcProxy(config, context_data={'session': session_token}) as rpc:
Toy authentication and authorization example using JSON Web Tokens
from nameko.extensions import DependencyProvider
from nameko.rpc import rpc, RpcProxy
import jwt
JWT_SECRET = "secret"
class NotAuthenticated(Exception):
class AuthService:
name = "auth"
def login(self, username, password):
if password == "secret":
# probably add roles or permissions for the user here
perms = []
roles = []
token = jwt.encode({
'username': username,
'permissions': [],
'roles': []
return token
raise NotAuthenticated()
class Authorization(DependencyProvider):
""" DependencyProvider giving services access to the current session.
class Api:
def __init__(self, token):
self._token = token
self._session = None
def session(self):
if self._session is None:
# lazily decode the token
self._session = jwt.decode(self._token, JWT_SECRET)
return self._session
def username(self):
return self.session['username']
def user_is(self, role):
return role in self.session['roles']
def user_can(self, perm):
return perm in self.session['permissions']
def get_dependency(self, worker_ctx):
return Authorization.Api(worker_ctx.context_data.get('session'))
class ServiceA:
name = 'a'
b_rpc = RpcProxy('b')
def foo(self):
class ServiceB:
name = 'b'
auth = Authorization()
def bar(self):
print("Logged in user: {}".format(self.auth.username))
if self.auth.user_is("admin"):
print("User is an admin")
print("User is not an admin")
Copy link

mattbennett commented Apr 14, 2017


$ python
$ nameko run services
starting services: a, b, auth
Connected to amqp://guest:**@
Connected to amqp://guest:**@
Connected to amqp://guest:**@
Logged in user: admin
User is not an admin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment