Skip to content

Commit

Permalink
set jwt token on plone login
Browse files Browse the repository at this point in the history
  • Loading branch information
mamico committed Jul 25, 2020
1 parent 06522d8 commit b93e410
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/plone/restapi/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,7 @@
provides="Products.CMFPlone.interfaces.INonInstallable"
/>

<subscriber handler=".subscribers.onUserLogsIn" />


</configure>
28 changes: 28 additions & 0 deletions src/plone/restapi/subscribers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from plone.restapi.interfaces import IPloneRestapiLayer
from Products.PluggableAuthService.interfaces.events import IUserLoggedInEvent
from Products.PluggableAuthService.interfaces.plugins import IAuthenticationPlugin
from zope.component import adapter
from zope.globalrequest import getRequest


@adapter(IUserLoggedInEvent)
def onUserLogsIn(event):
req = getRequest()
if IPloneRestapiLayer.providedBy(req):
user = event.principal
uf = user.aq_parent
plugins = uf._getOb("plugins")
authenticators = plugins.listPlugins(IAuthenticationPlugin)
plugin = None
for id_, authenticator in authenticators:
if authenticator.meta_type == "JWT Authentication Plugin":
plugin = authenticator
break
if plugin:
payload = {}
payload["fullname"] = user.getProperty("fullname")
token = plugin.create_token(user.getId(), data=payload)
# TODO: take care of path and domain options ?
req.response.setCookie('auth_token', token, path='/')

0 comments on commit b93e410

Please sign in to comment.