Created
August 13, 2020 13:49
-
-
Save ptbrowne/cfa1379ce4a1b3f8c72525744f7efe8d to your computer and use it in GitHub Desktop.
Registers firebase device token into the oauth client
This file contains hidden or 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
const { createClientInteractive } = require('cozy-client/dist/cli') | |
const { ArgumentParser } = require('argparse') | |
const main = async () => { | |
const parser = new ArgumentParser() | |
parser.addArgument('--url', { defaultValue: 'http://cozy.tools:8080' }) | |
parser.addArgument('--platform') | |
parser.addArgument('--registrationToken') | |
const client = await createClientInteractive({ | |
uri: 'http://cozy.tools:8080', | |
oauth: { | |
softwareID: 'adhoc.test-realtime' | |
}, | |
scope: ['io.cozy.jobs'] // Scope is not relevant here | |
}) | |
const clientInfos = client.stackClient.oauthOptions | |
const { clientName, clientID, registrationAccessToken } = clientInfos | |
console.log('Activating push notifications for OAuth client') | |
await client.stackClient.fetchJSON('PUT', `/auth/register/${clientID}`, snakeCaseOAuthData({ | |
...clientInfos, // Necessary to send the original information back | |
notificationPlatform: 'android', | |
notificationDeviceToken: 'My-token' // the firebase token | |
}), { | |
headers: { | |
'Authorization': `Bearer ${registrationAccessToken}` | |
} | |
}) | |
} | |
/** Transform oauth fields into snake case */ | |
const snakeCaseOAuthData = (data) => { | |
const mappedFields = { | |
softwareID: 'software_id', | |
softwareVersion: 'software_version', | |
clientID: 'client_id', | |
clientName: 'client_name', | |
clientKind: 'client_kind', | |
clientURI: 'client_uri', | |
logoURI: 'logo_uri', | |
policyURI: 'policy_uri', | |
notificationPlatform: 'notification_platform', | |
notificationDeviceToken: 'notification_device_token', | |
redirectURI: 'redirect_uris' | |
} | |
const result = {} | |
Object.keys(data).forEach(fieldName => { | |
const key = mappedFields[fieldName] || fieldName | |
const value = data[fieldName] | |
result[key] = value | |
}) | |
// special case: turn redirect_uris into an array | |
if ( | |
result['redirect_uris'] && | |
result['redirect_uris'] instanceof Array === false | |
) | |
result['redirect_uris'] = [result['redirect_uris']] | |
return result | |
} | |
main().catch(e => { | |
console.error(e) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment