Skip to content

Commit 12f4dcc

Browse files
authored
Added PATH_USER_PASS examples
1 parent f3a863a commit 12f4dcc

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

data-studio/auth.gs

+74
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ function getAuthType() {
2727
}
2828
// [END apps_script_data_studio_get_auth_type_oauth2]
2929

30+
// [START apps_script_data_studio_get_auth_type_path_user_pass]
31+
/**
32+
* Returns the Auth Type of this connector.
33+
* @return {object} The Auth type.
34+
*/
35+
function getAuthType() {
36+
var cc = DataStudioApp.createCommunityConnector();
37+
return cc.newAuthTypeResponse()
38+
.setAuthType(cc.AuthType.PATH_USER_PASS)
39+
.setHelpUrl('https://www.example.org/connector-auth-help')
40+
.build();
41+
}
42+
// [END apps_script_data_studio_get_auth_type_path_user_pass]
43+
3044
// [START apps_script_data_studio_get_auth_type_user_pass]
3145
/**
3246
* Returns the Auth Type of this connector.
@@ -91,6 +105,18 @@ function resetAuth() {
91105
}
92106
// [END apps_script_data_studio_auth_reset_oauth2]
93107

108+
// [START apps_script_data_studio_auth_reset_path_user]
109+
/**
110+
* Resets the auth service.
111+
*/
112+
function resetAuth() {
113+
var userProperties = PropertiesService.getUserProperties();
114+
userProperties.deleteProperty('dscc.path');
115+
userProperties.deleteProperty('dscc.username');
116+
userProperties.deleteProperty('dscc.password');
117+
}
118+
// [END apps_script_data_studio_auth_reset_path_user]
119+
94120
// [START apps_script_data_studio_auth_reset_user]
95121
/**
96122
* Resets the auth service.
@@ -133,6 +159,22 @@ function isAuthValid() {
133159
}
134160
// [END apps_script_data_studio_auth_valid_oauth2]
135161

162+
// [START apps_script_data_studio_auth_valid_path_user_pass]
163+
/**
164+
* Returns true if the auth service has access.
165+
* @return {boolean} True if the auth service has access.
166+
*/
167+
function isAuthValid() {
168+
var userProperties = PropertiesService.getUserProperties();
169+
var path = userProperties.getProperty('dscc.path');
170+
var userName = userProperties.getProperty('dscc.username');
171+
var password = userProperties.getProperty('dscc.password');
172+
// This assumes you have a validateCredentials function that
173+
// can validate if the userName and password are correct.
174+
return validateCredentials(path, userName, password);
175+
}
176+
// [END apps_script_data_studio_auth_valid_path_user_pass]
177+
136178
// [START apps_script_data_studio_auth_valid_user_pass]
137179
/**
138180
* Returns true if the auth service has access.
@@ -221,6 +263,38 @@ function get3PAuthorizationUrls() {
221263
}
222264
// [END apps_script_data_studio_auth_urls]
223265

266+
// [START apps_script_data_studio_auth_set_credentials_path_user_pass]
267+
/**
268+
* Sets the credentials.
269+
* @param {Request} request The set credentials request.
270+
* @return {object} An object with an errorCode.
271+
*/
272+
function setCredentials(request) {
273+
var creds = request.userPass;
274+
var path = creds.path;
275+
var username = creds.username;
276+
var password = creds.password;
277+
278+
// Optional
279+
// Check if the provided path, username and password are valid through
280+
// a call to your service. You would have to have a `checkForValidCreds`
281+
// function defined for this to work.
282+
var validCreds = checkForValidCreds(path, username, password);
283+
if (!validCreds) {
284+
return {
285+
errorCode: 'INVALID_CREDENTIALS'
286+
};
287+
}
288+
var userProperties = PropertiesService.getUserProperties();
289+
userProperties.setProperty('dscc.path', path);
290+
userProperties.setProperty('dscc.username', username);
291+
userProperties.setProperty('dscc.password', password);
292+
return {
293+
errorCode: 'NONE'
294+
};
295+
}
296+
// [END apps_script_data_studio_auth_set_credentials_path_user_pass]
297+
224298
// [START apps_script_data_studio_auth_set_credentials_user_pass]
225299
/**
226300
* Sets the credentials.

0 commit comments

Comments
 (0)