Guest User

Untitled

a guest
Aug 11th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. # This file shouldn't be run on it's own. It should be loaded using the Apigee Module.
  2.  
  3.  
  4. <#
  5. .SYNOPSIS
  6. Makes a call to the Apigee OAuth login endpoint and gets access tokens to use.
  7.  
  8. This should be used internally by the Apigee module. But, it shouldn't be needed by
  9. the developer.
  10.  
  11.  
  12. .EXAMPLE
  13. $result = Get-ApigeeAccessTokens
  14. #>
  15. Function Get-ApigeeAccessTokens {
  16. [CmdletBinding()]
  17. [OutputType([PSCustomObject])]
  18. Param ()
  19.  
  20. $otp = Get-ApigeeOTP `
  21. -SharedSecret $global:Apigee.OAuthLogin.OTPSharedSecret `
  22. -Email $global:Apigee.OAuthLogin.Username
  23.  
  24. $body = @{
  25. username = $global:Apigee.OAuthLogin.Username
  26. password = $global:Apigee.OAuthLogin.Password
  27. mfa_token = $otp.OTP
  28. grant_type = "password"
  29. }
  30.  
  31. $results = Invoke-WebRequest `
  32. -Uri $global:Apigee.OAuthLogin.Url `
  33. -Method $global:Apigee.OAuthLogin.Method `
  34. -Headers $global:Apigee.OAuthLogin.Headers `
  35. -ContentType $global:Apigee.OAuthLogin.ContentType `
  36. -Body $body
  37.  
  38. if($results.StatusCode -ne 200) {
  39. $resultsAsString = $results | Out-String
  40. throw "Authentication with Apigee's OAuth Failed. `r`n`r`nFull Response Object:`r`n$resultsAsString"
  41. }
  42.  
  43. $resultsObj = ConvertFrom-Json -InputObject $results.Content
  44. $resultsObj = Add-PsType -PSObject $resultsObj -PsType $global:Apigee.OAuthLogin.ResultObjectName
  45.  
  46. Set-ApigeeAuthHeader -Authorization $resultsObj.access_token
  47.  
  48. return $resultsObj
  49. }
  50.  
  51. <#
  52. .SYNOPSIS
  53. Sets $global:Apigee.AuthHeader @{ Authorization = "value passed in" }
  54.  
  55. This is used to authenticate all calls to the Apigee REST Management endpoints.
  56.  
  57. .EXAMPLE
  58. Set-ApigeeAuthHeader -Authorization "Bearer ..."
  59. #>
  60. Function Set-ApigeeAuthHeader {
  61. [CmdletBinding()]
  62. [OutputType([PSCustomObject])]
  63. Param (
  64. [Parameter(Mandatory = $true)]
  65. $Authorization
  66. )
  67.  
  68. $bearerAuth = "Bearer $Authorization"
  69. $global:Apigee.AuthHeader = @{ Authorization = $bearerAuth }
  70. }
  71.  
  72.  
  73.  
  74.  
  75. [string[]]$funcs =
  76. "Get-ApigeeAccessTokens", "Set-ApigeeAuthHeader"
  77.  
  78. Export-ModuleMember -Function $funcs
Add Comment
Please, Sign In to add comment