'Validate state value. Raises exception on error, returns state
value if valid.'

def validate_state(self):

    if ((not self.STATE_PARAMETER) and (not self.REDIRECT_STATE)):
        return None
    state = self.request.session.get((self.AUTH_BACKEND.name + '_state'))
    if state:
        request_state = (self.data.get('state') or self.data.get('redirect_state'))
        if (not request_state):
            raise AuthMissingParameter(self, 'state')
        elif (not state):
            raise AuthStateMissing(self, 'state')
        elif (not constant_time_compare(request_state, state)):
            raise AuthStateForbidden(self)
    return state
