weixin_33698043 2018-11-06 13:31 采纳率: 0%
浏览 353

如何保护主API密钥?

I created a Restful API, a website (ReactJS/Ruby on Rails), and a mobile app(React Native).

I am using the API to show and process data on the website and the mobile app.

In the website, I am using jQuery AJAX requests that somehow looks like this:

...some other code
componentDidMount () {
  $.getJSON('https://example.com/api/v1/accounts?key=MASTER-API-KEY', (data) => {
    this.setState({
      accounts: data.accounts
    });
  });
}
...some other code

In the mobile app, I am using fetch that somehow looks like this:

...some other code
fetch('https://example.com/api/v1/accounts?key=MASTER-API-KEY', {
  method: 'GET',
...some other code

The users also have their own API keys with limited privilege based on their user level.

I already have the validation to process the request if only they have sent a valid API key. But on the website and the app, I am using a master API key that has access to all.

I believe that this can be seen in the source file on the website and it can be reverse engineered in the mobile app.

The possible solution that I have for the website is to make the process in the server instead of using AJAX, but how can I access it on my ReactJS components?

For the mobile app, should I switch to using Swift/Java and make the request there instead of fetch?

  • 写回答

3条回答 默认 最新

  • weixin_33716941 2018-11-06 13:43
    关注

    This should work if you are developing on node (as i guess you are):

    using process.env by hackernoon

    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格