-
Notifications
You must be signed in to change notification settings - Fork 21
/
authenticate_sb.R
316 lines (234 loc) · 7.83 KB
/
authenticate_sb.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
316
#' Authenticate to SB for subsequent calls
#'
#' This connects to SB, authenticates and gets a session token for communicating
#' with SB. If you do not supply a username or password, you will be prompted to
#' enter them.
#'
#' @param username Sciencebase username
#' @param password Sciencebase password, prompts user if not supplied and
#' no password is returned by `keyring::key_get("sciencebase", username)`.
#' See \code{\link[keyring]{keyring-package}} documentation for more details.
#'
#' @import httr
#'
#' @export
authenticate_sb = function(username, password){
message(
"authenticate_sb no longer works for individual login sessions, because ",
"sciencebase now requires multi-factor authentication.\n\n",
"Use initialize_sciencebase_session() ",
"instead (except in the rare case where you are using a role account)."
)
# TODO: bring back session_details?
username <- try(get_username(username), silent = TRUE)
if((inherits(username, "try-error") | is.null(username)) && !interactive()){
stop('username required for authentication')
}
pkg.env$username <- username
keyring_pass = FALSE
if(missing(password)) {
password <- try(keyring::key_get("sciencebase", username), silent = TRUE)
if(!inherits(password, "try-error")) keyring_pass = TRUE
}
if(!interactive() & inherits(password, "try-error")){
stop('No password supplied to authenticate_sciencebase in a non-interactive session.')
} else {
password = ifelse(missing(password) | inherits(password, "try-error"),
readPassword('Please enter your Sciencebase password:'),
password)
}
h = handle(pkg.env$url_base)
## authenticate
resp = RETRY("GET", pkg.env$url_base, accept_json(),
authenticate(username, password, type='basic'),
handle=h, timeout = httr::timeout(default_timeout()))
if(!any(resp$cookies$name %in% 'JSESSIONID')){
if(keyring_pass) stop("Sciencebase login failed with stored password?")
}
token_url <- pkg.env$token_url
token <- RETRY("POST", token_url,
body = list(
client_id = pkg.env$keycloak_client_id,
grant_type = "password",
username = username,
password = password
), encode = "form")
if(!token$status_code == 200) {
warning('Unable to authenticate to SB cloud. Standard login is available.')
} else {
set_keycloak_env(token)
}
return(invisible(TRUE))
}
get_username <- function(username = NULL) {
if(is.null(username)) {
username <- Sys.getenv("sb_user")
if(username != "") {
pkg.env$username <- username
return(username)
}
if(interactive()) {
username = readline('Please enter your username:')
if(username == ""){
stop('Empty username supplied, stopping')
}
}else {
stop("username required for authentication")
}
# username <- try(session_details()$username)
}
pkg.env$username <- username
username
}
set_keycloak_env <- function(token_resp) {
try({
json <- jsonlite::fromJSON(rawToChar(token_resp$content))
if(!"error" %in% names(json)) {
pkg.env$keycloak_token <- json
pkg.env$keycloak_expire <- Sys.time() + pkg.env$keycloak_token$expires_in
}
}, silent = TRUE)
}
#' Initialize ScienceBase Session
#' @description Unless `token_text` is provided, will open a browser for two
#' factor authentication.
#'
#' Once logged in, retrieve the token from the user drop down in the upper
#' right hand corner of the browser. Click the icon with the silhouette of
#' a person, and select 'Copy API Token.' The token should be pasted into the
#' popup prompt.
#'
#' @param token_text character json formatted token text. `token_text` is
#' stashed in \link[tools]{R_user_dir} and does not need to be re-entered unless
#' it becomes stale.
#'
#' If the token text is provided as input, no popup prompt will be raised.
#'
#' @param username email address of sciencebase user. Will be retrieved from the
#' `sb_user` environment variable if set. A prompt will be raised if not provided.
#' @export
#'
initialize_sciencebase_session <- function(username = NULL, token_text = NULL) {
username <- get_username(username)
if(is.null(token_text)) {
token <- gsub("[\r\n]", "", grab_token())
if(token != "") {
check_current <- try(
initialize_keycloack_env(
token, warn_on_fail = FALSE),
silent = TRUE)
if(isTRUE(check_current)) {
pkg.env$username <- username
return(invisible(TRUE))
}
}
message("A browser will open ", pkg.env$manager_app)
message("Log in and copy a token from the 'User' menu in the upper right.")
message("Paste the token in the dialogue box that opens.")
utils::browseURL(pkg.env$manager_app)
token_text <- readPassword('Please enter your Sciencebase token string:')
}
pkg.env$username <- username
worked <- try(initialize_keycloack_env(token_text))
if(!inherits(worked, "try-error")) {
stache_token(token_text)
return(invisible(TRUE))
} else {
return(invisible(FALSE))
}
}
initialize_keycloack_env <- function(token_text, warn_on_fail = TRUE) {
pkg.env$keycloak_token <- jsonlite::fromJSON(token_text)
pkg.env$keycloak_expire <- Sys.time()
token_refresh(warn_on_fail = warn_on_fail)
}
# utility to clean environment for testing
clean_session <- function() {
pkg.env$keycloak_token <- NULL
pkg.env$keycloak_expire <- NULL
pkg.env$keycloak_client_id <- "catalog"
pkg.env$username = ""
pkg.env$uid <- NULL
}
#' Get or set token stache data directory
#' @description if left unset, will return the user data dir
#' as returned by `tools::R_user_dir` for this package.
#' @param dir path of desired token stache file
#' @return character path of data directory (silent when setting)
#' @importFrom tools R_user_dir
#' @noRd
#'
token_stache_path <- function(dir = NULL) {
if(is.null(dir)) {
token_stache <- try(get("token_stache", envir = pkg.env), silent = TRUE)
if(inherits(token_stache, "try-error")) {
assign("token_stache",
file.path(tools::R_user_dir(package = "sbtools"), "token"),
envir = pkg.env)
}
return(get("token_stache", envir = pkg.env))
} else {
assign("token_stache",
dir,
envir = pkg.env)
return(invisible(get("token_stache", envir = pkg.env)))
}
}
stache_token <- function(token_text) {
dir.create(dirname(token_stache_path()), recursive = TRUE, showWarnings = FALSE)
write(token_text, file = token_stache_path())
}
grab_token <- function() {
if(file.exists(token_stache_path())) {
readChar(token_stache_path(), file.info(token_stache_path())$size)
} else {
""
}
}
#' Read in a password from the user
#'
#' Uses the RStudio pop-up password window if available
#'
#' @importFrom utils globalVariables
#' @keywords internal
readPassword <- function(prompt) {
# found this cool function in rstudio
if (exists(".rs.askForPassword", mode = "function")) {
pass <- .rs.askForPassword(prompt)
} else {
message("paste your token")
pass <- readLines(n = 1)
}
return (pass)
}
globalVariables('.rs.askForPassword')
get_refresh_token <- function() {
token <- try(pkg.env$keycloak_token$refresh_token, silent = TRUE)
if(inherits(token, "try-error") | is.null(token)) {
stop("no token found, must call authenticate_sb()")
}
token
}
get_access_token <- function() {
token <- pkg.env$keycloak_token$access_token
if(is.null(token)) {
stop("no token found, must call authenticate_sb()")
}
token
}
get_token_header <- function() {
if(is.null(current_session()) | is.null(session_age())) return(httr::add_headers())
httr::add_headers(
.headers = c(authorization = paste("Bearer",
get_access_token())))
}
#' Check whether you're logged into a ScienceBase session
#'
#' @export
#' @return Logical, \code{TRUE} or \code{FALSE}
#' @examples \dontrun{
#' is_logged_in()
#' }
is_logged_in <- function() {
!is.null(current_session()) && !session_expired()
}