Firestore 安全规则,如何检查声明是否存在并且是字符串

时间:2021-01-31 11:56:50

标签: firebase google-cloud-firestore firebase-authentication firebase-security

我将游戏角色用户名存储在自定义声明中,这样做是为了避免对数据库进行多次获取请求以获取用户名并继续执行其他操作。我想定义一个安全规则,只允许具有用户名的用户访问某些数据。理想情况下,我会检查令牌声明是否是一个字符串并且有长度。但到目前为止,我想出的最好方法是通过以下方式检查此令牌声明是否已定义:

request.auth.token.username != null

是否也可以在这里检查类型(字符串)和长度?

1 个答案:

答案 0 :(得分:2)

def find_on_row(way, array, way_index, row_index): if way_index == len(way): # found answer return [] for col_index in range(len(array[0])): if array[row_index][col_index]== way[way_index]: candidate_answer = find_on_col(way, array, way_index + 1, col_index) if not candidate_answer is None: # unwind answer return [col_index] + candidate_answer return None def find_on_col(way, array, way_index, col_index): if way_index == len(way): # found answer return [] for row_index in range(len(array)): if array[row_index][col_index] == way[way_index]: candidate_answer = find_on_row(way, array, way_index + 1, row_index) if not candidate_answer is None: # unwind answer return [row_index] + candidate_answer return None way = ['BD', '1C', 'BD', '55'] array = [['1C', 'BD', '1C', '55', '55'], ['55', '55', '55', '1C', '1C'], ['E9', '1C', '55', '55', 'E9'], ['BD', '1C', '1C', '1C', 'BD'], ['55', 'BD', 'E9', '55', '1C']] print(f'Starting from first row the answer is {find_on_row(way, array, 0, 0)}') # Starting from first row the answer is [1, 3, 0, 1] 是 JWT 令牌声明的映射(请参阅 doc)。因此您可以使用 Map 属性和方法来检查类型。

要获取字符串长度,请使用 request.auth.tokensize 属性。