Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added LDAP authentication #263

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion R/Auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' \itemize{
#' \item {"db"}{Database authentication using Atlas/WebAPI built in auth}
#' \item {"ad"}{Active Directory}
#' \item {"ldap"}{LDAP protocol}
#' \item {"windows"}{Windows NT authentication}
#' }
#' The auth method must be enabled in the instance of WebAPI pointed to by
Expand All @@ -25,7 +26,7 @@ authorizeWebApi <- function(baseUrl, authMethod, webApiUsername = NULL, webApiPa
# check input
errorMessage <- checkmate::makeAssertCollection()
checkmate::assertCharacter(baseUrl, len = 1, min.chars = 1, add = errorMessage)
checkmate::assertChoice(authMethod, choices = c("db", "ad", "windows"), add = errorMessage)
checkmate::assertChoice(authMethod, choices = c("db", "ad", "ldap", "windows"), add = errorMessage)

# With windows type we can try NT user authentication
if (authMethod == "windows" & is.null(webApiUsername) & is.null(webApiPassword) & .Platform$OS.type ==
Expand All @@ -47,6 +48,7 @@ authorizeWebApi <- function(baseUrl, authMethod, webApiUsername = NULL, webApiPa
authHeader <- switch(authMethod,
db = .authDb(baseUrl, webApiUsername, webApiPassword),
ad = .authAd(baseUrl, webApiUsername, webApiPassword),
ldap = .authLdap(baseUrl, webApiUsername, webApiPassword),
windows = .authWindows(baseUrl, webApiUsername, webApiPassword))

# store token in package environment
Expand Down Expand Up @@ -81,6 +83,19 @@ authorizeWebApi <- function(baseUrl, authMethod, webApiUsername = NULL, webApiPa
authHeader
}

.authLdap <- function(baseUrl, webApiUsername, webApiPassword) {
checkmate::assertCharacter(webApiUsername, min.chars = 1, len = 1)
checkmate::assertCharacter(webApiPassword, min.chars = 1, len = 1)

authUrl <- paste0(baseUrl, "/user/login/ldap")
login <- list(login = webApiUsername, password = webApiPassword)
r <- httr::POST(authUrl, body = login, encode = "form")
if (length(httr::headers(r)$bearer) < 1)
stop("Authentication failed.")
authHeader <- paste0("Bearer ", httr::headers(r)$bearer)
authHeader
}

.authWindows <- function(baseUrl, webApiUsername, webApiPassword) {
checkmate::assertCharacter(webApiUsername, min.chars = 1, len = 1)
checkmate::assertCharacter(webApiPassword, min.chars = 1, len = 1)
Expand Down