How to implement Pubnub Access Manager in swift

孤街醉人 提交于 2019-12-20 06:33:21

问题


I am doing R&D on how to implement a pubnub access manager in swift, and after some research, I come to know :

  • Swift SDK does not include pubnub.grant
  • I need to achieve this using a pubnub function for serverless computing

I have created one function in the pubnub dashboard and created a module PubNub, also created a function with event type "On Request" and added code of grant.

  export default (request, response) => {
    const pubnub = require('pubnub');
    const kvstore = require('kvstore');

    let headersObject = request.headers;
    let paramsObject = request.params;
    let methodString = request.method;
    let bodyString = request.body;

    response.headers['Access-Control-Allow-Origin'] = '*';
    response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept';
    response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS, PUT, DELETE';
    response.headers['Content-Type'] = 'application/json';

    var uuid = 'aartisagarvadgama'

    return pubnub.grant({
        channels: ['channel_atts_pubnub'],
        read: true, // false to disallow
        write: false, // false to disallow,
        authKeys: [uuid],
        ttl: 0
    }).then(() => {
        console.log('grant success')
        response.status = 200;
      return response.send(uuid);
    }).catch((error) => {
        console.log(error);
        response.status = 400;
        return response.send();
    });
};

I am calling this above function by copying URL from function and getting success code, But how this can reflect my iOS application.

Please let me know anyway by which I can achieve the access manager in my app.

As per my understanding, I need to create a function and by calling that function I can grant the user. After that When I will try to subscribe or publish, I will get 200 instead of 403. Any help will be appreciated. Please help me.


回答1:


PubNub Access Manager for Swift Apps

The PubNub Swift SDK (Access Manager Tutorial) does not have grant method because you need to init PubNub with your secret key and you should never do so in a client application. You should only do so in a secure server. Your server grants permissions to an auth-key that is passed back to your clients, like a Swift app, and that auth-key is used to init PubNub with your subscribe key and optionally your publish key.

See Also

  • Access Manager Getting Started Guide
  • PubNub Access Manager - How It Works
  • Node SDK Docs - Security with Access Manager Tutorial


来源:https://stackoverflow.com/questions/56889108/how-to-implement-pubnub-access-manager-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!