-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #144
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\." | ||
) | ||
}) | ||
|
||
|