Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. function fnGetOauthXSToken()
  2. {
  3. param (
  4. [string]$ADFSOAUTHAuthorizeUri,
  5. [string]$ADFSOAUTHGetTokenUri,
  6. [string]$Client_ID,
  7. [string]$Resource,
  8. [string]$Redirect_URI,
  9. [string]$RefreshToken,
  10. [string]$UserName,
  11. [string]$Password
  12. )
  13.  
  14. if ( $RefreshToken -ne "" ) {
  15.  
  16. # Get OAUTH Access Token by using OAUTH refresh_token...
  17.  
  18. ## Retrieve OAUTH Token...
  19. $vPostValues = "grant_type=refresh_token&client_id=" + $Client_ID + "&redirect_uri=" + $Redirect_URI + "&refresh_token=" + $RefreshToken
  20. $oResult0 = Invoke-RestMethod -Method Post -Uri $ADFSOAUTHGetTokenUri -Body $vPostValues -ContentType application/x-www-form-urlencoded
  21.  
  22. } else {
  23.  
  24. # Get OAUTH Access Token by using authorization_code (username and password)...
  25.  
  26. ## Build authentication Uri and create websession...
  27. $sUri = $ADFSOAUTHAuthorizeUri + "?response_type=code&client_id=" + $Client_ID + "&resource=" + $Resource + "&redirect_uri=" + $Redirect_URI
  28. $oWebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
  29.  
  30. ## Authenticate by using username and password (formbased)...
  31. $aPostValues = @{UserName=$UserName; Password=$Password; AuthMethod='FormsAuthentication' }
  32. $oResult0 = Invoke-WebRequest -Method Post -Uri $sUri -Body $aPostValues -Websession $oWebSession -MaximumRedirection 0 -ErrorAction SilentlyContinue
  33.  
  34. ## Retrieve authorization code...
  35. $oResult0 = Invoke-WebRequest -Uri $sUri -Websession $oWebSession -MaximumRedirection 0 -ErrorAction SilentlyContinue
  36. $sCode = $oResult0.Headers.Location.Substring($oResult0.Headers.Location.IndexOf("?code=") + 6, ($oResult0.Headers.Location.Length - ($oResult0.Headers.Location.IndexOf("?code=") + 6) ) )
  37.  
  38. ## Cleanup websession...
  39. $oResult0 = $null
  40. $oWebSession = $null
  41. $aPostValues = $null
  42.  
  43. ## Retrieve OAUTH Token...
  44. $vPostValues = "grant_type=authorization_code&client_id=" + $Client_ID + "&redirect_uri=" + $Redirect_URI + "&code=" + $sCode
  45. $oResult0 = Invoke-RestMethod -Method Post -Uri $ADFSOAUTHGetTokenUri -Body $vPostValues -ContentType application/x-www-form-urlencoded
  46.  
  47. }
  48.  
  49. # Return Result and Cleanup...
  50. return $oResult0
  51. $vPostValues = $null
  52. $oResult0 = $null
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement