Skip to content

Commit

Permalink
Merge pull request #20 from markekraus/develop
Browse files Browse the repository at this point in the history
Add Token Update Capabilities

Documentation errors will be fixed in this branch.

Works towards #8 #11 #12
  • Loading branch information
markekraus committed May 19, 2017
2 parents 80f2ad1 + b6c5e2e commit 6643686
Show file tree
Hide file tree
Showing 14 changed files with 1,134 additions and 4 deletions.
39 changes: 39 additions & 0 deletions PSRAW/Classes/004-RedditOAuthToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,43 @@ Class RedditOAuthToken {
[string] ToString() {
Return ('GUID: {0} Expires: {1}' -f $This.GUID, $This.ExpireDate)
}
[void] Refresh ([Object]$Response) {
#URI Handler
If ($Response.psobject.typenames -contains 'system.uri') {
if (-not $Response.Fragment) {
$Exception = [System.ArgumentException]::New(
"Response does not include Fragment"
)
$Exception | Add-Member -Name Uri -MemberType NoteProperty -Value $Response
Throw $Exception
}
$Content = @{}
$Paresed = [System.Web.HttpUtility]::
ParseQueryString($Response.Fragment -replace '^#')
$Paresed.Getenumerator() |
ForEach-Object {
$Content.Add($_, $Paresed[$_])
}
}
#WebResponse Handler
Else {
If ( -not ($Response.Headers.'Content-type' -match 'application/json')) {
$Exception = [System.ArgumentException]::New(
"Response Content-Type is not 'application/json'"
)
$Exception | Add-Member -Name Response -MemberType NoteProperty -Value $Response
Throw $Exception
}
$Content = $Response.Content | ConvertFrom-Json -ErrorAction Stop
}
# Do the needful
$This.IssueDate = Get-Date
$This.TokenType = $Content.token_type
$This.Scope = $Content.Scope -split ' '
$This.ExpireDate = (get-date).AddSeconds($Content.expires_in)
if ($Content.access_token) {
$SecString = $Content.access_token | ConvertTo-SecureString -AsPlainText -Force
$This.TokenCredential = [pscredential]::new('access_token', $SecString)
}
}
}
4 changes: 2 additions & 2 deletions PSRAW/PSRAW.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
ModuleToProcess = 'PSRAW.psm1'

# Version number of this module.
ModuleVersion = '1.0.12.41'
ModuleVersion = '1.0.12.45'

# ID used to uniquely identify this module
GUID = '92c8f916-4890-45eb-a3e7-592f5b5b3f24'
Expand Down Expand Up @@ -76,7 +76,7 @@
NestedModules = @()

# Functions to export from this module
FunctionsToExport = @('Export-RedditApplication','Import-RedditApplication','New-RedditApplication','Export-RedditOAuthToken','Get-RedditOAuthScope','Import-RedditOAuthToken','Request-RedditOAuthToken')
FunctionsToExport = @('Export-RedditApplication','Import-RedditApplication','New-RedditApplication','Export-RedditOAuthToken','Get-RedditOAuthScope','Import-RedditOAuthToken','Request-RedditOAuthToken','Update-RedditOAuthToken')

# Cmdlets to export from this module
CmdletsToExport = @()
Expand Down
65 changes: 65 additions & 0 deletions PSRAW/Private/OAuth/Request-RedditOAuthTokenRefresh.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<#
.NOTES
Created with: VSCode
Created on: 5/17/2017 03:58 PM
Editied on: 5/18/2017
Created by: Mark Kraus
Organization:
Filename: Request-RedditOAuthTokenRefresh.ps1
.DESCRIPTION
Request-RedditOAuthTokenRefresh Function
#>
[CmdletBinding()]
param()

function Request-RedditOAuthTokenRefresh {
[CmdletBinding(
HelpUri = 'https://psraw.readthedocs.io/en/latest/PrivateFunctions/Request-RedditOAuthTokenRefresh'
)]
[OutputType([Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject])]
param (
[Parameter(
mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[ValidateScript(
{
If (-not ($_.GrantType -eq [RedditOAuthGrantType]::Authorization_Code )) {
$Exception = [System.Management.Automation.ValidationMetadataException]::new(
"RedditOAuthToken must be of RedditOAuthGrantType 'Authorization_Code'"
)
Throw $Exception
}
$true
}
)]
[Alias('Token')]
[RedditOAuthToken]$AccessToken,

[Parameter(
mandatory = $false,
ValueFromPipeline = $false,
ValueFromPipelineByPropertyName = $false
)]
[String]$AuthBaseUrl = [RedditOAuthToken]::AuthBaseURL
)
process {
$Params = @{
Uri = $AuthBaseUrl
Body = @{
grant_type = 'refresh_token'
refresh_token = $AccessToken.GetRefreshToken()
}
UserAgent = $AccessToken.Application.UserAgent
Headers = @{
Authorization = $AccessToken.Application.ClientCredential | Get-AuthorizationHeader
}
Method = 'POST'
UseBasicParsing = $true
}
Invoke-WebRequest @Params
}
}
1 change: 0 additions & 1 deletion PSRAW/Public/OAuth/Request-RedditOAuthToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ function Request-RedditOAuthToken {
Application = $Application
}
$Result = Request-RedditOAuthTokenPassword @Params
[RedditOAUthToken]::New($GrantType, $AuthCode.Application, $Result)
Break
}
'Client' {
Expand Down
92 changes: 92 additions & 0 deletions PSRAW/Public/OAuth/Update-RedditOAuthToken.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<#
.NOTES
Created with: VSCode
Created on: 5/17/2017 04:07 AM
Editied on: 5/18/2017
Created by: Mark Kraus
Organization:
Filename: Update-RedditOAuthToken.ps1
.DESCRIPTION
Update-RedditOAuthToken Function
#>
[CmdletBinding()]
param()

function Update-RedditOAuthToken {
[CmdletBinding(
HelpUri = 'https://psraw.readthedocs.io/en/latest/Module/Update-RedditOAuthToken',
SupportsShouldProcess = $true
)]
[OutputType([RedditOAuthToken])]
param
(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0
)]
[Alias('Token')]
[RedditOAuthToken[]]$AccessToken,

[switch]$Force,

[switch]$PassThru

)

process {
foreach ($Token in $AccessToken) {
if (-not $PSCmdlet.ShouldProcess($Token.GUID)) {
continue
}
if (-not $Token.IsExpired() -and -not $Force) {
continue
}
switch ($Token.GrantType) {
'Installed_Client' {
$Params = @{
Application = $Token.Application
DeviceID = $Token.DeviceID
}
$Result = Request-RedditOAuthTokenInstalled @Params
Break
}
'Authorization_Code' {
$Params = @{
AccessToken = $Token
}
$Result = Request-RedditOAuthTokenRefresh @Params
Break
}
'Password' {
$Params = @{
Application = $Token.Application
}
$Result = Request-RedditOAuthTokenPassword @Params
Break
}
'Client_Credentials' {
$Params = @{
Application = $Token.Application
}
$Result = Request-RedditOAuthTokenClient @Params
Break
}
'Implicit' {
$Params = @{
Application = $Token.Application
}
$Result = Request-RedditOAuthTokenImplicit @Params
Break
}
}
$Token.Refresh($Result)
if ($PassThru.IsPresent) {
$Token
}
}
}
}
131 changes: 131 additions & 0 deletions PSRAW/en-US/PSRAW-help.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1845,4 +1845,135 @@ $Token = New-RedditApplication @Params | Request-RedditOAuthToken -Code</dev:cod
</maml:navigationLink>
</command:relatedLinks>
</command:command>
<command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10" xmlns:MSHelp="http://msdn.microsoft.com/mshelp">
<command:details><command:name>Update-RedditOAuthToken</command:name>
<command:verb>Update</command:verb>
<command:noun>RedditOAuthToken</command:noun>
<maml:description><maml:para>{{Fill in the Synopsis}}
</maml:para>
</maml:description>
</command:details>
<maml:description><maml:para>{{Fill in the Description}}
</maml:para>
</maml:description>
<command:syntax><command:syntaxItem><maml:name>Update-RedditOAuthToken</maml:name>
<command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName, ByValue)" position="named" aliases="Token"><maml:name>AccessToken</maml:name>
<maml:Description><maml:para>{{Fill AccessToken Description}}
</maml:para>
</maml:Description>
<command:parameterValue required="true" variableLength="false">RedditOAuthToken[]</command:parameterValue>
<dev:type><maml:name>RedditOAuthToken[]</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"><maml:name>Force</maml:name>
<maml:Description><maml:para>{{Fill Force Description}}
</maml:para>
</maml:Description>
<dev:type><maml:name>SwitchParameter</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"><maml:name>PassThru</maml:name>
<maml:Description><maml:para>{{Fill PassThru Description}}
</maml:para>
</maml:Description>
<dev:type><maml:name>SwitchParameter</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="cf"><maml:name>Confirm</maml:name>
<maml:Description><maml:para>Prompts you for confirmation before running the cmdlet.
</maml:para>
</maml:Description>
<dev:type><maml:name>SwitchParameter</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="wi"><maml:name>WhatIf</maml:name>
<maml:Description><maml:para>Shows what would happen if the cmdlet runs. The cmdlet is not run.
</maml:para>
</maml:Description>
<dev:type><maml:name>SwitchParameter</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
</command:syntaxItem>
</command:syntax>
<command:parameters><command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName, ByValue)" position="named" aliases="Token"><maml:name>AccessToken</maml:name>
<maml:Description><maml:para>{{Fill AccessToken Description}}
</maml:para>
</maml:Description>
<command:parameterValue required="true" variableLength="false">RedditOAuthToken[]</command:parameterValue>
<dev:type><maml:name>RedditOAuthToken[]</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"><maml:name>Force</maml:name>
<maml:Description><maml:para>{{Fill Force Description}}
</maml:para>
</maml:Description>
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
<dev:type><maml:name>SwitchParameter</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"><maml:name>PassThru</maml:name>
<maml:Description><maml:para>{{Fill PassThru Description}}
</maml:para>
</maml:Description>
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
<dev:type><maml:name>SwitchParameter</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="cf"><maml:name>Confirm</maml:name>
<maml:Description><maml:para>Prompts you for confirmation before running the cmdlet.
</maml:para>
</maml:Description>
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
<dev:type><maml:name>SwitchParameter</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="wi"><maml:name>WhatIf</maml:name>
<maml:Description><maml:para>Shows what would happen if the cmdlet runs. The cmdlet is not run.
</maml:para>
</maml:Description>
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
<dev:type><maml:name>SwitchParameter</maml:name>
<maml:uri /></dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
</command:parameters>
<command:inputTypes><command:inputType><dev:type><maml:name>RedditOAuthToken[]</maml:name>
</dev:type>
<maml:description><maml:para>
</maml:para>
</maml:description>
</command:inputType>
</command:inputTypes>
<command:returnValues><command:returnValue><dev:type><maml:name>RedditOAuthToken</maml:name>
</dev:type>
<maml:description><maml:para>
</maml:para>
</maml:description>
</command:returnValue>
</command:returnValues>
<maml:alertSet><maml:alert><maml:para>
</maml:para>
</maml:alert>
</maml:alertSet>
<command:examples><command:example><maml:title>Example 1</maml:title>
<dev:code>PS C:\&gt; {{ Add example code here }}</dev:code>
<dev:remarks><maml:para>{{ Add example description here }}
</maml:para>
</dev:remarks>
</command:example>
</command:examples>
<command:relatedLinks><maml:navigationLink><maml:linkText>https://psraw.readthedocs.io/en/latest/Module/Update-RedditOAuthToken</maml:linkText>
<maml:uri>https://psraw.readthedocs.io/en/latest/Module/Update-RedditOAuthToken</maml:uri>
</maml:navigationLink>
</command:relatedLinks>
</command:command>
</helpItems>
Loading

0 comments on commit 6643686

Please sign in to comment.