-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
131 lines (119 loc) · 3.47 KB
/
plugin.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
/**
* Main We.js Google API integration plugin file
*/
const GA = require('./lib/index.js');
module.exports = function loadPlugin (projectPath, Plugin) {
const plugin = new Plugin(__dirname);
GA.init(plugin.we);
plugin.GA = GA;
plugin.setConfigs({
googleApi: {
scopes: {
//'https://www.googleapis.com/auth/youtube.upload': true,
//'https://www.googleapis.com/auth/youtube': true
}
},
API_KEYS: {
googleApi: { // configuration if not use system settings for configuration
client_id: null,
client_secret: null,
// If null will be automatcaly set with hostname+'/auth/google-api/callback'
redirect_uri: null
}
}
});
plugin.setRoutes({
// authorization and refresh token restrival
'get /auth/google-api/authenticate': {
controller: 'googleApi',
action: 'authenticate',
permission: 'manage_google_api_tokens'
},
'get /auth/google-api/callback': {
controller: 'googleApi',
action: 'callback',
permission: 'manage_google_api_tokens'
},
});
/**
* Plugin fast loader for speed up we.js projeto bootstrap
*
* @param {Object} we
* @param {Function} done callback
*/
plugin.fastLoader = function (we, done) {
// - Controllers:
we.controllers.googleApi = new we.class.Controller({
authenticate(req, res) {
// authenticate to get refresh token
GA.authenticate( (err, result)=> {
if (err) return res.serverError(err);
res.redirect(result.authUrl);
});
},
callback(req, res) {
if (!req.query.code) {
req.we.log.warn('gogoleApi:Unknow response on receive refresh token:', req.query);
return res.badRequest('we-plugin-google-api:callback.authorization.code.not.found');
}
GA.getTokenFromAuthorizationCode(req.query.code)
.then( (tokens)=> {
if (!tokens.refresh_token) {
res.goTo('/');
} else {
// first auth, save the refresh_token:
req.we.plugins['we-plugin-db-system-settings']
.setConfigs({
googleAccessToken: tokens.access_token,
googleRefreshToken: tokens.refresh_token,
googleTokenExpiryDate: tokens.expiry_date
}, (err)=> {
if (err) return res.queryError(err);
res.addMessage('success', {
text: 'we-plugin-google-api:authorization.success'
});
res.goTo('/');
});
}
});
}
});
// // - Models:
// we.db.modelsConfigs['ga-access-token'] = {
// definition: {
// access_token: {
// type: we.db.Sequelize.TEXT,
// allowNull: true
// },
// refresh_token: {
// type: we.db.Sequelize.TEXT,
// allowNull: false
// },
// token_type: {
// type: we.db.Sequelize.STRING,
// allowNull: false
// },
// expiry_date: {
// type: we.db.Sequelize.INTEGER,
// allowNull: false
// },
// status: {
// type: we.db.Sequelize.INTEGER,
// allowNull: false,
// size: 3
// }
// },
// associations: {
// creator: {
// type: 'belongsTo',
// model: 'user'
// }
// },
// options: {
// tableName: 'ga_access_tokens'
// }
// };
done();
};
return plugin;
};