def app_stripe(): if 'plan' not in request.values: plan = 'monthly2.99' else: plan = str(request.values['plan']) if plan not in payments.PLAN_NAMES: return Response(status=404) if 'monthly' in plan: term = 'monthly' else: term = 'yearly' template_values = { 'ACTOR_ID': str(request.values['id']), 'PLAN': plan, 'AMOUNT': payments.PLAN_NAMES[plan]['amount'], 'TERM': term, 'KEY': os.getenv('STRIPE_PUBLIC_KEY', 'pk_test_LWrQJLMJgvPWgtXfdMemnA4a') } if request.method == 'GET': return render_template('stripe-form.html', **template_values) if request.method == 'POST': me = actor.Actor(request.values['id'], get_config()) result = payments.process_card( actor=me, plan=plan, token=request.values['stripeToken'], config=get_config() ) if result: return render_template('stripe-form-success.html', **template_values) else: return render_template('stripe-form-failure.html', **template_values) return Response(status=404)
def re_init(self, actor_id=None, new_actor=None): self.bot_request = False if new_actor: self.me = new_actor self.actor_id = new_actor.id elif not actor_id and not self.person_id: return False elif not actor_id: self.me = actor.Actor(config=self.config) self.bot_request = True self.me.get_from_property(name='oauthId', value=self.person_id) if self.me.id: logging.debug('Found Actor(' + self.me.id + ')') else: self.me = None return False self.actor_id = self.me.id elif actor_id: self.actor_id = actor_id self.is_actor_user = True self.link = ciscowebexteams.CiscoWebexTeams(auth=self.auth, actor_id=self.actor_id, config=self.config) self.store = armyknife.ArmyKnife(actor_id=self.actor_id, config=self.config) return True
def run_backfill(actor_id=None): if not actor_id: return None this_actor = actor.Actor(actor_id=actor_id, config=get_config()) this_auth = auth.Auth(actor_id=actor_id, config=get_config()) res = on_aw.run_backfill(this_actor, config=get_config(), auth=this_auth) if res: return json.dumps(res) return None
def run_cron(): st = store.GlobalStore(id=None, config=get_config()) attrs = st.get_bucket("cron") res = list() for i in attrs: this_actor = actor.Actor(actor_id=i["id"], config=get_config()) this_auth = auth.Auth(actor_id=i["id"], config=get_config()) res.append( on_aw.run_cron(this_actor, config=get_config(), auth=this_auth)) if res: return json.dumps(res) return None
def app_root(): h = Handler(request) if not h.process(): return Response(status=404) if h.get_status() == 400: existing = actor.Actor(config=get_config()) existing.get_from_creator(request.values.get('creator')) if existing.id: return redirect(get_config().root + existing.id + '/www?refresh=true', 302) else: return render_template('aw-root-failed.html', **h.webobj.response.template_values) if request.method == 'GET': return render_template('aw-root-factory.html', **h.webobj.response.template_values) return h.get_response()
def post(self): myself = actor.Actor(config=self.config) try: params = json.loads(self.request.body.decode('utf-8', 'ignore')) is_json = True if 'creator' in params: creator = params['creator'] else: creator = '' if 'trustee_root' in params: trustee_root = params['trustee_root'] else: trustee_root = '' if 'passphrase' in params: passphrase = params['passphrase'] else: passphrase = '' except ValueError: is_json = False creator = self.request.get('creator') trustee_root = self.request.get('trustee_root') passphrase = self.request.get('passphrase') if not myself.create( url=self.request.url, creator=creator, passphrase=passphrase): self.response.set_status(400, 'Not created') logging.warning("Was not able to create new Actor(" + str(self.request.url) + " " + str(creator) + ")") return if len(trustee_root) > 0: myself.store.trustee_root = trustee_root self.response.headers["Location"] = str(self.config.root + myself.id) if self.config.www_auth == 'oauth' and not is_json: self.response.set_redirect(self.config.root + myself.id + '/www') return pair = { 'id': myself.id, 'creator': myself.creator, 'passphrase': str(myself.passphrase), } if len(trustee_root) > 0: pair['trustee_root'] = trustee_root if self.config.ui and not is_json: self.response.template_values = pair return out = json.dumps(pair) self.response.write(out) self.response.headers["Content-Type"] = "application/json" self.response.set_status(201, 'Created')
def __init__(self, actor_id, auth_type='basic', config=None): if not config: self.config = config_class.Config() else: self.config = config self.token = None self.cookie_redirect = None self.cookie = None self.type = auth_type self.trust = None self.oauth = None # Proposed response code after check_authentication() or authorise() have been called self.response = {'code': 403, 'text': "Forbidden", 'headers': {}} # Whether authentication is complete or not (depends on flow) self.authn_done = False # acl stores the actual verified credentials and access rights after # authentication and authorisation have been done self.acl = { "authenticated": False, # Has authentication been verified and passed? "authorised": False, # Has authorisation been done and appropriate acls set? "rights": '', # "a", "r" (approve or reject) "relationship": None, # E.g. creator, friend, admin, etc "peerid": '', # Peerid if there is a relationship "approved": False, # True if the peer is approved } self.actor = actor.Actor(actor_id, config=self.config) if not self.actor.id: self.actor = None self.oauth = oauth.OAuth(token=None, config=self.config) self.token = None self.expiry = None self.refresh_expiry = None self.refresh_token = None return # We need to initialise oauth for use towards the external oauth service # Property name used to set self.token self.oauth_token_property = 'oauth_token' self.token = self.actor.store.oauth_token if self.config.migrate_2_5_0 and not self.token: self.token = self.actor.property.oauth_token if self.token: self.actor.store.oauth_token = self.token self.actor.property.oauth_token = None self.oauth = oauth.OAuth(token=self.token, config=self.config) self.expiry = self.actor.store.oauth_token_expiry self.refresh_expiry = self.actor.store.oauth_refresh_token_expiry self.refresh_token = self.actor.store.oauth_refresh_token if self.config.migrate_2_5_0: if not self.expiry: self.expiry = self.actor.property.oauth_token_expiry if self.expiry: self.actor.store.oauth_token_expiry = self.expiry self.actor.property.oauth_token_expiry = None if not self.refresh_expiry: self.refresh_expiry = self.actor.property.oauth_refresh_token_expiry if self.refresh_expiry: self.actor.store.oauth_refresh_token_expiry = self.refresh_expiry self.actor.property.oauth_refresh_token_expiry = None if not self.refresh_token: self.refresh_token = self.actor.property.oauth_refresh_token if self.refresh_token: self.actor.store.oauth_refresh_token = self.refresh_token self.actor.property.oauth_refresh_token = None if self.type == 'basic': self.realm = self.config.auth_realm elif self.type == 'oauth': if self.oauth.enabled(): self.cookie = 'oauth_token' redir = self.actor.store.cookie_redirect if self.config.migrate_2_5_0 and not redir: redir = self.actor.property.cookie_redirect if redir: self.actor.store.cookie_redirect = redir self.actor.property.cookie_redirect = None if redir: self.cookie_redirect = self.config.root + redir else: self.cookie_redirect = None self.redirect = str(self.config.root + self.actor.id + '/oauth') else: self.type = 'none'
def process_webhook(payload, signature, config): try: event = stripe.Webhook.construct_event(payload, signature, WEBHOOK_SECRET) except Exception: # Invalid payload or signature logging.error('Webhook signature verification error') return 400 try: payload = json.loads(payload) logging.error('Stripe webhook json decode error') except: return 400 cust = None bot = CiscoWebexTeams(auth=None, actor_id=None, config=config) if 'invoice.' in payload['type']: try: cust = stripe.Customer.retrieve(payload['data']['object']['customer']) actor_id = cust.get('description', None) if not actor_id: return 204 except: # Not possible to know who this event belongs to bot.post_admin_message( text="Got a Stripe webhook where no actor could be found: " + payload['data']['object']['customer'], markdown=True ) return 204 store = ArmyKnife(actor_id, config) user = actor.Actor(actor_id, config) attr = store.get_perm_attribute('subscription') if 'invoice.payment_succeeded' in payload['type']: # Notify admin, update subscription end time if 'data' in payload and 'object' in payload['data'] and 'subscription' in payload['data']['object']: sub = payload['data']['object'] amount = sub.get('amount_paid', 0) if attr and 'data' in attr: data = attr['data'] else: data = {} if 'sub_id' not in data: data['sub_id'] = sub['subscription'] if data['sub_id'] == sub['subscription']: if data['sub_start'] < sub['period_start']: data['sub_start'] = sub['period_start'] if data['sub_end'] < sub['period_end']: data['sub_end'] = sub['period_end'] else: bot.post_admin_message( text="Got wrong subscription id for this user: "******"Payment of $" + str(amount/100.0) + " received from " + user.creator # + "\n\n```\n\n" + json.dumps(payload, sort_keys=True, indent=4) + "\n\n```\n\n" , markdown=True ) elif 'invoice.payment_failed' in payload['type']: # Notify admin and user bot.post_bot_message( email=actor.creator, text="Your subscription payment failed. Please do `/subscription` and update your card!", markdown=True ) bot.post_admin_message( text="Payment failed for " + actor.creator, markdown=True ) pass elif 'invoice.upcoming' in payload['type']: # Do nothing for now pass elif 'customer.source' in payload['type']: if 'customer.source.expiring' in payload['type']: # Notify user and share link to update card bot.post_bot_message( email=actor.creator, text="Your card will soon expire. Please do `/subscription` and update your card!", markdown=True ) elif 'customer.source.updated' in payload['type']: # Notify user on successful updated card bot.post_bot_message( email=actor.creator, text="Your card was successfully updated. Thanks!", markdown=True ) return 200