How to implement Pubnub Access Manager in swift

2020-05-06 05:35发布

问题:

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