-
Notifications
You must be signed in to change notification settings - Fork 2
/
getOAuthTokens.gs
146 lines (121 loc) · 5.07 KB
/
getOAuthTokens.gs
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
/**
* This script will list all Connected Applications to user accounts, one per row.
* This information can be more helpful compared to Google's admin console
* reporting of the number of users per app.
*
*/
// Get all users. Specify 'domain' to filter search to one domain
function listAllUsers(cb) {
const tokens = [];
let pageToken;
do {
const page = AdminDirectory.Users.list({
customer: "my_customer",
orderBy: "givenName",
maxResults: 500,
pageToken: pageToken,
});
const users = page.users;
if (users) {
users.forEach((user) => {
if (cb) {
cb(user);
}
});
} else {
console.log("No users found.");
}
pageToken = page.nextPageToken || "";
} while (pageToken);
}
// Gets all users and tokens
function getTokens() {
const oAuthTokenSheetName = "OAuth Tokens";
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheets = spreadsheet.getSheets();
const lastSheetIndex = sheets.length;
// Check if "OAuth Tokens" sheet exists and delete if it does
let oAuthTokenSheet = spreadsheet.getSheetByName(oAuthTokenSheetName);
if (oAuthTokenSheet) {
spreadsheet.deleteSheet(oAuthTokenSheet);
}
// Create new sheet at the last index
oAuthTokenSheet = spreadsheet.insertSheet(oAuthTokenSheetName, lastSheetIndex);
// Apply font
const headerRange = oAuthTokenSheet.getRange('A1:G1');
const font = headerRange.getFontFamily();
if (font != "Montserrat") {
headerRange.setFontFamily("Montserrat");
}
// Add headers
oAuthTokenSheet.getRange('A1:G1').setValues([['primaryEmail', 'displayText', 'clientId', 'anonymous', 'nativeApp', 'User Key', 'Scopes']]);
// Format header row
oAuthTokenSheet.getRange('A1:G1').setBackground('#fc3165').setFontColor('#ffffff').setFontWeight('bold');
// Freeze header row
oAuthTokenSheet.setFrozenRows(1);
// Hide column F
oAuthTokenSheet.hideColumns(6);
// Add note to cell G1
oAuthTokenSheet.getRange("G1").setNote("A light red highlighted row indicates the app uses a restricted or sensitive scope.");
// Apply conditional formatting
const range = oAuthTokenSheet.getRange('A2:G999');
const rule = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied('=OR(REGEXMATCH($G2, "https://mail.google.com/"), REGEXMATCH($G2, "https://www.googleapis.com/auth/gmail.compose"), REGEXMATCH($G2, "https://www.googleapis.com/auth/gmail.insert"), REGEXMATCH($G2, "https://www.googleapis.com/auth/gmail.metadata"), REGEXMATCH($G2, "https://www.googleapis.com/auth/gmail.modify"), REGEXMATCH($G2, "https://www.googleapis.com/auth/gmail.readonly"), REGEXMATCH($G2, "https://www.googleapis.com/auth/gmail.send"), REGEXMATCH($G2, "https://www.googleapis.com/auth/gmail.settings.basic"), REGEXMATCH($G2, "https://www.googleapis.com/auth/gmail.settings.sharing"), REGEXMATCH($G2, "https://www.googleapis.com/auth/drive"), REGEXMATCH($G2, "https://www.googleapis.com/auth/drive.apps.readonly"), REGEXMATCH($G2, "https://www.googleapis.com/auth/drive.metadata"), REGEXMATCH($G2, "https://www.googleapis.com/auth/drive.metadata.readonly"), REGEXMATCH($G2, "https://www.googleapis.com/auth/drive.readonly"), REGEXMATCH($G2, "https://www.googleapis.com/auth/drive.scripts"), REGEXMATCH($G2, "https://www.googleapis.com/auth/documents"))')
.setBackground("#f4cccc")
.setRanges([range])
.build();
const rules = oAuthTokenSheet.getConditionalFormatRules();
rules.push(rule);
oAuthTokenSheet.setConditionalFormatRules(rules);
const tokens = [];
listAllUsers(function (user) {
try {
if (user.suspended) {
console.log(
"[suspended] %s (%s)",
user.name.fullName,
user.primaryEmail
);
return;
}
const currentTokens = AdminDirectory.Tokens.list(user.primaryEmail);
if (
currentTokens &&
currentTokens.items &&
currentTokens.items.length
) {
currentTokens.items.forEach((tok) => {
if (!tok.nativeApp) {
tokens.push([
user.primaryEmail,
tok.displayText,
tok.clientId,
tok.anonymous,
tok.nativeApp,
tok.userKey,
tok.scopes.join(" "),
]);
}
});
}
} catch (e) {
console.log("[error] %s: %s", user.primaryEmail, e);
}
});
console.log("Tokens written to Sheet Users: %s", tokens.length);
const dataRange = oAuthTokenSheet.getRange(2, 1, tokens.length, tokens[0].length);
dataRange.setValues(tokens);
// --- Add Filter View ---
const lastRow = oAuthTokenSheet.getLastRow();
const filterRange = oAuthTokenSheet.getRange('A1:G' + lastRow); // Filter columns A through G, starting from row 1
filterRange.createFilter();
// Auto resize columns A, D, E
oAuthTokenSheet.autoResizeColumns(1, 1);
oAuthTokenSheet.autoResizeColumns(4, 2);
// Resize column G
oAuthTokenSheet.setColumnWidth(7, 320);
oAuthTokenSheet.setColumnWidth(2, 315);
// Delete columns H-Z
oAuthTokenSheet.deleteColumns(8, 18);
oAuthTokenSheet.deleteColumns(8);
}