Skip to content

Commit

Permalink
Merge pull request #184 from aappling-usgs/master
Browse files Browse the repository at this point in the history
changes to session validation/renewal
  • Loading branch information
Luke Winslow committed Feb 16, 2016
2 parents 0c1e426 + 96fcabb commit 25a6098
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 121 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: sbtools
Title: USGS ScienceBase Tools
Maintainer: Luke Winslow <[email protected]>
Version: 0.15.2
Version: 0.15.3
Authors@R: c(person("Luke", "Winslow", role = c("aut","cre"),
email = "[email protected]"),
person("Scott", "Chamberlain", role = c("aut"),
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export(sbtools_POST)
export(sbtools_PUT)
export(session_age)
export(session_age_reset)
export(session_check_reauth)
export(session_details)
export(session_expired)
export(session_logout)
export(session_renew)
export(session_validate)
export(set_endpoint)
export(set_expiration)
Expand Down
2 changes: 1 addition & 1 deletion R/REST_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ sbtools_DELETE <- function(url, ..., session) {

# HEAD fxn
sbtools_HEAD <- function(url, ..., session) {
check_session(session)
session_val(session)
r <- HEAD(url = url, ..., handle = session)
log <- if (r$status_code == 200) TRUE else FALSE
session_age_reset()
Expand Down
4 changes: 2 additions & 2 deletions R/identifier_exists.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#' @return Logical, \code{TRUE} or \code{FALSE}
#' @examples \dontrun{
#' # identifier exists
#' identifier_exists(id = "4f4e4b24e4b07f02db6aea14")
#' identifier_exists(sb_id = "4f4e4b24e4b07f02db6aea14")
#'
#' # identifier does not exist
#' identifier_exists(id = "aaaaaaakkkkkkkbbbbbb")
#' identifier_exists(sb_id = "aaaaaaakkkkkkkbbbbbb")
#' }
identifier_exists <- function(sb_id, ..., session = current_session()) {
sb_id = as.sbitem(sb_id)
Expand Down
2 changes: 1 addition & 1 deletion R/is_logged_in.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
#' is_logged_in()
#' }
is_logged_in <- function(..., session = current_session()) {
session_details(..., session = session)$isLoggedIn
!is.null(session) && session_details(..., session = session)$isLoggedIn
}
70 changes: 0 additions & 70 deletions R/session_check_reauth.R

This file was deleted.

58 changes: 58 additions & 0 deletions R/session_renew.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#' Checks current session and re-authenticates if necessary
#'
#' Checks the state of your Sciencebase session, re-authenticates if the session
#' is expired, and simply renews if the session is active.
#'
#' @param password The password to use, if needed, to renew the session.
#' @param username Optional. Used only to confirm that the current username is
#' what you expect; if you want to switch usernames, use
#' \code{authenticate_sb()} instead of this function.
#' @param session SB session object from \code{\link{authenticate_sb}}. Default
#' is the current session.
#' @param ... Any additional parameters are currently ignored.
#'
#' @return Returns the session object.
#'
#' @examples
#' \dontrun{
#' # an empty call is sufficient if the session is current,
#' # but will break if haven't been logged in before
#' session_renew()
#'
#' # include a password if session may be expired
#' session_renew('newpass')
#'
#' # optionally confirm the value of the current username
#' session_renew(username='[email protected]', 'newpass')
#' }
#' @import httr
#' @export
session_renew = function(password, ..., username, session=current_session()){

# if we'll need the current username, find it now. use the existing session
# info on SB if available; otherwise use the username stored locally
if(!missing(username) || !is_logged_in(session=session)) {
sb_username <- session_details(session=session)$username
if(is.null(sb_username)) sb_username <- pkg.env$username
}

# if username is provided, confirm that it matches the stored username
if(!missing(username)) {
if(username != sb_username) {
stop("username argument does not match session username")
}
}

# either renew or re-authenticate as needed
if(is_logged_in(session=session)) {
# the GET call to 'status' resets the remote (SB) info on session age, while
# sbtools_GET resets the local info on session age
sbtools_GET(url=paste0(pkg.env$url_base, "status?format=json"), session=session)
invisible(session)
} else {
# re-authenticate, handling missing parameters as needed
if(sb_username=="") stop("new authentication is necessary; call authenticate_sb()")
if(missing(password)) stop("re-authentication is necessary; need password")
invisible(authenticate_sb(sb_username, password))
}
}
4 changes: 2 additions & 2 deletions man/identifier_exists.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 0 additions & 42 deletions man/session_check_reauth.Rd

This file was deleted.

41 changes: 41 additions & 0 deletions man/session_renew.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/testthat/test-eg.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ test_that("session validation works appropriately", {
session <- httr::handle("http://google.com")
attributes(session) <- c(attributes(session), list(birthdate=Sys.time()))
expect_true(session_validate(session))
expect_is(session_check_reauth(session), 'handle')
set_expiration(as.difftime("00:00:01"))
Sys.sleep(2)
expect_false(session_validate(session))
Expand Down

0 comments on commit 25a6098

Please sign in to comment.