From 5b694428a9ee538b76636d7674de5f8a45bd7318 Mon Sep 17 00:00:00 2001 From: hong-revo Date: Wed, 13 Feb 2019 05:09:41 +1100 Subject: [PATCH] better presentation of common tokens --- R/format.R | 9 ++++++++- R/normalize.R | 2 ++ R/utils.R | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/R/format.R b/R/format.R index 44790cf..d860933 100644 --- a/R/format.R +++ b/R/format.R @@ -16,8 +16,15 @@ format_auth_header <- function(token) version <- if(token$version == 1) "v1.0" else "v2.0" + tenant <- token$tenant + if(tenant == "common") + { + token_obj <- decode_jwt(token$credentials$access_token) + tenant <- paste0(tenant, " / ", token_obj$payload$tid) + } + paste0("Azure Active Directory ", version, " token for ", res, "\n", - " Tenant: ", token$tenant, "\n", + " Tenant: ", tenant, "\n", " App ID: ", token$client$client_id, "\n", " Authentication method: ", token$auth_type, "\n", " Token valid from: ", format(obtained, usetz=TRUE), " to: ", format(expiry, usetz=TRUE), "\n", diff --git a/R/normalize.R b/R/normalize.R index 34317e3..57010af 100644 --- a/R/normalize.R +++ b/R/normalize.R @@ -47,6 +47,8 @@ #' @rdname guid normalize_tenant <- function(tenant) { + tenant <- tolower(tenant) + # check if supplied a guid; if not, check if a fqdn; # if not, check if 'common'; if not, append '.onmicrosoft.com' if(is_guid(tenant)) diff --git a/R/utils.R b/R/utils.R index 86ed84c..1017296 100644 --- a/R/utils.R +++ b/R/utils.R @@ -124,3 +124,26 @@ verify_v2_scope <- function(scope) scope } + +# decode info in a token (which is a JWT object) +decode_jwt <- function(token) +{ + decode <- function(string) + { + m <- nchar(string) %% 4 + if(m == 2) + string <- paste0(string, "==") + else if(m == 3) + string <- paste0(string, "=") + jsonlite::fromJSON(rawToChar(openssl::base64_decode(string))) + } + + token <- as.list(strsplit(token, "\\.")[[1]]) + token[1:2] <- lapply(token[1:2], decode) + + names(token)[1:2] <- c("header", "payload") + if(length(token) > 2) + names(token)[3] <- "signature" + + token +}