Skip to content

Commit

Permalink
Starting sanitize_token()
Browse files Browse the repository at this point in the history
See #144
  • Loading branch information
wibeasley committed Mar 24, 2017
1 parent 0f4d9e9 commit e6df5b3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export(regex_named_captures)
export(retrieve_credential_local)
export(retrieve_credential_mssql)
export(retrieve_token_mssql)
export(sanitize_token)
export(validate_for_write)
export(validate_no_logical)
export(validate_no_uppercase)
Expand Down
32 changes: 32 additions & 0 deletions R/sanitize-token.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' @name sanitize_token
#' @export
#'
#' @title Validate and sanitize the user's REDCap token.
#'
#' @description Verifies the token is nonmissing and conforms to the legal pattern of a 32-character hexadecimal value.
#' Trailing line endings are removed.
#'
#' @param token The REDCap token. Required.
#'
#' @return The token, without a terminal newline character.
#'
#' @note Contact your institution's REDCap administrator for more informationa about your project-specific token.
#'
#' @author Hao Zhu, Benjamin Nutter, Will Beasley
#'
#' @examples
#' library(REDCapR) #Load the package into the current R session.
#' secret_token_1 <- "12345678901234567890123456ABCDEF"
#' secret_token_2 <- "12345678901234567890123456ABCDEF\n"
#' sanitize_token(secret_token_1)
#' sanitize_token(secret_token_2)

sanitize_token <- function( token ) {
# Validate only 32-character hexadecimals, with an optional line ending.
pattern <- "^([0-9A-F]{32})(?:\\n)?$"

if( !grepl(pattern, token, perl=TRUE) )
stop("The token is not a valid 32-character hexademical value.")

sub(pattern, "\\1", token, perl=TRUE)
}
31 changes: 31 additions & 0 deletions man/sanitize_token.Rd

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

28 changes: 28 additions & 0 deletions tests/testthat/test-sanitize.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
library(testthat)
context("Sanitize Token")

test_that("sanitize token w/o line endings", {
secret_token <- "12345678901234567890123456ABCDEF"
sanitize_token(secret_token)

returned <- REDCapR::sanitize_token(secret_token)
expect_equal(returned, secret_token)
})

test_that("sanitize token w/ line endings", {
secret_token <- "12345678901234567890123456ABCDEF\n"
sanitize_token(secret_token)

returned <- REDCapR::sanitize_token(secret_token)
expect_equal(returned, substr(secret_token, 1L, 32L))
})

test_that("sanitize token w/o line endings", {
secret_token <- "12345678901234567"
expect_error(
object = sanitize_token(secret_token),
regexp = "The token is not a valid 32-character hexademical value\\."
)
})


0 comments on commit e6df5b3

Please sign in to comment.