简体   繁体   English

无法使用我在 React Native 中通过 Rest API 构建的移动应用程序访问 Cosmos DB

[英]Can't access Cosmos DB with my mobile app built in React Native via Rest API

I am fairly new to building mobile apps and have spent days trying to solve, or at least understand, the following problem.我对构建移动应用程序相当陌生,并且花了几天时间试图解决或至少理解以下问题。 I am building a mobile app on react native in expo.我正在 expo 中构建一个响应原生的移动应用程序。 I have selected Cosmos DB as my database.我已选择 Cosmos DB 作为我的数据库。 Is this a good decision?这是一个好的决定吗? Should I move to Firebase or another DB?我应该转移到 Firebase 还是其他数据库? Assuming I stick with Cosmos DB, I am unable to get an authorization string built for a REST API.假设我坚持使用 Cosmos DB,我无法获得为 REST API 构建的授权字符串。

Specifically, I find every search leading to Crypto-JS.具体来说,我发现每次搜索都会导致 Crypto-JS。 Crypto-JS is not supported in React Native (at least in expo). React Native 不支持 Crypto-JS(至少在 expo 中)。 So, I have been trying to build the encryption on my own.所以,我一直在尝试自己构建加密。 To the best of my research, three steps exist to get an authorization string for my REST API.根据我的研究,存在三个步骤来获取我的 REST API 的授权字符串。

  1. translate the secret key to a word array (not sure what this is - why does it need to be an array - versus a direct Base64 encode).将密钥转换为单词数组(不确定这是什么 - 为什么它需要是一个数组 - 与直接 Base64 编码相比)。 Need to use parse to build the array.需要使用 parse 来构建数组。 But, I am unable to use parse in expo react native.但是,我无法在 expo 中使用 parse 本机反应。 Or am I?还是我?
  2. encrypt with HMAC 256使用 HMAC 256 加密
  3. decode, in Base64, with stringify (again - not supported in React Naive - Expo).在 Base64 中使用 stringify 解码(同样 - 在 React Naive - Expo 中不支持)。

How does parse work on a key - what is the logic?解析如何在一个键上工作 - 逻辑是什么? For example, when I encode 'Hello world' in Base64, I get a different answer than using parse.例如,当我在 Base64 中编码“Hello world”时,我得到的答案与使用解析不同。

Base64 - SGVsbG8gd29ybGQ= Base64 - SGVsbG8gd29ybGQ=

Postman using Parse - 1de965a00c28ae57 (Microsoft Cosmos API likes this answer) Postman 使用 Parse - 1de965a00c28ae57 (Microsoft Cosmos API 喜欢这个答案)

How does this work?这是如何运作的? Why do I need parse.为什么我需要解析。 By the way, the Postman solution with Crypto-JS and parse works.顺便说一句,带有 Crypto-JS 和解析的 Postman 解决方案有效。 How do I build an authorization string in React Native in Expo?如何在 Expo 的 React Native 中构建授权字符串?

I would greatly appreciate insight and help as my head hurts from banging it against the wall.我将非常感谢您的洞察力和帮助,因为我的头因撞到墙上而受伤。 Thanks BC谢谢 BC

Posting as answer.发布为答案。

There are lots of people using Cosmos DB as a database for mobile device applications.有很多人使用 Cosmos DB 作为移动设备应用程序的数据库。 Most notably as many mobile gaming applications which like Cosmos DB because it is a distributed database.最值得注意的是,许多移动游戏应用程序都喜欢 Cosmos DB,因为它是一个分布式数据库。 But plenty of others as well.但还有很多其他人。

To connect to Cosmos DB from a mobile device where data is kept separate per tenant, you need a middle tier to serve as an auth provider for your app.要从每个租户单独保存数据的移动设备连接到 Cosmos DB,您需要一个中间层来充当应用程序的身份验证提供程序。 For Cosmos DB this scenario centers around the use of users and permissions with resource tokens and the implementation of a token broker middle-tier service that acts as a auth provider for the mobile application.对于 Cosmos DB,此方案的中心是使用用户和权限以及资源令牌,以及作为移动应用程序的身份验证提供程序的令牌代理中间层服务的实现。 Resource tokens are issued by the token broker service for each user and can pass tokens that provide access to that users data, typically with the granularity of a partition key which is the most granularity you can have in Cosmos.资源令牌由令牌代理服务为每个用户颁发,并且可以传递提供对该用户数据的访问权限的令牌,通常具有分区键的粒度,这是您在 Cosmos 中可以拥有的最大粒度。

You can learn more here about Resource Tokens in Cosmos DB which includes a simple (and somewhat dated) example for how to create a token broker for a mobile device application.您可以在此处了解有关Cosmos DB 中资源令牌的更多信息,其中包括一个简单(但有些过时)的示例,说明如何为移动设备应用程序创建令牌代理。 If you are looking for a more robust example of this you can also take a look at this example written in Java that was created by an engineer at Citrix.如果您正在寻找一个更强大的示例,您还可以查看由 Citrix 的工程师创建的Java 编写的示例

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM