public void Async(Action <PNDownloadFileResult, PNStatus> callback)
        {
            PNDownloadFileResult pnDownloadFileResult = new PNDownloadFileResult();
            string cipherKeyToUse = "";
            string dlFilePath     = DownloadFileSavePath;
            string tempFilePath   = string.Format("{0}/{1}.dl.enc", Application.temporaryCachePath, DownloadFileName);

            if (!string.IsNullOrEmpty(DownloadFileCipherKey))
            {
                cipherKeyToUse = DownloadFileCipherKey;
                dlFilePath     = tempFilePath;
            }
            else if (!string.IsNullOrEmpty(PubNubInstance.PNConfig.CipherKey))
            {
                cipherKeyToUse = PubNubInstance.PNConfig.CipherKey;
                dlFilePath     = tempFilePath;
            }
            s3.Channel(DownloadFileChannel).ID(DownloadFileID).Name(DownloadFileName).SavePath(dlFilePath);
            s3.ExecuteDownloadFile((status) => {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("request.GetResponseHeader {0}", status.StatusCode), PNLoggingMethod.LevelInfo);
                #endif
                if (status.StatusCode != 200)
                {
                    pnDownloadFileResult = null;
                    callback(pnDownloadFileResult, status);
                }
                else
                {
                    // Decrypt
                    // save file to temp, decrypt, save file to desired location
                    if (!string.IsNullOrEmpty(cipherKeyToUse))
                    {
                        PubnubCrypto pubnubCrypto = new PubnubCrypto(cipherKeyToUse, this.PubNubInstance.PNLog);
                        pubnubCrypto.DecryptFile(dlFilePath, DownloadFileSavePath);
                    }
                    #if (ENABLE_PUBNUB_LOGGING)
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("File successfully downloaded and saved to  {0}", DownloadFileSavePath), PNLoggingMethod.LevelInfo);
                    #endif
                    callback(pnDownloadFileResult, status);
                }
            });
        }
        public static string CreateV2Signature(string httpMethod, string path, string query, string publishKey, string cipherKey, string secretKey, string body)
        {
            StringBuilder stringToSign = new StringBuilder();

            stringToSign.Append(httpMethod)
            .Append("\n")
            .Append(publishKey)
            .Append("\n")
            .Append(path)
            .Append("\n")
            .Append(query)
            .Append("\n")
            .Append(body);

            PNLoggingMethod PNLog = new PNLoggingMethod(PNLogVerbosity.BODY);

            PubnubCrypto pubnubCrypto = new PubnubCrypto(cipherKey, PNLog);
            string       signature    = pubnubCrypto.PubnubAccessManagerSign(secretKey, stringToSign.ToString());

            signature = signature.TrimEnd('=');
            signature = string.Format("v2.{0}", signature);
            Debug.Log("signature ===== >" + signature);
            return(signature);
        }
