class IFacebookloginSettings(Interface): fb_app_id = schema.TextLine( title=_(u'App ID/API Key'), description= _(u'The App ID/API Key you got when creating the app at https://developers.facebook.com/apps' )) fb_app_secret = schema.TextLine( title=_(u'App Secret'), description= _(u'The App Secret Key you got when creating the app at https://developers.facebook.com/apps' ))
def __call__(self): registry = getUtility(IRegistry) FB_APP_ID = registry.get('cs.auth.facebook.controlpanel.IFacebookloginSettings.fb_app_id').encode() FB_APP_SECRET = registry.get('cs.auth.facebook.controlpanel.IFacebookloginSettings.fb_app_secret').encode() verificationCode = self.request.form.get("code", None) error = self.request.form.get("error", None) errorReason = self.request.form.get("error_reason", None) salt = hashlib.sha256().hexdigest() sdm = getToolByName(self.context, "session_data_manager") session = sdm.getSessionData(create=True) session[FB_AUTHENTICATION_SALT_KEY] = salt args = { 'state': salt, 'scope': PERMISSIONS, 'client_id': FB_APP_ID, 'redirect_uri': "%s/%s" % (self.context.absolute_url(), self.__name__,), } # Did we get an error back after a Facebook redirect? if error is not None or errorReason is not None: log.info(error) log.info(errorReason) IStatusMessage(self.request).add(_(u"Facebook authentication denied"), type="error") self.request.response.redirect(self.context.absolute_url()) return u"" # Check if the status is the same... return_salt = self.request.form.get('status', '') if return_salt and return_salt != session.get(FB_AUTHENTICATION_SALT_KEY): IStatusMessage(self.request).add(_(u"Facebook authentication denied"), type="error") self.request.response.redirect(self.context.absolute_url()) log.info('%s != %s' % (return_salt, session.get(FB_AUTHENTICATION_SALT_KEY))) return u"" # If there is no code, this is probably the first request, so redirect # to Facebook if verificationCode is None: self.request.response.redirect( "%s?%s" % (FACEBOOK_AUTH_URL, urllib.urlencode(args),) ) return u"" # If we are on the return path form Facebook, # exchange the return code for a token args["client_secret"] = FB_APP_SECRET args["code"] = verificationCode response = urlparse.parse_qs(urllib.urlopen( "%s?%s" % (FACEBOOK_ACCESS_TOKEN_URL, urllib.urlencode(args),) ).read()) # Load the profile using the access token we just received accessToken = response["access_token"][-1] profile = json.load(urllib.urlopen( "%s?%s" % (FACEBOOK_PROFILE_URL, urllib.urlencode({'access_token': accessToken}),) )) userId = profile.get('id').encode("utf-8") name = profile.get('name').encode("utf-8") email = profile.get('email', '').encode("utf-8") username = profile.get('username', '').encode("utf-8") location = profile.get('location', {}).get('name', '').encode("utf-8") profile_image = urllib.urlopen( "%s?%s" % (FACEBOOK_PROFILE_PICTURE_URL, urllib.urlencode({'access_token': accessToken}),) ).read() if not userId or not name: IStatusMessage(self.request).add(_(u"Insufficient information in Facebook profile"), type="error") self.request.response.redirect(self.context.absolute_url()) return u"" # Save the data in the session so that the extraction plugin can # authenticate the user to Plone session[SessionKeys.accessToken] = accessToken session[SessionKeys.userId] = userId session[SessionKeys.userName] = username or userId session[SessionKeys.fullname] = name session[SessionKeys.email] = email session[SessionKeys.location] = location session[SessionKeys.profile_image] = profile_image # Add user data into our plugin storage: acl = self.context.acl_users acl_plugins = acl.plugins ids = acl_plugins.listPluginIds(IExtractionPlugin) for id in ids: plugin = getattr(acl_plugins, id) if ICSFacebookPlugin.providedBy(plugin): user_data = plugin._storage.get(session[SessionKeys.userId], {}) user_data['username'] = session[SessionKeys.userName] user_data['fullname'] = session[SessionKeys.fullname] user_data['email'] = session[SessionKeys.email] user_data['location'] = session[SessionKeys.location] user_data['portrait'] = session[SessionKeys.profile_image] plugin._storage[session[SessionKeys.userId]] = user_data IStatusMessage(self.request).add(_(u"Welcome. You are now logged in."), type="info") return_args = '' if self.request.get('came_from', None) is not None: return_args = {'came_from': self.request.get('came_from')} return_args = '?' + urllib.urlencode(return_args) self.request.response.redirect(self.context.absolute_url() + '/logged_in' + return_args)
def __call__(self): registry = getUtility(IRegistry) S = 'cs.auth.facebook.controlpanel.IFacebookloginSettings' FB_APP_ID = registry.get(S + '.fb_app_id').encode() FB_APP_SECRET = registry.get(S + '.fb_app_secret').encode() verificationCode = self.request.form.get("code", None) error = self.request.form.get("error", None) errorReason = self.request.form.get("error_reason", None) salt = hashlib.sha256().hexdigest() sdm = getToolByName(self.context, "session_data_manager") session = sdm.getSessionData(create=True) session[FB_AUTHENTICATION_SALT_KEY] = salt args = { 'state': salt, 'scope': PERMISSIONS, 'client_id': FB_APP_ID, 'redirect_uri': "%s/%s" % ( self.context.absolute_url(), self.__name__, ), } # Did we get an error back after a Facebook redirect? if error is not None or errorReason is not None: log.info(error) log.info(errorReason) IStatusMessage(self.request).add( _(u"Facebook authentication denied"), type="error") self.request.response.redirect(self.context.absolute_url()) return u"" # Check if the status is the same... return_salt = self.request.form.get('status', '') if return_salt and \ (return_salt != session.get(FB_AUTHENTICATION_SALT_KEY)): IStatusMessage(self.request).add( _(u"Facebook authentication denied"), type="error") self.request.response.redirect(self.context.absolute_url()) log.info('%s != %s' % (return_salt, session.get(FB_AUTHENTICATION_SALT_KEY))) return u"" # If there is no code, this is probably the first request, so redirect # to Facebook if verificationCode is None: self.request.response.redirect("%s?%s" % ( FACEBOOK_AUTH_URL, urllib.urlencode(args), )) return u"" # If we are on the return path form Facebook, # exchange the return code for a token args["client_secret"] = FB_APP_SECRET args["code"] = verificationCode req = urllib.urlopen("%s?%s" % ( FACEBOOK_ACCESS_TOKEN_URL, urllib.urlencode(args), )) response = json.loads(req.read()) # Load the profile using the access token we just received accessToken = response["access_token"] profile = json.load( urllib.urlopen("%s?%s" % ( FACEBOOK_PROFILE_URL, urllib.urlencode({ 'access_token': accessToken, 'fields': 'email,name' }), ))) userId = profile.get('id').encode("utf-8") name = profile.get('name').encode("utf-8") email = profile.get('email', '').encode("utf-8") username = profile.get('username', '').encode("utf-8") location = profile.get('location', {}).get('name', '').encode("utf-8") profile_image = urllib.urlopen("%s?%s" % ( FACEBOOK_PROFILE_PICTURE_URL, urllib.urlencode({'access_token': accessToken}), )).read() if not userId or not name: IStatusMessage(self.request).add( _(u"Insufficient information in Facebook profile"), type="error") self.request.response.redirect(self.context.absolute_url()) return u"" # Save the data in the session so that the extraction plugin can # authenticate the user to Plone session[SessionKeys.accessToken] = accessToken session[SessionKeys.userId] = userId session[SessionKeys.userName] = username or userId session[SessionKeys.fullname] = name session[SessionKeys.email] = email session[SessionKeys.location] = location session[SessionKeys.profile_image] = profile_image # Add user data into our plugin storage: acl = self.context.acl_users acl_plugins = acl.plugins ids = acl_plugins.listPluginIds(IExtractionPlugin) for id in ids: plugin = getattr(acl_plugins, id) if ICSFacebookPlugin.providedBy(plugin): user_data = plugin._storage.get(session[SessionKeys.userId], {}) user_data['username'] = session[SessionKeys.userName] user_data['fullname'] = session[SessionKeys.fullname] user_data['email'] = session[SessionKeys.email] user_data['location'] = session[SessionKeys.location] user_data['portrait'] = session[SessionKeys.profile_image] plugin._storage[session[SessionKeys.userId]] = user_data IStatusMessage(self.request).add(_(u"Welcome. You are now logged in."), type="info") return_args = '' if self.request.get('came_from', None) is not None: return_args = {'came_from': self.request.get('came_from')} return_args = '?' + urllib.urlencode(return_args) self.request.response.redirect(self.context.absolute_url() + '/logged_in' + return_args)
from cs.auth.facebook import FBMessageFactory as _ from plone.app.registry.browser.controlpanel import ( ControlPanelFormWrapper, RegistryEditForm, ) from plone.z3cform import layout from zope import schema from zope.interface import Interface class IFacebookloginSettings(Interface): fb_app_id = schema.TextLine( title=_(u'App ID/API Key'), description= _(u'The App ID/API Key you got when creating the app at https://developers.facebook.com/apps' )) fb_app_secret = schema.TextLine( title=_(u'App Secret'), description= _(u'The App Secret Key you got when creating the app at https://developers.facebook.com/apps' )) class FacebookloginControlPanelForm(RegistryEditForm): schema = IFacebookloginSettings FacebookloginControlPanelView = layout.wrap_form(FacebookloginControlPanelForm, ControlPanelFormWrapper) FacebookloginControlPanelView.label = _(u"Facebooklogin Settings")
from cs.auth.facebook import FBMessageFactory as _ from plone.app.registry.browser.controlpanel import ( ControlPanelFormWrapper, RegistryEditForm, ) from plone.z3cform import layout from zope import schema from zope.interface import Interface class IFacebookloginSettings(Interface): fb_app_id = schema.TextLine(title=_(u'App ID/API Key'), description=_(u'The App ID/API Key you got when creating the app at https://developers.facebook.com/apps')) fb_app_secret = schema.TextLine(title=_(u'App Secret'), description=_(u'The App Secret Key you got when creating the app at https://developers.facebook.com/apps')) class FacebookloginControlPanelForm(RegistryEditForm): schema = IFacebookloginSettings FacebookloginControlPanelView = layout.wrap_form(FacebookloginControlPanelForm, ControlPanelFormWrapper) FacebookloginControlPanelView.label = _(u"Facebooklogin Settings")