/
auth.R
315 lines (260 loc) · 8.98 KB
/
auth.R
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
cleanupPasswordFile <- function(appDir) {
check_directory(appDir)
appDir <- normalizePath(appDir)
# get data dir from appDir
dataDir <- file.path(appDir, "shinyapps")
# get password file
passwordFile <- file.path(dataDir, paste("passwords", ".txt", sep = ""))
# check if password file exists
if (file.exists(passwordFile)) {
message("WARNING: Password file found! This application is configured to use scrypt ",
"authentication, which is no longer supported.\nIf you choose to proceed, ",
"all existing users of this application will be removed, ",
"and will NOT be recoverable.\nFor for more information please visit: ",
"http://shiny.rstudio.com/articles/migration.html")
response <- readline("Do you want to proceed? [Y/n]: ")
if (tolower(substring(response, 1, 1)) != "y") {
stop("Cancelled", call. = FALSE)
} else {
# remove old password file
file.remove(passwordFile)
}
}
invisible(TRUE)
}
#' Add authorized user to application
#'
#' @param email Email address of user to add.
#' @param appDir Directory containing application. Defaults to
#' current working directory.
#' @param appName Name of application.
#' @inheritParams deployApp
#' @param sendEmail Send an email letting the user know the application
#' has been shared with them.
#' @param emailMessage Optional character vector of length 1 containing a
#' custom message to send in email invitation. Defaults to NULL, which
#' will use default invitation message.
#' @seealso [removeAuthorizedUser()] and [showUsers()]
#' @note This function works only for ShinyApps servers.
#' @export
addAuthorizedUser <- function(email, appDir = getwd(), appName = NULL,
account = NULL, server = NULL, sendEmail = NULL,
emailMessage = NULL) {
accountDetails <- accountInfo(account, server)
checkShinyappsServer(accountDetails$server)
# resolve application
if (is.null(appName))
appName <- basename(appDir)
application <- resolveApplication(accountDetails, appName)
# check for and remove password file
cleanupPasswordFile(appDir)
# fetch authoriztion list
api <- clientForAccount(accountDetails)
api$inviteApplicationUser(application$id, validateEmail(email), sendEmail,
emailMessage)
message(paste("Added:", email, "to application", sep = " "))
invisible(TRUE)
}
#' Remove authorized user from an application
#'
#' @param user The user to remove. Can be id or email address.
#' @param appDir Directory containing application. Defaults to
#' current working directory.
#' @param appName Name of application.
#' @inheritParams deployApp
#' @seealso [addAuthorizedUser()] and [showUsers()]
#' @note This function works only for ShinyApps servers.
#' @export
removeAuthorizedUser <- function(user, appDir = getwd(), appName = NULL,
account = NULL, server = NULL) {
accountDetails <- accountInfo(account, server)
checkShinyappsServer(accountDetails$server)
# resolve application
if (is.null(appName))
appName <- basename(appDir)
application <- resolveApplication(accountDetails, appName)
# check and remove password file
cleanupPasswordFile(appDir)
# get users
users <- showUsers(appDir, appName, account, server)
if (is.numeric(user)) {
# lookup by id
if (user %in% users$id) {
user <- users[users$id == user, ]
} else {
stop("User ", user, " not found", call. = FALSE)
}
} else {
# lookup by email
if (user %in% users$email) {
user <- users[users$email == user, ]
} else {
stop("User \"", user, "\" not found", call. = FALSE)
}
}
# remove user
api <- clientForAccount(accountDetails)
api$removeApplicationUser(application$id, user$id)
message(paste("Removed:", user$email, "from application", sep = " "))
invisible(TRUE)
}
#' List authorized users for an application
#'
#' @param appDir Directory containing application. Defaults to
#' current working directory.
#' @param appName Name of application.
#' @inheritParams deployApp
#' @seealso [addAuthorizedUser()] and [showInvited()]
#' @note This function works only for ShinyApps servers.
#' @export
showUsers <- function(appDir = getwd(), appName = NULL, account = NULL,
server = NULL) {
accountDetails <- accountInfo(account, server)
checkShinyappsServer(accountDetails$server)
# resolve application
if (is.null(appName))
appName <- basename(appDir)
application <- resolveApplication(accountDetails, appName)
# fetch authoriztion list
api <- clientForAccount(accountDetails)
res <- api$listApplicationAuthorization(application$id)
# get interesting fields
users <- lapply(res, function(x) {
a <- list()
a$id <- x$user$id
a$email <- x$user$email
if (!is.null(x$account)) {
a$account <- x$account
} else {
a$account <- NA
}
return(a)
})
# convert to data frame
users <- do.call(rbind, users)
df <- as.data.frame(users, stringsAsFactors = FALSE)
return(df)
}
#' List invited users for an application
#'
#' @param appDir Directory containing application. Defaults to
#' current working directory.
#' @param appName Name of application.
#' @inheritParams deployApp
#' @seealso [addAuthorizedUser()] and [showUsers()]
#' @note This function works only for ShinyApps servers.
#' @export
showInvited <- function(appDir = getwd(), appName = NULL, account = NULL,
server = NULL) {
accountDetails <- accountInfo(account, server)
checkShinyappsServer(accountDetails$server)
# resolve application
if (is.null(appName))
appName <- basename(appDir)
application <- resolveApplication(accountDetails, appName)
# fetch invitation list
api <- clientForAccount(accountDetails)
res <- api$listApplicationInvitations(application$id)
# get interesting fields
users <- lapply(res, function(x) {
a <- list()
a$id <- x$id
a$email <- x$email
a$link <- x$link
a$expired <- x$expired
return(a)
})
# convert to data frame
users <- do.call(rbind, users)
df <- as.data.frame(users, stringsAsFactors = FALSE)
return(df)
}
#' Resend invitation for invited users of an application
#'
#' @param invite The invitation to resend. Can be id or email address.
#' @param regenerate Regenerate the invite code. Can be helpful is the
#' invitation has expired.
#' @param appDir Directory containing application. Defaults to
#' current working directory.
#' @param appName Name of application.
#' @inheritParams deployApp
#' @seealso [showInvited()]
#' @note This function works only for ShinyApps servers.
#' @export
resendInvitation <- function(invite, regenerate = FALSE,
appDir = getwd(), appName = NULL,
account = NULL, server = NULL) {
accountDetails <- accountInfo(account, server)
checkShinyappsServer(accountDetails$server)
# get invitations
invited <- showInvited(appDir, appName, account, server)
if (is.numeric(invite)) {
# lookup by id
if (invite %in% invited$id) {
invite <- invited[invited$id == invite, ]
} else {
stop("Invitation \"", invite, "\" not found", call. = FALSE)
}
} else {
# lookup by email
if (invite %in% invited$email) {
invite <- invited[invited$email == invite, ]
} else {
stop("Invitiation for \"", invite, "\" not found", call. = FALSE)
}
}
# resend invitation
api <- clientForAccount(accountDetails)
api$resendApplicationInvitation(invite$id, regenerate)
message(paste("Sent invitation to", invite$email, "", sep = " "))
invisible(TRUE)
}
#' (Deprecated) List authorized users for an application
#'
#' @param appDir Directory containing application. Defaults to current working
#' directory.
#' @export
authorizedUsers <- function(appDir = getwd()) {
.Deprecated("showUsers")
# read password file
path <- getPasswordFile(appDir)
if (file.exists(path)) {
passwords <- readPasswordFile(path)
} else {
passwords <- NULL
}
return(passwords)
}
validateEmail <- function(email) {
if (is.null(email) || !grepl(".+\\@.+\\..+", email)) {
stop("Invalid email address.", call. = FALSE)
}
invisible(email)
}
getPasswordFile <- function(appDir) {
check_directory(appDir)
file.path(normalizePath(appDir), "shinyapps", "passwords.txt")
}
readPasswordFile <- function(path) {
# open and read file
lines <- readLines(path)
# extract fields
fields <- do.call(rbind, strsplit(lines, ":"))
users <- fields[, 1]
hashes <- fields[, 2]
# convert to data frame
df <- data.frame(user = users, hash = hashes, stringsAsFactors = FALSE)
# return data frame
return(df)
}
writePasswordFile <- function(path, passwords) {
# open and file
f <- file(path, open = "w")
defer(close(f))
# write passwords
apply(passwords, 1, function(r) {
l <- paste(r[1], ":", r[2], "\n", sep = "")
cat(l, file = f, sep = "")
})
message("Password file updated. You must deploy your application for these changes to take effect.")
}