File: auth.R

package info (click to toggle)
r-cran-crul 1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: sh: 13; makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,564 bytes parent folder | download | duplicates (3)
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
#' Authentication
#'
#' @export
#' @param user (character) username, required. see Details.
#' @param pwd (character) password, required. see Details.
#' @param auth (character) authentication type, one of basic (default),
#' digest, digest_ie, gssnegotiate, ntlm, or any. required
#' 
#' @details
#' Only supporting simple auth for now, OAuth later maybe.
#' 
#' For `user` and `pwd` you are required to pass in some value. 
#' The value can be `NULL` to - which is equivalent to passing in an 
#' empty string like `""` in `httr::authenticate`. You may want to pass
#' in `NULL` for both `user` and `pwd` for example if you are using 
#' `gssnegotiate` auth type. See example below.
#' 
#' @examples
#' auth(user = "foo", pwd = "bar", auth = "basic")
#' auth(user = "foo", pwd = "bar", auth = "digest")
#' auth(user = "foo", pwd = "bar", auth = "ntlm")
#' auth(user = "foo", pwd = "bar", auth = "any")
#' 
#' # gssnegotiate auth
#' auth(NULL, NULL, "gssnegotiate")
#' 
#' \dontrun{
#' # with HttpClient
#' (res <- HttpClient$new(
#'   url = "https://httpbin.org/basic-auth/user/passwd",
#'   auth = auth(user = "user", pwd = "passwd")
#' ))
#' res$auth
#' x <- res$get()
#' jsonlite::fromJSON(x$parse("UTF-8"))
#'
#' # with HttpRequest
#' (res <- HttpRequest$new(
#'   url = "https://httpbin.org/basic-auth/user/passwd",
#'   auth = auth(user = "user", pwd = "passwd")
#' ))
#' res$auth
#' }
auth <- function(user, pwd, auth = "basic") {
  structure(ccp(list(
    userpwd = make_up(user, pwd),
    httpauth = auth_type(auth)
  )), class = "auth", type = auth)
}