-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.js
123 lines (111 loc) · 3.36 KB
/
authentication.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
/**
* Contains methods for handling authentication between dotCMS and Zapier
*/
'use strict';
/**
* Sends data to the dotZapier plugin on dotCMS to perform workflow action
* @param z Zapier object
* @param bundle Stores all the user input as well derived attributes
* @return String User-Friendly name for App Authentication details
*/
const getConnectionLabel = async (z, bundle) => {
return bundle.inputData.givenName + ' ' + '[' + bundle.authData.email + ']';
};
/**
* Invokes dotCMS Authentication API using basic authentication
* to obtain the API key. This API key will be used for all future
* communication with the dotCMS plugin
* @param z Zapier object
* @param bundle Stores all the user input as well derived attributes
* @return Dictionary Contains the API key. This will be stored as a
* derived attribute in the zapier bundle
*/
const generateApiKey = async (z, bundle) => {
const options = {
method: 'POST',
url: `${bundle.authData.url}/api/v1/authentication/api-token`,
headers: {
'content-type': 'application/json',
'accept': 'application/json'
},
body: {
user: bundle.authData.email,
password: bundle.authData.password,
expirationDays: 730,
label: 'Zapier Integration'
}
};
return z.request(options).then((response) => {
response.throwForStatus();
const data = response.json;
const apiKey = data.entity.token;
return {
apiKey: apiKey
};
});
};
/**
* Invokes dotCMS current user API using the generated API key
* Obtains the user details. This API must return 200 status code
* for Zapier to store the API key generated from the authentication method
* @param z Zapier object
* @param bundle Stores all the user input as well derived attributes
* @return Dictionary Contains User details. This will be stored as a
* derived attribute in the zapier bundle
*/
const getCurrentUser = async (z, bundle) => {
const options = {
url: `${bundle.authData.url}/api/v1/users/current`,
method: 'GET'
};
return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
return results;
});
};
/**
* Zapier authentication Object
* Session authentication is enabled for dotCMS
* URL, email and password are required fields that needs to be inputed
* when the app is integrated with Zapier
* API key authentication cannot be used as it does not allow one to store
* derived attributes
*/
const authentication = {
type: 'session',
fields: [
{
computed: false,
key: 'url',
required: true,
label: 'dotCMS URL',
type: 'string',
helpText: 'URL of the dotCMS Instance, for example: [Demo Site](https://demo.dotcms.com)'
},
{
computed: false,
key: 'email',
required: true,
label: 'Email',
type: 'string',
helpText: 'Email Address of the Administrator of the dotCMS instance, for example: [[email protected]](https://demo.dotcms.com)',
},
{
computed: false,
key: 'password',
required: true,
label: 'Password',
type: 'password',
helpText: 'The password of the dotCMS Administrator',
}
],
sessionConfig: {
perform: generateApiKey,
},
test: getCurrentUser,
connectionLabel: getConnectionLabel
};
module.exports = {
authentication: authentication
}