def generate_token(key, user_id): consumer = Consumer.fetch(key) if consumer is None: raise Exception, "Cannot generate token: invalid consumer key specified" issue_time = datetime.datetime.now(UTC).isoformat() token = hashlib.sha256(consumer.secret + user_id + issue_time).hexdigest() return dict(consumerKey=key, authToken=token, authTokenIssueTime=issue_time, authTokenTTL=consumer.ttl, userId=user_id)
def generate_token(key, user_id): consumer = Consumer.fetch(key) if consumer is None: raise Exception, "Cannot generate token: invalid consumer key specified" issue_time = datetime.datetime.now(UTC).isoformat() token = hashlib.sha256(consumer.secret + user_id + issue_time).hexdigest() return dict( consumerKey=key, authToken=token, authTokenIssueTime=issue_time, authTokenTTL=consumer.ttl, userId=user_id )
def verify_token(token, key, user_id, issue_time): consumer = Consumer.fetch(key) if consumer is None: return False # invalid account key computed_token = hashlib.sha256(consumer.secret + user_id + issue_time).hexdigest() if computed_token != token: return False # Token inauthentic: computed hash doesn't match. validity = iso8601.parse_date(issue_time) expiry = validity + datetime.timedelta(seconds=consumer.ttl) if validity > datetime.datetime.now(UTC): return False # Token not yet valid if expiry < datetime.datetime.now(UTC): return False # Token expired: issue_time + ttl > now return True
def test_key(self): c = Consumer(key='foo') save(c) c = Consumer.fetch('foo') h.assert_equal(c.key, 'foo')