Demo Auth

From DevWiki

Revision as of 17:55, 12 July 2011 by Spectrer (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This file is used in the Demo User/Pass Authentication Application. It manages the user account linking using the hidden Boxee service for the PPVBox demo app.

import mc
import re
import cgi
import sys
import urllib
from xml.dom.minidom import parse, parseString
app_id = 'ppvboxdemo.tapp.ppvboxauth'
def log(s):
   message = '@ppvbox: %s' % str(s)
def getServiceCreds():
   getStoredCreds will query the Boxee app api service to see if the current Boxee
   user is authenticated with the PPVBox service (previously created by Boxee devs)
   You do not have to specify the current user, the mc.Http() api will add the
   current user to the request header.
   log('checking if user is authenticated to service')
   http = mc.Http()
   data = http.Get('' % app_id)
   if http.GetHttpResponseCode() == 200:
      # request was successfull
      # 'key' value is not default, the data that can be stored and retrieved
      # must be predefined and set up by a boxee dev.
      key = re.compile('<key>(.*?)<\/key>').findall(data)
      if not key:
         log('user is not currently linked to service')
         return False
         log('user is still linked to ppvbox service ('+key[0]+')')
         return key[0]
      # request failed
      log('user is not currently linked to service')
      return False
def setServiceCreds(key):
   setServiceCreds will link the current Boxee user to your application service.
   This data is secure and stored on our servers. When request is made from
   within a Boxee application using the mc.Http api, the current logged in Boxee
   user will be added to the http request header. Anything stored here must be
   predefined and the service needs to be set up by a Boxee dev.
   log('linking user to the ppvbox service')
   data = '<app id="%s"><credentials action="set"><key>%s</key></credentials></app>'
   data = data % (app_id, key)
   http = mc.Http()
   http.SetHttpHeader('Content-Type', 'text/xml')
   set_data = http.Post('', data)
   if http.GetHttpResponseCode() == 200:
      # data successfully stored
      log('user linked to ppvbox service successfully')
      return True
   log('unable to link user to ppvbox service')
   return False
def delServiceCreds():
   delServiceCreds will unlink the current Boxee user from the applcation service
   Use this if previously stored credentials are no longer valid for any reason.
   log('unlinking user from ppvbox service')
   data = '<app id="%s"><credentials action="del"></credentials></app>'
   data = data % app_id
   http = mc.Http()
   http.SetHttpHeader('Content-Type', 'text/xml')
   set_data = http.Post('', data)
   if http.GetHttpResponseCode() == 200:
      # data successfully removed
      log('successfully unlinked user from the ppvbox service')
      return True
   log('error occured while unliking user')
   return False
def checkValidAuth(key):
   checkValidAuth will query the PPVBox servie to see if the key is still valid
   if the user's key is no longer valid it should be removed from the service
   using delServiceCreds()
   log('checking if auth key is still valid')
   http = mc.Http()
   set_data = http.Get('' % key)
   if http.GetHttpResponseCode() == 200:
      # user's key is still valid
      log('user\'s key is still valid')
      return True
   # key is no longer valid
   log('user\'s key is no longer valid')
   return False
def authenticateUser():
   authenticateUser will make a post request to the PPVBox service and attempt
   to validate the users credentials. For this example please use User01/password
   log('getting username & password for authentication.')
   user = mc.ShowDialogKeyboard('ppvbox_auth: username', 'user01', False)
   passw = mc.ShowDialogKeyboard('ppvbox_auth: password', 'password', True)
   auth = {'username': user, 'password': passw, 'watchid': args['id'][0]}
   http = mc.Http()
   data = http.Post('', urllib.urlencode(auth))
   dom = parseString(data)
   dom = dom.getElementsByTagName("result")[0]
   status = dom.attributes["status"].value
   if int(status) == 0:
      log('unable to validate users credentials')
      return False
      key = str(dom.attributes["auth"].value)
      log('users credentials where validated')
      return key
def play(id, key):
   play authenticated content that required cookie to be passed to the browser.
   # passing a cookie to the browser requires using boxee defined flash:// protocol
   host = '' # your domain
   cookie = urllib.quote_plus('auth=%s; ' % key) # cookie in form of  key=val;
   direct_link = urllib.quote_plus('' + str(id)) # video link
   flash_link = 'flash://%s/src=%s&bx-cookie=%s' # flash:// template
   item_path = flash_link % (host, direct_link, cookie) #put it all together
   log('flash_link: '+item_path)
   item = mc.GetApp().GetLaunchedListItem() # get the item that app was launched with
   item.SetPath(item_path) # set the new path
   item.Dump() # print this item to the log
   log('playing item')
   mc.GetPlayer().Play(item) # play the modified video item
def launch(args):
   launch function handles the entire process involved in playing the authenticated
   user_key = False
   title_id = args['id'][0]
   title_name = args['title'][0]
   # lets check to see if the current user is already authenticated to
   # your service
   stored_creds = getServiceCreds()
   if stored_creds:
      # user is linked to the PPVBox service
      user_key = stored_creds
      # lets check to make sure the user key is still valid
      valid_auth = checkValidAuth(user_key)
      if not valid_auth:
         user_key = False
   if not user_key:
      # if we get to this point and user_key is False we need to re-authenticate
      # and re-link user to the PPVBox application service.
      user_auth = authenticateUser()
      if user_auth:
         # user's credentials where validated, we'll now store keys and link user
         # to the service.
         user_key = user_auth
         # user's credentials could not be validated
         user_key = False
         mc.ShowDialogOk('ppvbox:login', 'Invalid user credentials, could not authenticate your account.')
   log('user_key: '+user_key)
   log('title_id: '+title_id)
   log('title_name: '+title_name)
   if user_key:
      # if user_key is still set, we can play the content
      play(title_id, user_key)
mc.ShowDialogOk('ppvbox_auth', 'the ppvbox authentication app has been launched...')
if sys.argv[1]:
   args = cgi.parse_qs(sys.argv[1])
Personal tools