-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #681 from Pylons-tech/fix/push-notification-end-po…
…ints Fix/push notification end points
- Loading branch information
Showing
6 changed files
with
293 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,5 @@ ostrio:flow-router-meta | |
ostrio:flow-router-extra | ||
server-render@0.4.0 | ||
nimble:restivus | ||
meteorhacks:async | ||
webapp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,138 @@ | ||
import { Meteor } from "meteor/meteor"; | ||
import { FCMToken } from "../fcmtoken.js"; | ||
import { admin } from "../../admin.js"; | ||
import { WebApp } from 'meteor/webapp' | ||
import { FCMToken } from '../fcmtoken.js' | ||
import { admin } from '../../admin.js' | ||
import connectRoute from 'connect-route' | ||
import { isString } from 'lodash' | ||
// Global API configuration | ||
var Api = new Restivus({ | ||
useDefaultAuth: true, | ||
prettyJson: true, | ||
}); | ||
|
||
const StatusOk = 200; | ||
const StatusInvalidInput = 400; | ||
const InternalServerError = 500; | ||
const Success = "Success"; | ||
const Failed = "Failed"; | ||
const BadRequest = "Bad Request"; | ||
const AppCheckFailed = "App Check Failed" | ||
const StatusOk = 200 | ||
const StatusInvalidInput = 400 | ||
const Success = 'Success' | ||
const Failed = 'Failed' | ||
const BadRequest = 'Bad Request' | ||
const AppCheckFailed = 'App Check Failed' | ||
|
||
Api.addRoute( | ||
"fcmtoken/update/:address/:token", | ||
{ authRequired: false }, | ||
{ | ||
//update fcm token against address | ||
post: function () { | ||
if (!Valid(this.urlParams.address) || !Valid(this.urlParams.token)) { | ||
return { | ||
Code: StatusInvalidInput, | ||
Message: BadRequest, | ||
Data: null, | ||
}; | ||
WebApp.connectHandlers.use( | ||
connectRoute(function (router) { | ||
router.post('/fcmtoken/update/:address/:token', async function (req, res) { | ||
// validate that params exist | ||
if (!Valid(req.params.address) || !Valid(req.params.token)) { | ||
res.writeHead(StatusOk, { | ||
'Content-Type': 'text/html' | ||
}) | ||
|
||
res.end( | ||
JSON.stringify({ | ||
Code: StatusInvalidInput, | ||
Message: BadRequest, | ||
Data: null | ||
}) | ||
) | ||
} | ||
|
||
const h = req.headers | ||
// app check header check | ||
if (!h['x-firebase-appcheck']) { | ||
res.writeHead(StatusOk, { | ||
'Content-Type': 'text/html' | ||
}) | ||
|
||
let h = this.request.headers; | ||
if(!h['x-firebase-appcheck']){ | ||
return { | ||
Code: StatusInvalidInput, | ||
Message: AppCheckFailed, | ||
Data: "x-firebase-appcheck header not found", | ||
}; | ||
} | ||
|
||
// admin.appCheck().verifyToken(h['x-firebase-appcheck']).then((res) => { | ||
var result = updateFCMToken(this.urlParams.address, this.urlParams.token); | ||
|
||
if (result === false) { | ||
return { | ||
Code: InternalServerError, | ||
res.end( | ||
JSON.stringify({ | ||
Code: StatusInvalidInput, | ||
Message: AppCheckFailed, | ||
Data: 'x-firebase-appcheck header missing' | ||
}) | ||
) | ||
} else { | ||
// performing app check | ||
const appCheckClaims = await verifyAppCheckToken( | ||
h['x-firebase-appcheck'] | ||
) | ||
|
||
// app check failed | ||
if (!appCheckClaims) { | ||
res.writeHead(StatusOk, { | ||
'Content-Type': 'text/html' | ||
}) | ||
|
||
res.end( | ||
JSON.stringify({ | ||
Code: StatusInvalidInput, | ||
Message: AppCheckFailed, | ||
Data: 'invalid x-firebase-appcheck header' | ||
}) | ||
) | ||
} | ||
|
||
const result = updateFCMToken(req.params.address, req.params.token) | ||
|
||
if (result === false) { | ||
res.writeHead(400, { | ||
'Content-Type': 'text/html' | ||
}) | ||
|
||
res.end( | ||
JSON.stringify({ | ||
Code: StatusInvalidInput, | ||
Message: Failed, | ||
Data: "", | ||
}; | ||
} | ||
|
||
return { | ||
Data: null | ||
}) | ||
) | ||
} | ||
|
||
res.writeHead(200, { | ||
'Content-Type': 'text/html' | ||
}) | ||
|
||
res.end( | ||
JSON.stringify({ | ||
Code: StatusOk, | ||
Message: Success, | ||
Data: result, | ||
}; | ||
|
||
// Useful for future debug | ||
// }).catch((e)=>{ | ||
// return { | ||
// Code: StatusInvalidInput, | ||
// Message: AppCheckFailed, | ||
// Data: "x-firebase-appcheck Failed", | ||
// }; | ||
// }) | ||
|
||
return { | ||
Code: StatusInvalidInput, | ||
Message: BadRequest, | ||
Data: null, | ||
}; | ||
|
||
}, | ||
} | ||
); | ||
Data: null | ||
}) | ||
) | ||
} | ||
}) | ||
}) | ||
) | ||
|
||
function updateFCMToken(userAddress, fcmToken) { | ||
function updateFCMToken (userAddress, fcmToken) { | ||
try { | ||
FCMToken.upsert( | ||
{ address: userAddress }, | ||
{ | ||
$set: { | ||
address: userAddress, | ||
token: fcmToken, | ||
}, | ||
token: fcmToken | ||
} | ||
} | ||
); | ||
) | ||
} catch (error) { | ||
console.log(error) | ||
return false; | ||
return false | ||
} | ||
return true; | ||
return true | ||
} | ||
|
||
function Valid(parameter) { | ||
if (typeof(parameter) != "string"){ | ||
function Valid (parameter) { | ||
if (isString(parameter)) { | ||
return false | ||
} | ||
if (parameter.length == 0){ | ||
if (parameter.length === 0) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
async function verifyAppCheckToken (appCheckToken) { | ||
if (!appCheckToken) { | ||
return null | ||
} | ||
try { | ||
const res = await admin.appCheck().verifyToken(appCheckToken) | ||
return res | ||
} catch (err) { | ||
return null | ||
} | ||
} |
Oops, something went wrong.