uport-core
Advanced tools
Comparing version 0.0.37 to 0.0.38
@@ -33,8 +33,38 @@ 'use strict'; | ||
*/ | ||
// const send = (token, pubEncKey, pushServiceUrl = PUTUTU_URL) => { | ||
// if (!token) throw new Error('Requires push notification token') | ||
// if (!pubEncKey) throw new Error('Requires public encryption key of the receiver') | ||
// | ||
// return (uri, {message, type, redirectUrl}={}) => new Promise((resolve, reject) => { | ||
// if (!uri) return reject(new Error('Requires uri request for sending to users device')) | ||
// if (type) uri = paramsToQueryString(uri, {callback_type: type}) | ||
// if (redirectUrl) uri = paramsToQueryString(uri, {'redirect_url': redirectUrl}) | ||
// console.log(uri) | ||
// const plaintext = padMessage(JSON.stringify({uri, message})) | ||
// console.log(plaintext) | ||
// console.log(pubEncKey) | ||
// const enc = encryptMessage(plaintext, pubEncKey) | ||
// console.log(enc) | ||
// const payload = { message: JSON.stringify(enc) } | ||
// nets({ | ||
// uri: pushServiceUrl, | ||
// json: payload, | ||
// method: 'POST', | ||
// withCredentials: false, | ||
// headers: { | ||
// Authorization: `Bearer ${token}` | ||
// } | ||
// }, | ||
// (error, res, body) => { | ||
// if (error) return reject(error) | ||
// if (res.statusCode === 200) return resolve(body) | ||
// if (res.statusCode === 403) { | ||
// return reject(new Error('Error sending push notification to user: Invalid Token')) | ||
// } | ||
// reject(new Error(`Error sending push notification to user: ${res.statusCode} ${body.toString()}`)) | ||
// }) | ||
// }) | ||
// } | ||
var send = function send(token, pubEncKey) { | ||
var pushServiceUrl = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : PUTUTU_URL; | ||
if (!token) throw new Error('Requires push notification token'); | ||
if (!pubEncKey) throw new Error('Requires public encryption key of the receiver'); | ||
return function (uri) { | ||
@@ -46,15 +76,27 @@ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
var payload = { uri: uri, message: message }; | ||
var PUTUTU_URL = 'https://pututu.uport.space'; // TODO - change to .me | ||
return new Promise(function (resolve, reject) { | ||
if (!uri) return reject(new Error('Requires uri request for sending to users device')); | ||
if (type) uri = paramsToQueryString(uri, { callback_type: type }); | ||
if (redirectUrl) uri = paramsToQueryString(uri, { 'redirect_url': redirectUrl }); | ||
console.log(uri); | ||
var plaintext = padMessage(JSON.stringify({ uri: uri, message: message })); | ||
console.log(plaintext); | ||
console.log(pubEncKey); | ||
var enc = (0, _index.encryptMessage)(plaintext, pubEncKey); | ||
console.log(enc); | ||
var payload = { message: JSON.stringify(enc) }; | ||
var endpoint = '/api/v2/sns'; | ||
if (!token) { | ||
return reject(new Error('Missing push notification token')); | ||
} | ||
//if (!pubEncKey) { | ||
//return reject(new Error('Missing public encryption key of the receiver')) | ||
//} | ||
if (pubEncKey.url) { | ||
console.error('WARNING: Calling push without a public encryption key is deprecated'); | ||
endpoint = '/api/v1/sns'; | ||
payload = pubEncKey; | ||
} else { | ||
if (!payload.url) { | ||
return reject(new Error('Missing payload url for sending to users device')); | ||
} | ||
var plaintext = padMessage(JSON.stringify(payload)); | ||
var enc = (0, _index.encryptMessage)(plaintext, pubEncKey); | ||
payload = { message: JSON.stringify(enc) }; | ||
} | ||
(0, _nets2.default)({ | ||
uri: pushServiceUrl, | ||
uri: PUTUTU_URL + endpoint, | ||
json: payload, | ||
@@ -68,3 +110,5 @@ method: 'POST', | ||
if (error) return reject(error); | ||
if (res.statusCode === 200) return resolve(body); | ||
if (res.statusCode === 200) { | ||
resolve(body); | ||
} | ||
if (res.statusCode === 403) { | ||
@@ -71,0 +115,0 @@ return reject(new Error('Error sending push notification to user: Invalid Token')); |
{ | ||
"name": "uport-core", | ||
"version": "0.0.37", | ||
"version": "0.0.38", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -20,37 +20,83 @@ import { encryptMessage } from './../crypto/index.js' | ||
*/ | ||
const send = (token, pubEncKey, pushServiceUrl = PUTUTU_URL) => { | ||
if (!token) throw new Error('Requires push notification token') | ||
if (!pubEncKey) throw new Error('Requires public encryption key of the receiver') | ||
// const send = (token, pubEncKey, pushServiceUrl = PUTUTU_URL) => { | ||
// if (!token) throw new Error('Requires push notification token') | ||
// if (!pubEncKey) throw new Error('Requires public encryption key of the receiver') | ||
// | ||
// return (uri, {message, type, redirectUrl}={}) => new Promise((resolve, reject) => { | ||
// if (!uri) return reject(new Error('Requires uri request for sending to users device')) | ||
// if (type) uri = paramsToQueryString(uri, {callback_type: type}) | ||
// if (redirectUrl) uri = paramsToQueryString(uri, {'redirect_url': redirectUrl}) | ||
// console.log(uri) | ||
// const plaintext = padMessage(JSON.stringify({uri, message})) | ||
// console.log(plaintext) | ||
// console.log(pubEncKey) | ||
// const enc = encryptMessage(plaintext, pubEncKey) | ||
// console.log(enc) | ||
// const payload = { message: JSON.stringify(enc) } | ||
// nets({ | ||
// uri: pushServiceUrl, | ||
// json: payload, | ||
// method: 'POST', | ||
// withCredentials: false, | ||
// headers: { | ||
// Authorization: `Bearer ${token}` | ||
// } | ||
// }, | ||
// (error, res, body) => { | ||
// if (error) return reject(error) | ||
// if (res.statusCode === 200) return resolve(body) | ||
// if (res.statusCode === 403) { | ||
// return reject(new Error('Error sending push notification to user: Invalid Token')) | ||
// } | ||
// reject(new Error(`Error sending push notification to user: ${res.statusCode} ${body.toString()}`)) | ||
// }) | ||
// }) | ||
// } | ||
return (uri, {message, type, redirectUrl}={}) => new Promise((resolve, reject) => { | ||
if (!uri) return reject(new Error('Requires uri request for sending to users device')) | ||
if (type) uri = paramsToQueryString(uri, {callback_type: type}) | ||
if (redirectUrl) uri = paramsToQueryString(uri, {'redirect_url': redirectUrl}) | ||
console.log(uri) | ||
const plaintext = padMessage(JSON.stringify({uri, message})) | ||
console.log(plaintext) | ||
console.log(pubEncKey) | ||
const enc = encryptMessage(plaintext, pubEncKey) | ||
console.log(enc) | ||
const payload = { message: JSON.stringify(enc) } | ||
nets({ | ||
uri: pushServiceUrl, | ||
json: payload, | ||
method: 'POST', | ||
withCredentials: false, | ||
headers: { | ||
Authorization: `Bearer ${token}` | ||
} | ||
}, | ||
(error, res, body) => { | ||
if (error) return reject(error) | ||
if (res.statusCode === 200) return resolve(body) | ||
if (res.statusCode === 403) { | ||
return reject(new Error('Error sending push notification to user: Invalid Token')) | ||
} | ||
reject(new Error(`Error sending push notification to user: ${res.statusCode} ${body.toString()}`)) | ||
}) | ||
}) | ||
} | ||
const send = (token, pubEncKey)=> (uri, {message, type, redirectUrl}={})=> { | ||
let payload = {uri, message} | ||
const PUTUTU_URL = 'https://pututu.uport.space' // TODO - change to .me | ||
return new Promise((resolve, reject) => { | ||
let endpoint = '/api/v2/sns' | ||
if (!token) { | ||
return reject(new Error('Missing push notification token')) | ||
} | ||
//if (!pubEncKey) { | ||
//return reject(new Error('Missing public encryption key of the receiver')) | ||
//} | ||
if (pubEncKey.url) { | ||
console.error('WARNING: Calling push without a public encryption key is deprecated') | ||
endpoint = '/api/v1/sns' | ||
payload = pubEncKey | ||
} else { | ||
if (!payload.url) { | ||
return reject(new Error('Missing payload url for sending to users device')) | ||
} | ||
const plaintext = padMessage(JSON.stringify(payload)) | ||
const enc = encryptMessage(plaintext, pubEncKey) | ||
payload = { message: JSON.stringify(enc) } | ||
} | ||
nets({ | ||
uri: PUTUTU_URL + endpoint, | ||
json: payload, | ||
method: 'POST', | ||
withCredentials: false, | ||
headers: { | ||
Authorization: `Bearer ${token}` | ||
} | ||
}, | ||
(error, res, body) => { | ||
if (error) return reject(error) | ||
if (res.statusCode === 200) { | ||
resolve(body) | ||
} | ||
if (res.statusCode === 403) { | ||
return reject(new Error('Error sending push notification to user: Invalid Token')) | ||
} | ||
reject(new Error(`Error sending push notification to user: ${res.statusCode} ${body.toString()}`)) | ||
}) | ||
}) | ||
} | ||
/** | ||
@@ -57,0 +103,0 @@ * Adds padding to a string |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
2930655
37989