|
| 1 | +/** |
| 2 | + * Copyright Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START apps_script_data_studio_reset_auth_oauth2] |
| 18 | +// [END apps_script_data_studio_reset_auth_oauth2] |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +// [START apps_script_data_studio_get_auth_type_oauth2] |
| 25 | +function getAuthType() { |
| 26 | + return { |
| 27 | + "type": "OAUTH2" |
| 28 | + }; |
| 29 | +} |
| 30 | +// [END apps_script_data_studio_get_auth_type_oauth2] |
| 31 | + |
| 32 | +// [START apps_script_data_studio_get_auth_type_user_pass] |
| 33 | +function getAuthType() { |
| 34 | + return { |
| 35 | + "type": "USER_PASS" |
| 36 | + }; |
| 37 | +} |
| 38 | +// [END apps_script_data_studio_get_auth_type_user_pass] |
| 39 | + |
| 40 | +// [START apps_script_data_studio_get_auth_type_key] |
| 41 | +function getAuthType() { |
| 42 | + return { |
| 43 | + "type": "KEY" |
| 44 | + }; |
| 45 | +} |
| 46 | +// [END apps_script_data_studio_get_auth_type_key] |
| 47 | + |
| 48 | +// [START apps_script_data_studio_get_auth_type_none] |
| 49 | +function getAuthType() { |
| 50 | + return { |
| 51 | + "type": "NONE" |
| 52 | + }; |
| 53 | +} |
| 54 | +// [END apps_script_data_studio_get_auth_type_none] |
| 55 | + |
| 56 | +// [START apps_script_data_studio_auth_reset_oauth2] |
| 57 | +function resetAuth() { |
| 58 | + getOAuthService().reset(); |
| 59 | +} |
| 60 | +// [END apps_script_data_studio_auth_reset_oauth2] |
| 61 | + |
| 62 | +// [START apps_script_data_studio_auth_reset_user] |
| 63 | +function resetAuth() { |
| 64 | + var userProperties = PropertiesService.getUserProperties(); |
| 65 | + userProperties.deleteProperty('dscc.username'); |
| 66 | + userProperties.deleteProperty('dscc.password'); |
| 67 | +} |
| 68 | +// [END apps_script_data_studio_auth_reset_user] |
| 69 | + |
| 70 | +// [START apps_script_data_studio_auth_reset_key] |
| 71 | +function resetAuth() { |
| 72 | + var userProperties = PropertiesService.getUserProperties(); |
| 73 | + userProperties.deleteProperty('dscc.key'); |
| 74 | +} |
| 75 | +// [END apps_script_data_studio_auth_reset_key] |
| 76 | + |
| 77 | +// [START apps_script_data_studio_auth_valid_oauth2] |
| 78 | +function isAuthValid() { |
| 79 | + return getOAuthService().hasAccess(); |
| 80 | +} |
| 81 | +// [END apps_script_data_studio_auth_valid_oauth2] |
| 82 | + |
| 83 | +// [START apps_script_data_studio_auth_valid_user_pass] |
| 84 | +function isAuthValid() { |
| 85 | + var userProperties = PropertiesService.getUserProperties(); |
| 86 | + var userName = userProperties.getProperty('dscc.username'); |
| 87 | + var password = userProperties.getProperty('dscc.password'); |
| 88 | + // This assumes you have a validateCredentials function that |
| 89 | + // can validate if the userName and password are correct. |
| 90 | + return validateCredentials(userName, password); |
| 91 | +} |
| 92 | +// [END apps_script_data_studio_auth_valid_user_pass] |
| 93 | + |
| 94 | +// [START apps_script_data_studio_auth_valid_key] |
| 95 | +function isAuthValid() { |
| 96 | + var userProperties = PropertiesService.getUserProperties(); |
| 97 | + var key = userProperties.getProperty('dscc.key'); |
| 98 | + // This assumes you have a validateKey function that can validate |
| 99 | + // if the key is valid. |
| 100 | + return validateKey(key); |
| 101 | +} |
| 102 | +// [END apps_script_data_studio_auth_valid_key] |
| 103 | + |
| 104 | +// [START apps_script_data_studio_auth_library] |
| 105 | +function getOAuthService() { |
| 106 | + return OAuth2.createService('exampleService') |
| 107 | + .setAuthorizationBaseUrl('...') |
| 108 | + .setTokenUrl('...') |
| 109 | + .setClientId('...') |
| 110 | + .setClientSecret('...') |
| 111 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 112 | + .setCallbackFunction('authCallback') |
| 113 | + .setScope('...'); |
| 114 | +}; |
| 115 | +// [END apps_script_data_studio_auth_library] |
| 116 | + |
| 117 | +// [START apps_script_data_studio_auth_callback] |
| 118 | +function authCallback(request) { |
| 119 | + var authorized = getOAuthService().handleCallback(request); |
| 120 | + if (authorized) { |
| 121 | + return HtmlService.createHtmlOutput('Success! You can close this tab.'); |
| 122 | + } else { |
| 123 | + return HtmlService.createHtmlOutput('Denied. You can close this tab'); |
| 124 | + }; |
| 125 | +}; |
| 126 | +// [END apps_script_data_studio_auth_callback] |
| 127 | + |
| 128 | +// [START apps_script_data_studio_auth_urls] |
| 129 | +function get3PAuthorizationUrls() { |
| 130 | + return getOAuthService().getAuthorizationUrl(); |
| 131 | +} |
| 132 | +// [END apps_script_data_studio_auth_urls] |
| 133 | + |
| 134 | +// [START apps_script_data_studio_auth_set_credentials_user_pass] |
| 135 | +function setCredentials(request) { |
| 136 | + var creds = request.userPass; |
| 137 | + var username = creds.username; |
| 138 | + var password = creds.password; |
| 139 | + |
| 140 | + // Optional |
| 141 | + // Check if the provided username and password are valid through a |
| 142 | + // call to your service. You would have to have a `checkForValidCreds` |
| 143 | + // function defined for this to work. |
| 144 | + var validCreds = checkForValidCreds(username, password); |
| 145 | + if (!validCreds) { |
| 146 | + return { |
| 147 | + errorCode: "INVALID_CREDENTIALS" |
| 148 | + }; |
| 149 | + } |
| 150 | + var userProperties = PropertiesService.getUserProperties(); |
| 151 | + userProperties.setProperty('dscc.username', username); |
| 152 | + userProperties.setProperty('dscc.password', password); |
| 153 | + return { |
| 154 | + errorCode: "NONE" |
| 155 | + }; |
| 156 | +} |
| 157 | +// [END apps_script_data_studio_auth_set_credentials_user_pass] |
| 158 | + |
| 159 | +// [START apps_script_data_studio_auth_set_credentials_key] |
| 160 | +function setCredentials(request) { |
| 161 | + var key = request.key; |
| 162 | + |
| 163 | + // Optional |
| 164 | + // Check if the provided key is valid through a call to your service. |
| 165 | + // You would have to have a `checkForValidKey` function defined for |
| 166 | + // this to work. |
| 167 | + var validKey = checkForValidKey(key); |
| 168 | + if (!validKey) { |
| 169 | + return { |
| 170 | + errorCode: "INVALID_CREDENTIALS" |
| 171 | + }; |
| 172 | + } |
| 173 | + var userProperties = PropertiesService.getUserProperties(); |
| 174 | + userProperties.setProperty('dscc.key', key); |
| 175 | + return { |
| 176 | + errorCode: "NONE" |
| 177 | + }; |
| 178 | +} |
| 179 | +// [END apps_script_data_studio_auth_set_credentials_key] |
0 commit comments