Skip to content

Instantly share code, notes, and snippets.

@mdmsua
Created September 11, 2024 14:02
Show Gist options
  • Save mdmsua/61f43dd93bae88dcf8bea0d6c3716432 to your computer and use it in GitHub Desktop.
Save mdmsua/61f43dd93bae88dcf8bea0d6c3716432 to your computer and use it in GitHub Desktop.
Generate access token based on OIDC
locals {
access_token_request_parameters = {
scope = "https://management.azure.com/.default"
grant_type = "client_credentials"
client_id = data.azurerm_client_config.main.client_id
client_assertion_type = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
client_assertion = data.local_file.jwt.content
}
}
data "http" "main" {
url = "https://login.microsoftonline.com/${data.azurerm_client_config.main.tenant_id}/oauth2/v2.0/token"
method = "POST"
request_body = join("&", [for k, v in local.access_token_request_parameters : "${k}=${urlencode(v)}"])
request_headers = {
"Accept" = "application/json"
"Content-Type" = "application/x-www-form-urlencoded"
}
}
output "access_token" {
value = jsondecode(data.http.main.response_body).access_token)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment