forked from konnectors/cozy-konnector-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
konnector.js
209 lines (177 loc) · 5.07 KB
/
konnector.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* MAIF Cozy's konnector
*/
'use strict'
const request = require('request')
const moment = require('moment')
const uuid = require('uuid')
const {baseKonnector,cozyClient, models, log, updateOrCreate} = require('cozy-konnector-libs');
const imp = require('./maifuser');
const MaifUser = imp.doctypeTest
const connectUrl = 'https://connect.maif.fr/connect'
const apikey = 'eeafd0bd-a921-420e-91ce-3b52ee5807e8'
const infoUrl = `https://openapiweb.maif.fr/prod/cozy/v1/mes_infos?apikey=${apikey}`
const clientId = '2921ebd6-5599-4fa6-a533-0537fac62cfe'
const secret = 'Z_-AMVTppsgj_F9tRLXfwUm6Wdq8OOv5a4ydDYzvbhFjMcp8aM90D0sdNp2kdaEczeGH_qYZhhd9JIzWkoWdGw'
const domain = cozyClient.cozyURL
const scope = 'openid+profile+offline_access'
const type = 'code'
const b64Client = new Buffer(`${clientId}:${secret}`).toString('base64')
let state = ''
let nonce = ''
if (state === '') {
state = uuid()
}
if (nonce === '') {
nonce = uuid()
}
const connector = module.exports = baseKonnector.createNew({
name: 'MAIF',
customView: '<%t konnector customview maif %>',
connectUrl: `${connectUrl}/authorize?response_type=${type}&client_id=${clientId}&scope=${scope}&state=${state}&nonce=${nonce}&redirect_uri=`,
color: {
hex: '#007858',
css: '#007858'
},
fields: {
code: {
type: 'hidden' // To get the Auth code returned on the redirection.
},
redirectPath: {
type: 'hidden'
},
refreshToken: {
type: 'hidden' // refreshToken
}
},
dataType: [
'bill',
'contact'
],
models: [MaifUser],
fetchOperations: [
refreshToken,
fetchData,
updateOrCreate(null, MaifUser)
]
})
function refreshToken (requiredFields, entries, data, next) {
log('info', 'refreshToken')
if (requiredFields.refreshToken && requiredFields.refreshToken !== '') {
// Get a new access_token using the refreshToken.
fetchToken({
grant_type: 'refresh_token',
refresh_token: requiredFields.refreshToken
}, requiredFields, data, next)
} else if (requiredFields.code && requiredFields.code !== '') {
// Obtain tokens with the auth code.
buildCallbackUrl(requiredFields, (err, redirectUrl) => {
if (err) { return next(err) }
fetchToken({
grant_type: 'authorization_code',
code: requiredFields.code,
state,
nonce,
redirect_uri: ''
}, requiredFields, data, next)
})
} else {
next('token not found') // Need to perform OpenIdConnect steps.
}
}
function fetchToken (form, requiredFields, data, next) {
log('info', 'fetchToken')
request.post({
url: `${connectUrl}/token`,
json: true,
headers: {
Authorization: `Basic ${b64Client}`
},
form
}, (err, response, body) => {
if (response.statusCode !== 200 && response.statusCode !== '200') {
log('error', `fetchToken error: ${response.statusCode} - ${response.statusMessage}`)
err = 'token not found'
}
if (err) {
return next(err)
}
if (!body.id_token || !body.refresh_token) {
log('error', `no token in body: ${body}`)
return next('token not found')
}
data.accessToken = body.id_token
requiredFields.refreshToken = body.refresh_token
next()
})
}
function buildCallbackUrl (requiredFields, callback) {
let url = null
let error = null
try {
let path = requiredFields.redirectPath.split('?')[0]
if (path[0] === '/') {
path = path.slice(1)
}
url = `${domain}apps/konnectors/${path}`
} catch (e) {
log('error', e)
console.log(e)
error = 'internal error'
}
callback(error, url)
}
/*
// Save konnector's fieldValues during fetch process.
function saveTokenInKonnector (requiredFields, entries, data, next) {
log('info', 'saveTokenInKonnector')
// Disable eslint because we can't require models/konnector at the top
// of this file (or Konnector will be empty). It's because in the require
// tree of models/konnector, there is the current file.
//eslint-disable-next-line
const Konnector = require('../models/konnector')
Konnector.get(connector.slug, (err, konnector) => {
if (err) {
log('error', err)
return next('internal error')
}
konnector.updateFieldValues({ accounts: [requiredFields] }, next)
})
}
*/
function fetchData (requiredFields, entries, data, next) {
log('info', 'fetchData')
request.get({
url: infoUrl,
json: true,
headers: {
Authorization: `Bearer ${data.accessToken}`
}
}, (err, response, body) => {
if (response.statusCode !== 200 && response.statusCode !== '200') {
connector.logger.error(`fetchToken error: ${response.statusCode} - ${response.statusMessage}`)
err = 'request error'
}
if (err) {
return next(err)
}
moment.locale('fr')
entries.maifusers = [{
profile: body,
date: moment().format('LLL')
}]
next()
})
}
/*
function createOrUpdateInDB (requiredFields, entries, data, next) {
log('info', 'createOrUpdateInDB')
updateOrCreate(entries.maifusers[0], (err) => {
if (err) {
log('error', err)
return next('internal error')
}
next()
})
}
*/