Esempio n. 3
0
        public static Uri BuildPublishRequest(string channel, string message, bool storeInHistory, string metadata, uint messageCounter, int ttl, bool usePost, bool repilicate, PubNubUnity pnInstance, Dictionary <string, string> queryParams)
        {
            StringBuilder parameterBuilder = new StringBuilder();

            parameterBuilder.AppendFormat("&seqn={0}", messageCounter.ToString());
            parameterBuilder.Append((storeInHistory) ? "" : "&store=0");
            if (ttl >= 0)
            {
                parameterBuilder.AppendFormat("&ttl={0}", ttl.ToString());
            }
            if (!repilicate)
            {
                parameterBuilder.Append("&norep=true");
            }

            if (!string.IsNullOrEmpty(metadata) || metadata.Equals("\"\""))
            {
                parameterBuilder.AppendFormat("&meta={0}", Utility.EncodeUricomponent(metadata, PNOperationType.PNPublishOperation, false, false));
            }

            // Generate String to Sign
            string signature = "0";

            if (!string.IsNullOrEmpty(pnInstance.PNConfig.SecretKey) && (pnInstance.PNConfig.SecretKey.Length > 0))
            {
                StringBuilder stringToSign = new StringBuilder();
                stringToSign
                .Append(pnInstance.PNConfig.PublishKey)
                .Append('/')
                .Append(pnInstance.PNConfig.SubscribeKey)
                .Append('/')
                .Append(pnInstance.PNConfig.SecretKey)
                .Append('/')
                .Append(channel);
                if (!usePost)
                {
                    stringToSign.Append('/');
                    stringToSign.Append(message);      // 1
                }

                // Sign Message
                PubnubCrypto pnCrypto = new PubnubCrypto(pnInstance.PNConfig.CipherKey, pnInstance.PNLog);
                signature = pnCrypto.ComputeHashRaw(stringToSign.ToString());
            }

            // Build URL
            List <string> url = new List <string> ();

            url.Add("publish");
            url.Add(pnInstance.PNConfig.PublishKey);
            url.Add(pnInstance.PNConfig.SubscribeKey);
            url.Add(signature);
            url.Add(channel);
            url.Add("0");
            if (!usePost)
            {
                url.Add(message);
            }

            return(BuildRestApiRequest <Uri> (url, PNOperationType.PNPublishOperation, parameterBuilder.ToString(), pnInstance, queryParams));
        }
        protected override void CreatePubNubResponse(object deSerializedResult, RequestState requestState)
        {
            PNSendFileResult pnSendFileResult = new PNSendFileResult();
            PNStatus         pnStatus         = new PNStatus();

            try{
                Dictionary <string, object> dictionary           = deSerializedResult as Dictionary <string, object>;
                PNSendFileInputsToS3        pnSendFileInputsToS3 = new PNSendFileInputsToS3();

                if (dictionary != null)
                {
                    pnSendFileInputsToS3.Data = ExtractPNFileData(dictionary);
                    pnSendFileInputsToS3.FileUploadRequest = ExtractFileUploadRequest(dictionary);

                    #if (ENABLE_PUBNUB_LOGGING)
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("pnSendFileToS3Result.Data: {0}", pnSendFileInputsToS3.Data.ID), PNLoggingMethod.LevelInfo);
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("pnSendFileToS3Result.FileUploadRequest.URL: {0}", pnSendFileInputsToS3.FileUploadRequest.URL), PNLoggingMethod.LevelInfo);
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("pnSendFileToS3Result.FileUploadRequest.Method: {0}", pnSendFileInputsToS3.FileUploadRequest.Method), PNLoggingMethod.LevelInfo);
                    foreach (PNFormField f in pnSendFileInputsToS3.FileUploadRequest.FormFields)
                    {
                        this.PubNubInstance.PNLog.WriteToLog(string.Format("pnSendFileToS3Result.FileUploadRequest.FormFields Key: {0}", f.Key), PNLoggingMethod.LevelInfo);
                        this.PubNubInstance.PNLog.WriteToLog(string.Format("pnSendFileToS3Result.FileUploadRequest.FormFields Value: {0}", f.Value), PNLoggingMethod.LevelInfo);
                    }
                    #endif

                    // read file, encrypt, save file to temp, send enc file path
                    string upFilePath   = SendFilePath;
                    string tempFilePath = string.Format("{0}/{1}.up.enc", Application.temporaryCachePath, SendFileName);
                    bool   delTempFile  = false;
                    if (!string.IsNullOrEmpty(SendFileCipherKey))
                    {
                        upFilePath = tempFilePath;
                        PubnubCrypto pubnubCrypto = new PubnubCrypto(SendFileCipherKey, this.PubNubInstance.PNLog);
                        pubnubCrypto.EncryptFile(null, SendFilePath, upFilePath);
                        delTempFile = true;
                    }
                    else if (!string.IsNullOrEmpty(PubNubInstance.PNConfig.CipherKey))
                    {
                        upFilePath = tempFilePath;
                        PubnubCrypto pubnubCrypto = new PubnubCrypto(PubNubInstance.PNConfig.CipherKey, this.PubNubInstance.PNLog);
                        pubnubCrypto.EncryptFile(null, SendFilePath, upFilePath);
                        delTempFile = true;
                    }

                    s3.FileName(SendFileName).File(upFilePath).FileUploadRequestData(pnSendFileInputsToS3.FileUploadRequest);
                    s3.Execute((pnSendFileToS3Result, status) => {
                        if (status.StatusCode != 204)
                        {
                            pnSendFileResult          = null;
                            requestState.ResponseCode = status.StatusCode;
                            pnStatus = base.CreateErrorResponseFromException(new PubNubException(string.Format("File Upload Failed {0}", pnSendFileToS3Result.Text)), requestState, PNStatusCategory.PNUnknownCategory);
                            Callback(pnSendFileResult, pnStatus);
                        }
                        else
                        {
                            pnSendFileResult.Data    = new PNFileData();
                            pnSendFileResult.Data.ID = pnSendFileInputsToS3.Data.ID;
                            PublishFileMessage(pnSendFileResult, pnStatus, requestState, 0);
                        }
                        if (delTempFile)
                        {
                            File.Delete(tempFilePath);
                        }
                    });
                }
                else
                {
                    pnSendFileResult = null;
                    pnStatus         = base.CreateErrorResponseFromException(new PubNubException("Data not present"), requestState, PNStatusCategory.PNUnknownCategory);
                    Callback(pnSendFileResult, pnStatus);
                }
            } catch (Exception ex) {
                pnSendFileResult = null;
                pnStatus         = base.CreateErrorResponseFromException(ex, requestState, PNStatusCategory.PNUnknownCategory);
                Callback(pnSendFileResult, pnStatus);
            }
        }