Skip to content

Commit f0f8c17

Browse files
committed
🔒️ Use the vault for API creds
1 parent f4e2450 commit f0f8c17

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

Get-PocketArticles.ps1

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -93,56 +93,69 @@ By default, the default vault is used.
9393
Set-ParameterDefault.ps1 Invoke-RestMethod Method Post
9494
Set-ParameterDefault.ps1 Invoke-RestMethod ContentType application/json
9595
Set-ParameterDefault.ps1 Invoke-RestMethod Headers @{'X-Accept'='application/json'}
96-
$consumerKey = ( $Vault ?
97-
(Get-Secret PocketApiConsumerKey -Vault $Vault -ErrorAction Ignore) :
98-
(Get-Secret PocketApiConsumerKey -ErrorAction Ignore) ) |
99-
ConvertFrom-SecureString -AsPlainText -ErrorAction Ignore
100-
if(!$consumerKey)
96+
$VaultOrDefault = if($Vault) {@{Vault=$Vault}} else {@{}}
97+
if(!(Get-SecretInfo PocketApi @VaultOrDefault))
10198
{
102-
$consumerKey = Get-Credential PocketApiConsumerKey -Message 'Pocket API consumer key'
103-
$VaultOrDefault = if($Vault) {@{Vault=$Vault}} else {@{}}
104-
Set-Secret PocketApiConsumerKey $consumerKey.Password @VaultOrDefault
105-
$consumerKey = $consumerKey.Password |ConvertFrom-SecureString -AsPlainText
99+
$oldtokenfile = Join-Path ~ .pocket
100+
$secret =
101+
if((Test-Path $oldtokenfile -Type Leaf) -and (Get-SecretInfo PocketApiConsumerKey @VaultOrDefault))
102+
{
103+
$key = Get-Secret PocketApiConsumerKey -AsPlainText @VaultOrDefault
104+
Remove-Secret PocketApiConsumerKey -Vault ($Vault ? $Vault : (Get-SecretVault |Select-Object -ExpandProperty Name))
105+
$token = Get-Content $oldtokenfile |ConvertTo-SecureString
106+
Remove-Item $oldtokenfile
107+
New-Object pscredential -ArgumentList $key,$token
108+
}
109+
else
110+
{
111+
$key = (Get-Credential PocketApiConsumerKey -Message 'Pocket API consumer key').GetNetworkCredential().Password
112+
$redirectUri = 'https://webcoder.info/auth-success.html'
113+
$code = @{
114+
consumer_key = $key
115+
redirect_uri = $redirectUri
116+
} |
117+
ConvertTo-Json -Compress |
118+
Invoke-RestMethod https://getpocket.com/v3/oauth/request -Method Post -ContentType application/json |
119+
Select-Object -ExpandProperty code
120+
$code |ConvertTo-Json |Write-Info.ps1 -fg Cyan
121+
Start-Process "https://getpocket.com/auth/authorize?request_token=$code&redirect_uri=$([uri]::EscapeDataString($redirectUri))" -Wait
122+
if(!$PSCmdlet.ShouldContinue('Has the token been authorized?','Authorize')) {return}
123+
$code = @{consumer_key=$key;code=$code} |
124+
ConvertTo-Json -Compress |
125+
Invoke-RestMethod https://getpocket.com/v3/oauth/authorize -Method Post -ContentType application/json
126+
if(!${code}?.username) {Stop-ThrowError.ps1 'Could not get an access token.' -OperationContext $code}
127+
Write-Verbose "Authenticated token for $($code.username)"
128+
New-Object pscredential -ArgumentList $key,($code.access_token |ConvertTo-SecureString -AsPlainText -Force)
129+
}
130+
Set-Secret PocketApi -Secret $secret -Metadata @{
131+
Title = 'Pocket API consumer key and token'
132+
Description = 'Contains the key as username and token as password.'
133+
Note = 'Used to access saved articles from Pocket'
134+
Uri = 'https://getpocket.com/developer/'
135+
Created = Get-Date
136+
} @VaultOrDefault
106137
}
107-
$tokenfile = Join-Path ~ .pocket
108-
$token =
109-
if(Test-Path $tokenfile -Type Leaf)
110-
{
111-
Get-Content $tokenfile |ConvertTo-SecureString |ConvertFrom-SecureString -AsPlainText
112-
}
113-
else
114-
{
115-
$redirectUri = 'https://webcoder.info/auth-success.html'
116-
$code = @{consumer_key=$consumerKey;redirect_uri=$redirectUri} |
117-
ConvertTo-Json -Compress |
118-
Invoke-RestMethod https://getpocket.com/v3/oauth/request -ContentType application/json |
119-
Select-Object -ExpandProperty code
120-
Start-Process "https://getpocket.com/auth/authorize?request_token=$code&redirect_uri=$([uri]::EscapeDataString($redirectUri))" -Wait
121-
if(!$PSCmdlet.ShouldContinue('Has the token been authorized?','Authorize')) {return}
122-
$code = @{consumer_key=$consumerKey;code=$code} |
123-
ConvertTo-Json -Compress |
124-
Invoke-RestMethod https://getpocket.com/v3/oauth/authorize -Method Post -ContentType application/json
125-
Write-Verbose "Authenticated token for $($code.username)"
126-
$code.access_token |ConvertTo-SecureString -AsPlainText -Force |ConvertFrom-SecureString |Out-File $tokenfile
127-
$code.access_token
128-
}
129-
Write-Verbose "Using key $consumerKey and token $token"
138+
$apiCredential = Get-Secret PocketApi @VaultOrDefault
130139
$articles = @{
131-
consumer_key = $consumerKey
132-
access_token = $token
140+
consumer_key = $apiCredential.UserName
141+
access_token = $apiCredential.GetNetworkCredential().Password
133142
state = $State.ToLower()
134143
favorite = if($Favorite) {1} else {$null};
135-
tag = $Tag
144+
tag = ($Tag ? $Tag : $null)
136145
contentType = if($ContentType) {$ContentType.ToLower()};
137146
sort = if($Sort) {$Sort.ToLower()};
138147
detailType = 'complete'
139-
search = $Search
140-
domain = $Domain
148+
search = ($Search ? $Search : $null)
149+
domain = ($Domain ? $Domain : $null)
141150
since = ConvertTo-EpochTime.ps1 $After
142151
} |
143152
Remove-NullValues.ps1 |
144153
ConvertTo-Json -Compress |
145-
Invoke-RestMethod https://getpocket.com/v3/get
154+
Invoke-RestMethod https://getpocket.com/v3/get -SkipHttpErrorCheck
155+
if(${articles}?.error)
156+
{
157+
Stop-ThrowError.ps1 $articles.error -OperationContext $articles
158+
}
146159
${articles}?.{list}?.{PSObject}?.{Properties}?.Value |
147160
ForEach-Object {[pscustomobject]@{
148161
ItemId = $_.item_id

0 commit comments

Comments
 (0)