/
mobile.js
88 lines (73 loc) · 2.48 KB
/
mobile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* global __APP_VERSION__ */
import CozyClient from 'cozy-client'
import { getDeviceName } from 'cozy-device-helper'
import { merge } from 'lodash'
import { getLinks } from '../links'
import { schema } from 'doctypes'
import manifest from 'ducks/client/manifest'
import PushPlugin from 'ducks/mobile/push'
import { checkForRevocation } from 'ducks/client/utils'
import { SentryCozyClientPlugin as SentryPlugin } from 'lib/sentry'
import { SOFTWARE_ID } from 'ducks/mobile/constants'
import { getRedirectUri } from 'ducks/client/mobile/redirect'
export const getScope = m => {
if (m.permissions === undefined) {
throw new Error(`Your manifest must have a 'permissions' key.`)
}
return Object.keys(m.permissions).map(permission => {
const { type /*, verbs, selector, values*/ } = m.permissions[permission]
return type
})
}
export const getSoftwareName = m => {
if (m.name === undefined) {
throw new Error(`Your manifest must have a 'name' key.`)
}
return m.name_prefix ? `${m.name_prefix} ${m.name}` : m.name
}
export const getClientName = m => `${getSoftwareName(m)} (${getDeviceName()})`
export const getManifestOptions = manifest => {
const cozyPolicyURI = 'https://files.cozycloud.cc/cgu.pdf'
return {
oauth: {
clientName: getClientName(manifest),
policyURI: manifest.name_prefix === 'Cozy' ? cozyPolicyURI : '',
scope: getScope(manifest)
}
}
}
const registerPluginsAndHandlers = client => {
client.register(PushPlugin)
client.register(SentryPlugin)
}
export const getClient = () => {
const manifestOptions = getManifestOptions(manifest)
const appSlug = manifest.slug
let client
const banksOptions = {
schema,
oauth: {
redirectURI: getRedirectUri(appSlug),
softwareID: SOFTWARE_ID,
softwareVersion: __APP_VERSION__,
clientKind: 'mobile',
clientURI: 'https://github.com/cozy/cozy-banks',
logoURI:
'https://downcloud.cozycloud.cc/upload/cozy-banks/email-assets/logo-bank.png',
notificationPlatform: 'firebase'
},
links: getLinks({
pouchLink: {
// TODO: the revocation check via cozy-pouch-link should not be
// done in the app. We should find a way to have this in a shared
// repository, possibly in cozy-client or cozy-pouch-link
onSyncError: async () => {
checkForRevocation(client)
}
}
})
}
client = new CozyClient(merge(manifestOptions, banksOptions))
registerPluginsAndHandlers(client)
return client
}