1#' Use http authentication.
2#'
3#' It's not obvious how to turn authentication off after using it, so
4#' I recommend using custom handles with authentication.
5#'
6#' @param user user name
7#' @param password password
8#' @param type type of HTTP authentication.  Should be one of the following
9#'   types supported by Curl: basic, digest, digest_ie, gssnegotiate,
10#'   ntlm, any.  It defaults to "basic", the most common type.
11#' @export
12#' @family config
13#' @examples
14#' GET("http://httpbin.org/basic-auth/user/passwd")
15#' GET(
16#'   "http://httpbin.org/basic-auth/user/passwd",
17#'   authenticate("user", "passwd")
18#' )
19authenticate <- function(user, password, type = "basic") {
20  stopifnot(is.character(user), length(user) == 1)
21  stopifnot(is.character(password), length(password) == 1)
22  stopifnot(is.character(type), length(type) == 1)
23
24  config(httpauth = auth_flags(type), userpwd = paste0(user, ":", password))
25}
26
27auth_flags <- function(x = "basic") {
28  constants <- c(
29    basic = 1,
30    digest = 2,
31    gssnegotiate = 4,
32    ntlm = 8,
33    digest_ie = 16,
34    any = -17
35  )
36  x <- match.arg(x, names(constants))
37
38  constants[[x]]
39}
40